0

The last two array expansions don't get proper highlighting:

This is the setting in sh.nanorc that defines it:

# More complicated variable names; handles braces and replacements and arrays.
color brightred "\$\{[#!]?([-@*#?$!]|[0-9]+|[[:alpha:]_][[:alnum:]_]*)(\[([[:space:]]*[[:alnum:]_]+[[:space:]]*|@)\])?(([#%/]|:?[-=?+])[^}]*\}|\[|\})"

What do I have to fix in the regex to catch this miss?

1 Answer 1

1

Regexes like that are a bit of a write-only language, but I think the (\[([[:space:]]*[[:alnum:]_]+[[:space:]]*|@)\])? in the middle catches the array indexes. It also doesn't recognize [*] as an index.

It's hard to fix that properly, as array indexes can be almost arbitrary shell "expressions". In an integer-indexed array, the index is taken as an arithmetic expansion, and something like [i+j] works to use the sum of i and j. In an associative array, it could be e.g. [$x$y] for concatenation. It could also be [i+a[j]] if one were to be doing something excessively complex in the shell. Parsing that for syntax highlighting would pretty much require a full parser, not a simple regex. (And then there's command substitutions, but let's not go there...)

Anyway, it's easy to make it accept the [*] and one $ in front of the variable name, here's the changed part:

... (\[([[:space:]]*\$?[[:alnum:]_]+[[:space:]]*|[@*])\])? ...
                    ^^^                          ^^^^

And the resulting full line:

color brightred "\$\{[#!]?([-@*#?$!]|[0-9]+|[[:alpha:]_][[:alnum:]_]*)(\[([[:space:]]*\$?[[:alnum:]_]+[[:space:]]*|[@*])\])?(([#%/]|:?[-=?+])[^}]*\}|\[|\})"

As far as I can see, ${arr[]} is an error, so I'm not sure if it should be highlighted in full or not.

If that's the regex from the latest version, you might want to consider also posting a bug report.

1
  • Thanks a bunch. This does fix the index case.
    – Multifix
    Commented Jul 7, 2022 at 9:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .