Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » IJvmModelInferrer is being called for a lexically invalid model(Is this ok and how to handle it?)
icon5.gif  IJvmModelInferrer is being called for a lexically invalid model [message #1435924] Thu, 02 October 2014 06:25 Go to next message
Alexander Shutyaev is currently offline Alexander ShutyaevFriend
Messages: 4
Registered: July 2009
Junior Member
Hi all!

Here's a short example:

MyDsl.xtext
grammar org.foo.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase

generate myDsl "http://www.eclipse.org/mydsl/MyDsl"

Model:
	greetings+=Greeting*
	farewells+=Farewell*;

Greeting:
	'Hello' name=ID '!';

Farewell:
	'Goodbye' greeting=[Greeting];

MyDslJvmModelInferrer.xtend
class MyDslJvmModelInferrer extends AbstractModelInferrer {

	@Inject extension JvmTypesBuilder

	def dispatch void infer(Greeting gr, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
		acceptor.accept(gr.toClass(gr.name + 'Greeting'))
	}

	def dispatch void infer(Farewell fw, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
		acceptor.accept(fw.toClass(fw.greeting.name + 'Farewell'))
	}

}

Now if I start typing in editor
Hello world!
Goodbye 

then when I hit spacebar after Goodbye I get a NullPointerException (full stacktrace attached). The reason is that although the file contents is not (yet) lexically valid, my IJvmModelInferrer has been called and the following expression:
fw.greeting.name

resulted in a NullPointerException.

Is this the expected behavior?
If yes, is there any way to ensure that IJvmModelInferrer is called only for a lexically valid model?
If there is no such way, what would be the best practice? Validate manually in the beginning of the infer method? How do I call such kind of validation (I assume there should be some generated lexical validator)?

Thanks in advance!
Re: IJvmModelInferrer is being called for a lexically invalid model [message #1435937 is a reply to message #1435924] Thu, 02 October 2014 06:52 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Alex,

the model inferrer is always called and your code should be written in a
robust way - which mainly means it should be null-safe. It is important
to do the model inference with broken models, too. Otherwise you'll see
way too many follow up errors that would be distracting thus the
syntactic problems would be mixed up with linking problems and
validation issues.
Generally speaking, you should not guard your model inferrer with smth
like #hasSyntaxErrors.

Best,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: IJvmModelInferrer is being called for a lexically invalid model [message #1435943 is a reply to message #1435937] Thu, 02 October 2014 07:05 Go to previous message
Alexander Shutyaev is currently offline Alexander ShutyaevFriend
Messages: 4
Registered: July 2009
Junior Member
Hi Sebastian,

Thanks for your answer. Well, if this is the recommended way, then I guess it's best to follow it. Smile
Previous Topic:share state between composed validators
Next Topic:avoid @SuppressWarnings("all") in Xtend batch compiler
Goto Forum:
  


Current Time: Sat Apr 27 00:36:13 GMT 2024

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

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

Back to the top