简体   繁体   中英

Parsing Xtext grammar with Xtext

I would like to parse a Xtext grammar with Xtext. Therefore I took the grammar from GitHub and adapted it a little bit. Everything works fine except the import of grammars and the defining of reused grammars with "with".

So when I create a Xtext-file that should be parsed, eg:

grammar org.xtext.example.mydsl.Expression with org.eclipse.xtext.common.Terminals

import "http://www.xtext.org/example/mydsl/MyDsl" as mydsl
generate expression "http://www.xtext.org/example/mydsl/Expression"

I got the following errors:

Line 1: Couldn't resolve reference to Grammar 'org.eclipse.xtext.common.Terminals'. (Even if I change the feature name to importURI or importedNamespace of the root rule and use a grammar defined in the same workspace!)

Line 3/4: Couldn't resolve reference to EPackage ' http://www.xtext.org/example/mydsl/ ...'.

However, I need the complete grammar for my further work and this includes especially the reused grammar (such like the Terminals, Xbase or any other grammar in the workspace) because the grammar could contain rules that reference rules from the reused one.

Is there a way to resolve the grammar? I thought already about Scoping but failed in understanding how I could use it in my case.

BTW, is there a way to parse the file extension .xtext? I get the warning, that two content parsers are implementing the same file extension and I get my model parsed in the normal Xtext-manner. Is there a way to switch to my content parser?

You may follow the dialog on https://www.eclipse.org/forums/index.php/t/1067192/ the dicussion is about using fragments to change the worflow.

For to programmatically parse (ONLY PARSING!!) xtext-files I wrote some lines of code:

public static void ParseGrammar()
{
    String t = "grammar org.xtext.example.Entity with org.eclipse.xtext.common.Terminals\n" +

    "generate entity \"http://www.xtext.org/example/Entity\"\n" +

    "Model:\n" +
    "  (types+=Type)*;\n" +

    "Type:\n" +
    "  TypeDef | Entity;\n" +

    "TypeDef:\n" +
    "  \"typedef\" name=ID (\"mapsto\" mappedType=JAVAID)?;\n" +

    "JAVAID:\n" +
     " name=ID(\".\" ID)*;\n" +

    "Entity:\n" +
    "  \"entity\" name=ID (\"extends\" superEntity=[Entity])?\n" +
    "  \"{\"\n" +
    "    (attributes+=Attribute)*\n" +
    "  \"}\";\n" +

    "Attribute:\n" +
    "  type=[Type] (many?=\"*\")? name=ID;";

    new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
    Injector injector = Guice.createInjector(new XtextRuntimeModule());

    Reader reader = new InputStreamReader(new StringInputStream(t));

    IParser parser = injector.getInstance(IParser.class);
    IParseResult result = parser.parse(reader);
    boolean err = result.hasSyntaxErrors();
    EObject eRoot = result.getRootASTElement();
}

like you can see it uses "... with org.eclipse.xtext.common.Terminals ...". It did run without any errors and produced an AST.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM