-
I think I must be missing something about how CONSUME should be used. I have a grammar where newlines are significant. Imagine this rule:
And this input: The problem I have is that the CST generated from this input looks like this:
If the input contains one of the optional newlines, then the CST only contains one newline - but there is no positional information to tell me where that Newline goes. I was expecting the What am I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey @k1w1,
You might have a misunderstanding of what the CST exactly represents. As the concrete syntax tree, it only contains what concretely exists within the input text - and that would be exactly one In order to figure out which of the options was actually invoked, you could analyze the offsets of the tokens. Though I'm generally not a big fan of using pure CST parsers, as they lose too much semantic information for my purposes (we built Langium with Chevrotain). I usually build an property = this.RULE('property', () => {
const newlines = [null, null, null, null];
this.OPTION1(() => newlines[0] = this.CONSUME1(Newline));
const firstText = this.CONSUME2(Text);
this.OPTION2(() => newlines[1] = this.CONSUME3(Newline));
this.CONSUME4(Colon);
this.OPTION3(() => newlines[2] = this.CONSUME5(Newline));
const secondText = this.CONSUME6(Text);
this.OPTION4(() => newlines[3] = this.CONSUME7(Newline));
return {
newlines,
firstText,
secondText
};
}); |
Beta Was this translation helpful? Give feedback.
But a bit more stepping through the code made me realize that there was a solution all along. The
LABEL
option forCONSUME
exactly solves my problem: