Search in sources :

Example 11 with AbstractElement

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;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 12 with AbstractElement

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;
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Group(org.eclipse.xtext.Group) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) ArrayList(java.util.ArrayList) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EClass(org.eclipse.emf.ecore.EClass) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) CompoundElement(org.eclipse.xtext.CompoundElement) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 13 with AbstractElement

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);
            }
        }
    }
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) Keyword(org.eclipse.xtext.Keyword) Grammar(org.eclipse.xtext.Grammar) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 14 with AbstractElement

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;
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) AbstractElement(org.eclipse.xtext.AbstractElement) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 15 with AbstractElement

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);
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement)

Aggregations

AbstractElement (org.eclipse.xtext.AbstractElement)126 EObject (org.eclipse.emf.ecore.EObject)39 AbstractRule (org.eclipse.xtext.AbstractRule)34 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)31 RuleCall (org.eclipse.xtext.RuleCall)26 ParserRule (org.eclipse.xtext.ParserRule)18 Grammar (org.eclipse.xtext.Grammar)17 EList (org.eclipse.emf.common.util.EList)15 Assignment (org.eclipse.xtext.Assignment)15 Test (org.junit.Test)14 Group (org.eclipse.xtext.Group)13 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)13 List (java.util.List)12 ArrayList (java.util.ArrayList)11 XtextResource (org.eclipse.xtext.resource.XtextResource)11 Action (org.eclipse.xtext.Action)10 TerminalRule (org.eclipse.xtext.TerminalRule)10 Alternatives (org.eclipse.xtext.Alternatives)9 EClassifier (org.eclipse.emf.ecore.EClassifier)8 InternalEObject (org.eclipse.emf.ecore.InternalEObject)8