Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Syntactic sugar
Syntactic sugar [message #708951] Wed, 03 August 2011 10:06 Go to next message
Alexander is currently offline AlexanderFriend
Messages: 7
Registered: July 2009
Junior Member
hello,

In the context of the domain model example, the generation of
getters/setters is done twice. The Validator has sources for that (the
new operations are added to the JvmType- Instances), later the code
generator does the same. That's not really code duplication here, but it
seems a little bit odd. Especially if a language has all the syntactic
features to express getters, it seems to me that it would be a better
approach to change the AST (so, insert getters and setters into the AST)
and have no special code for them in Validator and Code Generator.
Especially for languages with a lot of constructs which are to beautify
the syntax only (think of java- style foreach loops, they can easily be
expressed as for- loops or maybe C- style array access where a[5] is the
same as *(a+5), due to the fact that a is a pointer to the first array
element) model-to-model transformation first seems to be a good approach
to simplify code generation.

Is there some hook in xText to do that? So, can I register some model
transformer to be executed before the Validator or at least before the
generator?
(To do something like that before validation could be a problem, because
errors in the transformed parts of the AST can not be reported properly).

Thanks,
Alex
Re: Syntactic sugar [message #709192 is a reply to message #708951] Wed, 03 August 2011 16:02 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

I think you are mixing type inference and code generation. The domain model example generates - as an example - Java bean code, while the Jvm model inferer infers types for the concepts of the language in order to make them referable within expressions or as targets where type references are used. The generator could also produce other kind of artifacts, or just do a model-to-model transformation. At the end, it's just Java code that does something with a given Model instance. One often desired side effect is code generation, where Xtext just adds a first-level hook for. What you do in the doGenerate() method is up to the implementer.

Kind regards,
~Karsten


Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Re: Syntactic sugar [message #709273 is a reply to message #709192] Wed, 03 August 2011 17:44 Go to previous message
Alexander is currently offline AlexanderFriend
Messages: 7
Registered: July 2009
Junior Member
Yes, that's right.

What I was looking for is a way to do a model-to-model transformation
BEFORE my code generation starts.
I wasn't talking about type inference or references, but about a way to
perform model-to-model transformation in xText context.

Take the foreach- loop example.

Given a DSL, were (as in Java), those two statements are equivalent:

for(String str:strs){ System.out.println(str); }

and

for(Iterator<String> it=strs.iterator();it.hasNext();){
String str=it.next();
System.out.println(str);
}

so I don't need to write code generation for both constructs. I can
easily write code generation for the second and use model-to-model
transformation to transform all instances of the first example before
code generation. I may even transform them before validation, because
all possible errors in the first example still can be detected in the
second example (the type of strs has no "iterator()"- Method, the
iterator gives no strings, the println- method does not accept "String"
as argument).

Of course I can write some transformer in xTend and call it as first
thing in my generator and my validator as well.
What I am looking for is a build-in mechanism in xText so I don't need
to call it by myself.

Actually: if I call them in my generator and my validator, will they be
executed twice on any AST? Or is the same AST instance passed to
generator and validator?

On 03.08.2011 18:02, Karsten Thoms wrote:
> I think you are mixing type inference and code generation. The domain
> model example generates - as an example - Java bean code, while the Jvm
> model inferer infers types for the concepts of the language in order to
> make them referable within expressions or as targets where type
> references are used. The generator could also produce other kind of
> artifacts, or just do a model-to-model transformation. At the end, it's
> just Java code that does something with a given Model instance. One
> often desired side effect is code generation, where Xtext just adds a
> first-level hook for. What you do in the doGenerate() method is up to
> the implementer.
>
> Kind regards,
> ~Karsten
Previous Topic:Cross-references for multi-word identifiers
Next Topic:Custom validation of Java Language
Goto Forum:
  


Current Time: Thu Apr 25 07:26:08 GMT 2024

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

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

Back to the top