Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Unit testing hidden terminal - ParseHelper returning null model
Unit testing hidden terminal - ParseHelper returning null model [message #986131] Mon, 19 November 2012 06:35 Go to next message
Barrie Treloar is currently offline Barrie TreloarFriend
Messages: 52
Registered: July 2009
Member
Step debugging through my unit tests, I can see that my grammar is being parsed correctly and the hidden nodes are being created correctly.

At ParseHelper.parse(InputStream in, URI uriToUse, Map<?, ?> options, ResourceSet resourceSet), resource.getContents() is empty, but resource.parseResult contains the hidden tokens.

I was attempting to test SL_COMMENT in isolation but this is returning a null from the parser, which means I can not tell the difference between junk input and SL_COMMENT.

val usrprops = parser.parse('''This is JUNK!''')

is equivalent to

val usrprops = parser.parse('''// Single line comment''')

My current work around is to wrap the SL_COMMENT with other valid rules and check that only those non-hidden rules are returned.

parser.parse('''NOP
		// Single line comment
		NOP''')

Is there a better way?
Re: Unit testing hidden terminal - ParseHelper returning null model [message #986138 is a reply to message #986131] Mon, 19 November 2012 07:23 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 658
Registered: July 2009
Senior Member
Not quite sure what you mean, but the parser gets fed wit the tokens
from the lexer which has run independently. So invalid tokens will never
create a semantic element (by the parser) but should yield a different
node model (parseResult.getRootNode()).

An empty semantic model does not mean an empty node model.

The leaf nodes contain information about the lexer rule they conform to.
If they don't match, they should have a syntax error message attached.
I'd guess this is the way to make the distinction you're looking for.

Am 19.11.12 07:35, schrieb Barrie Treloar:
> Step debugging through my unit tests, I can see that my grammar is being
> parsed correctly and the hidden nodes are being created correctly.
>
> At ParseHelper.parse(InputStream in, URI uriToUse, Map<?, ?> options,
> ResourceSet resourceSet), resource.getContents() is empty, but
> resource.parseResult contains the hidden tokens.
>
> I was attempting to test SL_COMMENT in isolation but this is returning a
> null from the parser, which means I can not tell the difference between
> junk input and SL_COMMENT.
>
> val usrprops = parser.parse('''This is JUNK!''')
> is equivalent to
>
> val usrprops = parser.parse('''// Single line comment''')
> My current work around is to wrap the SL_COMMENT with other valid rules
> and check that only those non-hidden rules are returned.
>
>
> parser.parse('''NOP
> // Single line comment
> NOP''')
>
> Is there a better way?


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Unit testing hidden terminal - ParseHelper returning null model [message #986324 is a reply to message #986138] Tue, 20 November 2012 00:11 Go to previous messageGo to next message
Barrie Treloar is currently offline Barrie TreloarFriend
Messages: 52
Registered: July 2009
Member
Quote:

So invalid tokens will never
create a semantic element (by the parser) but should yield a different
node model (parseResult.getRootNode()).

An empty semantic model does not mean an empty node model.


Exactly.
parseResult.getRootNode() is behaving the way I expect, but ParserHelper returns resource.getContents() which is always null for junk input or hidden tokens.

And junk input is not given me errors.

This may be because I've just started and forgotten to do something...
Re: Unit testing hidden terminal - ParseHelper returning null model [message #986334 is a reply to message #986324] Tue, 20 November 2012 05:56 Go to previous messageGo to next message
Barrie Treloar is currently offline Barrie TreloarFriend
Messages: 52
Registered: July 2009
Member
Right, I can see the problem.

http://www.eclipse.org/forums/index.php/m/900856/ says ParserHelper isn't meant for testing specific errors.

What I really want is a blend of ParserHelper and the example code in the above link, i.e. return the List<Diagnostic> in the case of errors and since this is unit testing wrapped in an exception is fine.

Otherwise stupid human mistakes get swallowed up as null with nothing to help me solve the problems.
Re: Unit testing hidden terminal - ParseHelper returning null model [message #986447 is a reply to message #986334] Tue, 20 November 2012 12:59 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 658
Registered: July 2009
Senior Member
Then why not take ParseHelper as an inspiration and implement your own
helper?

Am 20.11.12 06:56, schrieb Barrie Treloar:
> Right, I can see the problem.
>
> http://www.eclipse.org/forums/index.php/m/900856/ says ParserHelper
> isn't meant for testing specific errors.
>
> What I really want is a blend of ParserHelper and the example code in
> the above link, i.e. return the List<Diagnostic> in the case of errors
> and since this is unit testing wrapped in an exception is fine.
>
> Otherwise stupid human mistakes get swallowed up as null with nothing to
> help me solve the problems.


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Unit testing hidden terminal - ParseHelper returning null model [message #986581 is a reply to message #986447] Wed, 21 November 2012 03:55 Go to previous messageGo to next message
Barrie Treloar is currently offline Barrie TreloarFriend
Messages: 52
Registered: July 2009
Member
Too late, already done that Smile

I was wondering why ParserHelper doesn't do this out of the box?

Went to file an enhancement, and there is already one
https://bugs.eclipse.org/bugs/show_bug.cgi?id=375027
Which does more than the hack I was doing.

Any chance this could get progressed?
Re: Unit testing hidden terminal - ParseHelper returning null model [message #986914 is a reply to message #986581] Thu, 22 November 2012 11:00 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 658
Registered: July 2009
Senior Member
We're also testing what happens with broken files, so it's a good thing
the parse helper does not automatically validate.

Am 21.11.12 04:55, schrieb Barrie Treloar:
> Too late, already done that :)
>
> I was wondering why ParserHelper doesn't do this out of the box?
>
> Went to file an enhancement, and there is already one
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=375027
> Which does more than the hack I was doing.
>
> Any chance this could get progressed?


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Unit testing hidden terminal - ParseHelper returning null model [message #987027 is a reply to message #986914] Fri, 23 November 2012 03:24 Go to previous messageGo to next message
Barrie Treloar is currently offline Barrie TreloarFriend
Messages: 52
Registered: July 2009
Member
Jan Kohnlein wrote on Thu, 22 November 2012 06:00
We're also testing what happens with broken files, so it's a good thing
the parse helper does not automatically validate.


I'm probably using incorrect terminology, i.e validate.

I'm assuming that broken files means they aren't available in the resource set, rather than the contents of the file are not valid examples of the grammar.

In my case, I'm unintentionally creating invalid instances of the grammar, which ParserHelper is returning null rather than throwing an exception.

Tell help out the newbies, it would be really beneficial if ParserHelper didn't do that. I wasted a bit of time tracking down what was going on.

I'm sure ParserHelper can be enhanced to suit everyone.
Re: Unit testing hidden terminal - ParseHelper returning null model [message #987265 is a reply to message #987027] Sun, 25 November 2012 11:28 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 658
Registered: July 2009
Senior Member
If there's nothing that can be parsed, what should the parser helper
return? Did you inspect the errors and warnings of the parsed resource
resource? This is where we usually store the syntax errors.

Am 23.11.12 04:24, schrieb Barrie Treloar:
> Jan Kohnlein wrote on Thu, 22 November 2012 06:00
>> We're also testing what happens with broken files, so it's a good
>> thing the parse helper does not automatically validate.
>
>
> I'm probably using incorrect terminology, i.e validate.
>
> I'm assuming that broken files means they aren't available in the
> resource set, rather than the contents of the file are not valid
> examples of the grammar.
>
> In my case, I'm unintentionally creating invalid instances of the
> grammar, which ParserHelper is returning null rather than throwing an
> exception.
>
> Tell help out the newbies, it would be really beneficial if ParserHelper
> didn't do that. I wasted a bit of time tracking down what was going on.
>
> I'm sure ParserHelper can be enhanced to suit everyone.
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Unit testing hidden terminal - ParseHelper returning null model [message #987313 is a reply to message #987265] Mon, 26 November 2012 00:58 Go to previous messageGo to next message
Barrie Treloar is currently offline Barrie TreloarFriend
Messages: 52
Registered: July 2009
Member
Jan Kohnlein wrote on Sun, 25 November 2012 06:28
If there's nothing that can be parsed, what should the parser helper
return? Did you inspect the errors and warnings of the parsed resource
resource? This is where we usually store the syntax errors.


Sure, my point is that as a noob I just started using ParserHelper as per http://www.eclipse.org/Xtext/documentation.html and found my errors being swallowed with no feedback. This is not the best experience for a new person when using a product.

Isn't this something that could be fixed relatively easily?

Especially since ParserHelper doesn't expose the resource to call getErrors() on.
I must roll my own version, which is duplicating effort.

[Updated on: Mon, 26 November 2012 01:03]

Report message to a moderator

Re: Unit testing hidden terminal - ParseHelper returning null model [message #987367 is a reply to message #987313] Mon, 26 November 2012 10:17 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 658
Registered: July 2009
Senior Member
No, errors are not swallowed by the parse helper. It just wraps checked
exceptions into unchecked ones.

The misunderstanding is that you expect an Exception. But there is none,
especially not from the underlying Resource API. This is because we use
the same API in our tool, e.g. in the editor. Note that while editing a
file a parse error is the rule, not the exception. Most of the time the
model is broken, as the user is in the midst of some typing. If we just
stopped parsing throwing an exception, you wouldn't get any feedback on
what's behind the (currently) unparseable section, and a huge amount of
follow up errors, e.g. form other resources trying to refer to something
in the unreachable part.

So instead, we provide an error resilient parser, taht tries to recover
from errors and that logs the syntax errors inside the resource such
that they can be handled gracefully by the clients (Resource.getErrors()).

Dealing with clean models is easy. We spent a lot more effort to keep
the tooling working for broken models, too. This is why we need a parse
helper that behaves as the tooling needs it.

For your own languages, you should also have that in mind, e.g. a NPE in
some custom component will likely lead to quite unacceptable tool
behavior. If you pretend no broken models will make it through, you are
on the wrong track.

Am 26.11.12 01:58, schrieb Barrie Treloar:
> [quote title=Jan Kohnlein wrote on Sun, 25 November 2012 06:28]If
> there's nothing that can be parsed, what should the parser helper
> return? Did you inspect the errors and warnings of the parsed resource
> resource? This is where we usually store the syntax errors.
>
> Sure, my point is that as a noob I just started using ParserHelper as
> per http://www.eclipse.org/Xtext/documentation.html and found my errors
> being swallowed with no feedback. This is not the best experience for a
> new person when using a product.
> Isn't this something that could be fixed relatively easily?
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Unit testing hidden terminal - ParseHelper returning null model [message #987369 is a reply to message #987367] Mon, 26 November 2012 10:25 Go to previous message
Barrie Treloar is currently offline Barrie TreloarFriend
Messages: 52
Registered: July 2009
Member
Jan Kohnlein wrote on Mon, 26 November 2012 05:17
No, errors are not swallowed by the parse helper. It just wraps checked
exceptions into unchecked ones.

The misunderstanding is that you expect an Exception. But there is none,
especially not from the underlying Resource API. This is because we use
the same API in our tool, e.g. in the editor. Note that while editing a
file a parse error is the rule, not the exception. Most of the time the
model is broken, as the user is in the midst of some typing. If we just
stopped parsing throwing an exception, you wouldn't get any feedback on
what's behind the (currently) unparseable section, and a huge amount of
follow up errors, e.g. form other resources trying to refer to something
in the unreachable part.


I dont expect an Exception, but I do expect to be able to get at the Errors/Warnings, which is not possible via ParserHelper.

The resource is scoped to the method and there is no state in this class.

If I am not meant to be doing it this way, alternative out-of-the-box suggestions are most welcome.
Previous Topic:javadoc for generated artifacts
Next Topic:Accessing model context in quickfix
Goto Forum:
  


Current Time: Sun Dec 21 02:09:38 GMT 2014

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

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