Skip to content

Commit

Permalink
enh(erlang) highlight character literals (#3970)
Browse files Browse the repository at this point in the history
* highlight erlang character literals

* fix regex, add extra markup test cases

* 3 digits are only allowed after backslash

* add char literal into basic modes to fix nesting issues

* adopt modern code standard

* improve markup tests
  • Loading branch information
nixxquality authored Jan 20, 2024
1 parent 14a5dcf commit e964bec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/languages/erlang.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export default function(hljs) {
}
]
};
const CHAR_LITERAL = {
scope: 'string',
match: /\$(\\([^0-9]|[0-9]{1,3}|)|.)/,
};

const BLOCK_STATEMENTS = {
beginKeywords: 'fun receive if try case',
Expand All @@ -93,7 +97,8 @@ export default function(hljs) {
TUPLE,
VAR1,
VAR2,
RECORD_ACCESS
RECORD_ACCESS,
CHAR_LITERAL
];

const BASIC_MODES = [
Expand All @@ -106,7 +111,8 @@ export default function(hljs) {
TUPLE,
VAR1,
VAR2,
RECORD_ACCESS
RECORD_ACCESS,
CHAR_LITERAL
];
FUNCTION_CALL.contains[1].contains = BASIC_MODES;
TUPLE.contains = BASIC_MODES;
Expand Down Expand Up @@ -142,6 +148,7 @@ export default function(hljs) {
end: '\\)',
contains: BASIC_MODES
};

return {
name: 'Erlang',
aliases: [ 'erl' ],
Expand Down Expand Up @@ -183,6 +190,7 @@ export default function(hljs) {
VAR1,
VAR2,
TUPLE,
CHAR_LITERAL,
{ begin: /\.$/ } // relevance booster
]
};
Expand Down
23 changes: 23 additions & 0 deletions test/markup/erlang/char_literal.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Character = <span class="hljs-string">$a</span>
TabCharacter = <span class="hljs-string">$\t</span>
TabCharacterDecimal = <span class="hljs-string">$\011</span>
TextWithDollar = <span class="hljs-string">&quot;This is just a normal text with a $ in the middle&quot;</span>
StringStart = <span class="hljs-string">$&quot;</span>
Backslash = <span class="hljs-string">$\\</span>
ListStart = <span class="hljs-string">$[</span>
ListEnd = <span class="hljs-string">$]</span>
SpaceChar = <span class="hljs-string">$ </span> <span class="hljs-comment">% yes, that works</span>
<span class="hljs-function"><span class="hljs-title">fun_takes_literal</span><span class="hljs-params">(<span class="hljs-string">$a</span>)</span> -&gt;</span> ok.
<span class="hljs-function"><span class="hljs-title">fun_takes_literal_list</span><span class="hljs-params">([<span class="hljs-string">$a</span>|Rest])</span> -&gt;</span> ok.
<span class="hljs-function"><span class="hljs-title">fun_takes_literal_binary</span><span class="hljs-params">(&lt;&lt;<span class="hljs-string">$a</span>, Rest/binary&gt;&gt;)</span> -&gt;</span> ok.
<span class="hljs-function"><span class="hljs-title">convert_escape_sequence</span><span class="hljs-params">(&lt;&lt;<span class="hljs-string">$\\</span>, Escaped, Rest/binary&gt;&gt;)</span> -&gt;</span>
C = <span class="hljs-keyword">case</span> Escaped <span class="hljs-keyword">of</span>
<span class="hljs-string">$b</span> -&gt; <span class="hljs-string">$\b</span>;
<span class="hljs-string">$f</span> -&gt; <span class="hljs-string">$\f</span>;
<span class="hljs-string">$n</span> -&gt; <span class="hljs-string">$\n</span>;
<span class="hljs-string">$r</span> -&gt; <span class="hljs-string">$\r</span>;
<span class="hljs-string">$t</span> -&gt; <span class="hljs-string">$\t</span>;
<span class="hljs-string">$&quot;</span> -&gt; <span class="hljs-string">$&quot;</span>;
<span class="hljs-string">$\\</span> -&gt; <span class="hljs-string">$\\</span>;
_ -&gt; error
<span class="hljs-keyword">end</span>
23 changes: 23 additions & 0 deletions test/markup/erlang/char_literal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Character = $a
TabCharacter = $\t
TabCharacterDecimal = $\011
TextWithDollar = "This is just a normal text with a $ in the middle"
StringStart = $"
Backslash = $\\
ListStart = $[
ListEnd = $]
SpaceChar = $ % yes, that works
fun_takes_literal($a) -> ok.
fun_takes_literal_list([$a|Rest]) -> ok.
fun_takes_literal_binary(<<$a, Rest/binary>>) -> ok.
convert_escape_sequence(<<$\\, Escaped, Rest/binary>>) ->
C = case Escaped of
$b -> $\b;
$f -> $\f;
$n -> $\n;
$r -> $\r;
$t -> $\t;
$" -> $";
$\\ -> $\\;
_ -> error
end

0 comments on commit e964bec

Please sign in to comment.