Skip to content

Commit 8c00f6a

Browse files
Valentin TouzeauValentin Touzeau
Valentin Touzeau
authored and
Valentin Touzeau
committed
Fix preproc alternatives (elif, elifdef, else)
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.
1 parent 72084f4 commit 8c00f6a

File tree

7 files changed

+78246
-102437
lines changed

7 files changed

+78246
-102437
lines changed

grammar.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -1375,39 +1375,29 @@ function preprocIf(suffix, content, precedence = 0) {
13751375
* @return {ChoiceRule}
13761376
*
13771377
*/
1378-
function elseBlock($) {
1378+
function alternativeBlock($) {
13791379
return choice(
13801380
suffix ? alias($['preproc_else' + suffix], $.preproc_else) : $.preproc_else,
13811381
suffix ? alias($['preproc_elif' + suffix], $.preproc_elif) : $.preproc_elif,
1382+
suffix ? alias($['preproc_elifdef' + suffix], $.preproc_elifdef) : $.preproc_elifdef,
13821383
);
13831384
}
13841385

1385-
/**
1386-
*
1387-
* @param {GrammarSymbols<string>} $
1388-
*
1389-
* @return {AliasRule | SymbolRule<string>}
1390-
*
1391-
*/
1392-
function elifBlock($) {
1393-
return suffix ? alias($['preproc_elifdef' + suffix], $.preproc_elifdef) : $.preproc_elifdef;
1394-
}
1395-
13961386
return {
13971387
['preproc_if' + suffix]: $ => prec(precedence, seq(
13981388
preprocessor('if'),
13991389
field('condition', $._preproc_expression),
14001390
'\n',
14011391
repeat(content($)),
1402-
field('alternative', optional(elseBlock($))),
1392+
field('alternative', optional(alternativeBlock($))),
14031393
preprocessor('endif'),
14041394
)),
14051395

14061396
['preproc_ifdef' + suffix]: $ => prec(precedence, seq(
14071397
choice(preprocessor('ifdef'), preprocessor('ifndef')),
14081398
field('name', $.identifier),
14091399
repeat(content($)),
1410-
field('alternative', optional(choice(elseBlock($), elifBlock($)))),
1400+
field('alternative', optional(alternativeBlock($))),
14111401
preprocessor('endif'),
14121402
)),
14131403

@@ -1421,14 +1411,14 @@ function preprocIf(suffix, content, precedence = 0) {
14211411
field('condition', $._preproc_expression),
14221412
'\n',
14231413
repeat(content($)),
1424-
field('alternative', optional(elseBlock($))),
1414+
field('alternative', optional(alternativeBlock($))),
14251415
)),
14261416

14271417
['preproc_elifdef' + suffix]: $ => prec(precedence, seq(
14281418
choice(preprocessor('elifdef'), preprocessor('elifndef')),
14291419
field('name', $.identifier),
14301420
repeat(content($)),
1431-
field('alternative', optional(elseBlock($))),
1421+
field('alternative', optional(alternativeBlock($))),
14321422
)),
14331423
};
14341424
}

src/grammar.json

+147-74
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)