Is it possible to parse a subset of the grammar [message #1828998] |
Tue, 23 June 2020 19:11 |
Ben Holland Messages: 34 Registered: April 2019 |
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
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03942 seconds