Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » XText Preprocess(Conditionally ignoring lines of input)
XText Preprocess [message #698098] Mon, 18 July 2011 18:52 Go to next message
jfisher is currently offline jfisherFriend
Messages: 2
Registered: July 2011
Junior Member
Hello, I am pretty new to the whole Xtext experience, and I am pretty impressed with it so far. So far I've been able to use it to create an editor for an existing DSL (originally written in lex/yacc). I've been able to get everything working except for one "feature" of the language which is giving me headaches. The current compiler takes a directive telling it which "context" it is targeting. It can then conditionally exclude statements that do not fit it's current context. In practice, this looks like this:

if_context(1,2,8): var=56
if_context(3..6): var=65


In this case if the context is 1, 2, or 8 the value will be 56, if it is 3,4, 5, or 6 it will be 65, if it is 7 the value will be untouched.

In this case, the editor does not care what the context is; however, this directive is valid on any line and can cause syntactic problems if it is not resolved properly. For example:
if (bool1
if_context(1,2,8): and bool2) {
if_context(3..7): and bool3 and bool4) {
...


In this case, if neither line is included, the syntax is incomplete, and if both lines are included there is a duplication. I can't see any way in the Xtext grammar to do any sort of value check at tokenization time, and trying to add that into every possible part of the grammar seems foolish and error prone.

The only solution I can think of is to have a preprocessor that changes the input as the file is read, and then changes it back when it is saved. I have seen posts indicating that this might be possible through use of an IResourceFactory, but I don't seem to know enough about the framework to begin such an undertaking. I presume I need to create my own IResource class, but I don't know in which methods I should be doing my magic.

For reference, here is the grammar snippet of what I have so far:

hidden(WS, SL_COMMENT, CONTEXT)

terminal SL_COMMENT:
	';' !('\n'|'\r')*('\r'? '\n')
;
terminal INT_RANGE:
	INT'..'INT
;
terminal CONTEXT:
	'if_context('(INT|INT_RANGE)(',' (INT|INT_RANGE))*')'(':')?
;


The general idea would be for the "open" function to replace the colon with a semicolon if the context doesn't match that of the editor. Likewise, when saving the file, any instances of if_context(*); would be replaced with if_context(*):
So my question boils down to A) is there a better way to do this? and B) If not, is there a reference for IResource that will help me figure out which methods are called and when by the UI, and what I need to do to re-preprocess the file when the user chooses a new context?

Thanks in advance!

Jon
Re: XText Preprocess [message #698257 is a reply to message #698098] Tue, 19 July 2011 06:07 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

why do you put everything into the terminals, a grammar should have as few terminals as possible, as the tokenisation is done without any context, whereas parser rules allow for a context? Xtext is also about instantiating a semantic model. The grammar should be forgiving in the sense that it checks only if the input is syntactically correct, everything else should be done on the semantic level (validation of the model) as that allowes good error messages. You probably should also look at the arithmetic example shipped with Xtext1, blogs on expressions in Xtext and possibly Xbase, as you seem to deal with expressions.
However your int range and context rule should rather look something like the following

Context:'if_context' '(' contextElement+=ContextElement (',' contextElement+=ContextElement)*')'(':')?;
ContextElement: IntRange|IntElement;
IntRange: from=INT '..' to=INT;
IntElement: value=INT;

That way you have access to the actual values in the context for semantic validation. Note that this snippet may not be the best alternative, it should just serve as inspiration.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: XText Preprocess [message #698424 is a reply to message #698257] Tue, 19 July 2011 13:00 Go to previous message
jfisher is currently offline jfisherFriend
Messages: 2
Registered: July 2011
Junior Member
Thanks for the input, I will make those changes. I originally had everything as terminals because I hadn't found the documentation that allowed the redefinition of hidden tokens on a per rule basis and whitespace is definitely not valid in the middle of some of these tokens. I have looked at many of the blogs for expressions and implemented those, I didn't include them in my snippet here because they work Wink
Previous Topic:Problem with unorder group
Next Topic:rename refactoring in the presence of nested elements
Goto Forum:
  


Current Time: Fri Apr 19 23:27:34 GMT 2024

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

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

Back to the top