Incorrect syntax highlight on coffee-script regexp

Issue #991 resolved
Mirek Rusin created an issue

Comments (4)

  1. Lev Lozhkin

    Here's a patch that allows proper highlighting of regex in this case.

    diff -r 2ba9b53c87ee pygments/lexers/web.py
    --- a/pygments/lexers/web.py    Tue May 20 07:44:45 2014 -0700
    +++ b/pygments/lexers/web.py    Wed May 21 19:05:18 2014 -0700
    @@ -2469,21 +2469,23 @@
                 (r'#{', String.Interpol, 'interpoling_string'),
                 (r'[/#]', String.Regex),
             ],  
    +        'operators': [
    +            (r'\+\+|~|&&|\band\b|\bor\b|\bis\b|\bisnt\b|\bnot\b|\?|:|'
    +             r'\|\||\\(?=\n)|'
    +             r'(<<|>>>?|==?(?!>)|!=?|=(?!>)|-(?!>)|[<>+*`%&\|\^/])=?',
    +             Operator),
    +        ],
             'slashstartsregex': [
    -            include('commentsandwhitespace'),
                 (r'///', String.Regex, ('#pop', 'multilineregex')),
                 (r'/(?! )(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
                  r'([gim]+\b|\B)', String.Regex, '#pop'),
    -            default('#pop'),
    +            include('operators'), # consume the slash to avoid inf loop
    +            default('#pop')
             ],
             'root': [
    -            # this next expr leads to infinite loops root -> slashstartsregex
    -            #(r'^(?=\s|/|<!--)', Text, 'slashstartsregex'),
                 include('commentsandwhitespace'),
    -            (r'\+\+|~|&&|\band\b|\bor\b|\bis\b|\bisnt\b|\bnot\b|\?|:|'
    -             r'\|\||\\(?=\n)|'
    -             r'(<<|>>>?|==?(?!>)|!=?|=(?!>)|-(?!>)|[<>+*`%&\|\^/])=?',
    -             Operator, 'slashstartsregex'),
    +            (r'(?=/)', Text, 'slashstartsregex'),
    +            include('operators'),
                 (r'(?:\([^()]*\))?\s*[=-]>', Name.Function),
                 (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
                 (r'[})\].]', Punctuation),
    

    Here are some test cases as well.

    diff -r 2ba9b53c87ee tests/examplefiles/example.coffee
    --- a/tests/examplefiles/example.coffee Tue May 20 07:44:45 2014 -0700
    +++ b/tests/examplefiles/example.coffee Wed May 21 18:55:59 2014 -0700
    @@ -25,3 +25,34 @@
     '#wow'
     'wow#'
     'w#ow'
    +
    +# regex
    +
    +z = /a/.test(string)
    +/\\/
    +/\// # comment shouldnt be part of the regex
    +/\\\//
    +/(\s)/
    +/a{2,8}/
    +/b*c?d+/
    +/(capture-match)/
    +/(?:do-not-capture-match)/
    +/this|or|that/
    +/[char-set]/
    +/[^neg-char_st]/
    +/^.*$/
    +/\n(\f)\0\1\d\b\cm\u1234/
    +/^.?([^/\\\n\w]*)a\1+$/.something(or_other) # something more complex
    +foo = (str) ->
    +  /'|"/.test str
    +a = a / b / c
    +a = a/b/c
    +a = a/b/ c
    +a = a /b/c
    +a = 1 + /d/.test(a)
    +
    +/// # multi-line regex
    +(capture)
    +[^excl-this] # a comment in multi-line regex
    +///
    

    All work as expected except a = a/b/ c. The /b/ is highlighted as regex there, any suggestions on how to fix that?

  2. Log in to comment