Skip to content

Commit f82fbd5

Browse files
committed
Parse multiline preprocessor comments
1 parent cf5b92e commit f82fbd5

File tree

6 files changed

+790061
-784265
lines changed

6 files changed

+790061
-784265
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: =?UTF-8?q?I=C3=B1aki=20Amatria=20Barral?= <[email protected]>
3+
Date: Fri, 4 Jul 2025 10:23:30 +0200
4+
Subject: Parse multiline preprocessor comments
5+
6+
---
7+
grammar.js | 32 +++++++++++++++-----
8+
test/corpus/preprocessor.txt | 57 ++++++++++++++++++++++++++++++++++--
9+
2 files changed, 79 insertions(+), 10 deletions(-)
10+
11+
diff --git a/grammar.js b/grammar.js
12+
index 40ac8b7..b5c11d1 100644
13+
--- a/grammar.js
14+
+++ b/grammar.js
15+
@@ -75,6 +75,7 @@ module.exports = grammar({
16+
// preprocessor statements
17+
/\s|\\\r?\n/,
18+
$.comment,
19+
+ $.multiline_preproc_comment,
20+
'&',
21+
],
22+
23+
@@ -248,7 +249,8 @@ module.exports = grammar({
24+
')',
25+
),
26+
27+
- preproc_comment: $ => choice(/\/\*.*\*\//, /\/\/.*/),
28+
+ inline_preproc_comment: $ => /\/\/.*/,
29+
+ multiline_preproc_comment: $ => /\/\*([^*]|\*+[^*/])*\*+\//,
30+
31+
preproc_binary_expression: $ => {
32+
const table = [
33+
@@ -2466,36 +2468,50 @@ function preprocIf(suffix, content, precedence = 0) {
34+
);
35+
}
36+
37+
+ /**
38+
+ *
39+
+ * @param {GrammarSymbols<string>} $
40+
+ *
41+
+ * @return {ChoiceRule}
42+
+ *
43+
+ */
44+
+ function preprocComment($) {
45+
+ return choice(
46+
+ $.inline_preproc_comment,
47+
+ $.multiline_preproc_comment,
48+
+ );
49+
+ }
50+
+
51+
return {
52+
['preproc_if' + suffix]: $ => prec(precedence, seq(
53+
preprocessor('if'),
54+
field('condition', $._preproc_expression),
55+
- optional($.preproc_comment),
56+
+ optional(preprocComment($)),
57+
field('content', content($)),
58+
field('alternative', optional(alternativeBlock($))),
59+
preprocessor('endif'),
60+
- optional($.preproc_comment),
61+
+ optional(preprocComment($)),
62+
)),
63+
64+
['preproc_ifdef' + suffix]: $ => prec(precedence, seq(
65+
choice(preprocessor('ifdef'), preprocessor('ifndef')),
66+
field('name', $.identifier),
67+
- optional($.preproc_comment),
68+
+ optional(preprocComment($)),
69+
field('content', content($)),
70+
field('alternative', optional(alternativeBlock($))),
71+
preprocessor('endif'),
72+
- optional($.preproc_comment),
73+
+ optional(preprocComment($)),
74+
)),
75+
76+
['preproc_else' + suffix]: $ => prec(precedence, seq(
77+
preprocessor('else'),
78+
- optional($.preproc_comment),
79+
+ optional(preprocComment($)),
80+
field('content', content($)),
81+
)),
82+
83+
['preproc_elif' + suffix]: $ => prec(precedence, seq(
84+
preprocessor('elif'),
85+
- optional($.preproc_comment),
86+
+ optional(preprocComment($)),
87+
field('condition', $._preproc_expression),
88+
field('content', content($)),
89+
field('alternative', optional(alternativeBlock($))),
90+
@@ -2504,7 +2520,7 @@ function preprocIf(suffix, content, precedence = 0) {
91+
['preproc_elifdef' + suffix]: $ => prec(precedence, seq(
92+
choice(preprocessor('elifdef'), preprocessor('elifndef')),
93+
field('name', $.identifier),
94+
- optional($.preproc_comment),
95+
+ optional(preprocComment($)),
96+
field('content', content($)),
97+
field('alternative', optional(alternativeBlock($))),
98+
)),
99+
diff --git a/test/corpus/preprocessor.txt b/test/corpus/preprocessor.txt
100+
index 02c61bb..b0b35bd 100644
101+
--- a/test/corpus/preprocessor.txt
102+
+++ b/test/corpus/preprocessor.txt
103+
@@ -169,7 +169,7 @@ end subroutine foo4
104+
content: (preproc_def
105+
name: (identifier)
106+
value: (preproc_arg))))
107+
- (preproc_comment))
108+
+ (multiline_preproc_comment))
109+
(preproc_ifdef
110+
name: (identifier)
111+
content: (subroutine
112+
@@ -179,7 +179,7 @@ end subroutine foo4
113+
(end_subroutine_statement
114+
(name)
115+
(end_of_statement)))
116+
- (preproc_comment)))
117+
+ (inline_preproc_comment)))
118+
119+
================================================================================
120+
Elifdefs
121+
@@ -991,3 +991,56 @@ Preprocessor in specification part
122+
(end_subroutine_statement
123+
(name)
124+
(end_of_statement))))
125+
+
126+
+================================================================================
127+
+Multiline preprocessor comments
128+
+================================================================================
129+
+
130+
+/* Foo */
131+
+#ifdef FOO
132+
+#endif
133+
+
134+
+ PROGRAM MAIN
135+
+ INTEGER /* FOO */ FOO
136+
+ INTEGER BAR /* BAR */
137+
+ INTEGER /* B
138+
+ A
139+
+ Z */ BAZ
140+
+ END
141+
+
142+
+/* Foo
143+
+// Bar
144+
+Baz */
145+
+#ifdef BAR
146+
+#endif
147+
+
148+
+--------------------------------------------------------------------------------
149+
+
150+
+(translation_unit
151+
+ (multiline_preproc_comment)
152+
+ (preproc_ifdef
153+
+ (identifier))
154+
+ (program
155+
+ (program_statement
156+
+ (name)
157+
+ (end_of_statement))
158+
+ (variable_declaration
159+
+ (intrinsic_type)
160+
+ (multiline_preproc_comment)
161+
+ (identifier))
162+
+ (end_of_statement)
163+
+ (variable_declaration
164+
+ (intrinsic_type)
165+
+ (identifier))
166+
+ (multiline_preproc_comment)
167+
+ (end_of_statement)
168+
+ (variable_declaration
169+
+ (intrinsic_type)
170+
+ (multiline_preproc_comment)
171+
+ (identifier))
172+
+ (end_of_statement)
173+
+ (end_program_statement
174+
+ (end_of_statement)))
175+
+ (multiline_preproc_comment)
176+
+ (preproc_ifdef
177+
+ (identifier)))

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
)),

0 commit comments

Comments
 (0)