Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Want to directly call generated parser
Want to directly call generated parser [message #554905] Tue, 24 August 2010 16:41 Go to next message
Rob  is currently offline Rob Friend
Messages: 3
Registered: August 2010
Junior Member
I would like to call the generated parser generated by xtext. In the previous version I was able to use a special workflow file.
With the new mwe2 workflow format I am a bit lost.
I tried creating the parser directly:

fr = new FileReader("scripts/test1.mrk");

LexerProvider lp = new LexerProvider(InternalMarkerLexer.class);
mp.setLexerProvider(lp);

IParseResult ipr = mp.parse(fr);
fr.close();

But I do not know what to do with the TokenDefProvider and the above code bails out with a null pointer exception on that object.

Does somebody have a snippet or suggestion of how to call the generated parser from Java ?
Re: Want to directly call generated parser [message #554907 is a reply to message #554905] Tue, 24 August 2010 16:47 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Rob,

why do you want to use the parser directly?

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 24.08.10 18:41, schrieb Rob:
> I would like to call the generated parser generated by xtext. In the
> previous version I was able to use a special workflow file.
> With the new mwe2 workflow format I am a bit lost.
> I tried creating the parser directly:
>
> fr = new FileReader("scripts/test1.mrk");
>
> LexerProvider lp = new LexerProvider(InternalMarkerLexer.class);
> mp.setLexerProvider(lp);
>
> IParseResult ipr = mp.parse(fr);
> fr.close();
>
> But I do not know what to do with the TokenDefProvider and the above
> code bails out with a null pointer exception on that object.
>
> Does somebody have a snippet or suggestion of how to call the generated
> parser from Java ?
Re: Want to directly call generated parser [message #554918 is a reply to message #554907] Tue, 24 August 2010 17:43 Go to previous messageGo to next message
Rob  is currently offline Rob Friend
Messages: 3
Registered: August 2010
Junior Member
Hi Sebastian,

I am using the abstract syntax tree generated by the parser in an interpreter. So it's a bit different from the usual usage of generating code.
In the ideal case I could just call some parse(filename) method and get the abstract syntax tree (EMF structure) back. Do you have a suggestion of how to achieve this with the new mve2 format or otherwise ?

Kind regards,
Rob
Re: Want to directly call generated parser [message #554921 is a reply to message #554918] Tue, 24 August 2010 18:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hello Rob,

you can do this with emf means. see http://www.eclipse.org/Xtext/documentation/latest/xtext.html #processing_Xtext_models in the docs.

Regards
Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Want to directly call generated parser [message #554923 is a reply to message #554905] Tue, 24 August 2010 18:12 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi Rob

I call the parser directly for JUnit tests.

I just set a breakpoint within my model access and followed the stack back.

Regards

Ed Willink

On 24/08/2010 17:41, Rob wrote:
> I would like to call the generated parser generated by xtext. In the
> previous version I was able to use a special workflow file.
> With the new mwe2 workflow format I am a bit lost.
> I tried creating the parser directly:
>
> fr = new FileReader("scripts/test1.mrk");
>
> LexerProvider lp = new LexerProvider(InternalMarkerLexer.class);
> mp.setLexerProvider(lp);
>
> IParseResult ipr = mp.parse(fr);
> fr.close();
>
> But I do not know what to do with the TokenDefProvider and the above
> code bails out with a null pointer exception on that object.
>
> Does somebody have a snippet or suggestion of how to call the generated
> parser from Java ?
Re: Want to directly call generated parser [message #554933 is a reply to message #554923] Tue, 24 August 2010 19:14 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Am 8/24/10 8:12 PM, schrieb Ed Willink:
> Hi Rob
>
> I call the parser directly for JUnit tests.

Never do this (and if don't tell others ;-))

Sven

--
--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: Want to directly call generated parser [message #554956 is a reply to message #554905] Tue, 24 August 2010 21:38 Go to previous messageGo to next message
Rob  is currently offline Rob Friend
Messages: 3
Registered: August 2010
Junior Member
Thanks guys,

With your info I could get it done. This is the snippet that does the trick:

Injector injector = new MarkerStandaloneSetup().createInjectorAndDoEMFRegistration() ;
ResourceSet set = injector.getInstance(ResourceSet.class);

URI uri = URI.createFileURI("scripts/test1.mrk");
Resource res = set.getResource(uri, true);
Object obj = res.getContents().get(0);
Model model = (Model) obj;

EList<Diagnostic> syntaxErrors = res.getErrors();


Kind regards,
Rob
Re: Want to directly call generated parser [message #554997 is a reply to message #554956] Wed, 25 August 2010 06:41 Go to previous message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Am 8/24/10 11:38 PM, schrieb Rob:
> Thanks guys,
>
> With your info I could get it done. This is the snippet that does the
> trick:
>
> Injector injector = new
> MarkerStandaloneSetup().createInjectorAndDoEMFRegistration() ;
> ResourceSet set = injector.getInstance(ResourceSet.class);
>
> URI uri = URI.createFileURI("scripts/test1.mrk");
> Resource res = set.getResource(uri, true);
> Object obj = res.getContents().get(0);
> Model model = (Model) obj;
>
> EList<Diagnostic> syntaxErrors = res.getErrors();

And if you are interested in semantic errors (i.e. validation) as well,
you should use
((XtextResource)res).getResourceServiceProvider().getResourc eValidator().validate(res,.....)

Sven


--
--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Previous Topic:creating your own scoping fragment
Next Topic:Xtext, GWT and Guice
Goto Forum:
  


Current Time: Fri Apr 19 22:20:57 GMT 2024

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

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

Back to the top