Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » how to parse a xtext grammar
how to parse a xtext grammar [message #1705595] Mon, 17 August 2015 17:02 Go to next message
Bruno Oliveira is currently offline Bruno OliveiraFriend
Messages: 5
Registered: February 2012
Junior Member
Hello,
Sorry by the noob question but i am very confused about xtext usage.
I created my grammar following the tutorials provided. Right now i have my grammar working but i don't know how used it in external programs. I want to create a separated project in which i can write the grammar (can also be stored in a file) and using the xtext API i want to validate the grammar and extract the values assigned.

For example, if i have a file: test.mysql with something like:
type = 'HELLO'

and a grammar like;
Composition:
'type=' type=STRING
;

How can i validate this grammar and how can i get the value that is associated to "type"?
To be more clear, i want to know if i can import some kind of jar that can give me a method like: validate(file.mydsl) and getvalue('Composition:type'), or something similar:)

please help me

Re: how to parse a xtext grammar [message #1705604 is a reply to message #1705595] Mon, 17 August 2015 17:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
you want to parse the model not the grammar right?

https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: how to parse a xtext grammar [message #1705683 is a reply to message #1705604] Tue, 18 August 2015 11:12 Go to previous messageGo to next message
Bruno Oliveira is currently offline Bruno OliveiraFriend
Messages: 5
Registered: February 2012
Junior Member
Yes, it is what i want. I follow the instructions and i am still confuse.

First, i got several errors related to the code:
resourceSet.createResource is not recognized
resource.load(in, resourceSet.getLoadOptions(); has an error and eclipse suggests a cast to ((ResourceImpl) resource).load(in, resourceSet.getLoadOptions());
Model is not recognized

There any xtext jar file (library) that i need to import to use these resources?
I want to create a java application without any relation to eclipse or emf.


[Updated on: Tue, 18 August 2015 11:24]

Report message to a moderator

Re: how to parse a xtext grammar [message #1705686 is a reply to message #1705683] Tue, 18 August 2015 11:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

simply place the code in a class in the .yourdsl project.
make sure you have the correct imports

org.eclipse.emf.ecore.resource.Resource
org.eclipse.emf.ecore.resource.ResourceSet
org.eclipse.emf.common.util.URI

If that does not work please share you complete class.

if your class is a java main you can use the export->
java-> runnable jar wizard to create a all in one jar


if you dont want to use the pseudo workspace use

URI.createURI("path/file.extension") and omit the
new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");



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

[Updated on: Tue, 18 August 2015 11:44]

Report message to a moderator

Re: how to parse a xtext grammar [message #1705737 is a reply to message #1705686] Tue, 18 August 2015 16:21 Go to previous messageGo to next message
Bruno Oliveira is currently offline Bruno OliveiraFriend
Messages: 5
Registered: February 2012
Junior Member
Christian Dietrich wrote on Tue, 18 August 2015 07:42
Hi,

simply place the code in a class in the .yourdsl project.
make sure you have the correct imports

org.eclipse.emf.ecore.resource.Resource
org.eclipse.emf.ecore.resource.ResourceSet
org.eclipse.emf.common.util.URI

If that does not work please share you complete class.

if your class is a java main you can use the export->
java-> runnable jar wizard to create a all in one jar


if you dont want to use the pseudo workspace use

URI.createURI("path/file.extension") and omit the
new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");


Thank you very much for your answer. Things are getting more clear now:)

Following the example in the faqs page, i have created a classe inside my org.text.raid.mysql (for now i am simplifying things). Everything makes sense with the imports you give me, but this line gives a cast convertion error:
Model model = (Model) resource.getContents().get(0);

i searched in forum and i added some lines. This is my class:
import java.io.IOException;

import org.eclipse.emf.ecore.EObject;

import org.eclipse.emf.ecore.resource.Resource;

import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.ui.internal.Model;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;



import com.google.inject.Injector;

import org.xtext.example.mydsl.MyDslStandaloneSetup;


public class teste {
	 
	    public void setup() throws IOException {
	    	//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.getResource(
	    	    URI.createURI("t.mydsl"), true);
	    	//Model model = (Model) resource.getContents().get(0);
	    	EObject myModel = resource.getContents().get(0);
	    	
	    	 Diagnostic diagnostic = Diagnostician.INSTANCE.validate(myModel);
	    	 System.out.println(diagnostic.toString());
	    	 if (myModel instanceof Model) {
	    		 System.out.println("WORKS");
	    	 }else{
	    		 System.out.println("FAIL");
	    	 }
	    	
	    }
	 
	    public static void main(String[] args){
	    	teste t = new teste();
	    	try {
				t.setup();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    
}


Well, it seems that myModel is not a instance of Model.

I have some questions about this code. When we create the URI to ("t.mydsl"), i am loading a file with an instance of my grammar. However how java knows how to validate this file? My dsl.xtext has the rules for grammar validation right? How things are connected?

Sorry by these basic questions.

Best regards
Re: how to parse a xtext grammar [message #1705738 is a reply to message #1705737] Tue, 18 August 2015 16:33 Go to previous message
Stefan Oehme is currently offline Stefan OehmeFriend
Messages: 159
Registered: April 2010
Location: Kiel
Senior Member

Hey Bruno,

the "Model" class is only an example. You have to adjust it to the entry rule of your Grammar.

EMF knows how to validate your model because you ran the StandaloneSetup. This registers your language's validator in the validator registry.

You can also use injector.getInstance(IResourceValidator) if you would like to use the Xtext validation API instead.

In general, injector.getInstance(...) allows you to get any service related to your language, so you could also run the code generator or whatever else you like.

Cheers,
Stefan
Previous Topic:Grammar with significant indentation
Next Topic:Problem with serializer comment insertion
Goto Forum:
  


Current Time: Tue Mar 19 05:23:32 GMT 2024

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

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

Back to the top