Search in sources :

Example 66 with Keyword

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

the class KeywordBasedValueConverter method setRule.

/**
 * @throws IllegalArgumentException if the rule is not a datatype rule or does not fulfill
 *   the pattern <pre>RuleName: 'keyword' | 'other';</pre>
 */
@Override
public void setRule(AbstractRule rule) {
    this.rule = rule;
    if (!GrammarUtil.isDatatypeRule(rule))
        throw new IllegalArgumentException(rule.getName() + " is not a data type rule");
    if (!(rule.getAlternatives() instanceof Alternatives) && !(rule.getAlternatives() instanceof Keyword)) {
        throw new IllegalArgumentException(rule.getName() + " is not a simple keyword nor an alternative");
    }
    if (rule.getAlternatives() instanceof Keyword) {
        keywords = ImmutableSet.of(keywordToString((Keyword) rule.getAlternatives()));
    } else {
        Alternatives alternatives = (Alternatives) rule.getAlternatives();
        ImmutableSet.Builder<String> builder = ImmutableSet.builder();
        for (AbstractElement element : alternatives.getElements()) {
            if (!(element instanceof Keyword)) {
                throw new IllegalArgumentException(rule.getName() + "'s body does not contain an alternative of keywords");
            }
            builder.add(keywordToString((Keyword) element));
        }
        keywords = builder.build();
    }
}
Also used : Keyword(org.eclipse.xtext.Keyword) ImmutableSet(com.google.common.collect.ImmutableSet) AbstractElement(org.eclipse.xtext.AbstractElement) Alternatives(org.eclipse.xtext.Alternatives)

Example 67 with Keyword

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

the class AbstractSyntacticSequencer method accept.

@SuppressWarnings("deprecation")
protected void accept(ISynState emitter, INode node, RuleCallStack stack) {
    switch(emitter.getType()) {
        case UNASSIGNED_PARSER_RULE_ENTER:
            RuleCall rc1 = (RuleCall) emitter.getGrammarElement();
            delegate.enterUnassignedParserRuleCall(rc1);
            stack.push(rc1);
            return;
        case UNASSIGNED_PARSER_RULE_EXIT:
            RuleCall rc2 = (RuleCall) emitter.getGrammarElement();
            delegate.leaveUnssignedParserRuleCall(rc2);
            RuleCall lastRc = stack.pop();
            if (lastRc != rc2) {
                if (errorAcceptor != null)
                    errorAcceptor.accept(diagnosticProvider.createUnexpectedStackStateDiagnostic(contexts.get(contexts.size() - 1).semanticObject, stack, lastRc, emitter));
            }
            return;
        case UNASSIGEND_ACTION_CALL:
            delegate.acceptUnassignedAction((Action) emitter.getGrammarElement());
            return;
        case UNASSIGEND_KEYWORD:
            Keyword keyword = (Keyword) emitter.getGrammarElement();
            String token = node != null ? node.getText() : keyword.getValue();
            delegate.acceptUnassignedKeyword(keyword, token, (ILeafNode) node);
            return;
        case UNASSIGNED_DATATYPE_RULE_CALL:
            RuleCall rc3 = (RuleCall) emitter.getGrammarElement();
            String value3 = getUnassignedRuleCallToken(rc3, node);
            delegate.acceptUnassignedDatatype(rc3, value3, (ICompositeNode) node);
            return;
        case UNASSIGNED_TERMINAL_RULE_CALL:
            RuleCall rc4 = (RuleCall) emitter.getGrammarElement();
            String value4 = getUnassignedRuleCallToken(rc4, node);
            delegate.acceptUnassignedTerminal(rc4, value4, (ILeafNode) node);
            return;
        case ASSIGNED_ACTION_CALL:
        case ASSIGNED_BOOLEAN_KEYWORD:
        case ASSIGNED_CROSSREF_DATATYPE_RULE_CALL:
        case ASSIGNED_CROSSREF_ENUM_RULE_CALL:
        case ASSIGNED_CROSSREF_KEYWORD:
        case ASSIGNED_CROSSREF_TERMINAL_RULE_CALL:
        case ASSIGNED_DATATYPE_RULE_CALL:
        case ASSIGNED_ENUM_RULE_CALL:
        case ASSIGNED_KEYWORD:
        case ASSIGNED_PARSER_RULE_CALL:
        case ASSIGNED_TERMINAL_RULE_CALL:
        case START:
        case STOP:
        case TRANSITION:
    }
    throw new RuntimeException("invalid state for emitting: " + emitter + " (" + emitter.getType() + ")");
}
Also used : Keyword(org.eclipse.xtext.Keyword) RuleCall(org.eclipse.xtext.RuleCall)

Example 68 with Keyword

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

the class NodeModelSemanticSequencer method createSequence.

@Override
public void createSequence(ISerializationContext context, EObject semanticObject) {
    SemanticNodeIterator ni = new SemanticNodeIterator(semanticObject);
    while (ni.hasNext()) {
        Triple<INode, AbstractElement, EObject> node = ni.next();
        if (node.getSecond() instanceof RuleCall) {
            RuleCall rc = (RuleCall) node.getSecond();
            TypeRef ruleType = rc.getRule().getType();
            if (ruleType == null || ruleType.getClassifier() instanceof EClass)
                acceptSemantic(semanticObject, rc, node.getThird(), node.getFirst());
            else if (GrammarUtil.containingCrossReference(node.getSecond()) != null) {
                EStructuralFeature feature = FeatureFinderUtil.getFeature(node.getSecond(), semanticObject.eClass());
                acceptSemantic(semanticObject, rc, semanticObject.eGet(feature), node.getFirst());
            } else {
                String strVal = NodeModelUtils.getTokenText(node.getFirst());
                Object val = valueConverter.toValue(strVal, ruleNames.getQualifiedName(rc.getRule()), node.getFirst());
                acceptSemantic(semanticObject, rc, val, node.getFirst());
            }
        } else if (node.getSecond() instanceof Keyword)
            acceptSemantic(semanticObject, node.getSecond(), node.getFirst().getText(), node.getFirst());
        else if (node.getSecond() instanceof Action) {
            acceptSemantic(semanticObject, node.getSecond(), semanticObject, node.getFirst());
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) Keyword(org.eclipse.xtext.Keyword) TypeRef(org.eclipse.xtext.TypeRef) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject)

Example 69 with Keyword

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

the class EnumLiteralSerializer method serializeAssignedEnumLiteral.

@Override
public String serializeAssignedEnumLiteral(EObject context, RuleCall ruleCall, Object value, INode node, Acceptor errorAcceptor) {
    Keyword nodeLit = getLiteral(node);
    Keyword modelLit = getLiteral(context, ruleCall, value);
    if (modelLit == null) {
        if (errorAcceptor != null)
            errorAcceptor.accept(diagnosticProvider.getInvalidEnumValueDiagnostic(context, ruleCall, value));
        return null;
    } else if (nodeLit != null && nodeLit.equals(modelLit))
        return tokenUtil.serializeNode(node);
    else
        return modelLit.getValue();
}
Also used : Keyword(org.eclipse.xtext.Keyword)

Example 70 with Keyword

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

the class ContentAssistContextFactory method isLikelyToBeValidProposal.

protected boolean isLikelyToBeValidProposal(INode lastCompleteNode, Iterable<ContentAssistContext> contexts) {
    for (ContentAssistContext context : contexts) {
        for (AbstractElement element : context.getFirstSetGrammarElements()) {
            if (element instanceof Keyword) {
                String keywordValue = ((Keyword) element).getValue();
                String lastText = ((ILeafNode) lastCompleteNode).getText();
                if (keywordValue.equals(lastText))
                    return true;
            }
        }
    }
    return false;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) AbstractElement(org.eclipse.xtext.AbstractElement) Keyword(org.eclipse.xtext.Keyword) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext)

Aggregations

Keyword (org.eclipse.xtext.Keyword)70 RuleCall (org.eclipse.xtext.RuleCall)17 EObject (org.eclipse.emf.ecore.EObject)16 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)14 INode (org.eclipse.xtext.nodemodel.INode)13 AbstractElement (org.eclipse.xtext.AbstractElement)10 AbstractRule (org.eclipse.xtext.AbstractRule)10 CrossReference (org.eclipse.xtext.CrossReference)9 Grammar (org.eclipse.xtext.Grammar)9 ParserRule (org.eclipse.xtext.ParserRule)9 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)9 Test (org.junit.Test)9 Action (org.eclipse.xtext.Action)8 Assignment (org.eclipse.xtext.Assignment)8 Alternatives (org.eclipse.xtext.Alternatives)7 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)7 EnumRule (org.eclipse.xtext.EnumRule)7 TerminalRule (org.eclipse.xtext.TerminalRule)6 Group (org.eclipse.xtext.Group)5 ArrayList (java.util.ArrayList)4