XText grammar with lookbehind [message #1756248] |
Tue, 14 March 2017 09:41  |
Eclipse User |
|
|
|
Hello,
I am new in developing a DSL using XText and I am currently stuck in a problem.
I have to parse a file which contains elements called Class and might look like this:
A {
B, //this comma is needed
C,
D {E}, //comma is optional after a closing curly brace
F {G}
H
}
I,
J
As you can see, a comma is a must after a class without subclasses (those enclosed by curly brackets) when another class follows. Otherwise, the comma is optional.
My current (wrong) solution is:
Model: (classes += Class)*
Class: name = ID ('{' (subclasses += Class) (',' subclasses += Class)* '}')?
Sadly, this works only when the comma is a must in any case. I believe the grammar should express the following structure:
Class: ID (',' Class | '{' subclasses += Class '}' ','? Class)?
Of course, that does not work since I need to assign the return value of Class to the subclasses of the parent. After some looking around different blogs, I belive that using a lookbehind would be a solution. However, I havn't found a way to express this in XText.
Is there a solution or a workaround for this issue?
Thanks in advance
Stephan
|
|
|
Re: XText grammar with lookbehind [message #1756272 is a reply to message #1756248] |
Tue, 14 March 2017 14:51  |
Eclipse User |
|
|
|
Did you try something like
Model:
((classes+=ClassWithBraces|classes+=ClassWithComma)* classes+=ClassAtEnd?);
ClassWithComma returns Class:
name=ID ","
;
ClassWithBraces returns Class:
name=ID =>("{" ((classes+=ClassWithBraces|classes+=ClassWithComma)* classes+=ClassAtEnd?) "}") ","?
;
ClassAtEnd returns Class:
name=ID
;
(you may need to add a validation if you dont want a "," at the very end
[Updated on: Tue, 14 March 2017 15:11] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.07395 seconds