Menu

#2182 Ruby wrong syntax highlighting for percent

Bug
open-accepted
nobody
5
2023-02-17
2020-06-04
No

Original issue: https://github.com/geany/geany/issues/2038
Geany wrongly colors sequences starting with a percentage sign (%).
Program that illustrates some issues:

n = 1337;
w = r = 5
x = n%w + 1; # "%w " is recognised as the beginning of an array of words, like %w[ ... ]
y = n%r ;   # "%r " is recognised as the beginning of a regular expression
string = %= this is a string = ;  # this one is correctly colored
n %= w;  # this is the most important issue, the expression is equivalent to n = n % w; but it's recognised like the previous line
print x%r  # %r is also recognised like a beginning of regex, but in this case, the %= of the previous line is still not terminated (no equal sign encoutered).

Discussion

  • Neil Hodgson

    Neil Hodgson - 2020-06-04
    • labels: lexer, Ruby, Ruby Lexer, highlighting --> lexer, Ruby Lexer, highlighting, ruby
    • status: open --> open-accepted
    • Priority: 4 --> 5
     
  • Neil Hodgson

    Neil Hodgson - 2020-06-04

    Can you explain the rule used by Ruby to treat these occurrences of '%' as not quote prefixes?

     
    • Redouane NIBOUCHA

      Yes, it's the modulo operator, like in other programming languages, not sure on the grammar rules used by Ruby to distinguish between when it's a quote prefix and when it's an operator, but it seems like:

      %w+5 1 + => ["5","1"]
      w=2; 5 %w+2 +  => (waits for more inpout, % is the modulo operator and not a prefix)
      5 %w + 2  => 3
      # n %= w is like n = n % w, as in C/C++
      

      (the + is just a delimiter, [ ] or ( ) can be used, or & &, = =, and many others)

       
  • Neil Hodgson

    Neil Hodgson - 2020-06-11

    Without an easy way to work out what each '%' means, I'll leave this to others more involved with Ruby.

     
  • Zufu Liu

    Zufu Liu - 2020-06-22

    [bugs:#1255] is a related bug for '%' in Ruby.

     

    Related

    Bugs: #1255

  • Zufu Liu

    Zufu Liu - 2023-02-15

    Here (parse_percent function) is how % is parsed
    https://github.com/ruby/ruby/blob/master/parse.y#L9299

    I think %= is fixed:

        if ((c = nextc(p)) == '=') {
        set_yylval_id('%');
        SET_LEX_STATE(EXPR_BEG);
        return tOP_ASGN;
        }
    

    fix others is complex.

     
  • Zufu Liu

    Zufu Liu - 2023-02-17
    • summary: Ruby wrong syntax highlighting --> Ruby wrong syntax highlighting for percent
     

Log in to post a comment.

MongoDB Logo MongoDB