Search in sources :

Example 16 with EnumRule

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

the class AbstractSyntacticSequencer method acceptNode.

protected void acceptNode(INode node) {
    Object ge = node.getGrammarElement();
    if (ge instanceof Keyword)
        acceptUnassignedKeyword((Keyword) ge, node.getText(), (ILeafNode) node);
    else if (ge instanceof RuleCall) {
        RuleCall rc = (RuleCall) ge;
        if (rc.getRule() instanceof TerminalRule)
            acceptUnassignedTerminal(rc, node.getText(), (ILeafNode) node);
        else if (rc.getRule() instanceof ParserRule) {
            StringBuilder text = new StringBuilder();
            for (ILeafNode leaf : node.getLeafNodes()) if (text.length() > 0 || !leaf.isHidden())
                text.append(leaf.getText());
            acceptUnassignedDatatype(rc, text.toString(), (ICompositeNode) node);
        } else if (rc.getRule() instanceof EnumRule)
            acceptUnassignedEnum(rc, node.getText(), (ICompositeNode) node);
    } else if (ge instanceof Action)
        acceptUnassignedAction((Action) ge);
    else
        throw new RuntimeException("Unexpected grammar element: " + node.getGrammarElement());
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) Action(org.eclipse.xtext.Action) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Keyword(org.eclipse.xtext.Keyword) EObject(org.eclipse.emf.ecore.EObject) TerminalRule(org.eclipse.xtext.TerminalRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 17 with EnumRule

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

the class NodeModelSemanticSequencer method acceptSemantic.

protected boolean acceptSemantic(EObject semanticObject, AbstractElement ele, Object value, INode node) {
    Assignment ass = GrammarUtil.containingAssignment(ele);
    int index = -2;
    if (ass != null) {
        EStructuralFeature feat = semanticObject.eClass().getEStructuralFeature(ass.getFeature());
        if (feat != null) {
            if (feat.isMany())
                index = ((List<?>) semanticObject.eGet(feat)).indexOf(value);
            else
                index = -1;
        }
    }
    String token = node.getText().trim();
    if (ele instanceof Action) {
        if (((Action) ele).getFeature() != null) {
            if (sequenceAcceptor.enterAssignedAction((Action) ele, (EObject) value, (ICompositeNode) node)) {
                createSequence(ele, (EObject) value);
                sequenceAcceptor.leaveAssignedAction((Action) ele, (EObject) value);
            }
            return true;
        }
    } else if (GrammarUtil.containingCrossReference(ele) != null) {
        if (ele instanceof RuleCall) {
            RuleCall rc = (RuleCall) ele;
            if (rc.getRule() instanceof ParserRule) {
                sequenceAcceptor.acceptAssignedCrossRefDatatype(rc, token, (EObject) value, index, (ICompositeNode) node);
                return true;
            }
            if (rc.getRule() instanceof TerminalRule) {
                sequenceAcceptor.acceptAssignedCrossRefTerminal(rc, token, (EObject) value, index, (ILeafNode) node);
                return true;
            }
            if (rc.getRule() instanceof EnumRule) {
                sequenceAcceptor.acceptAssignedCrossRefEnum(rc, token, (EObject) value, index, (ICompositeNode) node);
                return true;
            }
        // } else if (ele instanceof Keyword) {
        // acceptor.acceptAssignedCrossRefKeyword((Keyword) ele, token, (EObject) value, index,(ILeafNode) node);
        // return true;
        }
    } else if (ass != null) {
        if (ele instanceof RuleCall) {
            RuleCall rc = (RuleCall) ele;
            if (rc.getRule() instanceof ParserRule) {
                if (rc.getRule().getType().getClassifier() instanceof EClass) {
                    if (sequenceAcceptor.enterAssignedParserRuleCall(rc, (EObject) value, (ICompositeNode) node)) {
                        createSequence(rc.getRule(), (EObject) value);
                        sequenceAcceptor.leaveAssignedParserRuleCall(rc, (EObject) value);
                    }
                } else
                    sequenceAcceptor.acceptAssignedDatatype(rc, token, value, index, (ICompositeNode) node);
                return true;
            }
            if (rc.getRule() instanceof TerminalRule) {
                sequenceAcceptor.acceptAssignedTerminal(rc, token, value, index, (ILeafNode) node);
                return true;
            }
            if (rc.getRule() instanceof EnumRule) {
                sequenceAcceptor.acceptAssignedEnum(rc, token, value, index, (ICompositeNode) node);
                return true;
            }
        } else if (ele instanceof Keyword) {
            if (GrammarUtil.isBooleanAssignment(ass))
                sequenceAcceptor.acceptAssignedKeyword((Keyword) ele, token, true, index, (ILeafNode) node);
            else
                sequenceAcceptor.acceptAssignedKeyword((Keyword) ele, token, (String) value, index, (ILeafNode) node);
            return true;
        }
    }
    return false;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) Keyword(org.eclipse.xtext.Keyword) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EnumRule(org.eclipse.xtext.EnumRule) EClass(org.eclipse.emf.ecore.EClass) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) List(java.util.List) TerminalRule(org.eclipse.xtext.TerminalRule)

Example 18 with EnumRule

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

the class AntlrGrammarGenerator method compileReturns.

protected CharSequence compileReturns(final AbstractRule it, final AntlrOptions options) {
    CharSequence _switchResult = null;
    boolean _matched = false;
    if (it instanceof EnumRule) {
        _matched = true;
        _switchResult = "[Enumerator current=null]";
    }
    if (!_matched) {
        if (it instanceof ParserRule) {
            boolean _isDatatypeRule = GrammarUtil.isDatatypeRule(AntlrGrammarGenUtil.<ParserRule>getOriginalElement(((ParserRule) it)));
            if (_isDatatypeRule) {
                _matched = true;
                _switchResult = "[AntlrDatatypeRuleToken current=new AntlrDatatypeRuleToken()]";
            }
        }
    }
    if (!_matched) {
        if (it instanceof ParserRule) {
            boolean _isEObjectFragmentRule = GrammarUtil.isEObjectFragmentRule(AntlrGrammarGenUtil.<ParserRule>getOriginalElement(((ParserRule) it)));
            if (_isEObjectFragmentRule) {
                _matched = true;
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("[");
                String _currentType = this.getCurrentType();
                _builder.append(_currentType);
                _builder.append(" current=in_current]");
                _switchResult = _builder;
            }
        }
    }
    if (!_matched) {
        if (it instanceof ParserRule) {
            _matched = true;
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("[");
            String _currentType = this.getCurrentType();
            _builder.append(_currentType);
            _builder.append(" current=null]");
            _switchResult = _builder;
        }
    }
    if (!_matched) {
        throw new IllegalStateException(("Unexpected rule: " + it));
    }
    return _switchResult;
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation)

Example 19 with EnumRule

use of org.eclipse.xtext.EnumRule in project xtext-xtend by eclipse.

the class XtendAntlrGrammarGeneratorHelper method getTokens.

public LinkedHashSet<String> getTokens(final Grammar it) {
    final LinkedHashSet<String> tokens = CollectionLiterals.<String>newLinkedHashSet();
    List<ParserRule> _allParserRules = GrammarUtil.allParserRules(it);
    List<EnumRule> _allEnumRules = GrammarUtil.allEnumRules(it);
    Iterable<AbstractRule> _plus = Iterables.<AbstractRule>concat(_allParserRules, _allEnumRules);
    for (final AbstractRule rule : _plus) {
        this.collectTokens(rule, tokens);
    }
    return tokens;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 20 with EnumRule

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

the class GrammarParserTest method testEnum_05.

@Test
public void testEnum_05() throws Exception {
    String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "import 'classpath:/org/eclipse/xtext/enumrules/enums.ecore'\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/5'\n" + "Model: enumValue=ExistingEnum;\n" + "enum ExistingEnum: SameName = 'value';";
    Grammar grammar = (Grammar) getModel(modelAsString);
    assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
    checkEnums(grammar);
    EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
    assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTestLanguage/imported", pack.getNsURI());
    EEnum eEnum = (EEnum) pack.getEClassifier("ExistingEnum");
    assertNotNull(eEnum);
    assertEquals(3, eEnum.getELiterals().size());
    EEnumLiteral value = eEnum.getELiterals().get(0);
    assertEquals(ExistingEnum.SAME_NAME.getName(), value.getName());
    assertEquals(ExistingEnum.SAME_NAME.getValue(), value.getValue());
    assertEquals(ExistingEnum.SAME_NAME.getLiteral(), value.getLiteral());
    EnumRule rule = (EnumRule) grammar.getRules().get(1);
    assertEquals(eEnum, rule.getType().getClassifier());
    EnumLiteralDeclaration decl = (EnumLiteralDeclaration) rule.getAlternatives();
    assertEquals(value, decl.getEnumLiteral());
    assertNotNull(decl.getLiteral());
    assertEquals("value", decl.getLiteral().getValue());
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) Grammar(org.eclipse.xtext.Grammar) EEnum(org.eclipse.emf.ecore.EEnum) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.Test)

Aggregations

EnumRule (org.eclipse.xtext.EnumRule)29 ParserRule (org.eclipse.xtext.ParserRule)21 TerminalRule (org.eclipse.xtext.TerminalRule)17 AbstractRule (org.eclipse.xtext.AbstractRule)14 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)9 RuleCall (org.eclipse.xtext.RuleCall)9 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)8 EEnum (org.eclipse.emf.ecore.EEnum)5 EPackage (org.eclipse.emf.ecore.EPackage)5 Grammar (org.eclipse.xtext.Grammar)5 List (java.util.List)4 EObject (org.eclipse.emf.ecore.EObject)4 Action (org.eclipse.xtext.Action)4 Assignment (org.eclipse.xtext.Assignment)4 Keyword (org.eclipse.xtext.Keyword)4 TypeRef (org.eclipse.xtext.TypeRef)4 EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)3 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)3 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)3 CrossReference (org.eclipse.xtext.CrossReference)3