Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How do I use my language
How do I use my language [message #711896] Sat, 06 August 2011 19:42 Go to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
I'm trying to use Xtext to write a simple scripting language that defines functions that calculate a value. I think I understand the basics of defining the grammar for the language and generating the lexer/parser/etc. Now I'm trying to figure out how to use the language at run time to process the functions and calculate the values.

I'm expecting that I need to initialize the system and pass a file/string containing the script statements to something that will parse the script. I then need to interact with the parsed data to pull out the statements and process them. It isn't obvious from the documentation how this is done. Is there an example somewhere that shows how to actually use your language after you have created it?
Re: How do I use my language [message #711907 is a reply to message #711896] Sat, 06 August 2011 20:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

there are several possibilities to process the models. basically (code/text) generation or interpretation.
sadly some of the docs on this seem to be lost in the Xtext 2.0 docs.

(1) writing a code generator using Xtend2, Xpand, Acceleo or any other EMF capable generator technology. In the domain model example you'll find an example for this. the docs mention this too. http://www.eclipse.org/Xtext/documentation/2_0_0/040-first-code-generator.php
(2) writing an interpreter (reading the model and processing it)
Details depend on if you want to do this standalone or live.
In the Xtext 1.0 docs there was a hint on standalone. this should still work. http://www.eclipse.org/Xtext/documentation/1_0_1/xtext.html#processing_Xtext_models
Markus built a framework for this http://code.google.com/a/eclipselabs.org/p/xtext-typesystem. don't know if this works with Xtext 2.0 too.
The Xtext 1.0 Arithmetics example might be a look worth for a live interpreter too.

In both casing the task is basically:
(1) read the model
(2) walk over the model (a java object tree) and process some output.

To give further hints please tell if you want to execute the stuff standalone / action triggered / save triggered etc
and what kind of output you want to produce.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I use my language [message #711916 is a reply to message #711907] Sat, 06 August 2011 20:28 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
The Eclipse b3 language has a quite comprehensive interpreter and type
inference that you may want to take a look at as an example.

Regards
- henrik

On 8/6/11 10:06 PM, Christian Dietrich wrote:
> Hi,
>
> there are several possibilities to process the models. basically
> (code/text) generation or interpretation.
> sadly some of the docs on this seem to be lost in the Xtext 2.0 docs.
>
> (1) writing a code generator using Xtend2, Xpand, Acceleo or any other
> EMF capable generator technology. In the domain model example you'll
> find an example for this. the docs mention this too.
> http://www.eclipse.org/Xtext/documentation/2_0_0/040-first-code-generator.php
>
> (2) writing an interpreter (reading the model and processing it) Details
> depend on if you want to do this standalone or live.
> In the Xtext 1.0 docs there was a hint on standalone. this should still
> work.
> http://www.eclipse.org/Xtext/documentation/1_0_1/xtext.html#processing_Xtext_models
>
> Markus built a framework for this
> http://code.google.com/a/eclipselabs.org/p/xtext-typesystem. don't know
> if this works with Xtext 2.0 too.
> The Xtext 1.0 Arithmetics example might be a look worth for a live
> interpreter too.
>
> In both casing the task is basically:
> (1) read the model
> (2) walk over the model (a java object tree) and process some output.
>
> To give further hints please tell if you want to execute the stuff
> standalone / action triggered / save triggered etc
> and what kind of output you want to produce.
>
> ~Christian
Re: How do I use my language [message #712132 is a reply to message #711916] Sun, 07 August 2011 03:09 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
On 08/06/2011 03:28 PM, Henrik Lindberg wrote:
> The Eclipse b3 language has a quite comprehensive interpreter and type
> inference that you may want to take a look at as an example.
>
> Regards
> - henrik
>

I'm not looking for a build managment system which is what b3 appears to
be from the description. I need to be able to process the language I
develop.

> On 8/6/11 10:06 PM, Christian Dietrich wrote:
>> Hi,
>>
>> there are several possibilities to process the models. basically
>> (code/text) generation or interpretation.
>> sadly some of the docs on this seem to be lost in the Xtext 2.0 docs.
>>
>> (1) writing a code generator using Xtend2, Xpand, Acceleo or any other
>> EMF capable generator technology. In the domain model example you'll
>> find an example for this. the docs mention this too.
>> http://www.eclipse.org/Xtext/documentation/2_0_0/040-first-code-generator.php
>>
>>
>> (2) writing an interpreter (reading the model and processing it) Details
>> depend on if you want to do this standalone or live.
>> In the Xtext 1.0 docs there was a hint on standalone. this should still
>> work.
>> http://www.eclipse.org/Xtext/documentation/1_0_1/xtext.html#processing_Xtext_models
>>
>>
>> Markus built a framework for this
>> http://code.google.com/a/eclipselabs.org/p/xtext-typesystem. don't know
>> if this works with Xtext 2.0 too.
>> The Xtext 1.0 Arithmetics example might be a look worth for a live
>> interpreter too.
>>
>> In both casing the task is basically:
>> (1) read the model
>> (2) walk over the model (a java object tree) and process some output.
>>
>> To give further hints please tell if you want to execute the stuff
>> standalone / action triggered / save triggered etc
>> and what kind of output you want to produce.
>>
>> ~Christian
>
Re: How do I use my language [message #712139 is a reply to message #711907] Sun, 07 August 2011 03:19 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
On 08/06/2011 03:06 PM, Christian Dietrich wrote:
> Hi,
>
> there are several possibilities to process the models. basically
> (code/text) generation or interpretation.
> sadly some of the docs on this seem to be lost in the Xtext 2.0 docs.
>
> (1) writing a code generator using Xtend2, Xpand, Acceleo or any other
> EMF capable generator technology. In the domain model example you'll
> find an example for this. the docs mention this too.
> http://www.eclipse.org/Xtext/documentation/2_0_0/040-first-code-generator.php
>
> (2) writing an interpreter (reading the model and processing it) Details
> depend on if you want to do this standalone or live.
> In the Xtext 1.0 docs there was a hint on standalone. this should still
> work.
> http://www.eclipse.org/Xtext/documentation/1_0_1/xtext.html#processing_Xtext_models
>

Hint is an appropriate description of the link. It doesn't really show
you how you can process the language, it basically says that you can and
there a optional properties that you can use to deal with the
processing. The Xtext project appears to really be lacking an example
of actually processing a language and doing something with it. I'm not
a language expert nor a model expert. Without an example, I'm not sure
Xtext will be useful to my needs.

> Markus built a framework for this
> http://code.google.com/a/eclipselabs.org/p/xtext-typesystem. don't know
> if this works with Xtext 2.0 too.
> The Xtext 1.0 Arithmetics example might be a look worth for a live
> interpreter too.
>
> In both casing the task is basically:
> (1) read the model
> (2) walk over the model (a java object tree) and process some output.
>
> To give further hints please tell if you want to execute the stuff
> standalone / action triggered / save triggered etc
> and what kind of output you want to produce.
>
I guess I want to execute the stuff standalone. I would like to develop
a calculation engine. The language would be used to define calculations
for financial values. A given calculation would be defined in a
"source" file using the DSL I'm developing. The engine would need to
read the calculation source files and interpret them. The calculations
would produce a result that would be published out.
> ~Christian
Re: How do I use my language [message #712427 is a reply to message #712139] Sun, 07 August 2011 08:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

i don't get it: hendrik pointed to b3 as an example for an interpreter so have a look at b3s sourcecode.
i still do not understand what problems ou have with processing since it is just instances of java classes
you walk over. can you be a bit more elaborating why this makes problems to you
or ask questions on concrete problems?

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I use my language [message #712442 is a reply to message #712427] Sun, 07 August 2011 08:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

and just do complete this: here is a "very fancy" interpreter for the Xtexts Hello World Greeting DSL

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.xtext.example.mydsl.MyDslStandaloneSetup;
import org.xtext.example.mydsl.myDsl.Greeting;
import org.xtext.example.mydsl.myDsl.Model;

import com.google.inject.Injector;


public class Interpreter {

	public static void main(String[] args) {
		Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		ResourceSet rs = injector.getInstance(ResourceSet.class);
		Resource r = rs.getResource(URI.createURI("src/model.mydsl"), true);
		Model m = (Model) r.getContents().get(0);
		for (Greeting g : m.getGreetings()) {
			System.out.println("Hello, " + g.getName());
		}
	}
	
}



~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I use my language [message #712583 is a reply to message #712132] Sun, 07 August 2011 13:02 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 8/7/11 5:09 AM, David Wegener wrote:
> On 08/06/2011 03:28 PM, Henrik Lindberg wrote:
>> The Eclipse b3 language has a quite comprehensive interpreter and type
>> inference that you may want to take a look at as an example.
>
> I'm not looking for a build managment system which is what b3 appears to
> be from the description. I need to be able to process the language I
> develop.
>
The tip was that if you want to write an interpreter you could look at
the *source code* of b3, not run it.

- henrik
Re: How do I use my language [message #712802 is a reply to message #712442] Sun, 07 August 2011 18:41 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
On 08/07/2011 03:49 AM, Christian Dietrich wrote:
> Hi,
>
> and just do complete this: here is a "very fancy" interpreter for the
> Xtexts Hello World Greeting DSL
>
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.xtext.example.mydsl.MyDslStandaloneSetup;
> import org.xtext.example.mydsl.myDsl.Greeting;
> import org.xtext.example.mydsl.myDsl.Model;
>
> import com.google.inject.Injector;
>
>
> public class Interpreter {
>
> public static void main(String[] args) {
> Injector injector = new
> MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
> ResourceSet rs = injector.getInstance(ResourceSet.class);
> Resource r = rs.getResource(URI.createURI("src/model.mydsl"), true);
> Model m = (Model) r.getContents().get(0);
> for (Greeting g : m.getGreetings()) {
> System.out.println("Hello, " + g.getName());
> }
> }
>
> }
>
>
>
> ~Christian
Thanks, I think I'm starting to get it now. I think part of the problem
was not realizing that the classes that you need to process the language
are created when you generate the grammar. I kept thinking that you
needed to call Xtext provided classes to access the AST. This is where
the confusion came from.

I'm still not completely sure about the relationship between the src,
src-gen, and xtend-gen directories in the projects. A better
explanation of what classes go into each location and in what cases you
need to use them would be helpful.
Re: How do I use my language [message #713173 is a reply to message #712802] Mon, 08 August 2011 07:20 Go to previous message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

src: Sources that are ment to be manually maintained
src-gen: Generated sources from Xtext. Do not touch. They will be regenerated when you start the generator workflow.
xtend-gen: Generated sources from .xtend files. Do not touch. They will be regenerated when you save an .xtend file.

Xtext follows the Generation Gap Pattern. You can read about it on the web. Short: Strict seperation of manual, generated and generic code.

~Karsten


Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Previous Topic:[XTEND] Standalone compiler
Next Topic:[Question] Xtext 2.0 / Xtend2 Generator Development
Goto Forum:
  


Current Time: Thu Mar 28 20:06:34 GMT 2024

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

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

Back to the top