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

Fix parse error of system default /usr/share/nano/json.nanorc #1157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

snazy
Copy link

@snazy 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 org.jline.builtins.SyntaxHighlighter$NanorcParser.doPattern(SyntaxHighlighter.java:696)
...

Fixes #1156

(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 22, 2025

@snazy, I think we should do a more general fix rather than start to resolve patterns one by one.

In java regex characters [ and ] must be always escaped in a bracket expression. In POSIX regex [ character can be added in a bracket expression without escaping. Also ] character can be added without escaping if it is the first character of the bracket expression.

We can add a static method fixRegex(String regex) to Parse class that will escape [ and ] characters in a bracket expression.

@mattirn mattirn modified the milestone: 3.28.1 Jan 22, 2025
@snazy
Copy link
Author

snazy commented Jan 22, 2025

Wonder whether this gets into parsing the regex itself. However, do you think its good enough to handle the cases that the Character classes chapter in Pattern's javadoc describes? I.e. looking for [ (unless it's not escaped as \[ and then handle the escaping [ ] ?

@mattirn
Copy link
Collaborator

mattirn commented Jan 22, 2025

Wonder whether this gets into parsing the regex itself. However, do you think its good enough to handle the cases that the Character classes chapter in Pattern's javadoc describes?

Regexes in nanorc-files follow POSIX standard. Intersections ([a-z&&[def]]), unions ([a-d[m-p]]) etc descriped in Pattern's javadoc are not valid POSIX regexes.

I.e. looking for [ (unless it's not escaped as \[ and then handle the escaping [ ] ?

In regex the first unescaped '[' starts a bracket expression. In POSIX a bracket expression is closed by unescaped ']' if it is not the next character after '[' which started the bracket expression. Every square bracket between the starting and closing brackets must be escaped in order to obtain valid java pattern.

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 this pull request may close these issues.

Nano syntax highlighter fails to parse pre-installed json.nanorc
2 participants