Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext and error recovery in block-level statements
Xtext and error recovery in block-level statements [message #45521] Tue, 26 May 2009 22:02 Go to next message
Shane Beasley is currently offline Shane Beasley
Messages: 6
Registered: July 2009
Junior Member
Consider the following grammar and input:

Model :
( outers += Outer ';' )* ;
Outer :
'create' 'outer' name = ID
( inners += Inner ';' )*
'end' 'outer' ;
Inner : 'create' 'inner' name = ID ;

create outer Foo TYPO create inner X; create inner Y; end outer;
create outer Bar create inner X; create inner Y; end outer;

Because of the unexpected token TYPO, the Outline Pane shows a node for
Foo but not for Foo/X, Foo/Y, or Bar or its children.

http://wiki.eclipse.org/Xtext/Documentation mentions error recovery in
passing, but I don't think it describes anything that would help here.

I've seen the following technique in ANTLR grammars to recover from
errors inside the block:

Outer :
'create' 'outer' name = ID
( inners += Inner ';' | !'end' )*
'end' 'outer' ;

Unfortunately, Xtext does not accept terminal negation (!'end') in a
nonterminal rule.

Are there any other tricks for getting better error recovery, or should
I just file an enhancement request for the above (or both)? :)

Thanks!
Re: Xtext and error recovery in block-level statements [message #45553 is a reply to message #45521] Wed, 27 May 2009 03:29 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2809
Registered: July 2009
Senior Member
Hi Shane,

Xtext relies on the error recovery, that comes with Antlr by default.
Currently there is no way to alter this behavior to get better error
recovery for your special case.

Please feel free to file (an) enhancement request(s).

Btw: You may want to try to rewrite your grammar to something like this:

Model :
(outers += Outer )*;

Outer:
'create' 'outer' name = ID
( inners += Inner )*
'end' 'outer' ';'
;

Inner:
'create' 'inner' name = ID ';'
;

Note the keyword ';' has been moved into the rules "Outer" and "Inner".
You'll get better error recovery and - at least from my point of view -
the syntax communicates better, how a complete Outer or Inner should
look like.

Don't forget to file the tickets ;-)

Regards,
Sebastian

Am 27.05.2009 4:02 Uhr, schrieb Shane Beasley:
> Consider the following grammar and input:
>
> Model :
> ( outers += Outer ';' )* ;
> Outer :
> 'create' 'outer' name = ID
> ( inners += Inner ';' )*
> 'end' 'outer' ;
> Inner : 'create' 'inner' name = ID ;
>
> create outer Foo TYPO create inner X; create inner Y; end outer;
> create outer Bar create inner X; create inner Y; end outer;
>
> Because of the unexpected token TYPO, the Outline Pane shows a node for
> Foo but not for Foo/X, Foo/Y, or Bar or its children.
>
> http://wiki.eclipse.org/Xtext/Documentation mentions error recovery in
> passing, but I don't think it describes anything that would help here.
>
> I've seen the following technique in ANTLR grammars to recover from
> errors inside the block:
>
> Outer :
> 'create' 'outer' name = ID
> ( inners += Inner ';' | !'end' )*
> 'end' 'outer' ;
>
> Unfortunately, Xtext does not accept terminal negation (!'end') in a
> nonterminal rule.
>
> Are there any other tricks for getting better error recovery, or should
> I just file an enhancement request for the above (or both)? :)
>
> Thanks!
Re: Xtext and error recovery in block-level statements [message #45702 is a reply to message #45553] Wed, 27 May 2009 21:36 Go to previous messageGo to next message
Shane Beasley is currently offline Shane Beasley
Messages: 6
Registered: July 2009
Junior Member
Sebastian Zarnekow wrote:

> Xtext relies on the error recovery, that comes with Antlr by default.
> Currently there is no way to alter this behavior to get better error
> recovery for your special case.
>
> Please feel free to file (an) enhancement request(s).

ANTLR error recovery is supposed to be powerful, but it obviously needs
help... I guess I need to read up on that.

I did file #278144 about consuming arbitrary tokens ("any" or "any but
these"). However, it occurred to me that it's probably a bad idea for
error recovery, for a lot of reasons -- for one, it doesn't tell Xtext
that it's an error. :)

I was going to file a generic "better error recovery" ticket, but I see
that Sven already did that (#232011) and later marked it "won't fix ->
too generic."

Actually, a related question: Can you point me to anything (docs or the
relevant code) about how Xtext does incremental parsing -- how it
decides which rules to execute and when, etc.? Seems interesting in
general, but also relevant to the task at hand.

> Btw: You may want to try to rewrite your grammar to something like
> this: [...] Note the keyword ';' has been moved into the rules
> "Outer" and "Inner".

Moving the semicolons did improve recovery: It still lost the first
"outer" block, but was able to recover the second one. Maybe that'll
help me figure out how to get it to recover bits of the first block,
too. Thanks for the suggestion. :)

Shane
Re: Xtext and error recovery in block-level statements [message #45732 is a reply to message #45702] Thu, 28 May 2009 02:31 Go to previous message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2809
Registered: July 2009
Senior Member
Hi Shane,

please find my comments inline.

Am 28.05.2009 3:36 Uhr, schrieb Shane Beasley:
> Sebastian Zarnekow wrote:
>
>> Xtext relies on the error recovery, that comes with Antlr by default.
>> Currently there is no way to alter this behavior to get better error
>> recovery for your special case.
>>
>> Please feel free to file (an) enhancement request(s).
>
> ANTLR error recovery is supposed to be powerful, but it obviously needs
> help... I guess I need to read up on that.
>
> I did file #278144 about consuming arbitrary tokens ("any" or "any but
> these"). However, it occurred to me that it's probably a bad idea for
> error recovery, for a lot of reasons -- for one, it doesn't tell Xtext
> that it's an error. :)
>
> I was going to file a generic "better error recovery" ticket, but I see
> that Sven already did that (#232011) and later marked it "won't fix ->
> too generic."
>
> Actually, a related question: Can you point me to anything (docs or the
> relevant code) about how Xtext does incremental parsing -- how it
> decides which rules to execute and when, etc.? Seems interesting in
> general, but also relevant to the task at hand.

It should basically provide the same results as a full parse. We track
the textual modifications in a document, collect them, group them
together and compute the affected part of the parse tree. This
information is combined with lookahead infos from the parse to decide
whether a certain rule / node combination is a valid entry point or not.
After identifying the smallest region, that may be theoretically
reparsed, we try that. A full parse is performed as a fallback strategy
if the partial parsing results in an erroneous model.

I'm afraid there is currently not much documentation about this topic
available, as it is quite internal stuff. We focus more on concepts, the
big picture and customization hooks. But I promise, documentation will
be improved before the galileo release.

Have a look at PartialParsingUtil for implementation details.

>
>> Btw: You may want to try to rewrite your grammar to something like
>> this: [...] Note the keyword ';' has been moved into the rules "Outer"
>> and "Inner".
>
> Moving the semicolons did improve recovery: It still lost the first
> "outer" block, but was able to recover the second one. Maybe that'll
> help me figure out how to get it to recover bits of the first block,
> too. Thanks for the suggestion. :)

Nice to meet you.

>
> Shane

Regards,
Sebastian
Previous Topic:[XText] best practices for variable handling
Next Topic:[XText] a standalone Java application that parses text to model
Goto Forum:
  


Current Time: Sat Aug 23 03:46:11 EDT 2014

Powered by FUDForum. Page generated in 0.10306 seconds