highlightjs / highlight.js

JavaScript syntax highlighter with language auto-detection and zero dependencies.
https://highlightjs.org/
BSD 3-Clause "New" or "Revised" License
23.3k stars 3.52k forks source link

Highlight Erlang char literals #3970

Closed nixxquality closed 5 months ago

nixxquality commented 5 months ago

I've noticed that Erlang's character literals have been ignored and caused havoc in highlighting before, see example fiddle: https://jsfiddle.net/k5xm69vu/1/

Strangely enough, the best documentation I could find for this syntax feature was the Erlang module that parses its own syntax... but anyway, here it is: https://www.erlang.org/doc/man/erl_syntax#char-1 I was reading another part of the documentation and happened to find it: https://www.erlang.org/doc/reference_manual/data_types#number (the scratched out link above is also helpful as it mentioned the octal syntax) It's similar to the char literal in C, but the syntax is a $ followed by either (a character) or (a backslash and (a character or up to 3 digits)). The character can be anything, from alphanumerics to whitespace. If there's a backslash that means you've escaped some value and the 3 digits let you specify an exact octal character number. There were a few edge cases I had to work out but I believe this is now the final version.

You can compare the markup tests to the following REPL output:

Eshell V14.0 (press Ctrl+G to abort, type help(). for help)
1> $a.
97
2> $\t.
9
3> $\011.
9
4> $a85.
* 1:3: syntax error before: 85
4> $aa.
* 1:3: syntax error before: a
4> $123.
* 1:3: syntax error before: 23
4> $\1234.
* 1:6: syntax error before: 4
4> $".
34
5> $\\.
92
6> $[.
91
7> $].
93
8> $ .
32

I followed the guide on https://highlightjs.readthedocs.io/en/latest/css-classes-reference.html to assign this new check the "string" class because it's supposed to apply to "literal string, character".

Checklist

nixxquality commented 5 months ago

I noticed nesting issues once I highlighted some real code (d'oh) and I believe I've fixed it now by merging together a bit with the earlier work. I added more test cases to cover this.

github-actions[bot] commented 5 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +67 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/core.min.js | 8.17 KB | 8.17 KB | -2 B | | es/highlight.min.js | 8.17 KB | 8.17 KB | -2 B | | es/languages/erlang.min.js | 1.03 KB | 1.06 KB | +37 B | | highlight.min.js | 8.21 KB | 8.21 KB | -2 B | | languages/erlang.min.js | 1.03 KB | 1.07 KB | +36 B |
github-actions[bot] commented 5 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +82 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/core.min.js | 8.17 KB | 8.17 KB | -2 B | | es/highlight.min.js | 8.17 KB | 8.17 KB | -2 B | | es/languages/erlang.min.js | 1.03 KB | 1.07 KB | +44 B | | highlight.min.js | 8.21 KB | 8.21 KB | -2 B | | languages/erlang.min.js | 1.03 KB | 1.08 KB | +44 B |
github-actions[bot] commented 5 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +82 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/core.min.js | 8.17 KB | 8.17 KB | -2 B | | es/highlight.min.js | 8.17 KB | 8.17 KB | -2 B | | es/languages/erlang.min.js | 1.03 KB | 1.07 KB | +44 B | | highlight.min.js | 8.21 KB | 8.21 KB | -2 B | | languages/erlang.min.js | 1.03 KB | 1.08 KB | +44 B |
nixxquality commented 5 months ago

Sorry about the delay. I removed the examples that had broken syntax per your request and I also added some more real code that acts as a test case for leaving CHAR_LITERAL out of the BLOCK_STATEMENT array.