Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Is it possible to parse a subset of the grammar
Is it possible to parse a subset of the grammar [message #1828998] Tue, 23 June 2020 15:11 Go to next message
Eclipse UserFriend
I have an XText grammar that has a top level element named model `Model` that consists of multiple `Statement`s which contain `Expression`s. I can parse the Model object from the a string of dsl using the following code below. Since my grammar defines Expression I am wondering can I also parse a string containing just an Expression type by defining a sub tree in the grammar somehow? I'm hoping it is possible to optionally parse a subset of the grammar.

String source = "x = 1 + 1;" + "\n" + "y = 2+2;";

Injector injector = new DslStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.createResource(URI.createURI("dummy:/dummy.dsl"));
InputStream in = new ByteArrayInputStream(source.getBytes());
resource.load(in, resourceSet.getLoadOptions());
Model model = (Model) resource.getContents().get(0);


I want to do:

String source = "1 + 1";
Expression expression = (Expression) resource.getContents().get(0);

[Updated on: Tue, 23 June 2020 15:22] by Moderator

Re: Is it possible to parse a subset of the grammar [message #1829000 is a reply to message #1828998] Tue, 23 June 2020 15:26 Go to previous messageGo to next message
Eclipse UserFriend
your resource will be a XtextResource which allows you to call

org.eclipse.xtext.resource.XtextResource.setEntryPoint(ParserRule)
Re: Is it possible to parse a subset of the grammar [message #1829001 is a reply to message #1829000] Tue, 23 June 2020 15:57 Go to previous messageGo to next message
Eclipse UserFriend
How do I access the generated parser rules?

I see GrammarUtil.findRuleForName(grammar, "com.example.Expression"), but I can't seem to find the instance of grammar. Is there another helper somewhere?

Thank you for the quick reply, this is exactly what I was looking for.
Re: Is it possible to parse a subset of the grammar [message #1829002 is a reply to message #1829001] Tue, 23 June 2020 15:59 Go to previous message
Eclipse UserFriend
you can inject/ask injector for YourDslGrammarAccess
Previous Topic:cross-reference linking and xbase
Next Topic:Formatter in whitespace aware language
Goto Forum:
  


Current Time: Thu Jul 17 06:57:51 EDT 2025

Powered by FUDForum. Page generated in 0.05266 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top