No announcement yet.

Incorrect syntax highlighting for certain bash constructs

  • Filter
  • Time
  • Show
Clear All
new posts

  • Incorrect syntax highlighting for certain bash constructs

    I found that the following type of bash construct is incorrectly highlighted, with everything following the "#" being treated as a comment:


    This causes differences in those regions to be treated as "unimportant", which is clearly not the case here, as we are just getting the number of elements in the pathparts array.

  • #2
    Thanks for the report. Our highlighting is controlled by the detected grammar elements, defined in the Tools menu -> File Formats dialog, Bash format, Grammar tab.

    My bash knowledge is a bit weaker, but would we need a new grammar to grab a larger section of the above code? Would the concept of ${...} need to be defined? If there is a defined larger grammar that would match and swallow the #, then it would be part of that grammar and not a comment. Or as a workaround, you can delete the Comment grammar to prevent it from matching.
    Aaron P Scooter Software


    • #3
      Sorry, my example was probably more complex than required. I think you just need to treat everything between ${ and } as not a comment. Here is the official word from the posix specification for the shell here:

      If the current character is an unquoted '$' or '`', the shell shall identify the start of any candidates for parameter expansion (Parameter Expansion), command substitution (Command Substitution), or arithmetic expansion (Arithmetic Expansion) from their introductory unquoted character sequences: '$' or "${", "$(" or '`', and "$((", respectively. The shell shall read sufficient input to determine the end of the unit to be expanded (as explained in the cited sections). While processing the characters, if instances of expansions or quoting are found nested within the substitution, the shell shall recursively process them in the manner specified for the construct that is found. The characters found from the beginning of the substitution to its end, allowing for any recursion necessary to recognize embedded constructs, shall be included unmodified in the result token, including any embedded or enclosing substitution operators or quotes. The token shall not be delimited by the end of the substitution.
      I took a stab at modifying the current grammar for "Environment Variable", which I think captures everything:




      • #4

        Another strategy would be to add a new Grammar element, type delimited, that matches on ${ to }. If this element is Important, then everything inside of it would also be important (and would prevent Comment from matching). Would this work as a general strategy for all of your bash files or might that conflict with another grammar type?
        Aaron P Scooter Software