-
I would like to preserve the whitespace ignoring for most rules, but at the same time, I want to prohibit having spaces in certain rules. For example, I have variable declaration like this this.RULE("variableName", () => {
this.CONSUME(t.$);
this.CONSUME(t.Identifier);
});
this.RULE("type", () => {
this.CONSUME(t.Identifier);
this.OPTION(() => {
this.CONSUME(t.LSquare);
this.CONSUME(t.RSquare);
})
}); and I want to prevent "correct" parsing of something like |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hello @rzcoder Note that the syntax you want to prevent is allowed TypeScript and Java, so it is not that "strange".
Whitespace decides between alternative exampleEven though whitespace is ignored in the rest of the grammar. chevrotain/examples/grammars/css/css.js Lines 426 to 447 in 4a68802 Recommended approach: Treat this as a linting/formatting warning, not a syntax error.Not all errors should be syntax (grammar) errors, it is often easier to parse a super-set of the allowed grammar
|
Beta Was this translation helpful? Give feedback.
-
Hey @bd82! Great thanks for answering! Checking LookAhead works for me so far. Can I also ask, if code like this is common way to use simple CONSUME's with GATES? this.RULE("variableName", () => {
this.OR([{
GATE: () => {
const prevToken = this.LA(1);
const nextToken = this.LA(2);
return prevToken && nextToken && prevToken.endOffset + 1 === nextToken!.startOffset;
},
ALT: () => {
this.CONSUME(t.$);
this.CONSUME(t.Identifier);
},
}]);
}); And I also want to note that whitespace characters are a rather important part of the language syntax. Even for something like TypeScript, there's a significant difference in how a newline character is handled depending on the context. For example https://www.typescriptlang.org/play?ssl=5&ssc=14&pln=1&pc=1#code/MYewdgzgLgBAHgLjAVwLYCMCmAnGMBQMAvDANoCMANAEyUDMAuvoaJLAJ5Jpa4FkPEyVWoyA It would be nice to have the ability to generate whitespace characters in the main token stream (like special group) and have the option for automatic ignoring or, conversely, explicit indication of the absence/presence of such a character within the rules. |
Beta Was this translation helpful? Give feedback.
Hello @rzcoder
Note that the syntax you want to prevent is allowed TypeScript and Java, so it is not that "strange".
Whitespace decides between alternative example
Even though whitespace is ignored in the rest of the grammar.
chevrotain/examples/grammars/css/css.js
Lines 426 to 447 in 4a68802