Skip to content

Commit

Permalink
enh(erlang) Erlang/OTP 27 enhancements (#4063)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nixxquality authored Jul 13, 2024
1 parent 78f3b1c commit 9267f50
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Core Grammars:

- fix(makefile) - allow strings inside `$()` expressions [aneesh98][]
- 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][]
- enh(erlang) OTP 27 triple-quoted strings [nixxquality][]
- enh(erlang) OTP 27 doc attribute [nixxquality][]
- enh(erlang) OTP 27 Sigil type [nixxquality][]
- enh(erlang) OTP25/27 maybe statement [nixxquality][]

New Grammars:

Expand All @@ -28,6 +32,7 @@ CONTRIBUTORS
[aneesh98]: https://github.com/aneesh98
[BaliBalo]: https://github.com/BaliBalo
[William Wilkinson]: https://github.com/wilkinson4
[nixxquality]: https://github.com/nixxquality


## Version 11.10.0
Expand Down
44 changes: 40 additions & 4 deletions src/languages/erlang.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function(hljs) {
const ERLANG_RESERVED = {
keyword:
'after and andalso|10 band begin bnot bor bsl bzr bxor case catch cond div end fun if '
+ 'let not of orelse|10 query receive rem try when xor',
+ 'let not of orelse|10 query receive rem try when xor maybe else',
literal:
'false true'
};
Expand Down Expand Up @@ -80,9 +80,31 @@ export default function(hljs) {
scope: 'string',
match: /\$(\\([^0-9]|[0-9]{1,3}|)|.)/,
};
const TRIPLE_QUOTE = {
scope: 'string',
match: /"""("*)(?!")[\s\S]*?"""\1/,
};

const SIGIL = {
scope: 'string',
contains: [ hljs.BACKSLASH_ESCAPE ],
variants: [
{match: /~\w?"""("*)(?!")[\s\S]*?"""\1/},
{begin: /~\w?\(/, end: /\)/},
{begin: /~\w?\[/, end: /\]/},
{begin: /~\w?{/, end: /}/},
{begin: /~\w?</, end: />/},
{begin: /~\w?\//, end: /\//},
{begin: /~\w?\|/, end: /\|/},
{begin: /~\w?'/, end: /'/},
{begin: /~\w?"/, end: /"/},
{begin: /~\w?`/, end: /`/},
{begin: /~\w?#/, end: /#/},
],
};

const BLOCK_STATEMENTS = {
beginKeywords: 'fun receive if try case',
beginKeywords: 'fun receive if try case maybe',
end: 'end',
keywords: ERLANG_RESERVED
};
Expand All @@ -92,6 +114,8 @@ export default function(hljs) {
hljs.inherit(hljs.APOS_STRING_MODE, { className: '' }),
BLOCK_STATEMENTS,
FUNCTION_CALL,
SIGIL,
TRIPLE_QUOTE,
hljs.QUOTE_STRING_MODE,
NUMBER,
TUPLE,
Expand All @@ -106,6 +130,8 @@ export default function(hljs) {
NAMED_FUN,
BLOCK_STATEMENTS,
FUNCTION_CALL,
SIGIL,
TRIPLE_QUOTE,
hljs.QUOTE_STRING_MODE,
NUMBER,
TUPLE,
Expand All @@ -128,6 +154,7 @@ export default function(hljs) {
"-author",
"-copyright",
"-doc",
"-moduledoc",
"-vsn",
"-import",
"-include",
Expand All @@ -139,7 +166,9 @@ export default function(hljs) {
"-file",
"-behaviour",
"-behavior",
"-spec"
"-spec",
"-on_load",
"-nifs",
];

const PARAMS = {
Expand Down Expand Up @@ -182,9 +211,16 @@ export default function(hljs) {
$pattern: '-' + hljs.IDENT_RE,
keyword: DIRECTIVES.map(x => `${x}|1.5`).join(" ")
},
contains: [ PARAMS ]
contains: [
PARAMS,
SIGIL,
TRIPLE_QUOTE,
hljs.QUOTE_STRING_MODE
]
},
NUMBER,
SIGIL,
TRIPLE_QUOTE,
hljs.QUOTE_STRING_MODE,
RECORD_ACCESS,
VAR1,
Expand Down
9 changes: 9 additions & 0 deletions test/markup/erlang/doc_attribute.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<span class="hljs-keyword">-module</span><span class="hljs-params">(arith)</span>.
<span class="hljs-keyword">-moduledoc</span> <span class="hljs-string">&quot;&quot;&quot;
A module for basic arithmetic.
&quot;&quot;&quot;</span>.

<span class="hljs-keyword">-export</span><span class="hljs-params">([add/<span class="hljs-number">2</span>])</span>.

<span class="hljs-keyword">-doc</span> <span class="hljs-string">&quot;Adds two numbers.&quot;</span>.
<span class="hljs-function"><span class="hljs-title">add</span><span class="hljs-params">(One, Two)</span> -&gt;</span> One + Two.
9 changes: 9 additions & 0 deletions test/markup/erlang/doc_attribute.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-module(arith).
-moduledoc """
A module for basic arithmetic.
""".

-export([add/2]).

-doc "Adds two numbers.".
add(One, Two) -> One + Two.
9 changes: 9 additions & 0 deletions test/markup/erlang/maybe.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<span class="hljs-keyword">maybe</span>
{ok, A} ?= a(),
<span class="hljs-literal">true</span> = A &gt;= <span class="hljs-number">0</span>,
{ok, B} ?= b(),
A + B
<span class="hljs-keyword">else</span>
error -&gt; error;
wrong -&gt; error
<span class="hljs-keyword">end</span>
9 changes: 9 additions & 0 deletions test/markup/erlang/maybe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
maybe
{ok, A} ?= a(),
true = A >= 0,
{ok, B} ?= b(),
A + B
else
error -> error;
wrong -> error
end
34 changes: 34 additions & 0 deletions test/markup/erlang/sigil.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<span class="hljs-function"><span class="hljs-title">greek_quote</span><span class="hljs-params">()</span> -&gt;</span>
S = <span class="hljs-string">~B[&quot;Know thyself&quot; (Greek: Γνῶθι σαυτόν)]</span>,
io:format(<span class="hljs-string">&quot;~ts\n&quot;</span>, [S]).

<span class="hljs-string">~&#x27;foo&#x27;</span>.

&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; =
<span class="hljs-string">~b&quot;&quot;&quot;
&quot;\\µA&quot;
&quot;&quot;&quot;</span> = <span class="hljs-string">~b&#x27;&quot;\\µA&quot;&#x27;</span> =
<span class="hljs-string">~B&quot;&quot;&quot;
&quot;\µA&quot;
&quot;&quot;&quot;</span> = <span class="hljs-string">~B&lt;&quot;\µA&quot;&gt;</span> =
<span class="hljs-string">~&quot;&quot;&quot;
&quot;\µA&quot;
&quot;&quot;&quot;</span> = <span class="hljs-string">~&quot;\&quot;\\µA\&quot;&quot;</span> = <span class="hljs-string">~/&quot;\\µA&quot;/</span>

<span class="hljs-function"><span class="hljs-title">quotes</span><span class="hljs-params">()</span> -&gt;</span>
S = <span class="hljs-string">~&quot;&quot;&quot;
&quot;I always have a quotation for everything -
it saves original thinking.&quot; - Dorothy L. Sayers

&quot;Real stupidity beats artificial intelligence every time.&quot;
- Terry Pratchett
&quot;&quot;&quot;</span>,
io:put_chars(S),
io:nl().

<span class="hljs-string">~s{&quot;abc\txyz&quot;}</span>.
<span class="hljs-string">~(parenthesis)</span>.
<span class="hljs-string">~&lt;alligators&gt;</span>.
<span class="hljs-string">~`backticks`</span>.
<span class="hljs-string">~#hashpounds#</span>.
<span class="hljs-string">~|pipes|</span>.
34 changes: 34 additions & 0 deletions test/markup/erlang/sigil.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
greek_quote() ->
S = ~B["Know thyself" (Greek: Γνῶθι σαυτόν)],
io:format("~ts\n", [S]).

~'foo'.

<<"\"\\µA\""/utf8>> = <<$",$\\,194,181,$A,$">> =
~b"""
"\\µA"
""" = ~b'"\\µA"' =
~B"""
"\µA"
""" = ~B<"\µA"> =
~"""
"\µA"
""" = ~"\"\\µA\"" = ~/"\\µA"/

quotes() ->
S = ~"""
"I always have a quotation for everything -
it saves original thinking." - Dorothy L. Sayers

"Real stupidity beats artificial intelligence every time."
- Terry Pratchett
""",
io:put_chars(S),
io:nl().

~s{"abc\txyz"}.
~(parenthesis).
~<alligators>.
~`backticks`.
~#hashpounds#.
~|pipes|.
23 changes: 23 additions & 0 deletions test/markup/erlang/triple_quote_string.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<span class="hljs-function"><span class="hljs-title">quotes</span><span class="hljs-params">()</span> -&gt;</span>
S = <span class="hljs-string">&quot;&quot;&quot;
&quot;I always have a quotation for everything -
it saves original thinking.&quot; - Dorothy L. Sayers

&quot;Real stupidity beats artificial intelligence every time.&quot;
- Terry Pratchett
&quot;&quot;&quot;</span>,
io:put_chars(S),
io:nl().

<span class="hljs-function"><span class="hljs-title">effect_warning</span><span class="hljs-params">()</span> -&gt;</span>
<span class="hljs-string">&quot;&quot;&quot;
f() -&gt;
%% Test that the compiler warns for useless tuple building.
{a,b,c},
ok.
&quot;&quot;&quot;</span>.

<span class="hljs-function"><span class="hljs-title">extra_delim</span><span class="hljs-params">()</span> -&gt;</span>
<span class="hljs-string">&quot;&quot;&quot;&quot;&quot;
&quot;&quot;&quot;&quot;
&quot;&quot;&quot;&quot;&quot;</span>.
23 changes: 23 additions & 0 deletions test/markup/erlang/triple_quote_string.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
quotes() ->
S = """
"I always have a quotation for everything -
it saves original thinking." - Dorothy L. Sayers

"Real stupidity beats artificial intelligence every time."
- Terry Pratchett
""",
io:put_chars(S),
io:nl().

effect_warning() ->
"""
f() ->
%% Test that the compiler warns for useless tuple building.
{a,b,c},
ok.
""".

extra_delim() ->
"""""
""""
""""".

0 comments on commit 9267f50

Please sign in to comment.