Whitespace without newlines [message #1059120] |
Thu, 16 May 2013 20:23  |
Brad Riching Messages: 20 Registered: May 2012 |
Junior Member |
|
|
Hello Xtext enthusiasts,
My language has some whitespace-sensitive aspects with newlines which have forced me to implement the whitespace rule like this with only spaces and tabs:
terminal WS:
(' '|'\t')+;
Throughout the rest of my grammar I look for one of three possible alternatives that end in a newline character. I created a datatype rule for an end-of-line (EOL).
EOL:
SL_COMMENT | LINEWRAP | NEWLINE;
terminal LINEWRAP:
('\\' '\r'? '\n');
terminal SL_COMMENT:
'//' !('\r'|'\n')* ('\r'? '\n');
terminal NEWLINE:
('\r'? '\n');
I hide my whitespace rule throughout the grammar, so all spaces and tabs are hidden. A simplified example of this is as follows:
grammar XXXXX
hidden(WS)
generate wirelist "http://XXXX/Wirelist"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
Model: {Model}
(blocks+=Block)* EOL*;
Block:
EOL*
( Block1
| Block2
);
Block1:
'BLOCK1' EOL* 'BEGIN' EOL*
(
('PARAM1' ',' p1val1=ID',' p1val2=Number ',' p1val3=ID EOL+) &
('PARAM2' ',' p2val1=ID EOL+) &
('PARAM3' ',' p3val1=ID EOL+)
) 'END';
Block2:
'BLOCK2' ...
As you can see, I use my EOL rule whenever I need to have at least one newline character end the line before anything else can happen. In this sense, this language is truly a whitespace-sensitive language since there must be at least one new-line detected after every parameter. A valid model for this might be:
BLOCK1
BEGIN
PARAM1, abc, 123, def
PARAM3, ghi
PARAM2, jkl
END
whereas an invalid model might be:
BLOCK1
BEGIN
PARAM1, abc, 123, def PARAM3, ghi
PARAM2, jkl
END
The good news is that the editor seems to be working nicely. The bad news is that when I run the formatter, it adds a bunch of new lines everywhere with each invocation, and the text grows in length. So, formatting the valid model code above one time yields:
BLOCK1
BEGIN
PARAM1, abc, 123, def
PARAM3, ghi
PARAM2, jkl
END
Formatting again yields:
BLOCK1
BEGIN
PARAM1, abc, 123, def
PARAM3, ghi
PARAM2, jkl
END
My guess is that since I have made newline tokens separate from whitespace tokens the formatter API is getting confused. I have taken a look at IHiddenTokenHelper and DefaultHiddenTokenHelper in the hopes that I could override its behavior but have not yet had any success. I've set breakpoints in the setLinewrap() routine, but can't figure out exactly what is happening. Is there something else that I have to do to tell xtext that I want to consider all WS and EOL tokens when formatting?
Thanks in advance!
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02186 seconds