Search in sources :

Example 6 with EnumRule

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

the class GrammarParserTest method testEnum_04.

@Test
public void testEnum_04() 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/4'\n" + "Model: enumValue=ExistingEnum;\n" + "enum ExistingEnum: SameName;";
    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.getLiteral(), 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)

Example 7 with EnumRule

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

the class GrammarParserTest method testEnum_10.

@Test
public void testEnum_10() throws Exception {
    String modelAsString = "grammar TestLanguage with org.eclipse.xtext.enumrules.EnumRulesTestLanguage\n" + "import 'classpath:/org/eclipse/xtext/enumrules/enums.ecore'\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/10'\n" + "Model: enumValue=ExistingEnum;\n" + "enum ExistingEnum: SameName;";
    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("SameName", 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)

Example 8 with EnumRule

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

the class XtextValidationTest method testCheckRuleCallInUnorderedGroup_03.

@Test
public void testCheckRuleCallInUnorderedGroup_03() throws Exception {
    XtextValidator validator = get(XtextValidator.class);
    UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
    RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
    EnumRule enumRule = XtextFactory.eINSTANCE.createEnumRule();
    ruleCall.setRule(enumRule);
    unorderedGroup.getElements().add(ruleCall);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
    validator.setMessageAcceptor(messageAcceptor);
    validator.checkRuleCallInUnorderedGroup(ruleCall);
    messageAcceptor.validate();
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 9 with EnumRule

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

the class PsiAntlrGrammarGenerator method _dataTypeEbnf2.

@Override
protected String _dataTypeEbnf2(final RuleCall it, final boolean supportActions) {
    String _xifexpression = null;
    if (supportActions) {
        String _switchResult = null;
        AbstractRule _rule = it.getRule();
        boolean _matched = false;
        if (_rule instanceof EnumRule) {
            boolean _isAssigned = GrammarUtil.isAssigned(it);
            if (_isAssigned) {
                _matched = true;
            }
        }
        if (!_matched) {
            if (_rule instanceof ParserRule) {
                boolean _isAssigned = GrammarUtil.isAssigned(it);
                if (_isAssigned) {
                    _matched = true;
                }
            }
        }
        if (_matched) {
            _switchResult = super._dataTypeEbnf2(it, supportActions);
        }
        if (!_matched) {
            if (_rule instanceof EnumRule) {
                _matched = true;
            }
            if (!_matched) {
                if (_rule instanceof ParserRule) {
                    _matched = true;
                }
            }
            if (_matched) {
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("{");
                _builder.newLine();
                _builder.append("\t");
                CharSequence _markComposite = this.markComposite(it);
                _builder.append(_markComposite, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("}");
                _builder.newLine();
                String __dataTypeEbnf2 = super._dataTypeEbnf2(it, supportActions);
                _builder.append(__dataTypeEbnf2);
                _builder.newLineIfNotEmpty();
                _builder.append("{");
                _builder.newLine();
                _builder.append("\t");
                CharSequence _doneComposite = this.doneComposite(it);
                _builder.append(_doneComposite, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("}");
                _builder.newLine();
                _switchResult = _builder.toString();
            }
        }
        if (!_matched) {
            if (_rule instanceof TerminalRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _markLeaf = this.markLeaf(it);
                _builder_1.append(_markLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                String _localVar = this._grammarAccessExtensions.localVar(it);
                _builder_1.append(_localVar);
                _builder_1.append("=");
                String __dataTypeEbnf2_1 = super._dataTypeEbnf2(it, supportActions);
                _builder_1.append(__dataTypeEbnf2_1);
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _doneLeaf = this.doneLeaf(it, this._grammarAccessExtensions.localVar(it));
                _builder_1.append(_doneLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            _switchResult = super._dataTypeEbnf2(it, supportActions);
        }
        _xifexpression = _switchResult;
    } else {
        _xifexpression = super._dataTypeEbnf2(it, supportActions);
    }
    return _xifexpression;
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 10 with EnumRule

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

the class PsiAntlrGrammarGenerator method _ebnf2.

@Override
protected String _ebnf2(final RuleCall it, final AntlrOptions options, final boolean supportActions) {
    String _xifexpression = null;
    if (supportActions) {
        String _switchResult = null;
        AbstractRule _rule = it.getRule();
        boolean _matched = false;
        if (_rule instanceof EnumRule) {
            boolean _isAssigned = GrammarUtil.isAssigned(it);
            if (_isAssigned) {
                _matched = true;
            }
        }
        if (!_matched) {
            if (_rule instanceof ParserRule) {
                boolean _isAssigned = GrammarUtil.isAssigned(it);
                if (_isAssigned) {
                    _matched = true;
                }
            }
        }
        if (_matched) {
            _switchResult = super._ebnf2(it, options, supportActions);
        }
        if (!_matched) {
            if (_rule instanceof EnumRule) {
                _matched = true;
            }
            if (!_matched) {
                if (_rule instanceof ParserRule) {
                    boolean _isDatatypeRule = GrammarUtil.isDatatypeRule(AntlrGrammarGenUtil.<AbstractRule>getOriginalElement(it.getRule()));
                    if (_isDatatypeRule) {
                        _matched = true;
                    }
                }
            }
            if (_matched) {
                StringConcatenation _builder = new StringConcatenation();
                {
                    boolean _isBacktrack = options.isBacktrack();
                    if (_isBacktrack) {
                        _builder.append("{");
                        _builder.newLine();
                        _builder.append("\t");
                        _builder.append("/* */");
                        _builder.newLine();
                        _builder.append("}");
                        _builder.newLine();
                    }
                }
                _builder.append("{");
                _builder.newLine();
                _builder.append("\t");
                CharSequence _markComposite = this.markComposite(it);
                _builder.append(_markComposite, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("}");
                _builder.newLine();
                String __ebnf2 = super._ebnf2(it, options, supportActions);
                _builder.append(__ebnf2);
                _builder.newLineIfNotEmpty();
                _builder.append("{");
                _builder.newLine();
                _builder.append("\t");
                CharSequence _doneComposite = this.doneComposite(it);
                _builder.append(_doneComposite, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("}");
                _builder.newLine();
                _switchResult = _builder.toString();
            }
        }
        if (!_matched) {
            if (_rule instanceof ParserRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                {
                    boolean _isBacktrack_1 = options.isBacktrack();
                    if (_isBacktrack_1) {
                        _builder_1.append("{");
                        _builder_1.newLine();
                        _builder_1.append("\t");
                        _builder_1.append("/* */");
                        _builder_1.newLine();
                        _builder_1.append("}");
                        _builder_1.newLine();
                    }
                }
                _builder_1.append("{");
                _builder_1.newLine();
                {
                    boolean _isEObjectFragmentRuleCall = GrammarUtil.isEObjectFragmentRuleCall(it);
                    if (_isEObjectFragmentRuleCall) {
                        _builder_1.append("\t");
                        _builder_1.append("if (!$current) {");
                        _builder_1.newLine();
                        _builder_1.append("\t");
                        _builder_1.append("\t");
                        CharSequence _associateWithSemanticElement = this.associateWithSemanticElement();
                        _builder_1.append(_associateWithSemanticElement, "\t\t");
                        _builder_1.newLineIfNotEmpty();
                        _builder_1.append("\t");
                        _builder_1.append("\t");
                        _builder_1.append("$current = true;");
                        _builder_1.newLine();
                        _builder_1.append("\t");
                        _builder_1.append("}");
                        _builder_1.newLine();
                    }
                }
                _builder_1.append("\t");
                CharSequence _markComposite_1 = this.markComposite(it);
                _builder_1.append(_markComposite_1, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                String _localVar = this._grammarAccessExtensions.localVar(it);
                _builder_1.append(_localVar);
                _builder_1.append("=");
                String __ebnf2_1 = super._ebnf2(it, options, supportActions);
                _builder_1.append(__ebnf2_1);
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                _builder_1.append("$current = $");
                String _localVar_1 = this._grammarAccessExtensions.localVar(it);
                _builder_1.append(_localVar_1, "\t");
                _builder_1.append(".current;");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("\t");
                CharSequence _doneComposite_1 = this.doneComposite(it);
                _builder_1.append(_doneComposite_1, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            if (_rule instanceof TerminalRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _markLeaf = this.markLeaf(it);
                _builder_1.append(_markLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                String _localVar = this._grammarAccessExtensions.localVar(it);
                _builder_1.append(_localVar);
                _builder_1.append("=");
                String __ebnf2_1 = super._ebnf2(it, options, supportActions);
                _builder_1.append(__ebnf2_1);
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _doneLeaf = this.doneLeaf(it, this._grammarAccessExtensions.localVar(it));
                _builder_1.append(_doneLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            _switchResult = super._ebnf2(it, options, supportActions);
        }
        _xifexpression = _switchResult;
    } else {
        _xifexpression = super._ebnf2(it, options, supportActions);
    }
    return _xifexpression;
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

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