|
| 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))) |
0 commit comments