Replies: 2 comments 6 replies
-
Hi @ivokosir, as per the documentation, the A short explanation what's happening here: During the grammar recording phase, the grammar doesn't execute the |
Beta Was this translation helpful? Give feedback.
-
Hello @ivokosir I am guessing this is the pattern you want to reuse? this.MANY(() => {
// INJECTED GRAMMAR (single subrule?)
this.CONSUME(L.SemiColon);
}); How often does this pattern repeat in your source code? It may not be worth the effort to extract. Anyhow it seems the basic level support for macros should exist: #1004
Dynamic rules creationI'm not sure if macros are even needed for this pattern. Could you dynamically create subrules instead? // e.g using something like:
function createManyWithSemiColonGrammar(innerGrammar, name) {
return this.RULE("name", () => {
this.MANY(() => {
innerGrammar()
this.CONSUME(L.SemiColon);
});
} I am not sure its worth the effort for your example, but perhaps the productive use case is more complex MacrosAvoiding indices conflicts is indeed a problem with macros. Additionally the parsing methods (CONSUME/MANY/...) have a variant which accepts the the index as a parameter, e.g:
So you could do a little bit of offsetting in your macros, e.g: export class DummyParser extends EmbeddedActionsParser {
// ...
macroBodySemiColon(idx, subRuleRef) {
this.many(idx + 10, () => {
this.subrule(idx + 10, subRuleRef)
this.consume(idx + 10, L.SemiColon);
});
}
definition = this.RULE("definition", () => {
macroBodySemiColon(1, this.model)
macroBodySemiColon(2, this.foo)
macroBodySemiColon(3, this.bar)
CONSUME3(L.SemiColon) // won't conflict with `consume` inside the macros because those consumes use 11/12/13 indices
});
model = this.RULE("model", () => {
this.CONSUME(L.Model);
this.CONSUME(L.Identifier);
});
} Perhaps this helps? |
Beta Was this translation helpful? Give feedback.
-
I want to create my own DSL function that would be something like a more complex
MANY
. How would I implement that? This was my simplified attempt (wanted DSL isbodySemicolon
), but parser throwsNotAllInputParsedException: Redundant input, expecting EOF but found: model
when trying to parse "model Name;"Attempt:
Beta Was this translation helpful? Give feedback.
All reactions