Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to build multiple entities in one rule?
How to build multiple entities in one rule? [message #1006763] Fri, 01 February 2013 17:25 Go to next message
Alexandre Frey is currently offline Alexandre FreyFriend
Messages: 3
Registered: February 2013
Junior Member
Hello:

How would you handle this situation: I want to parse blocks of statements mixed with variable declarations. Something like this:
{
var y = x+z;
z = 3;
var z;
}

The line "var y = x + z;" is a shortcut for: "var y; y = x + z;"

I'd like this equivalence to be reflected in the model: the model should define a class Block that contains a list of variable names and a list of statements. So, the line "var y = x + z" should produce both a variable declaration and a statement.

How would you suggest to proceed in Xtext in this situation?

Thanks, -- Alexandre.
Re: How to build multiple entities in one rule? [message #1006767 is a reply to message #1006763] Fri, 01 February 2013 18:04 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
I would keep it simple and have the model close to how your DSL looks.

The variables are in the statements which are in the block so have a Block rule that contains multiple Statements. The Statements are either a VariableDeclaration (first and third lines) or VariableAssignment (middle line). The two Variable types both have a 'name' (which would get values of y, z and z in your three example lines).

You can then get a list of declared variable names from the list of statements whenever you need it in the model inferrer processing. The name should be an accessible property of each Statement.

I know that's not what you asked but I can't think how to create multiple entities in one rule and do think it's going to cause less trouble later on to keep the model simple and close to the language structure.
Re: How to build multiple entities in one rule? [message #1006783 is a reply to message #1006767] Fri, 01 February 2013 19:40 Go to previous messageGo to next message
Alexandre Frey is currently offline Alexandre FreyFriend
Messages: 3
Registered: February 2013
Junior Member
Hi:

OK, you suggest the model should be closer to the concrete language syntax rather than to the abstract syntax without the shortcuts. I assume doing otherwise will likely complicate the language infrastructure (Eclipse editor and such) ?

Assuming I have lots of such shortcuts, I'm now considering having two levels of models:

  • the first model is generated by XText and is close to the concrete syntax
  • a second model would be designed by hand and closer to the language semantic

A kind of preprocessor would transform the first model into the second before the compilers/interpreters would run.

Does that sound reasonable and do you know if such techniques are commonly implemented?

-- Alexandre.
Re: How to build multiple entities in one rule? [message #1006796 is a reply to message #1006783] Sat, 02 February 2013 00:30 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
That's my thinking. It's not that you couldn't do it by modelling an abstract syntax but you are writing a compiler, which can be difficult enough as these forums show, and it has to parse the actual syntax so that's how I'd want to try to do it first.

I don't know how common it is but I have tried keeping a second model as I wanted extra details or concepts that weren't part of the visible syntax but after a struggle I found all of what I needed was met by re-examining the parsed model. Overall it made the code easier to work with. the second model which was a set of my own classes did not keep in step as the validation and compilation parts of xtext were running independently and since the compiler part was updating my own model the validator often complained that my model was missing data (since the compiler parts didn't run until after successful validation). It was chicken and egg and then more chickens. Or maybe I mean Catch 22.

So you may not get a complete run through your first generator and then start your second as you are hoping. The framework will keep reparsing and compiling parts as you type.

I'm not clear why you want a separate step of converting var y = x + z into var y and y = x + z. Presumably you are ultimately producing a compiled output so the quicker you can get to it the better. I don't see any problem in compiling the example code you give, apart from z being declared after it is first assigned.
Re: How to build multiple entities in one rule? [message #1006811 is a reply to message #1006796] Sat, 02 February 2013 08:19 Go to previous messageGo to next message
Alexandre Frey is currently offline Alexandre FreyFriend
Messages: 3
Registered: February 2013
Junior Member
Ian McDevitt wrote on Fri, 01 February 2013 19:30

I'm not clear why you want a separate step of converting var y = x + z into var y and y = x + z. Presumably you are ultimately producing a compiled output so the quicker you can get to it the better. I don't see any problem in compiling the example code you give, apart from z being declared after it is first assigned.


Well, somebody has to deal with syntactic sugar like the "var y = x +y" shortcut. I thought that trying to handle it upfront would simplify all the downstream processing. On the other hand, I now understand that XText is really there to handle the concrete syntax because all the language infrastructure (Eclipse editor and such) really works on the concrete syntax with the sugar.

It seems reasonable to let the compiler process the syntactic sugar on the fly while fetching the information from the model.

Thanks, -- Alexandre.
Re: How to build multiple entities in one rule? [message #1006858 is a reply to message #1006811] Sun, 03 February 2013 00:25 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
Yes, you have to parse the concrete syntax first but you can certainly add a lot of sugar once you understand the basics.
You could deal with it by an optional clause on the VariableDeclaration so it can be var z or var z = something. Your inferrer will either assign the expression value or not depending on whether there is an expression there or not. Something like this:

CodeBlock: '{' statements += Statement* '}' ;
Statement:  VariableDeclaration  | VariableAssignment  ;
VariableDeclaration: 'var' name=ID ( '=' expr=Expression )? ';' ;
VariableAssignment: name=[VariableDeclaration] '=' value=Expression ;
Expression: expr=XExpressionInsideBlock ;  // or whatever you like

Ian
Re: How to build multiple entities in one rule? [message #1007011 is a reply to message #1006763] Sun, 03 February 2013 21:56 Go to previous message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
We have a rather elegant hook for such things. Your entry point for code
archeology is the org.eclipse.xtext.resource.DerivedStateAwareResource
The basic idea is: A resource can have multiple root elements. The first
one is the one produced by the Xtext parser, the remaining ones are
somehow derived from that during linking. This "derived state" is
discarded and recomputed everytime the parsed model changes.
So you have to
- implement your own IDerivedStateComputer that creates the missing
elements representing the declarations
- bind it in your runtime modulre
- bind DerivedStateResource for your models.

Am 01.02.13 18:34, schrieb Alexandre Frey:
> Hello:
>
> How would you handle this situation: I want to parse blocks of
> statements mixed with variable declarations. Something like this:
> {
> var y = x+z;
> z = 3;
> var z;
> }
>
> The line "var y = x + z;" is a shortcut for: "var y; y = x + z;"
>
> I'd like this equivalence to be reflected in the model: the model should
> define a class Block that contains a list of variable names and a list
> of statements. So, the line "var y = x + z" should produce both a
> variable declaration and a statement.
>
> How would you suggest to proceed in Xtext in this situation?
>
> Thanks, -- Alexandre.


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


---
Get professional support from the Xtext committers at www.typefox.io
Previous Topic:Unique key constrain in json
Next Topic:[Xbase] LValue-like expressions in DSL
Goto Forum:
  


Current Time: Thu Apr 18 12:08:49 GMT 2024

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

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

Back to the top