-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to implement states for conditional language features #244
Comments
In general, our approach has been to handle things like this in an approximate way, by handling a superset of the actual language, and trying to parse ambiguous constructs in the most way that's most likely to be correct. For example, in C it's technically impossible to parse a single source file by itself, without looking at all the other source files that are brought in via Similarly, I don't know anything about SystemVerilog, but this may be the most practical approach for you - design the parser so that it handles You also could use an external scanner for parsing |
Thanks. Let's say I carry around which revision is active (i.e.. a state). How do I use that to enable/disable rules in the grammar.json code? |
It's totally late answer, I wasn't here at the time.
You can introduce several external scanner tokens that external scanner would emit depending on the state like externals: $ => [
// mode capturing externals
$.mod2001,
$.mod2017,
// mode enforcing externals
$._mod2001,
$._mod2017,
],
rules: {
_statements: $ => choice(
$._mod_selection,
seq($._mod2001, $._statements_2001),
seq($._mod2017, $._statements_2017),
),
_statements_2001: $ => choice(
// ... repeat here common statements + 1364-2001 specific
),
_statements_2017: $ => choice(
// ... repeat here common statements + 1800-2017 specific
),
_mod_selection: $ => seq("`begin_keywords",
'"',
choice(
alias($.mod2001, "1364-2001"),
alias($.mod2017, "1800-2017"),
),
'"'
),
} The mode enforcing externals are special invisible zero sized tokens that would have no impact on position of other tokens in a resulting parsing tree. |
I'm closing this because it's very old question, feel free to re-open it or move the discussion into the Discussions tab. |
SystemVerilog has the following:
That is the grammar changes based on the current language revision, and the parsing itself can change the revision.
How can this be expressed in a grammar, e.g. suppress certain rules based on runtime state?
In flex this was handled with different states, e.g.
This seems similar to the documented support for multi-language-documents, but I prefer not to maintain (nor preprocess to create) a grammar for each language version.
Thanks
The text was updated successfully, but these errors were encountered: