Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nano syntax highlighter fails to parse pre-installed json.nanorc #1156

Open
snazy opened this issue Jan 20, 2025 · 1 comment · May be fixed by #1157
Open

Nano syntax highlighter fails to parse pre-installed json.nanorc #1156

snazy opened this issue Jan 20, 2025 · 1 comment · May be fixed by #1157
Milestone

Comments

@snazy
Copy link
Contributor

snazy commented Jan 20, 2025

(Recent) nano packages in Ubuntu come with some .nanorc files preinstalled.

jline's NanorcParser sadly fails parsing /usr/share/nano/json.nanorc that comes with Ubuntu's package.

In particular, it fails at this line in /usr/share/nano/json.nanorc:

color brightblue "[][]"

with the exception

java.util.regex.PatternSyntaxException: Unclosed character class near index 3
[][]
   ^
[][]
   ^
	at java.base/java.util.regex.Pattern.error(Pattern.java:2204)
	at java.base/java.util.regex.Pattern.clazz(Pattern.java:2869)
	at java.base/java.util.regex.Pattern.clazz(Pattern.java:2812)
	at java.base/java.util.regex.Pattern.sequence(Pattern.java:2315)
	at java.base/java.util.regex.Pattern.expr(Pattern.java:2245)
	at java.base/java.util.regex.Pattern.compile(Pattern.java:1945)
	at java.base/java.util.regex.Pattern.<init>(Pattern.java:1576)
	at java.base/java.util.regex.Pattern.compile(Pattern.java:1101)
	at org.jline.builtins.SyntaxHighlighter$NanorcParser.doPattern(SyntaxHighlighter.java:696)
	at org.jline.builtins.SyntaxHighlighter$NanorcParser.addHighlightRule(SyntaxHighlighter.java:666)
	at org.jline.builtins.SyntaxHighlighter$NanorcParser.addHighlightRule(SyntaxHighlighter.java:596)
	at org.jline.builtins.SyntaxHighlighter$NanorcParser.parse(SyntaxHighlighter.java:556)
	at org.jline.builtins.SyntaxHighlighter.build(SyntaxHighlighter.java:96)
	at org.jline.builtins.Nano$Buffer.<init>(Nano.java:200)
	at org.jline.builtins.Nano.open(Nano.java:1749)
	at org.jline.builtins.Nano.open(Nano.java:1738)

There's another one, which I tried out of curiosity, but that doesn't highlight anything, whereas highlighting with the one from Ubuntu works (after removing the offending line).

snazy added a commit to snazy/jline3 that referenced this issue Jan 20, 2025
(Recent) `nano` packages in Ubuntu come with some `.nanorc` files preinstalled.

jline's `NanorcParser` sadly fails parsing `/usr/share/nano/json.nanorc` that comes with Ubuntu's package.

In particular, it fails at this line in `/usr/share/nano/json.nanorc`:
```
color brightblue "[][]"
```
with the exception
```
java.util.regex.PatternSyntaxException: Unclosed character class near index 3
[][]
   ^
...
	at org.jline.builtins.SyntaxHighlighter$NanorcParser.doPattern(SyntaxHighlighter.java:696)
...
```

Fixes jline#1156
@mattirn
Copy link
Collaborator

mattirn commented Jan 21, 2025

There's another one, which I tried out of curiosity, but that doesn't highlight anything...

I did not find any problems with this one. I used it with less and nano.

@mattirn mattirn added this to the 3.28.1 milestone Jan 22, 2025
snazy added a commit to snazy/jline3 that referenced this issue Jan 23, 2025
(Recent) `nano` packages in Ubuntu come with some `.nanorc` files preinstalled.

jline's `NanorcParser` sadly fails parsing a couple of the regular expressions.

This change translates the regular expressions to Java regular expressions.

The differences are described in `org.jline.builtins.SyntaxHighlighter#posixToJavaRegex`:
* The first `]` in a bracket expression does not need to be escaped in Posix,translate to `\]`.
* Same as above for a negating bracket expression like `[^][]`, translate to `[^\]\[]`.
* Any `[` in a bracket expression does not need to be escaped in Posix, translate to `\[`.
* Any `]` not in a bracket expression is valid in both Posix and Java, no translation.
* A backslash before the closing bracket like `[.f\]` is not an escape of the closing bracket, the backslash needs to be escaped for Java, translate to `[.f\\]`.
* Do not perform the above translations within an escape via `\`.
* Do not perform the above translations for Posix "classes" like `[[:word:]]` or `[[:digit:]]` and their negation `[-[:word]]`.
* Do not perform the above translations for single-bracket Posix classes like `[:digit:]`, and handle the case of single-bracket Posix classes inside bracket expressions, like `[[:digit:]-.]`.

Test cases have been added.

There are however two regexes that still don't work, but those look invalid. To let jnano not trip over these, any `PatternSyntaxException` lets jnano just ignore the particular rule. A warning is logged in such cases.

Fixes jline#1156
snazy added a commit to snazy/jline3 that referenced this issue Jan 23, 2025
(Recent) `nano` packages in Ubuntu come with some `.nanorc` files preinstalled.

jline's `NanorcParser` sadly fails parsing a couple of the regular expressions.

This change translates the regular expressions to Java regular expressions.

The differences are described in `org.jline.builtins.SyntaxHighlighter#posixToJavaRegex`:
* The first `]` in a bracket expression does not need to be escaped in Posix,translate to `\]`.
* Same as above for a negating bracket expression like `[^][]`, translate to `[^\]\[]`.
* Any `[` in a bracket expression does not need to be escaped in Posix, translate to `\[`.
* Any `]` not in a bracket expression is valid in both Posix and Java, no translation.
* A backslash before the closing bracket like `[.f\]` is not an escape of the closing bracket, the backslash needs to be escaped for Java, translate to `[.f\\]`.
* Do not perform the above translations within an escape via `\`.
* Do not perform the above translations for Posix "classes" like `[[:word:]]` or `[[:digit:]]` and their negation `[-[:word]]`.
* Do not perform the above translations for single-bracket Posix classes like `[:digit:]`, and handle the case of single-bracket Posix classes inside bracket expressions, like `[[:digit:]-.]`.

Test cases have been added.

There are however two regexes that still don't work, but those look invalid. To let jnano not trip over these, any `PatternSyntaxException` lets jnano just ignore the particular rule. A warning is logged in such cases.

Fixes jline#1156
snazy added a commit to snazy/jline3 that referenced this issue Jan 23, 2025
(Recent) `nano` packages in Ubuntu come with some `.nanorc` files preinstalled.

jline's `NanorcParser` sadly fails parsing a couple of the regular expressions.

This change translates the regular expressions to Java regular expressions.

The differences are described in `org.jline.builtins.SyntaxHighlighter#posixToJavaRegex`:
* The first `]` in a bracket expression does not need to be escaped in Posix,translate to `\]`.
* Same as above for a negating bracket expression like `[^][]`, translate to `[^\]\[]`.
* Any `[` in a bracket expression does not need to be escaped in Posix, translate to `\[`.
* Any `]` not in a bracket expression is valid in both Posix and Java, no translation.
* A backslash before the closing bracket like `[.f\]` is not an escape of the closing bracket, the backslash needs to be escaped for Java, translate to `[.f\\]`.
* Do not perform the above translations within an escape via `\`.
* Do not perform the above translations for Posix "classes" like `[[:word:]]` or `[[:digit:]]` and their negation `[-[:word]]`.
* Do not perform the above translations for single-bracket Posix classes like `[:digit:]`, and handle the case of single-bracket Posix classes inside bracket expressions, like `[[:digit:]-.]`.

Test cases have been added.

There are however two regexes that still don't work, but those look invalid. To let jnano not trip over these, any `PatternSyntaxException` lets jnano just ignore the particular rule. A warning is logged in such cases.

Fixes jline#1156
snazy added a commit to snazy/jline3 that referenced this issue Jan 27, 2025
(Recent) `nano` packages in Ubuntu come with some `.nanorc` files preinstalled.

jline's `NanorcParser` sadly fails parsing a couple of the regular expressions.

This change translates the regular expressions to Java regular expressions.

The differences are described in `org.jline.builtins.SyntaxHighlighter#posixToJavaRegex`:
* The first `]` in a bracket expression does not need to be escaped in Posix,translate to `\]`.
* Same as above for a negating bracket expression like `[^][]`, translate to `[^\]\[]`.
* Any `[` in a bracket expression does not need to be escaped in Posix, translate to `\[`.
* Any `]` not in a bracket expression is valid in both Posix and Java, no translation.
* A backslash before the closing bracket like `[.f\]` is not an escape of the closing bracket, the backslash needs to be escaped for Java, translate to `[.f\\]`.
* Do not perform the above translations within an escape via `\`.
* Do not perform the above translations for Posix "classes" like `[[:word:]]` or `[[:digit:]]` and their negation `[-[:word]]`.
* Do not perform the above translations for single-bracket Posix classes like `[:digit:]`, and handle the case of single-bracket Posix classes inside bracket expressions, like `[[:digit:]-.]`.

Test cases have been added.

There are however two regexes that still don't work, but those look invalid. To let jnano not trip over these, any `PatternSyntaxException` lets jnano just ignore the particular rule. A warning is logged in such cases.

Fixes jline#1156
@gnodet gnodet modified the milestones: 3.28.1, 3.28.2 Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants