Skip to content

Commit 9267f50

Browse files
authored
enh(erlang) Erlang/OTP 27 enhancements (#4063)
* enh(erlang) OTP 27 triple-quoted strings https://www.erlang.org/blog/highlights-otp-27/ https://www.erlang.org/doc/system/data_types#string Test examples taken from the blog and the documentation. * enh(erlang) OTP 27 doc attribute The doc/moduledoc attributes can contain strings without params so I've adjusted the directive segment to accomodate this. https://www.erlang.org/blog/highlights-otp-27/ https://www.erlang.org/doc/system/documentation Also added some missing directives: https://www.erlang.org/doc/system/modules#pre-defined-module-attributes * enh(erlang) OTP 27 Sigil type This one is the most involved change. https://www.erlang.org/blog/highlights-otp-27/ https://www.erlang.org/doc/system/data_types#sigil * enh(erlang) OTP25/27 maybe statement https://www.erlang.org/blog/highlights-otp-27/#no-need-to-enable-feature-maybe https://www.erlang.org/doc/system/expressions#maybe * Pure regex TRIPLE_QUOTE Lifted from csharp * change sigil to use variants * Fix priority on Sigil variants matching. * update changelog * forgot contributor link
1 parent 78f3b1c commit 9267f50

File tree

10 files changed

+195
-4
lines changed

10 files changed

+195
-4
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Core Grammars:
99

1010
- fix(makefile) - allow strings inside `$()` expressions [aneesh98][]
1111
- enh(css) add all properties listed on MDN (96 additions including `anchor-name`, `aspect-ratio`, `backdrop-filter`, `container`, `margin-trim`, `place-content`, `scroll-timeline`, ...) [BaliBalo][]
12+
- enh(erlang) OTP 27 triple-quoted strings [nixxquality][]
13+
- enh(erlang) OTP 27 doc attribute [nixxquality][]
14+
- enh(erlang) OTP 27 Sigil type [nixxquality][]
15+
- enh(erlang) OTP25/27 maybe statement [nixxquality][]
1216

1317
New Grammars:
1418

@@ -28,6 +32,7 @@ CONTRIBUTORS
2832
[aneesh98]: https://github.com/aneesh98
2933
[BaliBalo]: https://github.com/BaliBalo
3034
[William Wilkinson]: https://github.com/wilkinson4
35+
[nixxquality]: https://github.com/nixxquality
3136

3237

3338
## Version 11.10.0

src/languages/erlang.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function(hljs) {
1313
const ERLANG_RESERVED = {
1414
keyword:
1515
'after and andalso|10 band begin bnot bor bsl bzr bxor case catch cond div end fun if '
16-
+ 'let not of orelse|10 query receive rem try when xor',
16+
+ 'let not of orelse|10 query receive rem try when xor maybe else',
1717
literal:
1818
'false true'
1919
};
@@ -80,9 +80,31 @@ export default function(hljs) {
8080
scope: 'string',
8181
match: /\$(\\([^0-9]|[0-9]{1,3}|)|.)/,
8282
};
83+
const TRIPLE_QUOTE = {
84+
scope: 'string',
85+
match: /"""("*)(?!")[\s\S]*?"""\1/,
86+
};
87+
88+
const SIGIL = {
89+
scope: 'string',
90+
contains: [ hljs.BACKSLASH_ESCAPE ],
91+
variants: [
92+
{match: /~\w?"""("*)(?!")[\s\S]*?"""\1/},
93+
{begin: /~\w?\(/, end: /\)/},
94+
{begin: /~\w?\[/, end: /\]/},
95+
{begin: /~\w?{/, end: /}/},
96+
{begin: /~\w?</, end: />/},
97+
{begin: /~\w?\//, end: /\//},
98+
{begin: /~\w?\|/, end: /\|/},
99+
{begin: /~\w?'/, end: /'/},
100+
{begin: /~\w?"/, end: /"/},
101+
{begin: /~\w?`/, end: /`/},
102+
{begin: /~\w?#/, end: /#/},
103+
],
104+
};
83105

84106
const BLOCK_STATEMENTS = {
85-
beginKeywords: 'fun receive if try case',
107+
beginKeywords: 'fun receive if try case maybe',
86108
end: 'end',
87109
keywords: ERLANG_RESERVED
88110
};
@@ -92,6 +114,8 @@ export default function(hljs) {
92114
hljs.inherit(hljs.APOS_STRING_MODE, { className: '' }),
93115
BLOCK_STATEMENTS,
94116
FUNCTION_CALL,
117+
SIGIL,
118+
TRIPLE_QUOTE,
95119
hljs.QUOTE_STRING_MODE,
96120
NUMBER,
97121
TUPLE,
@@ -106,6 +130,8 @@ export default function(hljs) {
106130
NAMED_FUN,
107131
BLOCK_STATEMENTS,
108132
FUNCTION_CALL,
133+
SIGIL,
134+
TRIPLE_QUOTE,
109135
hljs.QUOTE_STRING_MODE,
110136
NUMBER,
111137
TUPLE,
@@ -128,6 +154,7 @@ export default function(hljs) {
128154
"-author",
129155
"-copyright",
130156
"-doc",
157+
"-moduledoc",
131158
"-vsn",
132159
"-import",
133160
"-include",
@@ -139,7 +166,9 @@ export default function(hljs) {
139166
"-file",
140167
"-behaviour",
141168
"-behavior",
142-
"-spec"
169+
"-spec",
170+
"-on_load",
171+
"-nifs",
143172
];
144173

145174
const PARAMS = {
@@ -182,9 +211,16 @@ export default function(hljs) {
182211
$pattern: '-' + hljs.IDENT_RE,
183212
keyword: DIRECTIVES.map(x => `${x}|1.5`).join(" ")
184213
},
185-
contains: [ PARAMS ]
214+
contains: [
215+
PARAMS,
216+
SIGIL,
217+
TRIPLE_QUOTE,
218+
hljs.QUOTE_STRING_MODE
219+
]
186220
},
187221
NUMBER,
222+
SIGIL,
223+
TRIPLE_QUOTE,
188224
hljs.QUOTE_STRING_MODE,
189225
RECORD_ACCESS,
190226
VAR1,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<span class="hljs-keyword">-module</span><span class="hljs-params">(arith)</span>.
2+
<span class="hljs-keyword">-moduledoc</span> <span class="hljs-string">&quot;&quot;&quot;
3+
A module for basic arithmetic.
4+
&quot;&quot;&quot;</span>.
5+
6+
<span class="hljs-keyword">-export</span><span class="hljs-params">([add/<span class="hljs-number">2</span>])</span>.
7+
8+
<span class="hljs-keyword">-doc</span> <span class="hljs-string">&quot;Adds two numbers.&quot;</span>.
9+
<span class="hljs-function"><span class="hljs-title">add</span><span class="hljs-params">(One, Two)</span> -&gt;</span> One + Two.

test/markup/erlang/doc_attribute.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-module(arith).
2+
-moduledoc """
3+
A module for basic arithmetic.
4+
""".
5+
6+
-export([add/2]).
7+
8+
-doc "Adds two numbers.".
9+
add(One, Two) -> One + Two.

test/markup/erlang/maybe.expect.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<span class="hljs-keyword">maybe</span>
2+
{ok, A} ?= a(),
3+
<span class="hljs-literal">true</span> = A &gt;= <span class="hljs-number">0</span>,
4+
{ok, B} ?= b(),
5+
A + B
6+
<span class="hljs-keyword">else</span>
7+
error -&gt; error;
8+
wrong -&gt; error
9+
<span class="hljs-keyword">end</span>

test/markup/erlang/maybe.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
maybe
2+
{ok, A} ?= a(),
3+
true = A >= 0,
4+
{ok, B} ?= b(),
5+
A + B
6+
else
7+
error -> error;
8+
wrong -> error
9+
end

test/markup/erlang/sigil.expect.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<span class="hljs-function"><span class="hljs-title">greek_quote</span><span class="hljs-params">()</span> -&gt;</span>
2+
S = <span class="hljs-string">~B[&quot;Know thyself&quot; (Greek: Γνῶθι σαυτόν)]</span>,
3+
io:format(<span class="hljs-string">&quot;~ts\n&quot;</span>, [S]).
4+
5+
<span class="hljs-string">~&#x27;foo&#x27;</span>.
6+
7+
&lt;&lt;<span class="hljs-string">&quot;\&quot;\\µA\&quot;&quot;</span>/utf8&gt;&gt; = &lt;&lt;<span class="hljs-string">$&quot;</span>,<span class="hljs-string">$\\</span>,<span class="hljs-number">194</span>,<span class="hljs-number">181</span>,<span class="hljs-string">$A</span>,<span class="hljs-string">$&quot;</span>&gt;&gt; =
8+
<span class="hljs-string">~b&quot;&quot;&quot;
9+
&quot;\\µA&quot;
10+
&quot;&quot;&quot;</span> = <span class="hljs-string">~b&#x27;&quot;\\µA&quot;&#x27;</span> =
11+
<span class="hljs-string">~B&quot;&quot;&quot;
12+
&quot;\µA&quot;
13+
&quot;&quot;&quot;</span> = <span class="hljs-string">~B&lt;&quot;\µA&quot;&gt;</span> =
14+
<span class="hljs-string">~&quot;&quot;&quot;
15+
&quot;\µA&quot;
16+
&quot;&quot;&quot;</span> = <span class="hljs-string">~&quot;\&quot;\\µA\&quot;&quot;</span> = <span class="hljs-string">~/&quot;\\µA&quot;/</span>
17+
18+
<span class="hljs-function"><span class="hljs-title">quotes</span><span class="hljs-params">()</span> -&gt;</span>
19+
S = <span class="hljs-string">~&quot;&quot;&quot;
20+
&quot;I always have a quotation for everything -
21+
it saves original thinking.&quot; - Dorothy L. Sayers
22+
23+
&quot;Real stupidity beats artificial intelligence every time.&quot;
24+
- Terry Pratchett
25+
&quot;&quot;&quot;</span>,
26+
io:put_chars(S),
27+
io:nl().
28+
29+
<span class="hljs-string">~s{&quot;abc\txyz&quot;}</span>.
30+
<span class="hljs-string">~(parenthesis)</span>.
31+
<span class="hljs-string">~&lt;alligators&gt;</span>.
32+
<span class="hljs-string">~`backticks`</span>.
33+
<span class="hljs-string">~#hashpounds#</span>.
34+
<span class="hljs-string">~|pipes|</span>.

test/markup/erlang/sigil.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
greek_quote() ->
2+
S = ~B["Know thyself" (Greek: Γνῶθι σαυτόν)],
3+
io:format("~ts\n", [S]).
4+
5+
~'foo'.
6+
7+
<<"\"\\µA\""/utf8>> = <<$",$\\,194,181,$A,$">> =
8+
~b"""
9+
"\\µA"
10+
""" = ~b'"\\µA"' =
11+
~B"""
12+
"\µA"
13+
""" = ~B<"\µA"> =
14+
~"""
15+
"\µA"
16+
""" = ~"\"\\µA\"" = ~/"\\µA"/
17+
18+
quotes() ->
19+
S = ~"""
20+
"I always have a quotation for everything -
21+
it saves original thinking." - Dorothy L. Sayers
22+
23+
"Real stupidity beats artificial intelligence every time."
24+
- Terry Pratchett
25+
""",
26+
io:put_chars(S),
27+
io:nl().
28+
29+
~s{"abc\txyz"}.
30+
~(parenthesis).
31+
~<alligators>.
32+
~`backticks`.
33+
~#hashpounds#.
34+
~|pipes|.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<span class="hljs-function"><span class="hljs-title">quotes</span><span class="hljs-params">()</span> -&gt;</span>
2+
S = <span class="hljs-string">&quot;&quot;&quot;
3+
&quot;I always have a quotation for everything -
4+
it saves original thinking.&quot; - Dorothy L. Sayers
5+
6+
&quot;Real stupidity beats artificial intelligence every time.&quot;
7+
- Terry Pratchett
8+
&quot;&quot;&quot;</span>,
9+
io:put_chars(S),
10+
io:nl().
11+
12+
<span class="hljs-function"><span class="hljs-title">effect_warning</span><span class="hljs-params">()</span> -&gt;</span>
13+
<span class="hljs-string">&quot;&quot;&quot;
14+
f() -&gt;
15+
%% Test that the compiler warns for useless tuple building.
16+
{a,b,c},
17+
ok.
18+
&quot;&quot;&quot;</span>.
19+
20+
<span class="hljs-function"><span class="hljs-title">extra_delim</span><span class="hljs-params">()</span> -&gt;</span>
21+
<span class="hljs-string">&quot;&quot;&quot;&quot;&quot;
22+
&quot;&quot;&quot;&quot;
23+
&quot;&quot;&quot;&quot;&quot;</span>.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
quotes() ->
2+
S = """
3+
"I always have a quotation for everything -
4+
it saves original thinking." - Dorothy L. Sayers
5+
6+
"Real stupidity beats artificial intelligence every time."
7+
- Terry Pratchett
8+
""",
9+
io:put_chars(S),
10+
io:nl().
11+
12+
effect_warning() ->
13+
"""
14+
f() ->
15+
%% Test that the compiler warns for useless tuple building.
16+
{a,b,c},
17+
ok.
18+
""".
19+
20+
extra_delim() ->
21+
"""""
22+
""""
23+
""""".

0 commit comments

Comments
 (0)