Using ParseHelper outside of the test project [message #1771002] |
Mon, 21 August 2017 00:26  |
Eclipse User |
|
|
|
I am trying to use Xtext to write a parser for a project that uses EMF metamodels (ECore files) as its backbone. The idea is that I create a new Xtext project (from existing Ecore models), select the relevant ECore files to generate the grammar and get an autogenerated parser.
Now, in this metamodel, there will be a node "Expression" which will have leaves as, say -- an integer literal and operator. So my objective is that I should be able to pass as arguments an expression like -- 10+6 and I should get in return the concrete syntax tree (Like I should know 10 is a literal integer, "+" is an operator, etc. ). Currently I am able to do this by using the XtextResourceSet class as below where I am returned the elements in this tree in a "EObjectContainmentList". However, this required me to use a file (t.dmodel in the example) which I cannot use in my project for some reason.
I looked in the ParseHelper class which lets me do the same thing without having to have an intermediate file. However, this does not work for me when I use the class outside the test project. If I try to, the object of the parsehelper is always returned as null. Why cannot I do this, I don't seem to understand -- request your help here.
// Sample code for XtextResourceSet which works
Injector injector = new DomainmodelStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.getResource(
URI.createURI("t.dmodel"), true);
EObject myModel = resource.getContents().get(0);
// Sample code for ParseHelper which doesn't work
Injector injector = new DomainmodelStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
ParseHelper<?> parseHelper = new ParseHelper();
try {
parseHelper.parse("");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I get the same issue if I try to inject the right classes like this:
@RunWith(XtextRunner.class)
@InjectWith(DomainmodelInjectorProvider.class)
@SuppressWarnings("all")
public class DomainmodelParsingTest {
@Inject
private ParseHelper<Domainmodel> parseHelper;
|
|
|
|
Re: Using ParseHelper outside of the test project [message #1771403 is a reply to message #1771087] |
Thu, 24 August 2017 13:27  |
Eclipse User |
|
|
|
Christian Dietrich wrote on Mon, 21 August 2017 19:50what about injector.getInstance(ParseHelper.class) ?
what is your actual goal?
Thank you very much Christian ! That was the only thing I needed to make it work.
Appreciate you taking the time to reply. My goal was to just instantiate an object of the ParseHelper class the way it was being done inside the JUnit test container.
|
|
|
Powered by
FUDForum. Page generated in 0.06439 seconds