From eaa878ce957df778a23e1ea0fece3955401de253 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 20 Jun 2017 09:33:19 -0700 Subject: [PATCH] Allow preprocessor conditionals inside functions --- corpus/preprocessor.txt | 24 ++++++++++++++++++++++++ grammar.js | 8 +++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/corpus/preprocessor.txt b/corpus/preprocessor.txt index 2c839a1..5dee6a3 100644 --- a/corpus/preprocessor.txt +++ b/corpus/preprocessor.txt @@ -109,3 +109,27 @@ General if blocks (preproc_if (logical_expression (call_expression (identifier) (identifier)) (call_expression (identifier) (identifier))) (preproc_def (identifier) (preproc_arg)))) + +============================================ +Preprocessor conditionals in functions +============================================ + +int main() { + #if a + return 0; + #else + return 1; + #endif +} + +--- + +(translation_unit + (function_definition + (identifier) + (function_declarator (identifier)) + (compound_statement + (preproc_if (identifier) + (return_statement (number_literal)) + (preproc_else + (return_statement (number_literal))))))) \ No newline at end of file diff --git a/grammar.js b/grammar.js index 2f1f5a5..d3dc7ea 100644 --- a/grammar.js +++ b/grammar.js @@ -43,6 +43,7 @@ module.exports = grammar({ $._preproc_statement, $.function_definition, $.declaration, + $._statement, $._empty_declaration, $.linkage_specification ), @@ -98,6 +99,7 @@ module.exports = grammar({ preproc_if: $ => seq( '#if', $._expression, + '\n', repeat($._top_level_item), optional($.preproc_else), '#endif' @@ -225,11 +227,7 @@ module.exports = grammar({ compound_statement: $ => seq( '{', - repeat(choice( - $.declaration, - $._empty_declaration, - $._statement - )), + repeat($._top_level_item), '}' ),