Search in sources :

Example 31 with Assignment

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

the class FollowElementComputer method computeFollowElements.

protected void computeFollowElements(FollowElementCalculator calculator, FollowElement element, Multimap<Integer, List<AbstractElement>> visited) {
    List<AbstractElement> currentState = Lists.newArrayList(element.getLocalTrace());
    if (currentState.isEmpty() || currentState.get(currentState.size() - 1) != element.getGrammarElement()) {
        currentState.add(element.getGrammarElement());
    }
    if (!visited.put(element.getLookAhead(), currentState))
        return;
    if (element.getLookAhead() <= 1) {
        List<Integer> paramStack = element.getParamStack();
        int paramIndex = computeParamStackOffset(currentState, paramStack);
        for (AbstractElement abstractElement : currentState) {
            paramIndex = setParamConfigAndUpdateOffset(calculator, paramStack, paramIndex, abstractElement);
            Assignment ass = EcoreUtil2.getContainerOfType(abstractElement, Assignment.class);
            if (ass != null)
                calculator.doSwitch(ass);
            else {
                if (abstractElement instanceof UnorderedGroup && abstractElement == element.getGrammarElement()) {
                    calculator.doSwitch((UnorderedGroup) abstractElement, element.getHandledUnorderedGroupElements());
                } else {
                    calculator.doSwitch(abstractElement);
                    if (GrammarUtil.isOptionalCardinality(abstractElement)) {
                        EObject container = abstractElement.eContainer();
                        if (container instanceof Group) {
                            Group group = (Group) container;
                            int idx = group.getElements().indexOf(abstractElement);
                            if (idx == group.getElements().size() - 1) {
                                if (!currentState.contains(group) && GrammarUtil.isMultipleCardinality(group)) {
                                    calculator.doSwitch(group);
                                }
                            } else if (idx < group.getElements().size() - 1 && "?".equals(abstractElement.getCardinality())) {
                                // loops are fine
                                AbstractElement nextElement = group.getElements().get(idx + 1);
                                if (!currentState.contains(nextElement)) {
                                    calculator.doSwitch(nextElement);
                                }
                            }
                        }
                    } else if (isAlternativeWithEmptyPath(abstractElement)) {
                        EObject container = abstractElement.eContainer();
                        if (container instanceof Group) {
                            Group group = (Group) container;
                            int idx = group.getElements().indexOf(abstractElement);
                            if (!currentState.contains(group) && idx != group.getElements().size() - 1) {
                                AbstractElement next = group.getElements().get(idx + 1);
                                if (!currentState.contains(next)) {
                                    calculator.doSwitch(next);
                                }
                            }
                        }
                    }
                }
            }
        }
        // we need a synthetic rule call
        if (element.getTrace().equals(element.getLocalTrace())) {
            ParserRule parserRule = GrammarUtil.containingParserRule(element.getGrammarElement());
            if (parserRule != null) {
                RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
                ruleCall.setRule(parserRule);
                calculator.doSwitch(ruleCall);
            }
        }
        return;
    }
    Collection<FollowElement> followElements = parser.getFollowElements(element);
    for (FollowElement newElement : followElements) {
        if (newElement.getLookAhead() != element.getLookAhead() || newElement.getGrammarElement() != element.getGrammarElement()) {
            if (newElement.getLookAhead() == element.getLookAhead()) {
                int originalTraceSize = element.getLocalTrace().size();
                List<AbstractElement> newTrace = newElement.getLocalTrace();
                if (newTrace.size() > originalTraceSize) {
                    if (Collections.indexOfSubList(element.getLocalTrace(), newTrace.subList(originalTraceSize, newTrace.size())) != -1) {
                        continue;
                    }
                }
            }
            computeFollowElements(calculator, newElement, visited);
        }
    }
}
Also used : Group(org.eclipse.xtext.Group) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) ParserRule(org.eclipse.xtext.ParserRule) AbstractElement(org.eclipse.xtext.AbstractElement) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) EObject(org.eclipse.emf.ecore.EObject)

Example 32 with Assignment

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

the class FollowElementComputer method collectAbstractElements.

public void collectAbstractElements(Grammar grammar, EStructuralFeature feature, IFollowElementAcceptor followElementAcceptor) {
    for (Grammar superGrammar : grammar.getUsedGrammars()) {
        collectAbstractElements(superGrammar, feature, followElementAcceptor);
    }
    EClass declarator = feature.getEContainingClass();
    for (ParserRule rule : GrammarUtil.allParserRules(grammar)) {
        for (Assignment assignment : GrammarUtil.containedAssignments(rule)) {
            if (assignment.getFeature().equals(feature.getName())) {
                EClassifier classifier = GrammarUtil.findCurrentType(assignment);
                EClassifier compType = EcoreUtil2.getCompatibleType(declarator, classifier);
                if (compType == declarator) {
                    followElementAcceptor.accept(assignment);
                }
            }
        }
    }
}
Also used : Assignment(org.eclipse.xtext.Assignment) ParserRule(org.eclipse.xtext.ParserRule) EClass(org.eclipse.emf.ecore.EClass) EClassifier(org.eclipse.emf.ecore.EClassifier) Grammar(org.eclipse.xtext.Grammar)

Example 33 with Assignment

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

the class SequenceFeeder method accept.

public void accept(Keyword keyword, Object value, String token, int index) {
    Assignment ass = getAssignment(keyword);
    EStructuralFeature feature = getFeature(ass.getFeature());
    assertIndex(feature, index);
    assertValue(feature, value);
    ILeafNode node = getLeafNode(feature, index, index, value);
    acceptKeyword(ass, keyword, value, token, index, node);
}
Also used : Assignment(org.eclipse.xtext.Assignment) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature)

Example 34 with Assignment

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

the class SequenceFeeder method accept.

public void accept(Keyword keyword, Object value, String token) {
    Assignment ass = getAssignment(keyword);
    EStructuralFeature feature = getFeature(ass.getFeature());
    assertIndex(feature);
    assertValue(feature, value);
    ILeafNode node = getLeafNode(feature, value);
    acceptKeyword(ass, keyword, value, token, ISemanticSequenceAcceptor.NO_INDEX, node);
}
Also used : Assignment(org.eclipse.xtext.Assignment) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature)

Example 35 with Assignment

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

the class FeatureFinderUtil method getFeature.

/**
 * @since 2.0
 */
public static EStructuralFeature getFeature(AbstractElement grammarElement, EClass owner) {
    Preconditions.checkNotNull(owner);
    if (grammarElement == null)
        return null;
    String featureName = null;
    if (grammarElement instanceof Action)
        featureName = ((Action) grammarElement).getFeature();
    else {
        Assignment ass = GrammarUtil.containingAssignment(grammarElement);
        if (ass != null)
            featureName = ass.getFeature();
    }
    if (featureName != null)
        return owner.getEStructuralFeature(featureName);
    return null;
}
Also used : Assignment(org.eclipse.xtext.Assignment) Action(org.eclipse.xtext.Action)

Aggregations

Assignment (org.eclipse.xtext.Assignment)60 EObject (org.eclipse.emf.ecore.EObject)27 Test (org.junit.Test)25 RuleCall (org.eclipse.xtext.RuleCall)22 Grammar (org.eclipse.xtext.Grammar)21 AbstractElement (org.eclipse.xtext.AbstractElement)15 ParserRule (org.eclipse.xtext.ParserRule)15 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)14 Action (org.eclipse.xtext.Action)13 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)12 AbstractRule (org.eclipse.xtext.AbstractRule)12 Group (org.eclipse.xtext.Group)11 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)9 Alternatives (org.eclipse.xtext.Alternatives)8 CrossReference (org.eclipse.xtext.CrossReference)7 ArrayList (java.util.ArrayList)6 EClass (org.eclipse.emf.ecore.EClass)6 InternalEObject (org.eclipse.emf.ecore.InternalEObject)6 NamedArgument (org.eclipse.xtext.NamedArgument)6 LiteralCondition (org.eclipse.xtext.LiteralCondition)5