Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext standalone application beginner tutorial
Xtext standalone application beginner tutorial [message #900652] Wed, 08 August 2012 01:01 Go to next message
Alfredo Motta is currently offline Alfredo MottaFriend
Messages: 41
Registered: June 2012
Member
Hello everybody,

I just jumped into the xtext project and I went through the different tutorials here
http://www.eclipse.org/Xtext/documentation.html#FirstFiveMinutes

My goal is to use the parser generated by Xtext in my own java project so that I could invoke something like

parser.parse(myDSLString)


and navigate the AST of myDSLString to do my own stuff.
However I could not found any stap-by-step tutorial on how to import the parser generated by xtext in my standalone java application. Any help is appreciated.

Thank you

[Updated on: Wed, 08 August 2012 01:01]

Report message to a moderator

Re: Xtext standalone application beginner tutorial [message #900859 is a reply to message #900652] Wed, 08 August 2012 18:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

this highly depends on your needs: do you want to read a single file or a bunch.
do you want to use a mwe/mwe2 workflow ...
never the less as a starting point
http://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F

~Christian

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext standalone application beginner tutorial [message #901104 is a reply to message #900859] Thu, 09 August 2012 19:43 Go to previous message
Alfredo Motta is currently offline Alfredo MottaFriend
Messages: 41
Registered: June 2012
Member
Thank you Christian, I was able to solve the problem and this is the guide step by step for the next one interested

1- Create your sample grammar in Xtext with the 5 minute tutorial (Cattura.png)
2- Create a run configuration for the project (Cattura1.png)
3- Export the project as a runnable jar in the lib folder of your standalone project (Cattura2.png+Cattura3.png)
4- I got warnings and incomprehensible stuffs, but it works even with them (Cattura4.png)
5- Add the jar to your standalone project (Cattura5.png)
6- Run with the code from the XText FAQ and check your greeting

		new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
		Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
		
		Resource resource = resourceSet.createResource(URI.createURI("file:/example.mydsl"));
		InputStream in = new ByteArrayInputStream("Hello World".getBytes());
		resource.load(in, resourceSet.getLoadOptions());
		Model model = (Model) resource.getContents().get(0);
		
		for(Greeting e:model.getGreetings()){
			System.out.println(e);
		}


IMPORTANT: The extension in this line
	
Resource resource = resourceSet.createResource(URI.createURI("file:/example.mydsl"));

should be the same of the mwe2 workflow in the grammar project
var file.extensions = "mydsl"


Cheers
  • Attachment: Cattura.PNG
    (Size: 131.57KB, Downloaded 638 times)
  • Attachment: Cattura1.PNG
    (Size: 183.55KB, Downloaded 687 times)
  • Attachment: Cattura2.PNG
    (Size: 178.97KB, Downloaded 550 times)
  • Attachment: Cattura4.PNG
    (Size: 213.19KB, Downloaded 581 times)
  • Attachment: Cattura5.PNG
    (Size: 176.46KB, Downloaded 545 times)
Previous Topic:Loading a resource
Next Topic:Make my model nodes implement an interface?
Goto Forum:
  


Current Time: Fri Mar 29 08:19:39 GMT 2024

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

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

Back to the top