Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 19:11 Go to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 22
Registered: April 2019
Junior Member
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 19:22]

Report message to a moderator

Re: Is it possible to parse a subset of the grammar [message #1829000 is a reply to message #1828998] Tue, 23 June 2020 19:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
your resource will be a XtextResource which allows you to call

org.eclipse.xtext.resource.XtextResource.setEntryPoint(ParserRule)


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Is it possible to parse a subset of the grammar [message #1829001 is a reply to message #1829000] Tue, 23 June 2020 19:57 Go to previous messageGo to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 22
Registered: April 2019
Junior Member
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 19:59 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
you can inject/ask injector for YourDslGrammarAccess

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:cross-reference linking and xbase
Next Topic:Formatter in whitespace aware language
Goto Forum:
  


Current Time: Fri Oct 23 00:51:38 GMT 2020

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

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

Back to the top