use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class ContextFinder method findAssignedElements.
protected Set<AbstractElement> findAssignedElements(EObject obj, EStructuralFeature feature, Multimap<AbstractElement, ISerializationContext> assignments) {
if (feature.isMany()) {
Set<AbstractElement> r = Sets.newLinkedHashSet();
INodesForEObjectProvider nodes = nodesProvider.getNodesForSemanticObject(obj, null);
switch(transientValues.isListTransient(obj, feature)) {
case SOME:
List<?> values1 = (List<?>) obj.eGet(feature);
int j = 0;
for (int i = 0; i < values1.size(); i++) if (!transientValues.isValueInListTransient(obj, i, feature)) {
Object value = values1.get(i);
INode node = nodes.getNodeForMultiValue(feature, i, j, value);
r.addAll(assignmentFinder.findAssignmentsByValue(obj, assignments, value, node));
j++;
}
return r;
case NO:
List<?> values2 = (List<?>) obj.eGet(feature);
for (int i = 0; i < values2.size(); i++) {
Object value = values2.get(i);
INode node = nodes.getNodeForMultiValue(feature, i, i, value);
r.addAll(assignmentFinder.findAssignmentsByValue(obj, assignments, value, node));
}
return r;
default:
return Collections.emptySet();
}
} else {
if (transientValues.isValueTransient(obj, feature) == ValueTransient.YES)
return Collections.emptySet();
Object value = obj.eGet(feature);
INode node = nodesProvider.getNodesForSemanticObject(obj, null).getNodeForSingelValue(feature, value);
return assignmentFinder.findAssignmentsByValue(obj, assignments, value, node);
}
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class XtextAntlrGeneratorFragment2 method initNameMappings.
/**
* @since 2.14
*/
protected StringConcatenationClient initNameMappings(final Grammar it) {
StringConcatenationClient _xblockexpression = null;
{
final RuleFilter filter = new RuleFilter();
filter.setDiscardUnreachableRules(this.getOptions().isSkipUnusedRules());
final RuleNames ruleNames = RuleNames.getRuleNames(it, true);
final Grammar flattened = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
final Set<AbstractElement> seenElements = CollectionLiterals.<AbstractElement>newHashSet();
Collection<? extends AbstractElement> _allAlternatives = GrammarUtil.getAllAlternatives(flattened);
Collection<? extends AbstractElement> _allGroups = GrammarUtil.getAllGroups(flattened);
Iterable<AbstractElement> _plus = Iterables.<AbstractElement>concat(_allAlternatives, _allGroups);
Collection<? extends AbstractElement> _allAssignments = GrammarUtil.getAllAssignments(flattened);
Iterable<AbstractElement> _plus_1 = Iterables.<AbstractElement>concat(_plus, _allAssignments);
Collection<? extends AbstractElement> _allUnorderedGroups = GrammarUtil.getAllUnorderedGroups(flattened);
final Function1<AbstractElement, Boolean> _function = (AbstractElement it_1) -> {
return Boolean.valueOf(seenElements.add(AntlrGrammarGenUtil.<AbstractElement>getOriginalElement(it_1)));
};
final List<AbstractElement> elements = IterableExtensions.<AbstractElement>toList(IterableExtensions.<AbstractElement>filter(Iterables.<AbstractElement>filter(Iterables.<AbstractElement>concat(_plus_1, _allUnorderedGroups), AbstractElement.class), _function));
final List<List<AbstractElement>> partitions = Lists.<AbstractElement>partition(elements, 2500);
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("@");
_builder.append(Singleton.class);
_builder.newLineIfNotEmpty();
_builder.append("public static final class NameMappings {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
{
int _size = partitions.size();
boolean _greaterThan = (_size > 1);
if (_greaterThan) {
{
Iterable<Pair<Integer, List<AbstractElement>>> _indexed = IterableExtensions.<List<AbstractElement>>indexed(partitions);
for (final Pair<Integer, List<AbstractElement>> partition : _indexed) {
_builder.append("\t");
_builder.append("private static final class Init");
Integer _key = partition.getKey();
_builder.append(_key, "\t");
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("private static void doInit(");
_builder.append(ImmutableMap.class, "\t\t");
_builder.append(".Builder<");
_builder.append(AbstractElement.class, "\t\t");
_builder.append(", ");
_builder.append(String.class, "\t\t");
_builder.append("> builder, ");
TypeReference _grammarAccess = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
_builder.append(_grammarAccess, "\t\t");
_builder.append(" grammarAccess) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t\t");
StringConcatenationClient _initNameMappings = XtextAntlrGeneratorFragment2.this.initNameMappings(partition.getValue());
_builder.append(_initNameMappings, "\t\t\t");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
}
}
}
}
_builder.append("\t");
_builder.append("private final ");
_builder.append(Map.class, "\t");
_builder.append("<");
_builder.append(AbstractElement.class, "\t");
_builder.append(", ");
_builder.append(String.class, "\t");
_builder.append("> mappings;");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("@");
_builder.append(Inject.class, "\t");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("public NameMappings(");
TypeReference _grammarAccess_1 = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
_builder.append(_grammarAccess_1, "\t");
_builder.append(" grammarAccess) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append(ImmutableMap.class, "\t\t");
_builder.append(".Builder<");
_builder.append(AbstractElement.class, "\t\t");
_builder.append(", ");
_builder.append(String.class, "\t\t");
_builder.append("> builder = ");
_builder.append(ImmutableMap.class, "\t\t");
_builder.append(".builder();");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("init(builder, grammarAccess);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("this.mappings = builder.build();");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("public ");
_builder.append(String.class, "\t");
_builder.append(" getRuleName(");
_builder.append(AbstractElement.class, "\t");
_builder.append(" element) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("return mappings.get(element);");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("private static void init(");
_builder.append(ImmutableMap.class, "\t");
_builder.append(".Builder<");
_builder.append(AbstractElement.class, "\t");
_builder.append(", ");
_builder.append(String.class, "\t");
_builder.append("> builder, ");
TypeReference _grammarAccess_2 = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
_builder.append(_grammarAccess_2, "\t");
_builder.append(" grammarAccess) {");
_builder.newLineIfNotEmpty();
{
int _size_1 = partitions.size();
boolean _greaterThan_1 = (_size_1 > 1);
if (_greaterThan_1) {
{
Iterable<Pair<Integer, List<AbstractElement>>> _indexed_1 = IterableExtensions.<List<AbstractElement>>indexed(partitions);
for (final Pair<Integer, List<AbstractElement>> partition_1 : _indexed_1) {
_builder.append("\t\t");
_builder.append("Init");
Integer _key_1 = partition_1.getKey();
_builder.append(_key_1, "\t\t");
_builder.append(".doInit(builder, grammarAccess);");
_builder.newLineIfNotEmpty();
}
}
} else {
{
for (final List<AbstractElement> partition_2 : partitions) {
_builder.append("\t\t");
StringConcatenationClient _initNameMappings_1 = XtextAntlrGeneratorFragment2.this.initNameMappings(partition_2);
_builder.append(_initNameMappings_1, "\t\t");
_builder.newLineIfNotEmpty();
}
}
}
}
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("@");
_builder.append(Inject.class);
_builder.newLineIfNotEmpty();
_builder.append("private NameMappings nameMappings;");
_builder.newLine();
}
};
_xblockexpression = _client;
}
return _xblockexpression;
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class FlattenedGrammarAccess method copyRuleBodies.
private Multimap<TerminalRule, AbstractRule> copyRuleBodies(final List<AbstractRule> copies, final Map<RuleWithParameterValues, AbstractRule> origToCopy) {
abstract class __FlattenedGrammarAccess_1 extends EcoreUtil.Copier {
final __FlattenedGrammarAccess_1 _this__FlattenedGrammarAccess_1 = this;
abstract Set<Parameter> getParameterConfig(final RuleCall origRuleCall, final RuleCall copyRuleCall);
abstract void mergePredicates(final AbstractElement into, final AbstractElement from);
abstract void mergeCardinalities(final AbstractElement into, final AbstractElement from);
abstract boolean evaluate(final Condition condition);
}
final HashMultimap<TerminalRule, AbstractRule> calledFrom = HashMultimap.<TerminalRule, AbstractRule>create();
for (final AbstractRule copy : copies) {
{
AbstractRule orig = RuleWithParameterValues.getOriginalRule(copy);
final Set<Parameter> paramValues = RuleWithParameterValues.getParamValues(copy);
EcoreUtil.Copier copier = new __FlattenedGrammarAccess_1() {
@Override
protected void copyReference(final EReference eReference, final EObject eObject, final EObject copyEObject) {
if ((eReference == XtextPackage.Literals.RULE_CALL__RULE)) {
RuleCall origRuleCall = ((RuleCall) eObject);
RuleCall copyRuleCall = ((RuleCall) copyEObject);
AbstractRule _rule = origRuleCall.getRule();
Set<Parameter> _parameterConfig = this.getParameterConfig(origRuleCall, copyRuleCall);
RuleWithParameterValues _ruleWithParameterValues = new RuleWithParameterValues(_rule, _parameterConfig);
AbstractRule calledCopy = origToCopy.get(_ruleWithParameterValues);
copyRuleCall.setRule(calledCopy);
if ((calledCopy instanceof TerminalRule)) {
calledFrom.put(((TerminalRule) calledCopy), copy);
}
} else {
super.copyReference(eReference, eObject, copyEObject);
}
}
Set<Parameter> getParameterConfig(final RuleCall origRuleCall, final RuleCall copyRuleCall) {
boolean _isEmpty = origRuleCall.getArguments().isEmpty();
if (_isEmpty) {
return Collections.<Parameter>emptySet();
}
final Function1<NamedArgument, Boolean> _function = (NamedArgument it) -> {
return Boolean.valueOf(this.evaluate(it.getValue()));
};
final Function1<NamedArgument, Parameter> _function_1 = (NamedArgument it) -> {
return it.getParameter();
};
Set<Parameter> result = IterableExtensions.<Parameter>toSet(IterableExtensions.<NamedArgument, Parameter>map(IterableExtensions.<NamedArgument>filter(origRuleCall.getArguments(), _function), _function_1));
return result;
}
@Override
protected void copyContainment(final EReference eReference, final EObject eObject, final EObject copyEObject) {
boolean _matched = false;
if (Objects.equal(eReference, XtextPackage.Literals.RULE_CALL__ARGUMENTS)) {
_matched = true;
}
if (!_matched) {
if (Objects.equal(eReference, XtextPackage.Literals.GROUP__GUARD_CONDITION)) {
_matched = true;
}
}
if (_matched) {
return;
}
super.copyContainment(eReference, eObject, copyEObject);
}
@Override
public EObject copy(final EObject eObject) {
if ((eObject instanceof Group)) {
Group group = ((Group) eObject);
Condition _guardCondition = group.getGuardCondition();
boolean _tripleNotEquals = (_guardCondition != null);
if (_tripleNotEquals) {
boolean _evaluate = this.evaluate(group.getGuardCondition());
boolean _not = (!_evaluate);
if (_not) {
return null;
}
}
}
EObject result = super.copy(eObject);
if ((result instanceof CompoundElement)) {
List<AbstractElement> elements = ((CompoundElement) result).getElements();
int _size = elements.size();
boolean _tripleEquals = (_size == 1);
if (_tripleEquals) {
if (((!((CompoundElement) result).isFirstSetPredicated()) && (!((CompoundElement) result).isPredicated()))) {
AbstractElement element = elements.get(0);
this.mergeCardinalities(element, ((AbstractElement) result));
this.mergePredicates(element, ((AbstractElement) result));
return element;
} else {
AbstractElement element_1 = elements.get(0);
this.mergePredicates(((AbstractElement) result), element_1);
element_1.setFirstSetPredicated(false);
element_1.setPredicated(false);
}
}
}
if ((eObject instanceof AbstractElement)) {
OriginalElement original = new OriginalElement(((AbstractElement) eObject));
EClass _eClass = ((AbstractElement) eObject).eClass();
EClass _eClass_1 = result.eClass();
boolean _notEquals = (!Objects.equal(_eClass, _eClass_1));
if (_notEquals) {
String _name = result.eClass().getName();
String _plus = ("copy is: \'" + _name);
String _plus_1 = (_plus + "\' but original was: \'");
String _name_1 = ((AbstractElement) eObject).eClass().getName();
String _plus_2 = (_plus_1 + _name_1);
String _plus_3 = (_plus_2 + "\'");
throw new IllegalStateException(_plus_3);
}
original.attachToEmfObject(result);
}
return result;
}
void mergePredicates(final AbstractElement into, final AbstractElement from) {
boolean _isPredicated = from.isPredicated();
if (_isPredicated) {
into.setPredicated(true);
into.setFirstSetPredicated(false);
} else {
if (((!into.isPredicated()) && from.isFirstSetPredicated())) {
into.setFirstSetPredicated(true);
}
}
}
void mergeCardinalities(final AbstractElement into, final AbstractElement from) {
String c1 = into.getCardinality();
String c2 = from.getCardinality();
String _switchResult = null;
boolean _matched = false;
if ((Objects.equal(c1, "*") || Objects.equal(c2, "*"))) {
_matched = true;
}
if (!_matched) {
if ((Objects.equal(c1, "+") && Objects.equal(c2, "?"))) {
_matched = true;
}
}
if (!_matched) {
if ((Objects.equal(c1, "?") && Objects.equal(c2, "+"))) {
_matched = true;
}
}
if (_matched) {
_switchResult = "*";
}
if (!_matched) {
if (Objects.equal(c1, null)) {
_matched = true;
_switchResult = c2;
}
}
if (!_matched) {
_switchResult = c1;
}
into.setCardinality(_switchResult);
}
boolean evaluate(final Condition condition) {
boolean result = new ConditionEvaluator(paramValues).evaluate(condition);
return result;
}
};
EObject _copy = copier.copy(orig.getAlternatives());
AbstractElement copiedBody = ((AbstractElement) _copy);
copier.copyReferences();
copy.setAlternatives(copiedBody);
if ((orig instanceof ParserRule)) {
ParserRule castedCopy = ((ParserRule) copy);
boolean _isDefinesHiddenTokens = ((ParserRule) orig).isDefinesHiddenTokens();
if (_isDefinesHiddenTokens) {
castedCopy.setDefinesHiddenTokens(true);
EList<AbstractRule> _hiddenTokens = ((ParserRule) orig).getHiddenTokens();
for (final AbstractRule rule : _hiddenTokens) {
{
RuleWithParameterValues _ruleWithParameterValues = new RuleWithParameterValues(rule);
final AbstractRule copiedTerminalRule = origToCopy.get(_ruleWithParameterValues);
EList<AbstractRule> _hiddenTokens_1 = castedCopy.getHiddenTokens();
_hiddenTokens_1.add(copiedTerminalRule);
calledFrom.put(((TerminalRule) copiedTerminalRule), castedCopy);
}
}
}
}
}
}
return calledFrom;
}
use of org.eclipse.xtext.AbstractElement in project xtext-xtend by eclipse.
the class Linker method afterCreateAndSetProxy.
@Override
protected void afterCreateAndSetProxy(EObject obj, INode node, EReference eRef, CrossReference xref, IDiagnosticProducer diagnosticProducer) {
AbstractElement terminal = xref.getTerminal();
if (!(terminal instanceof RuleCall)) {
throw new IllegalArgumentException(String.valueOf(xref));
}
AbstractRule rule = ((RuleCall) terminal).getRule();
try {
String tokenText = NodeModelUtils.getTokenText(node);
valueConverterService.toValue(tokenText, rule.getName(), node);
} catch (ValueConverterException e) {
diagnosticProducer.addDiagnostic(new DiagnosticMessage(e.getMessage(), Severity.ERROR, Diagnostic.SYNTAX_DIAGNOSTIC, Strings.EMPTY_ARRAY));
}
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class ContentAssistContextFactory method isLikelyToBeValidProposal.
protected boolean isLikelyToBeValidProposal(INode lastCompleteNode, Iterable<ContentAssistContext> contexts) {
for (ContentAssistContext context : contexts) {
for (AbstractElement element : context.getFirstSetGrammarElements()) {
if (element instanceof Keyword) {
String keywordValue = ((Keyword) element).getValue();
String lastText = ((ILeafNode) lastCompleteNode).getText();
if (keywordValue.equals(lastText))
return true;
}
}
}
return false;
}
Aggregations