tree-sitter-cli icon indicating copy to clipboard operation
tree-sitter-cli copied to clipboard

Non greedy regular expressions aren't working.

Open jonhue opened this issue 7 years ago • 1 comments

I have a grammar that uses this regex to match comments (it's in the extras array):

comment: $ => /((\/\/[^\r\n]*)|(\/\*(.|\s)*?\*\/))/

This fails on the following test:

=====================================
Comments
=====================================

1 // C$mment
1// C$mment
1/* C$mment
C$mment

C$mment*/
1

---

(file
  (integer) (comment)
  (integer) (comment)
  (integer) (comment)
  (integer))

However, when I make the regex greedy (/((\/\/[^\r\n]*)|(\/\*(.|\s)*\*\/))/), the test is successful. The regex has to be non greedy for cases like this:

1 // C$mment
1// C$mment
1/* C$mment
C$mment

C$mment*/ + 1
/* C$mment */
1

jonhue avatar Jan 20 '19 17:01 jonhue

Yeah, non-greedy regexes are not supported.

It looks like you're trying to match C-style comments. If so, have you tried this regex?

maxbrunsfeld avatar Jan 21 '19 00:01 maxbrunsfeld