Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Ensure entries in ParserRule are comma separated
Ensure entries in ParserRule are comma separated [message #766661] Fri, 16 December 2011 08:14 Go to next message
Arvind Sharma is currently offline Arvind SharmaFriend
Messages: 17
Registered: December 2011
Junior Member
I have written following ParserRule in my Xtext grammar file:

TestSpec:
'{'
......
(('"my"' ':' myValue=(MySpec) (',' | '}'))?) &
......
'}'

MySpec:
'{'
(
(('"suffix"' ':' suffixValue=(STRING) (',')?)?) &
(('"prefix"' ':' prefixValue=(STRING) (',')?)?) &
(('"none"' ':' noneValue=(STRING) (',')?)?) &
)
'}';

Now in my Xtext editor, I want to ensure that the entries in "my" end with comma except the last one and it should give an error if comma is missing between say "suffix" and "prefix" like in following example entries:


"my": {
"suffix": "testString" ---> I want this to give an error for missing comma (,)
"prefix": "prefixString",
"none": "noneString"
}

One solution that I thought of is to change the grammar to following:

MySpec:
'{'
(
(('"suffix"' ':' suffixValue=(STRING) (',' | '}'))?) &
(('"prefix"' ':' prefixValue=(STRING) (',' | '}'))?) &
(('"none"' ':' noneValue=(STRING) (',' | '}'))?) &
);

But I am not sure if this the preferred way of doing it. Is it possible to achieve this through Validations or some other more elegant ways?

[Updated on: Fri, 16 December 2011 08:15]

Report message to a moderator

Re: Ensure entries in ParserRule are comma separated [message #766692 is a reply to message #766661] Fri, 16 December 2011 09:17 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
The standard pattern for comma-separated lists is:
List:
    listItems+=ListItem (',' listItems+=ListItem)*;

In your case, if you insist on using groups like this (which is fine), you'll have to assign the optional ',' to some feature and @Check in the Java validator for its presence and raise an error accordingly.


Re: Ensure entries in ParserRule are comma separated [message #766734 is a reply to message #766661] Fri, 16 December 2011 10:29 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
The standard pattern for separated lists is

SeparatedList:
token (separator token)*

In your case something along the lines of

TestSpec:
'{' 'my' ':' myValues+=MySpec (',' 'my' ':' myValues+=MySpec)* '}'

Note that you should use a += assignment to store *all* MySpecs in the
multi-valued reference TestSpec.myValues

Am 16.12.11 09:14, schrieb Arvind Sharma:
> I have written following ParserRule in my Xtext grammar file:
>
> TestSpec:
> '{'
> ......
> (('"my"' ':' myValue=(MySpec) (',' | '}'))?) &
> ......
> '}'
>
> MySpec:
> '{'
> (
> (('"suffix"' ':' suffixValue=(STRING) (',')?)?) &
> (('"prefix"' ':' prefixValue=(STRING) (',')?)?) &
> (('"none"' ':' noneValue=(STRING) (',')?)?) &
> )
> '}';
>
> Now in my Xtext editor, I want to ensure that the entries in "my" end
> with comma except the last one and it should give an error if comma is
> missing between say "suffix" and "prefix" like in following example
> entries:
>
>
> "my": {
> "suffix": "testString" ---> I want this to give an error for missing
> comma (,)
> "prefix": "prefixString",
> "none": "noneString"
> }
>
> One solution that I thought of is to change the grammar to following:
>
> MySpec:
> '{'
> (
> (('"suffix"' ':' suffixValue=(STRING) (',' | '}'))?) &
> (('"prefix"' ':' prefixValue=(STRING) (',' | '}'))?) &
> (('"none"' ':' noneValue=(STRING) (',' | '}'))?) &
> );
>
> But I am not sure if this the preferred way of doing it. Is it possible
> to achieve this through Validations or some other more elegant ways?


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


---
Get professional support from the Xtext committers at www.typefox.io
Re: Ensure entries in ParserRule are comma separated [message #766741 is a reply to message #766734] Fri, 16 December 2011 10:43 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
@Jan: hitting /Ctrl-R before typing a reply could have saved you a little time Wink

Re: Ensure entries in ParserRule are comma separated [message #766829 is a reply to message #766741] Fri, 16 December 2011 13:55 Go to previous messageGo to next message
Arvind Sharma is currently offline Arvind SharmaFriend
Messages: 17
Registered: December 2011
Junior Member
Hi,

Thanks a lot for your prompt reply. The list pattern you gave is fine when it is an array having objects of same type but when the entity contains non related set of attributes for example

RgbaColorSpec:
'{'
(
(('"a"' ':' aValue=(NUMBER) (',')?)?) &
(('"b"' ':' bValue=(NUMBER) (',')?)?) &
(('"r"' ':' rValue=(NUMBER) (',')?)?) &
(('"g"' ':' gValue=(NUMBER) (',')?)?)
)
'}';

Here the attributes a, r, g, and b all are optional and can be present in any order in grammar. I need a way to find out if user has entered more than one attribute for this entity and there should be a comma present for all but last attribute.

Thanks
Arvind

[Updated on: Fri, 16 December 2011 13:57]

Report message to a moderator

Re: Ensure entries in ParserRule are comma separated [message #767075 is a reply to message #766829] Sat, 17 December 2011 00:52 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
RgbaColorSpec : '{' (colors += Color (',' colors += Color)*)? '}' ;
Color : color = ('a'|'b'|'r'|'g') value = NUMBER ;

(you could also use an Enum for the color letter).

Then add a @Check method in validation that checks RgbaColorSpec for
duplicate entries.

You probably want one for Color as well to check that NUMBER is within
range.

This approach has the advantage that you can tailor the error messages
instead of letting the user just see "got ... expected... ". In general
I recommend having a lenient grammar and tight validation as it will
save you tons of trouble trying to encode semantics with syntax rules.
It is also far easier to issue special errors to which you can attach
quick fixes.

One potential issue is that a, b, r, g becomes keywords in your grammar.
If that is not ok (you want to be able to use identifiers with these
names without having to escape them), then there are alternative
solutions (use ID as the color, and validate that it is one of the
allowed, plus write custom content assist to only suggest the valid
letters, and only letters that are not already seen).

Regards
- henrik

On 2011-16-12 14:56, Arvind Sharma wrote:
> Hi,
>
> Thanks a lot for your prompt reply. The list pattern you gave is fine
> when it is an array but when the entity contains not related set of
> attributes for example
> RgbaColorSpec:
> '{'
> (
> (('"a"' ':' aValue=(NUMBER) (',')?)?) &
> (('"b"' ':' bValue=(NUMBER) (',')?)?) &
> (('"r"' ':' rValue=(NUMBER) (',')?)?) &
> (('"g"' ':' gValue=(NUMBER) (',')?)?)
> )
> '}';
>
> Here the attributes a, r, g, and b all are optional and can be present
> in any order in grammar. I need a way to find out if user has entered
> more than one attribute for this entity and there should be a comma
> present for all but last attribute.
>
> Thanks
> Arvind
Re: Ensure entries in ParserRule are comma separated [message #767150 is a reply to message #767075] Sat, 17 December 2011 06:24 Go to previous messageGo to next message
Arvind Sharma is currently offline Arvind SharmaFriend
Messages: 17
Registered: December 2011
Junior Member
Hi,


Thanks for this approach...one last thing what if my entity has attributes having different data type and i want to use the same comma validation

FillSpec:
'{'
(
(('"global"' ':' globalValue=(BOOLEAN) (',')?)?) &
(('"angle"' ':' angleValue=(NUMBER) (',')?)?) &
(('"focus"' ':' focusValue=(FocusSpec) (',')?)?) &
(('"type"' ':' typeValue=(FillSpec_typeEnum) (',')?)?)
)
'}';


Can i do something like this

Model:
'"my"' ':' myValue=(FillSpec)
;

FillSpec: '{' (fills += Fill (',' fills += Fill)*)? '}' ;
Fill : fill = (Global|Angle|Focus|Type);

Global : global=('"global":') value=(BOOLEAN);
Angle : angle=('"angle":') value=(NUMBER);
Focus : focus=('"focus":') value=(STRING);
Type : angle=('"angle":') value=(FillSpec_typeEnum);


NUMBER:
(('-')? INT ('.' INT (('E'|'e') '-'? INT)?)?);

BOOLEAN:
('true' | 'false'| '"true"' | '"false"');

enum FillSpec_typeEnum:
linear='"linear"' | radial='"radial"';


Thanks
Arvind

[Updated on: Sat, 17 December 2011 08:04]

Report message to a moderator

Re: Ensure entries in ParserRule are comma separated [message #767406 is a reply to message #767150] Sat, 17 December 2011 23:08 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Something like this perhaps?

FillSpec : '{' (data += FillData (',' data += FillData)*)? '}' ;
FillData : Global | Angle | Focus | Type ;
Global : 'global' value = BOOLEAN ;
Angle : 'angle' value = NUMBER ;
Focus : 'focus' value = FocusSpec ;
Type : 'type' value = FillSpec_typeEnum ;

Validate that there is only one instance per FillData type in the data
list. Validate derived FillData classes as required.

I am not sure exactly what the Xtext generator produces, if it
understands that there is no "value" feature in FillData, and thus
allows them to have different type in the subclasses (Global, Angle, etc.)

- henrik



On 2011-17-12 7:24, Arvind Sharma wrote:
> Hi,
>
>
> Thanks for this approach...one last thing what if my entity has
> attributes having different data type and i want to use the same comma
> validation
>
> FillSpec:
> '{'
> (
> (('"global"' ':' globalValue=(BOOLEAN) (',')?)?) &
> (('"angle"' ':' angleValue=(NUMBER) (',')?)?) &
> (('"focus"' ':' focusValue=(FocusSpec) (',')?)?) &
> (('"type"' ':' typeValue=(FillSpec_typeEnum) (',')?)?)
> )
> '}';
>
> Thanks
> Arvind
Re: Ensure entries in ParserRule are comma separated [message #767549 is a reply to message #767406] Sun, 18 December 2011 09:38 Go to previous message
Arvind Sharma is currently offline Arvind SharmaFriend
Messages: 17
Registered: December 2011
Junior Member
Thanks a lot for the help

I am able to resolve issue with this.

Ragards
Arvind
Previous Topic:Positioning cursor after proposal accept
Next Topic:merge scope blocks?
Goto Forum:
  


Current Time: Fri Apr 26 20:09:28 GMT 2024

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

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

Back to the top