Search in sources :

Example 16 with Grammar

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

the class GrammarParserTest method testEnum_08.

@Test
public void testEnum_08() throws Exception {
    String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/8'\n" + "Model: enumValue=MyEnum;\n" + "enum MyEnum: Value | Value = 'foo';";
    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/AbstractEnumRulesTest/TestEnum/8", pack.getNsURI());
    EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
    assertNotNull(eEnum);
    assertEquals(1, eEnum.getELiterals().size());
    EEnumLiteral value = eEnum.getELiterals().get(0);
    assertEquals("Value", value.getName());
    assertEquals(0, value.getValue());
    assertEquals("Value", value.getLiteral());
}
Also used : 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 17 with Grammar

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

the class ElementFinderTest method grammarFinder.

private AbstractGrammarElementFinder grammarFinder() {
    final String file = "classpath:/org/eclipse/xtext/generator/grammarAccess/ElementFinderTestLanguage.xtext";
    XtextResourceSet rs = get(XtextResourceSet.class);
    rs.setClasspathURIContext(getClass());
    final Resource r = rs.getResource(URI.createURI(file), true);
    return new AbstractGrammarElementFinder() {

        @Override
        public Grammar getGrammar() {
            return (Grammar) r.getContents().get(0);
        }
    };
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Resource(org.eclipse.emf.ecore.resource.Resource) AbstractGrammarElementFinder(org.eclipse.xtext.service.AbstractElementFinder.AbstractGrammarElementFinder) Grammar(org.eclipse.xtext.Grammar)

Example 18 with Grammar

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

the class Bug265111Test method testParseGrammar.

@Test
public void testParseGrammar() throws Exception {
    String grammar = "grammar test with org.eclipse.xtext.grammarinheritance.BaseInheritanceTestLanguage\n" + "generate test 'http://test'\n" + "Root: A | Model;" + "A: 'A' name=ID;";
    Resource r = getResourceFromStringAndExpect(grammar, 1);
    Grammar g = (Grammar) r.getContents().get(0);
    assertEquals("test", g.getName());
    assertEquals(r.getErrors().toString(), 1, r.getErrors().size());
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) Grammar(org.eclipse.xtext.Grammar) Test(org.junit.Test)

Example 19 with Grammar

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

the class ToEcoreTrafoTest method testConcreteLanguageToMetamodel1.

@Test
public void testConcreteLanguageToMetamodel1() throws Exception {
    XtextResource r = getResource("classpath:/" + ConcreteTestLanguage.class.getName().replace('.', '/') + ".xtext");
    EObject element = r.getContents().get(0);
    Grammar g = (Grammar) element;
    List<AbstractMetamodelDeclaration> mms = Lists.<AbstractMetamodelDeclaration>newArrayList(Iterables.filter(g.getMetamodelDeclarations(), GeneratedMetamodel.class));
    assertNotNull(mms);
    assertEquals(1, mms.size());
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) Test(org.junit.Test)

Example 20 with Grammar

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

the class ToEcoreTrafoTest method testAbstractLanguageToMetamodel.

@Test
public void testAbstractLanguageToMetamodel() throws Exception {
    XtextResource r = getResource("classpath:/" + AbstractTestLanguage.class.getName().replace('.', '/') + ".xtext");
    Grammar element = (Grammar) r.getParseResult().getRootASTElement();
    if (!r.getErrors().isEmpty()) {
        EList<Diagnostic> errors = r.getErrors();
        for (Diagnostic syntaxError : errors) {
            logger.debug(syntaxError.getMessage() + " - " + syntaxError.getLine());
        }
        fail(errors.toString());
    }
    List<TerminalRule> lexerRules = GrammarUtil.allTerminalRules(element);
    assertEquals(8, lexerRules.size());
    List<EPackage> list = Xtext2EcoreTransformer.doGetGeneratedPackages(element);
    assertNotNull(list);
    assertEquals(0, list.size());
}
Also used : Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) TerminalRule(org.eclipse.xtext.TerminalRule) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.Test)

Aggregations

Grammar (org.eclipse.xtext.Grammar)356 Test (org.junit.Test)273 ParserRule (org.eclipse.xtext.ParserRule)151 AbstractRule (org.eclipse.xtext.AbstractRule)106 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)101 EObject (org.eclipse.emf.ecore.EObject)70 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 XtextResource (org.eclipse.xtext.resource.XtextResource)33 RuleCall (org.eclipse.xtext.RuleCall)28 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)23 TypeReference (org.eclipse.xtext.xtext.generator.model.TypeReference)23 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)21 Assignment (org.eclipse.xtext.Assignment)21 EPackage (org.eclipse.emf.ecore.EPackage)20 AbstractElement (org.eclipse.xtext.AbstractElement)17 TerminalRule (org.eclipse.xtext.TerminalRule)17 Resource (org.eclipse.emf.ecore.resource.Resource)16 EClassifier (org.eclipse.emf.ecore.EClassifier)13 InternalEObject (org.eclipse.emf.ecore.InternalEObject)13 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)12