Grammar Variants

I’m gonna write a grammar that has variants across the different versions of the language, this differences are somewhat small, like adding or removing operators, or changing the definition of a number to include int64 and uint64 instead of only doubles, and such.

Is there a simple way to do this? Would it require four grammars? if so can there be a base grammar that each would extend in their own way?

I’m coincidentally working on this problem already, in an attempt to cover TypeScript and JSX within the JavaScript grammar. The direction I’m currently working in is to allow a set of dialect flags to be specified when parsing, and to allow rules to be conditional on such flags. That’s probably not going to cover every case, but it might be a start.

I would like to do something similar for SQL and the different variations (primarily Presto and Spark). Any suggestions on a starting point?

Please take a look at lezer 0.10.0—specifically the dialect feature it adds.