Skip to content

Commit 21fc11b

Browse files
committed
Parse multiline preprocessor comments
1 parent cf5b92e commit 21fc11b

File tree

2 files changed

+79
-10
lines changed

2 files changed

+79
-10
lines changed

grammar.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = grammar({
7575
// preprocessor statements
7676
/\s|\\\r?\n/,
7777
$.comment,
78+
$.multiline_preproc_comment,
7879
'&',
7980
],
8081

@@ -248,7 +249,8 @@ module.exports = grammar({
248249
')',
249250
),
250251

251-
preproc_comment: $ => choice(/\/\*.*\*\//, /\/\/.*/),
252+
inline_preproc_comment: $ => /\/\/.*/,
253+
multiline_preproc_comment: $ => /\/\*([^*]|\*+[^*/])*\*+\//,
252254

253255
preproc_binary_expression: $ => {
254256
const table = [
@@ -2466,36 +2468,50 @@ function preprocIf(suffix, content, precedence = 0) {
24662468
);
24672469
}
24682470

2471+
/**
2472+
*
2473+
* @param {GrammarSymbols<string>} $
2474+
*
2475+
* @return {ChoiceRule}
2476+
*
2477+
*/
2478+
function preprocComment($) {
2479+
return choice(
2480+
$.inline_preproc_comment,
2481+
$.multiline_preproc_comment,
2482+
);
2483+
}
2484+
24692485
return {
24702486
['preproc_if' + suffix]: $ => prec(precedence, seq(
24712487
preprocessor('if'),
24722488
field('condition', $._preproc_expression),
2473-
optional($.preproc_comment),
2489+
optional(preprocComment($)),
24742490
field('content', content($)),
24752491
field('alternative', optional(alternativeBlock($))),
24762492
preprocessor('endif'),
2477-
optional($.preproc_comment),
2493+
optional(preprocComment($)),
24782494
)),
24792495

24802496
['preproc_ifdef' + suffix]: $ => prec(precedence, seq(
24812497
choice(preprocessor('ifdef'), preprocessor('ifndef')),
24822498
field('name', $.identifier),
2483-
optional($.preproc_comment),
2499+
optional(preprocComment($)),
24842500
field('content', content($)),
24852501
field('alternative', optional(alternativeBlock($))),
24862502
preprocessor('endif'),
2487-
optional($.preproc_comment),
2503+
optional(preprocComment($)),
24882504
)),
24892505

24902506
['preproc_else' + suffix]: $ => prec(precedence, seq(
24912507
preprocessor('else'),
2492-
optional($.preproc_comment),
2508+
optional(preprocComment($)),
24932509
field('content', content($)),
24942510
)),
24952511

24962512
['preproc_elif' + suffix]: $ => prec(precedence, seq(
24972513
preprocessor('elif'),
2498-
optional($.preproc_comment),
2514+
optional(preprocComment($)),
24992515
field('condition', $._preproc_expression),
25002516
field('content', content($)),
25012517
field('alternative', optional(alternativeBlock($))),
@@ -2504,7 +2520,7 @@ function preprocIf(suffix, content, precedence = 0) {
25042520
['preproc_elifdef' + suffix]: $ => prec(precedence, seq(
25052521
choice(preprocessor('elifdef'), preprocessor('elifndef')),
25062522
field('name', $.identifier),
2507-
optional($.preproc_comment),
2523+
optional(preprocComment($)),
25082524
field('content', content($)),
25092525
field('alternative', optional(alternativeBlock($))),
25102526
)),

test/corpus/preprocessor.txt

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ end subroutine foo4
169169
content: (preproc_def
170170
name: (identifier)
171171
value: (preproc_arg))))
172-
(preproc_comment))
172+
(multiline_preproc_comment))
173173
(preproc_ifdef
174174
name: (identifier)
175175
content: (subroutine
@@ -179,7 +179,7 @@ end subroutine foo4
179179
(end_subroutine_statement
180180
(name)
181181
(end_of_statement)))
182-
(preproc_comment)))
182+
(inline_preproc_comment)))
183183

184184
================================================================================
185185
Elifdefs
@@ -991,3 +991,56 @@ Preprocessor in specification part
991991
(end_subroutine_statement
992992
(name)
993993
(end_of_statement))))
994+
995+
================================================================================
996+
Multiline preprocessor comments
997+
================================================================================
998+
999+
/* Foo */
1000+
#ifdef FOO
1001+
#endif
1002+
1003+
PROGRAM MAIN
1004+
INTEGER /* FOO */ FOO
1005+
INTEGER BAR /* BAR */
1006+
INTEGER /* B
1007+
A
1008+
Z */ BAZ
1009+
END
1010+
1011+
/* Foo
1012+
// Bar
1013+
Baz */
1014+
#ifdef BAR
1015+
#endif
1016+
1017+
--------------------------------------------------------------------------------
1018+
1019+
(translation_unit
1020+
(multiline_preproc_comment)
1021+
(preproc_ifdef
1022+
(identifier))
1023+
(program
1024+
(program_statement
1025+
(name)
1026+
(end_of_statement))
1027+
(variable_declaration
1028+
(intrinsic_type)
1029+
(multiline_preproc_comment)
1030+
(identifier))
1031+
(end_of_statement)
1032+
(variable_declaration
1033+
(intrinsic_type)
1034+
(identifier))
1035+
(multiline_preproc_comment)
1036+
(end_of_statement)
1037+
(variable_declaration
1038+
(intrinsic_type)
1039+
(multiline_preproc_comment)
1040+
(identifier))
1041+
(end_of_statement)
1042+
(end_program_statement
1043+
(end_of_statement)))
1044+
(multiline_preproc_comment)
1045+
(preproc_ifdef
1046+
(identifier)))

0 commit comments

Comments
 (0)