From 31c966ece24da893b3a1d16f6c1d959ee3bf5ddd Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 4 Oct 2017 09:24:51 -0700 Subject: [PATCH] Handle multi-line comments with asterisks at the end --- corpus/statements.txt | 22 ++++++++++++++++++++++ grammar.js | 12 +++++------- src/grammar.json | 18 +++--------------- src/parser.c | 4 ++++ 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/corpus/statements.txt b/corpus/statements.txt index c2a1eee..c52025c 100644 --- a/corpus/statements.txt +++ b/corpus/statements.txt @@ -109,3 +109,25 @@ recur: (identifier) (call_expression (field_expression (identifier) (field_identifier)) (argument_list))))) (if_statement (identifier) (goto_statement (statement_identifier)))))) + +============================================ +Comments with asterisks +============================================ + +/************************* + * odd number of asterisks + *************************/ +int a; + +/************************** + * even number of asterisks + **************************/ +int b; + +--- + +(translation_unit + (comment) + (declaration (type_identifier) (identifier)) + (comment) + (declaration (type_identifier) (identifier))) diff --git a/grammar.js b/grammar.js index 905f0e0..abbca8f 100644 --- a/grammar.js +++ b/grammar.js @@ -706,17 +706,15 @@ module.exports = grammar({ ')' ), + // http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890 comment: $ => token(choice( - seq( - '//', - /.*/ - ), + seq('//', /.*/), seq( '/*', - repeat(choice(/[^\*]/, /\*[^/]/)), - '*/' + /[^*]*\*+([^/*][^*]*\*+)*/, + '/' ) - )) + )), } }); diff --git a/src/grammar.json b/src/grammar.json index eb30b93..8880701 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -3578,24 +3578,12 @@ "value": "/*" }, { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "PATTERN", - "value": "[^\\*]" - }, - { - "type": "PATTERN", - "value": "\\*[^\\/]" - } - ] - } + "type": "PATTERN", + "value": "[^*]*\\*+([^\\/*][^*]*\\*+)*" }, { "type": "STRING", - "value": "*/" + "value": "/" } ] } diff --git a/src/parser.c b/src/parser.c index 5f2bff5..58dcef2 100644 --- a/src/parser.c +++ b/src/parser.c @@ -2053,6 +2053,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { ADVANCE(94); END_STATE(); case 95: + if (lookahead == '*') + ADVANCE(95); if (lookahead == '/') ADVANCE(96); if (lookahead != 0) @@ -3866,6 +3868,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { ACCEPT_TOKEN(sym_preproc_arg); if (lookahead == '\n') ADVANCE(94); + if (lookahead == '*') + ADVANCE(287); if (lookahead == '/') ADVANCE(96); if (lookahead == '\\')