use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class ContentAssistContextFactory method computeCurrentModel.
protected Multimap<EObject, AbstractElement> computeCurrentModel(EObject currentModel, INode lastCompleteNode, Collection<AbstractElement> followElements) {
Multimap<EObject, AbstractElement> result = LinkedHashMultimap.create();
ICompositeNode currentParserNode = NodeModelUtils.getNode(currentModel);
if (currentParserNode == null) {
result.putAll(currentModel, followElements);
return result;
}
EObject currentGrammarElement = currentParserNode.getGrammarElement();
AbstractRule currentRule = getRule(currentGrammarElement);
for (AbstractElement grammarElement : followElements) {
AbstractRule rule = currentRule;
ICompositeNode loopParserNode = currentParserNode;
EObject loopLastGrammarElement = lastCompleteNode.getGrammarElement();
while (!canBeCalledAfter(rule, loopLastGrammarElement, lastCompleteNode.getText(), grammarElement) && loopParserNode.getParent() != null) {
loopLastGrammarElement = loopParserNode.getGrammarElement();
loopParserNode = loopParserNode.getParent();
while (loopParserNode.getGrammarElement() == null && loopParserNode.getParent() != null) loopParserNode = loopParserNode.getParent();
EObject loopGrammarElement = loopParserNode.getGrammarElement();
rule = getRule(loopGrammarElement);
}
EObject context = loopParserNode.getSemanticElement();
result.put(context, grammarElement);
}
return result;
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class ConcreteSyntaxConstraintProvider method createElement.
protected ISyntaxConstraint createElement(EObject obj) {
if (!(obj instanceof AbstractElement))
return null;
AbstractElement ele = (AbstractElement) obj;
boolean multiple = false;
boolean optional = false;
EClass semanticType = null;
while (true) {
multiple = multiple || isMultipleCardinality(ele);
optional = optional || isOptionalCardinality(ele);
if (ele.eContainer() instanceof ParserRule && ((ParserRule) ele.eContainer()).getType().getClassifier() instanceof EClass)
semanticType = (EClass) ((ParserRule) ele.eContainer()).getType().getClassifier();
if (ele instanceof Assignment) {
return createElement(ConstraintType.ASSIGNMENT, ele, semanticType, multiple, optional);
} else if (ele instanceof Group || ele instanceof UnorderedGroup) {
CompoundElement comp = (CompoundElement) ele;
AbstractElement lastChild = null;
for (AbstractElement o : comp.getElements()) if (containsRelevantElement(o)) {
if (lastChild == null)
lastChild = o;
else {
List<AbstractElement> c = new ArrayList<AbstractElement>(comp.getElements());
List<ISyntaxConstraint> e = createSummarizedAssignments(comp, c, semanticType, optional);
if (e.size() == 1 && c.size() == 0)
return e.get(0);
return createElement(ConstraintType.GROUP, ele, c, e, semanticType, multiple, optional);
}
}
if (lastChild == null)
return null;
ele = lastChild;
continue;
} else if (ele instanceof Alternatives) {
int relevantChildren = 0;
AbstractElement lastChild = null;
for (AbstractElement o : ((CompoundElement) ele).getElements()) if (containsRelevantElement(o)) {
relevantChildren++;
lastChild = o;
}
if (relevantChildren < ((CompoundElement) ele).getElements().size())
optional = true;
if (relevantChildren > 1)
return createElement(ConstraintType.ALTERNATIVE, ele, semanticType, multiple, optional);
if (lastChild == null)
return null;
ele = lastChild;
continue;
} else if (ele instanceof Action) {
semanticType = (EClass) ((Action) ele).getType().getClassifier();
return createElement(ConstraintType.ACTION, ele, semanticType, multiple, optional);
} else if (ele instanceof RuleCall) {
AbstractRule rule = ((RuleCall) ele).getRule();
if (rule.getType().getClassifier() instanceof EClass) {
ele = rule.getAlternatives();
continue;
}
}
return null;
}
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class KeywordInspector method inspectKeywordHidesTerminalRule.
public void inspectKeywordHidesTerminalRule(Keyword keyword) {
AbstractRule container = GrammarUtil.containingRule(keyword);
if (container instanceof TerminalRule)
return;
Grammar grammar = GrammarUtil.getGrammar(container);
List<TerminalRule> rules = GrammarUtil.allTerminalRules(grammar);
for (TerminalRule rule : rules) {
if (!rule.isFragment()) {
AbstractElement element = rule.getAlternatives();
if (element instanceof Keyword && Strings.isEmpty(element.getCardinality())) {
String value = ((Keyword) element).getValue();
if (value.equals(keyword.getValue()))
acceptor.acceptError("The keyword '" + value + "' hides the terminal rule " + rule.getName() + ".", keyword, XtextPackage.Literals.KEYWORD__VALUE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, null);
}
}
}
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class OverriddenValueInspector method caseRuleCall.
@Override
public Boolean caseRuleCall(RuleCall object) {
AbstractRule calledRule = object.getRule();
if (calledRule == null || calledRule.eIsProxy() || calledRule instanceof TerminalRule || calledRule instanceof EnumRule)
return Boolean.FALSE;
ParserRule parserRule = (ParserRule) calledRule;
if (GrammarUtil.isDatatypeRule(parserRule))
return Boolean.FALSE;
if (parserRule.isFragment()) {
visitFragment(object);
if (GrammarUtil.isMultipleCardinality(object))
visitFragment(object);
}
if (!addVisited(parserRule))
return Boolean.FALSE;
Multimap<String, AbstractElement> prevAssignedFeatures = assignedFeatures;
assignedFeatures = newMultimap();
doSwitch(parserRule.getAlternatives());
for (String feature : assignedFeatures.keySet()) prevAssignedFeatures.put(feature, object);
assignedFeatures = prevAssignedFeatures;
removeVisited(parserRule);
return Boolean.FALSE;
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class ValidEntryRuleInspector method caseAlternatives.
@Override
public Pair<Boolean, Boolean> caseAlternatives(Alternatives object) {
boolean valid = true;
boolean instantiated = !GrammarUtil.isOptionalCardinality(object);
for (AbstractElement element : object.getElements()) {
Pair<Boolean, Boolean> elementResult = doSwitch(element);
valid &= elementResult.getFirst().booleanValue();
instantiated &= elementResult.getSecond().booleanValue();
}
return Tuples.create(valid, instantiated);
}
Aggregations