Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Modifying the default grammar
Modifying the default grammar [message #947013] Tue, 16 October 2012 14:29 Go to next message
Eclipse UserFriend
Hi,

I use xbase and want to support using a tick (') behind a variable
reference to refer to the variable's previous value. The idea (at the
semantic level) is to have a hidden set of variables suffixed by
__PREVIOUS__ that remember the previous state and translate a reference
with the tick to refer to the hidden one. E.g. the identifier "a" refers
to "a" and "a'" refers to "a__PREVIOUS__".

The translation works, but I have problems redefining the grammar to
support this, without also redefining the ID terminal.

What I tried was the following:

// remove the use of tick in the String terminal
terminal STRING: '"' ( '\\' ('b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\') |
!('\\'|'"') )* '"'

// support trailing tick, as 'previous' value
PreviousValueID: ValidID "'"?;

// use PreviousValueID in XMemberFeatureCall
XMemberFeatureCall returns xbase::XExpression: ...

The idea is to limit the use of the tick-suffixed references to variable
references. However, I still got syntax errors when I use the tick. I
also need to redefine the ID terminal to allow a trailing tick, but this
will allow it almost everywhere, including in class names. I couldn't
find other usages of tick in the grammar than in the STRING terminal, so
shouldn't it be enough to redefine only STRING and not ID?

Hallvard
Re: Modifying the default grammar [message #947056 is a reply to message #947013] Tue, 16 October 2012 15:22 Go to previous messageGo to next message
Eclipse UserFriend
Hi Hallvard,

what kind of syntax error do you get?
Did you override XFeatureCall, too?

Overriding the ID rule should not be necessary.

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 16.10.12 20:29, schrieb Hallvard Trætteberg:
> Hi,
>
> I use xbase and want to support using a tick (') behind a variable
> reference to refer to the variable's previous value. The idea (at the
> semantic level) is to have a hidden set of variables suffixed by
> __PREVIOUS__ that remember the previous state and translate a reference
> with the tick to refer to the hidden one. E.g. the identifier "a" refers
> to "a" and "a'" refers to "a__PREVIOUS__".
>
> The translation works, but I have problems redefining the grammar to
> support this, without also redefining the ID terminal.
>
> What I tried was the following:
>
> // remove the use of tick in the String terminal
> terminal STRING: '"' ( '\\' ('b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\') |
> !('\\'|'"') )* '"'
>
> // support trailing tick, as 'previous' value
> PreviousValueID: ValidID "'"?;
>
> // use PreviousValueID in XMemberFeatureCall
> XMemberFeatureCall returns xbase::XExpression: ...
>
> The idea is to limit the use of the tick-suffixed references to variable
> references. However, I still got syntax errors when I use the tick. I
> also need to redefine the ID terminal to allow a trailing tick, but this
> will allow it almost everywhere, including in class names. I couldn't
> find other usages of tick in the grammar than in the STRING terminal, so
> shouldn't it be enough to redefine only STRING and not ID?
>
> Hallvard
Re: Modifying the default grammar [message #947084 is a reply to message #947056] Tue, 16 October 2012 16:00 Go to previous message
Eclipse UserFriend
Sebastian,

You are right, I discovered a bit after posting. The problem was I
didn't override XFeatureCall (the rule used when there is no dot, which
is the case for ordinary variables).

I ended up redefining only the STRING terminal and the IdOrSuper rule,
which is used by XFeatureCall. XFeatureCall itself needn't be redefined.

Here's the overrides:

// remove apostrophe variant
terminal STRING: '"' ( '\\' ('b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\') |
!('\\'|'"') )* '"';

// support trailing apostrophe, as 'previous' value
IdOrSuper: (ValidID "'"?) | "'" | "super";

I also needed to inject a new value converter for the IdOrSuper rule, so
the apostrophe suffix is converted to __PREVIOUS__.

I get a warning stating that "Decision can match input such as "'\''"
using multiple alternatives: 1, 2", but it seems to work...

Grammar overrides are very powerful, but you really need to understand
the grammar you're overriding.

Hallvard

On 16.10.12 12.22, Sebastian Zarnekow wrote:
> Hi Hallvard,
>
> what kind of syntax error do you get?
> Did you override XFeatureCall, too?
>
> Overriding the ID rule should not be necessary.
>
> Regards,
> Sebastian
Previous Topic:How do I create Java & Python parsers for the same Xtext DSL
Next Topic:[xbase] Extension functions, inlining and arrays
Goto Forum:
  


Current Time: Sat Jul 05 05:31:08 EDT 2025

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

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

Back to the top