Search in sources :

Example 26 with RuleCall

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

the class TreeConstructionReportImpl method getDiagnosticMessage.

protected String getDiagnosticMessage(AssignmentToken token) {
    Assignment ass = (Assignment) token.getGrammarElement();
    Object value = token.getEObjectConsumer().getConsumable(ass.getFeature(), false);
    if (value == null) {
        EStructuralFeature f = token.getEObjectConsumer().getEObject().eClass().getEStructuralFeature(ass.getFeature());
        if (f == null)
            return "The current object of type '" + token.getEObjectConsumer().getEObject().eClass().getName() + "' does not have a feature named '" + ass.getFeature() + "'";
        String cls = f.getEContainingClass() == token.getEObjectConsumer().getEObject().eClass() ? f.getEContainingClass().getName() : f.getEContainingClass().getName() + "(" + token.getEObjectConsumer().getEObject().eClass().getName() + ")";
        String feat = cls + "." + f.getName();
        if (f.isMany()) {
            int size = ((List<?>) token.getEObjectConsumer().getEObject().eGet(f)).size();
            return "All " + size + " values of " + feat + " have been consumed. " + "More are needed to continue here.";
        } else
            return feat + " is not set.";
    } else {
        ErrorAcceptor err = new ErrorAcceptor();
        for (RuleCall ruleCall : GrammarUtil.containedRuleCalls(token.getGrammarElement())) {
            if (ruleCall.getRule() instanceof EnumRule) {
                if (enumSerializer.isValid(token.getEObject(), ruleCall, value, err))
                    return null;
            } else if (ruleCall.getRule().getType().getClassifier() instanceof EDataType) {
                if (valueSerializer.isValid(token.getEObject(), ruleCall, value, err))
                    return null;
            }
        }
        return err.getMessage();
    }
}
Also used : Assignment(org.eclipse.xtext.Assignment) EnumRule(org.eclipse.xtext.EnumRule) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) List(java.util.List) IErrorAcceptor(org.eclipse.xtext.parsetree.reconstr.ITokenSerializer.IErrorAcceptor) RuleCall(org.eclipse.xtext.RuleCall)

Example 27 with RuleCall

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

the class DefaultLocationInFileProvider method getLocationNodes.

protected List<INode> getLocationNodes(EObject obj) {
    final EStructuralFeature nameFeature = getIdentifierFeature(obj);
    if (nameFeature != null) {
        List<INode> result = NodeModelUtils.findNodesForFeature(obj, nameFeature);
        if (!result.isEmpty())
            return result;
    }
    List<INode> resultNodes = Lists.newArrayList();
    final ICompositeNode startNode = findNodeFor(obj);
    INode keywordNode = null;
    // use LeafNodes instead of children?
    for (INode child : startNode.getChildren()) {
        EObject grammarElement = child.getGrammarElement();
        if (grammarElement instanceof RuleCall) {
            RuleCall ruleCall = (RuleCall) grammarElement;
            String ruleName = ruleCall.getRule().getName();
            if (ruleName.equals("ID")) {
                resultNodes.add(child);
            }
        } else if (grammarElement instanceof Keyword) {
            // TODO use only keywords, that aren't symbols like '=' ?
            if (keywordNode == null && useKeyword((Keyword) grammarElement, obj)) {
                keywordNode = child;
            }
        }
    }
    if (resultNodes.isEmpty() && keywordNode != null)
        resultNodes.add(keywordNode);
    return resultNodes;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Keyword(org.eclipse.xtext.Keyword) EObject(org.eclipse.emf.ecore.EObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) RuleCall(org.eclipse.xtext.RuleCall)

Example 28 with RuleCall

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

the class Linker method ensureLinked.

private void ensureLinked(EObject obj, IDiagnosticProducer producer, ICompositeNode node, Set<EReference> handledReferences, boolean dontCheckParent) {
    for (INode abstractNode = node.getFirstChild(); abstractNode != null; abstractNode = abstractNode.getNextSibling()) {
        EObject grammarElement = abstractNode.getGrammarElement();
        if (grammarElement instanceof CrossReference) {
            CrossReference ref = (CrossReference) grammarElement;
            producer.setNode(abstractNode);
            ensureIsLinked(obj, abstractNode, ref, handledReferences, producer);
        } else if (grammarElement instanceof RuleCall && abstractNode instanceof ICompositeNode) {
            RuleCall ruleCall = (RuleCall) grammarElement;
            AbstractRule calledRule = ruleCall.getRule();
            if (calledRule instanceof ParserRule && ((ParserRule) calledRule).isFragment()) {
                ensureLinked(obj, producer, (ICompositeNode) abstractNode, handledReferences, true);
            }
        }
    }
    if (!dontCheckParent && shouldCheckParentNode(node)) {
        ensureLinked(obj, producer, node.getParent(), handledReferences, false);
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) INode(org.eclipse.xtext.nodemodel.INode) EObject(org.eclipse.emf.ecore.EObject) CrossReference(org.eclipse.xtext.CrossReference) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 29 with RuleCall

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

the class KeywordAlternativeConverter method setRule.

/**
 * @throws IllegalArgumentException if the rule is not a datatype rule or does not match
 *   the pattern <pre>RuleName: 'keyword' | 'other';</pre>
 */
@Override
public void setRule(AbstractRule rule) {
    if (!GrammarUtil.isDatatypeRule(rule))
        throw new IllegalArgumentException(rule.getName() + " is not a data type rule");
    if (!(rule.getAlternatives() instanceof Alternatives)) {
        if (rule.getAlternatives() instanceof RuleCall) {
            delegateRule = ((RuleCall) rule.getAlternatives()).getRule();
            keywords = Collections.emptySet();
            return;
        }
        throw mismatchedRuleBody(rule);
    }
    Alternatives alternatives = (Alternatives) rule.getAlternatives();
    ImmutableSet.Builder<String> builder = ImmutableSet.builder();
    for (AbstractElement element : alternatives.getElements()) {
        processElement(element, rule, builder);
    }
    keywords = builder.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) AbstractElement(org.eclipse.xtext.AbstractElement) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall)

Example 30 with RuleCall

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

the class AbstractFormattingConfig method getAbstractElements.

protected List<AbstractElement> getAbstractElements(EObject obj) {
    if (obj instanceof AbstractElement)
        return Collections.singletonList((AbstractElement) obj);
    if (obj instanceof AbstractRule) {
        AbstractRule rule = (AbstractRule) obj;
        if (rule.getType().getClassifier() instanceof EClass)
            return Collections.singletonList(rule.getAlternatives());
        List<AbstractElement> result = Lists.newArrayList();
        for (RuleCall rc : grammarAccess.findRuleCalls(rule)) {
            CrossReference cr = GrammarUtil.containingCrossReference(rc);
            result.add(cr == null ? rc : cr);
        }
        return result;
    }
    return null;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) AbstractElement(org.eclipse.xtext.AbstractElement) CrossReference(org.eclipse.xtext.CrossReference) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Aggregations

RuleCall (org.eclipse.xtext.RuleCall)95 ParserRule (org.eclipse.xtext.ParserRule)41 AbstractRule (org.eclipse.xtext.AbstractRule)36 EObject (org.eclipse.emf.ecore.EObject)33 Test (org.junit.Test)33 Grammar (org.eclipse.xtext.Grammar)28 AbstractElement (org.eclipse.xtext.AbstractElement)26 Assignment (org.eclipse.xtext.Assignment)22 TerminalRule (org.eclipse.xtext.TerminalRule)19 Action (org.eclipse.xtext.Action)17 CrossReference (org.eclipse.xtext.CrossReference)13 Group (org.eclipse.xtext.Group)13 TypeRef (org.eclipse.xtext.TypeRef)13 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)13 ArrayList (java.util.ArrayList)11 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)11 InternalEObject (org.eclipse.emf.ecore.InternalEObject)10 Keyword (org.eclipse.xtext.Keyword)10 EClass (org.eclipse.emf.ecore.EClass)9 EnumRule (org.eclipse.xtext.EnumRule)9