ammar / regexp_parser

A regular expression parser library for Ruby
MIT License
144 stars 23 forks source link

Failure to parse `\g` #65

Closed marcandre closed 4 years ago

marcandre commented 4 years ago

The following regexp /[a]\g/ is accepted by Ruby, yet RegexpParser raises an error:

> ruby -e "p(/[a]\g/ =~ 'ag')"
0
> ruby -r regexp_parser -e "Regexp::Parser.parse '/[a]\g/'"
Traceback (most recent call last):
    6: from -e:1:in `<main>'
    5: from /Users/mal/.rvm/gems/ruby-2.7.1/gems/regexp_parser-1.7.1/lib/regexp_parser/parser.rb:22:in `parse'
    4: from /Users/mal/.rvm/gems/ruby-2.7.1/gems/regexp_parser-1.7.1/lib/regexp_parser/parser.rb:38:in `parse'
    3: from /Users/mal/.rvm/gems/ruby-2.7.1/gems/regexp_parser-1.7.1/lib/regexp_parser/lexer.rb:15:in `lex'
    2: from /Users/mal/.rvm/gems/ruby-2.7.1/gems/regexp_parser-1.7.1/lib/regexp_parser/lexer.rb:28:in `lex'
    1: from /Users/mal/.rvm/gems/ruby-2.7.1/gems/regexp_parser-1.7.1/lib/regexp_parser/scanner.rb:71:in `scan'
/Users/mal/.rvm/gems/ruby-2.7.1/gems/regexp_parser-1.7.1/lib/regexp_parser/scanner.rb:2664:in `scan': Scan error at '\\g/' (Regexp::Scanner::ScannerError)

The same reasoning as #63 applies: while technically incorrect, this Regexp is actually accepted and might exist in the wild. Would it be possible to tweak the gem to parse it the same way Ruby does?

jaynetics commented 4 years ago

this works back to ruby 1.9, lets support it...

jaynetics commented 4 years ago

@marcandre thanks for reporting this, i'll release a new version with a fix in the next few days.

jaynetics commented 4 years ago

released with v1.8.0

marcandre commented 4 years ago

Awesome, thanks 🎉