Question

I'm trying to create a grammar that would parse the following:

reference: java.util.String

but as well

reference: {java.util.String, java.lang.Integer}

In other words, I want it to parse both a list of Qualified names but also only one item (not marked by '{' in this case).

What I tried is this:

Reference:
    'reference' ':' ('{' values+=QualifiedName (',' values+=QualifiedName)* '}') | (values+=QualifiedName);

However, I am getting an error: missing '{' at 'java', when using the first form of the reference (without {). Any suggestions what I should try?

EDIT: Also tried

Reference:
    'reference' ':' ('{' values+=QualifiedName (',' values+=QualifiedName)* '}') | ((!'{')values+=QualifiedName);

but getting a no viable alternative at input '!' error in the grammar definition.

EDIT2: I am not having problems with the "comma separated list", I tried this separatly and it works well. My only problem is distinguishing between the two parts of the rule based on the '{' character.

Was it helpful?

Solution

This will do the trick:

Reference:
'reference' ':' (
     '{' values+=QualifiedName (',' values+=QualifiedName)* '}' 
   | values+=QualifiedName
);

Please mind the precedencies of groups and alternatives.

OTHER TIPS

I am quite new to Xtext, so just giving it a try:

Reference:
    'reference' ':' ('{' values+=QualifiedName (',' values+=QualifiedName)* '}') | (values=QualifiedName);

or

Reference:
    'reference' ':' ('{' values+=QualifiedName (',' values+=QualifiedName)+ '}') | (values=QualifiedName);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top