Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » model to text and comments
model to text and comments [message #644374] Fri, 10 December 2010 12:49 Go to next message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2499
Registered: July 2009
Senior Member
Hi,
I have a use case where I create a (DSL) model using a model to model
transformation and I would like to also insert a few comments (in
selected places). What would be the approach to take for something like
this?

My DSL does not contain any 'assigned rules' for comments, but the
comments are not hidden from the grammar.

I imagine (a somewhat heavy) approach would be to first serialize to
text without comments, then parse the result to get a node model, and
then perform textual changes.

My other idea is to use a "comment adapter" and pick up any comment text
while serializing. However since comments would not be found the same
way when going in the other direction, I am not so fond of this solution.

Some more detail info about the grammar:

In this grammar there are issues with overlapping terminals so basically
everything that is normally handled with terminals is handled as data
rules (i.e. strings, comments, etc.). This also means that there are no
hidden terminals. Instead, all rules typically ends with an
OptionalWhiteSpace (OWS) rule that is defined to be an optional sequence
of white space and comments.

A rule may look like this:
AdditiveExpression returns Expression:
MultiplicativeExpression ({AdditiveExpression.leftExpr=current}
opName = AdditiveOperator OWS
rightExpr = MultiplicativeExpression)*
;

This works fine as all rules that consumes a token (that may be followed
by WS/Comments) also ends by consuming OWS. (And the root rule consumes
any leading OWS).

Regards
- henrik
Re: model to text and comments [message #644774 is a reply to message #644374] Tue, 14 December 2010 03:32 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2809
Registered: July 2009
Senior Member
Hi Henrik,

I'd usually try to use a custom serializer for any use case that
involves heavy customizing.
The other approaches (adapter + comment associater or unassigned text
serializer) involve heavy customizing and I'm not that sure about the
runtime invariants of these components.

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

Am 10.12.10 18:49, schrieb Henrik Lindberg:
> Hi,
> I have a use case where I create a (DSL) model using a model to model
> transformation and I would like to also insert a few comments (in
> selected places). What would be the approach to take for something like
> this?
>
> My DSL does not contain any 'assigned rules' for comments, but the
> comments are not hidden from the grammar.
>
> I imagine (a somewhat heavy) approach would be to first serialize to
> text without comments, then parse the result to get a node model, and
> then perform textual changes.
>
> My other idea is to use a "comment adapter" and pick up any comment text
> while serializing. However since comments would not be found the same
> way when going in the other direction, I am not so fond of this solution.
>
> Some more detail info about the grammar:
>
> In this grammar there are issues with overlapping terminals so basically
> everything that is normally handled with terminals is handled as data
> rules (i.e. strings, comments, etc.). This also means that there are no
> hidden terminals. Instead, all rules typically ends with an
> OptionalWhiteSpace (OWS) rule that is defined to be an optional sequence
> of white space and comments.
>
> A rule may look like this:
> AdditiveExpression returns Expression:
> MultiplicativeExpression ({AdditiveExpression.leftExpr=current}
> opName = AdditiveOperator OWS
> rightExpr = MultiplicativeExpression)*
> ;
>
> This works fine as all rules that consumes a token (that may be followed
> by WS/Comments) also ends by consuming OWS. (And the root rule consumes
> any leading OWS).
>
> Regards
> - henrik
Re: model to text and comments [message #645098 is a reply to message #644774] Wed, 15 December 2010 10:11 Go to previous message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2499
Registered: July 2009
Senior Member
I managed to make things work by implementing a value serializer and
then dealing with whitespace by returning null. (Returning "" had the
effect that extra whitespace was generated).

What is the difference between returning null and "" when serializing?

I also modified the HiddenTokenHelper to report my non terminal comments
and whitespace rules as such.

I was then able to serialize and also use formatting.

- henrik

On 12/14/10 9:32 AM, Sebastian Zarnekow wrote:
> Hi Henrik,
>
> I'd usually try to use a custom serializer for any use case that
> involves heavy customizing.
> The other approaches (adapter + comment associater or unassigned text
> serializer) involve heavy customizing and I'm not that sure about the
> runtime invariants of these components.
>
> Regards,
> Sebastian
Previous Topic: retrieving crossreferences from external file
Next Topic:Xtext - Formatting - SetIndentation
Goto Forum:
  


Current Time: Thu Aug 21 04:33:27 EDT 2014

Powered by FUDForum. Page generated in 0.08304 seconds