Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Using ParseHelper outside of the test project (How can I use ParseHelper outside of the test project )
Using ParseHelper outside of the test project [message #1771002] Mon, 21 August 2017 04:26 Go to next message
Rohit Verma is currently offline Rohit VermaFriend
Messages: 21
Registered: August 2017
Junior Member

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 #1771087 is a reply to message #1771002] Mon, 21 August 2017 19:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
what about injector.getInstance(ParseHelper.class) ?
what is your actual goal?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Mon, 21 August 2017 19:51]

Report message to a moderator

Re: Using ParseHelper outside of the test project [message #1771403 is a reply to message #1771087] Thu, 24 August 2017 17:27 Go to previous message
Rohit Verma is currently offline Rohit VermaFriend
Messages: 21
Registered: August 2017
Junior Member
Christian Dietrich wrote on Mon, 21 August 2017 19:50
what 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.
Previous Topic:building the xtext project with the maven build with other java projects
Next Topic:Blank *not* being hidden (ignored) despite rules demanding it
Goto Forum:
  


Current Time: Fri Apr 26 06:43:55 GMT 2024

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

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

Back to the top