Skip to content

Commit a55e4a6

Browse files
authored
Fix C# regression in preprocessor directive parsing (#514)
* Fix C# regression in preprocessor directive parsing It was due to a left-over syntax extension workaround that no longer applies and was causing conflicts. * Fix parsing of typed metavariables Test 'Typed metavariable 3' was failing and was being parsed as binary '<' and '>' operations.
1 parent 208d243 commit a55e4a6

File tree

4 files changed

+144
-18
lines changed

4 files changed

+144
-18
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
void F()
2+
{
3+
#if DEBUG
4+
a();
5+
b();
6+
#endif
7+
}

lang/semgrep-grammars/src/semgrep-c-sharp-pro/grammar.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ module.exports = grammar(standard_grammar, {
1717
conflicts: ($, previous) => [
1818
...previous,
1919
[$.expression, $.parameter],
20-
[$.file_scoped_namespace_declaration, $.preproc_if_in_top_level],
21-
[$.file_scoped_namespace_declaration, $.preproc_else_in_top_level],
2220
],
2321

2422
rules: {
@@ -77,20 +75,6 @@ module.exports = grammar(standard_grammar, {
7775
)
7876
},
7977

80-
// We do this because a file scoped namespace declaration is a top-level
81-
// thing, but ellipses are more particular. We want ellipses to be used
82-
// in conjunction with file scoped namespace declarations.
83-
// Unfortunately, in the base grammar, we can either have ellipsis
84-
// statements, or a file scoped declaration! That's no good. To play
85-
// around the previous grammar, we simply allow what came before to also
86-
// occur before a file scoped namespace declaration.
87-
file_scoped_namespace_declaration: ($, previous) => {
88-
return seq(
89-
seq(repeat($.global_statement), repeat($._namespace_member_declaration)),
90-
previous,
91-
)
92-
},
93-
9478
enum_member_declaration: ($, previous) => choice(
9579
previous,
9680
$.ellipsis,
@@ -129,12 +113,12 @@ module.exports = grammar(standard_grammar, {
129113

130114
// use syntax similar to a cast_expression, but with metavar
131115
//TODO: use PREC.CAST from original grammar instead of 17 below
132-
typed_metavariable: $ => prec.right(17, seq(
116+
typed_metavariable: $ => prec(17, prec.dynamic(1, seq(
133117
'(',
134118
field('type', $.type),
135119
field('metavar', $._semgrep_metavariable),
136120
')',
137-
)),
121+
))),
138122

139123
deep_ellipsis: $ => seq(
140124
'<...', $.expression, '...>'

lang/semgrep-grammars/src/semgrep-c-sharp-pro/test/corpus/semgrep.txt

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,52 @@ __SEMGREP_EXPRESSION
260260
(identifier))
261261
(argument_list))))
262262

263+
===============================================================================
264+
Typed metavariable 2
265+
===============================================================================
266+
267+
__SEMGREP_EXPRESSION (T $X)
268+
269+
---
270+
271+
(compilation_unit
272+
(semgrep_expression
273+
(typed_metavariable
274+
(identifier))))
275+
276+
===============================================================================
277+
Typed metavariable 3
278+
===============================================================================
279+
280+
__SEMGREP_EXPRESSION (List<T> $X)
281+
282+
---
283+
284+
(compilation_unit
285+
(semgrep_expression
286+
(typed_metavariable
287+
(generic_name
288+
(identifier)
289+
(type_argument_list
290+
(identifier))))))
291+
292+
===============================================================================
293+
Cast expression
294+
===============================================================================
295+
296+
__SEMGREP_EXPRESSION (List<T>) x
297+
298+
---
299+
300+
(compilation_unit
301+
(semgrep_expression
302+
(cast_expression
303+
(generic_name
304+
(identifier)
305+
(type_argument_list
306+
(identifier)))
307+
(identifier))))
308+
263309
================================================================================
264310
Ellipses + metavariables as args
265311
================================================================================
@@ -376,3 +422,63 @@ namespace $N;
376422
(global_statement
377423
(expression_statement
378424
(ellipsis))))
425+
426+
================================================================================
427+
Preprocessor conditional with single statement
428+
================================================================================
429+
430+
void F()
431+
{
432+
#if DEBUG
433+
a();
434+
#endif
435+
}
436+
437+
---
438+
439+
(compilation_unit
440+
(global_statement
441+
(local_function_statement
442+
(predefined_type)
443+
(identifier)
444+
(parameter_list)
445+
(block
446+
(preproc_if
447+
(identifier)
448+
(expression_statement
449+
(invocation_expression
450+
(identifier)
451+
(argument_list))))))))
452+
453+
===============================================================================
454+
Preprocessor conditional with multiple statements
455+
===============================================================================
456+
457+
void F()
458+
{
459+
#if DEBUG
460+
a();
461+
b();
462+
#endif
463+
}
464+
465+
---
466+
467+
(compilation_unit
468+
(global_statement
469+
(local_function_statement
470+
(predefined_type)
471+
(identifier)
472+
(parameter_list)
473+
(block
474+
(preproc_if
475+
(identifier)
476+
(expression_statement
477+
(invocation_expression
478+
(identifier)
479+
(argument_list)))
480+
(expression_statement
481+
(invocation_expression
482+
(identifier)
483+
(argument_list))))))))
484+

lang/semgrep-grammars/src/semgrep-c-sharp/test/corpus/semgrep.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,35 @@ __SEMGREP_EXPRESSION
267267
(identifier))
268268
(argument_list))))
269269

270+
===============================================================================
271+
Typed metavariable 2
272+
===============================================================================
273+
274+
__SEMGREP_EXPRESSION (T $X)
275+
276+
---
277+
278+
(compilation_unit
279+
(semgrep_expression
280+
(typed_metavariable
281+
(identifier))))
282+
283+
===============================================================================
284+
Typed metavariable 3
285+
===============================================================================
286+
287+
__SEMGREP_EXPRESSION (List<T> $X)
288+
289+
---
290+
291+
(compilation_unit
292+
(semgrep_expression
293+
(typed_metavariable
294+
(generic_name
295+
(identifier)
296+
(type_argument_list
297+
(identifier))))))
298+
270299
================================================================================
271300
Ellipses + metavariables as args
272301
================================================================================

0 commit comments

Comments
 (0)