Skip to content

Commit

Permalink
fix: correctly handle preproc alternatives
Browse files Browse the repository at this point in the history
This commit merges functions "elseBlock" and "elifBlock" in a single
function "alternativeBlock". Any call to either function is replaced by
a call to "alternativeBlock", which cover all alternatives (elif,
elifdef and else). The goal is twofold:

1) Correct the grammar to allow all kind of alternatives independently
   of the "if" directive. Before the fix, "#if" alternative was
   generated by elseBlock only, and thus disallow "#elifdef" as a
   follow-up.
2) As a side effect, simplify the parser.
  • Loading branch information
Valentin Touzeau authored and amaanq committed Apr 5, 2024
1 parent 72084f4 commit ff7f769
Show file tree
Hide file tree
Showing 6 changed files with 78,244 additions and 102,433 deletions.
22 changes: 6 additions & 16 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1375,39 +1375,29 @@ function preprocIf(suffix, content, precedence = 0) {
* @return {ChoiceRule}
*
*/
function elseBlock($) {
function alternativeBlock($) {
return choice(
suffix ? alias($['preproc_else' + suffix], $.preproc_else) : $.preproc_else,
suffix ? alias($['preproc_elif' + suffix], $.preproc_elif) : $.preproc_elif,
suffix ? alias($['preproc_elifdef' + suffix], $.preproc_elifdef) : $.preproc_elifdef,
);
}

/**
*
* @param {GrammarSymbols<string>} $
*
* @return {AliasRule | SymbolRule<string>}
*
*/
function elifBlock($) {
return suffix ? alias($['preproc_elifdef' + suffix], $.preproc_elifdef) : $.preproc_elifdef;
}

return {
['preproc_if' + suffix]: $ => prec(precedence, seq(
preprocessor('if'),
field('condition', $._preproc_expression),
'\n',
repeat(content($)),
field('alternative', optional(elseBlock($))),
field('alternative', optional(alternativeBlock($))),
preprocessor('endif'),
)),

['preproc_ifdef' + suffix]: $ => prec(precedence, seq(
choice(preprocessor('ifdef'), preprocessor('ifndef')),
field('name', $.identifier),
repeat(content($)),
field('alternative', optional(choice(elseBlock($), elifBlock($)))),
field('alternative', optional(alternativeBlock($))),
preprocessor('endif'),
)),

Expand All @@ -1421,14 +1411,14 @@ function preprocIf(suffix, content, precedence = 0) {
field('condition', $._preproc_expression),
'\n',
repeat(content($)),
field('alternative', optional(elseBlock($))),
field('alternative', optional(alternativeBlock($))),
)),

['preproc_elifdef' + suffix]: $ => prec(precedence, seq(
choice(preprocessor('elifdef'), preprocessor('elifndef')),
field('name', $.identifier),
repeat(content($)),
field('alternative', optional(elseBlock($))),
field('alternative', optional(alternativeBlock($))),
)),
};
}
Expand Down
221 changes: 147 additions & 74 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ff7f769

Please sign in to comment.