From 0e89e94d5fbdae99706e9749ebb2ad37157c1b96 Mon Sep 17 00:00:00 2001 From: susliko <1istoobig@gmail.com> Date: Mon, 12 Jun 2023 15:48:51 +0300 Subject: [PATCH] Better comments Problem ------- The following comment combinations are not parsed: 1. ``` // /* // * // */ ``` 2. ``` /* // */ ``` Solution ------- - set higher lexing priority for `$._comment_text` token - include "//" as an alternative to the contents of `$.block_comment` --- corpus/comments.txt | 29 +++++++++++++++++++++++++++-- grammar.js | 8 ++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/corpus/comments.txt b/corpus/comments.txt index 3b1d12cc4..27bc42a6f 100644 --- a/corpus/comments.txt +++ b/corpus/comments.txt @@ -29,8 +29,33 @@ Block comments (block_comment) (block_comment (block_comment - (block_comment) - (comment)))) + (block_comment)))) + +================================================================================ +Single line comments with block comment +================================================================================ + +// /* +// * This is awesome comment +// */ + +-------------------------------------------------------------------------------- + +(compilation_unit + (comment) + (comment) + (comment)) + +================================================================================ +Block comment with single-line comment inside +================================================================================ + +/* // */ + +-------------------------------------------------------------------------------- + +(compilation_unit + (block_comment)) ================================================================================ Using directives diff --git a/grammar.js b/grammar.js index 0ed9dd1b4..a1babbddb 100644 --- a/grammar.js +++ b/grammar.js @@ -1,6 +1,6 @@ const PREC = { - comment: 0, - using_directive: 1, + comment: 1, + using_directive: 2, control: 1, stable_type_id: 2, while: 2, @@ -1602,7 +1602,6 @@ module.exports = grammar({ ), comment: $ => seq(token("//"), choice($.using_directive, $._comment_text)), - _comment_text: $ => token(prec(PREC.comment, /.*/)), using_directive: $ => @@ -1615,7 +1614,8 @@ module.exports = grammar({ using_directive_key: $ => token(/[^\s]+/), using_directive_value: $ => token(/.*/), - block_comment: $ => seq(token("/*"), repeat(token(/./)), token("*/")), + block_comment: $ => + seq(token("/*"), repeat(choice(token(/./), token("//"))), token("*/")), }, });