Search in sources :

Example 1 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class BaseContentAssistParser method getFollowElements.

protected Collection<FE> getFollowElements(InternalParser parser, AbstractElement entryPoint) {
    String ruleName = getRuleName(entryPoint);
    if (ruleName == null) {
        if (entryPoint instanceof RuleCall) {
            RuleCall call = (RuleCall) entryPoint;
            AbstractRule rule = call.getRule();
            if (rule instanceof ParserRule) {
                ruleName = getRuleNames().getAntlrRuleName(rule);
            }
        }
    }
    if (ruleName == null) {
        throw new IllegalStateException("entryPoint: " + entryPoint);
    }
    return getFollowElements(parser, ruleName);
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 2 with AbstractRule

use of org.eclipse.xtext.AbstractRule 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 3 with AbstractRule

use of org.eclipse.xtext.AbstractRule 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 4 with AbstractRule

use of org.eclipse.xtext.AbstractRule 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 5 with AbstractRule

use of org.eclipse.xtext.AbstractRule 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)

Aggregations

AbstractRule (org.eclipse.xtext.AbstractRule)222 Test (org.junit.Test)128 Grammar (org.eclipse.xtext.Grammar)106 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)96 ParserRule (org.eclipse.xtext.ParserRule)94 EObject (org.eclipse.emf.ecore.EObject)59 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 RuleCall (org.eclipse.xtext.RuleCall)36 AbstractElement (org.eclipse.xtext.AbstractElement)34 TerminalRule (org.eclipse.xtext.TerminalRule)20 XtextResource (org.eclipse.xtext.resource.XtextResource)17 EClassifier (org.eclipse.emf.ecore.EClassifier)14 EnumRule (org.eclipse.xtext.EnumRule)14 ArrayList (java.util.ArrayList)12 Assignment (org.eclipse.xtext.Assignment)12 Group (org.eclipse.xtext.Group)12 InternalEObject (org.eclipse.emf.ecore.InternalEObject)11 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)11 EClass (org.eclipse.emf.ecore.EClass)10 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)10