Skip to content

Commit 9347b55

Browse files
author
Pi-Cla
authored
Add Gleam syntax highlighting (#959)
Resolves #958
1 parent 6b7ffe1 commit 9347b55

File tree

3 files changed

+322
-0
lines changed

3 files changed

+322
-0
lines changed

lexers/embedded/gleam.xml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<lexer>
2+
<config>
3+
<name>Gleam</name>
4+
<alias>gleam></alias>
5+
<filename>*.gleam</filename>
6+
<mime_type>text/x-gleam</mime_type>
7+
</config>
8+
<rules>
9+
<state name="root">
10+
<rule pattern="\s+">
11+
<token type="TextWhitespace"/>
12+
</rule>
13+
<rule pattern="///(.*?)\n">
14+
<token type="LiteralStringDoc"/>
15+
</rule>
16+
<rule pattern="//(.*?)\n">
17+
<token type="CommentSingle"/>
18+
</rule>
19+
<rule pattern="(as|assert|case|opaque|panic|pub|todo)\b">
20+
<token type="Keyword"/>
21+
</rule>
22+
<rule pattern="(import|use)\b">
23+
<token type="KeywordNamespace"/>
24+
</rule>
25+
<rule pattern="(auto|const|delegate|derive|echo|else|if|implement|macro|test)\b">
26+
<token type="KeywordReserved"/>
27+
</rule>
28+
<rule pattern="(let)\b">
29+
<token type="KeywordDeclaration"/>
30+
</rule>
31+
<rule pattern="(fn)\b">
32+
<token type="Keyword"/>
33+
</rule>
34+
<rule pattern="(type)\b">
35+
<token type="Keyword"/>
36+
<push state="typename"/>
37+
</rule>
38+
<rule pattern="(True|False)\b">
39+
<token type="KeywordConstant"/>
40+
</rule>
41+
<rule pattern="0[bB][01](_?[01])*">
42+
<token type="LiteralNumberBin"/>
43+
</rule>
44+
<rule pattern="0[oO][0-7](_?[0-7])*">
45+
<token type="LiteralNumberOct"/>
46+
</rule>
47+
<rule pattern="0[xX][\da-fA-F](_?[\dA-Fa-f])*">
48+
<token type="LiteralNumberHex"/>
49+
</rule>
50+
<rule pattern="\d(_?\d)*\.\d(_?\d)*([eE][-+]?\d(_?\d)*)?">
51+
<token type="LiteralNumberFloat"/>
52+
</rule>
53+
<rule pattern="\d(_?\d)*">
54+
<token type="LiteralNumberInteger"/>
55+
</rule>
56+
<rule pattern="&#34;">
57+
<token type="LiteralString"/>
58+
<push state="string"/>
59+
</rule>
60+
<rule pattern="@([a-z_]\w*[!?]?)">
61+
<token type="NameAttribute"/>
62+
</rule>
63+
<rule pattern="[{}()\[\],]|[#(]|\.\.|&lt;&gt;|&lt;&lt;|&gt;&gt;">
64+
<token type="Punctuation"/>
65+
</rule>
66+
<rule pattern="[+\-*/%!=&lt;&gt;&amp;|.]|&lt;-">
67+
<token type="Operator"/>
68+
</rule>
69+
<rule pattern=":|-&gt;">
70+
<token type="Operator"/>
71+
<push state="typename"/>
72+
</rule>
73+
<rule pattern="([a-z_][A-Za-z0-9_]*)(\()">
74+
<bygroups>
75+
<token type="NameFunction"/>
76+
<token type="Punctuation"/>
77+
</bygroups>
78+
</rule>
79+
<rule pattern="([A-Z][A-Za-z0-9_]*)(\()">
80+
<bygroups>
81+
<token type="NameClass"/>
82+
<token type="Punctuation"/>
83+
</bygroups>
84+
</rule>
85+
<rule pattern="([a-z_]\w*[!?]?)">
86+
<token type="Name"/>
87+
</rule>
88+
</state>
89+
<state name="typename">
90+
<rule pattern="\s+">
91+
<token type="TextWhitespace"/>
92+
</rule>
93+
<rule pattern="[A-Z][A-Za-z0-9_]*">
94+
<token type="NameClass"/>
95+
<pop depth="1"/>
96+
</rule>
97+
<rule>
98+
<pop depth="1"/>
99+
</rule>
100+
</state>
101+
<state name="string">
102+
<rule pattern="&#34;">
103+
<token type="LiteralString"/>
104+
<pop depth="1"/>
105+
</rule>
106+
<rule pattern="\\[&#34;\\fnrt]|\\u\{[\da-fA-F]{1,6}\}">
107+
<token type="LiteralStringEscape"/>
108+
</rule>
109+
<rule pattern="[^\\&#34;]+">
110+
<token type="LiteralString"/>
111+
</rule>
112+
<rule pattern="\\">
113+
<token type="LiteralString"/>
114+
</rule>
115+
</state>
116+
</rules>
117+
</lexer>

lexers/testdata/gleam.actual

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import gleam/io
2+
3+
@external(erlang, "lists", "reverse")
4+
pub fn reverse_list(items: List(e)) -> List(e) {
5+
tail_recursive_reverse(items, [])
6+
}
7+
8+
/// Tail recursion!
9+
fn tail_recursive_reverse(items: List(e), reversed: List(e)) -> List(e) {
10+
case items {
11+
[] -> reversed
12+
[first, ..rest] -> tail_recursive_reverse(rest, [first, ..reversed])
13+
}
14+
}
15+
16+
pub fn main() {
17+
let _thing = <<"Hello, Joe!":utf8>>
18+
io.debug(reverse_list([1, 2, 3, 4, 5]))
19+
io.debug(reverse_list(["a", "b", "c", "d", "e"]))
20+
}

lexers/testdata/gleam.expected

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
[
2+
{"type":"KeywordNamespace","value":"import"},
3+
{"type":"TextWhitespace","value":" "},
4+
{"type":"Name","value":"gleam"},
5+
{"type":"Operator","value":"/"},
6+
{"type":"Name","value":"io"},
7+
{"type":"TextWhitespace","value":"\n\n"},
8+
{"type":"NameAttribute","value":"@external"},
9+
{"type":"Punctuation","value":"("},
10+
{"type":"Name","value":"erlang"},
11+
{"type":"Punctuation","value":","},
12+
{"type":"TextWhitespace","value":" "},
13+
{"type":"LiteralString","value":"\"lists\""},
14+
{"type":"Punctuation","value":","},
15+
{"type":"TextWhitespace","value":" "},
16+
{"type":"LiteralString","value":"\"reverse\""},
17+
{"type":"Punctuation","value":")"},
18+
{"type":"TextWhitespace","value":"\n"},
19+
{"type":"Keyword","value":"pub"},
20+
{"type":"TextWhitespace","value":" "},
21+
{"type":"Keyword","value":"fn"},
22+
{"type":"TextWhitespace","value":" "},
23+
{"type":"NameFunction","value":"reverse_list"},
24+
{"type":"Punctuation","value":"("},
25+
{"type":"Name","value":"items"},
26+
{"type":"Operator","value":":"},
27+
{"type":"TextWhitespace","value":" "},
28+
{"type":"NameClass","value":"List"},
29+
{"type":"Punctuation","value":"("},
30+
{"type":"Name","value":"e"},
31+
{"type":"Punctuation","value":"))"},
32+
{"type":"TextWhitespace","value":" "},
33+
{"type":"Operator","value":"-\u003e"},
34+
{"type":"TextWhitespace","value":" "},
35+
{"type":"NameClass","value":"List"},
36+
{"type":"Punctuation","value":"("},
37+
{"type":"Name","value":"e"},
38+
{"type":"Punctuation","value":")"},
39+
{"type":"TextWhitespace","value":" "},
40+
{"type":"Punctuation","value":"{"},
41+
{"type":"TextWhitespace","value":"\n "},
42+
{"type":"NameFunction","value":"tail_recursive_reverse"},
43+
{"type":"Punctuation","value":"("},
44+
{"type":"Name","value":"items"},
45+
{"type":"Punctuation","value":","},
46+
{"type":"TextWhitespace","value":" "},
47+
{"type":"Punctuation","value":"[])"},
48+
{"type":"TextWhitespace","value":"\n"},
49+
{"type":"Punctuation","value":"}"},
50+
{"type":"TextWhitespace","value":"\n\n"},
51+
{"type":"LiteralStringDoc","value":"/// Tail recursion!\n"},
52+
{"type":"Keyword","value":"fn"},
53+
{"type":"TextWhitespace","value":" "},
54+
{"type":"NameFunction","value":"tail_recursive_reverse"},
55+
{"type":"Punctuation","value":"("},
56+
{"type":"Name","value":"items"},
57+
{"type":"Operator","value":":"},
58+
{"type":"TextWhitespace","value":" "},
59+
{"type":"NameClass","value":"List"},
60+
{"type":"Punctuation","value":"("},
61+
{"type":"Name","value":"e"},
62+
{"type":"Punctuation","value":"),"},
63+
{"type":"TextWhitespace","value":" "},
64+
{"type":"Name","value":"reversed"},
65+
{"type":"Operator","value":":"},
66+
{"type":"TextWhitespace","value":" "},
67+
{"type":"NameClass","value":"List"},
68+
{"type":"Punctuation","value":"("},
69+
{"type":"Name","value":"e"},
70+
{"type":"Punctuation","value":"))"},
71+
{"type":"TextWhitespace","value":" "},
72+
{"type":"Operator","value":"-\u003e"},
73+
{"type":"TextWhitespace","value":" "},
74+
{"type":"NameClass","value":"List"},
75+
{"type":"Punctuation","value":"("},
76+
{"type":"Name","value":"e"},
77+
{"type":"Punctuation","value":")"},
78+
{"type":"TextWhitespace","value":" "},
79+
{"type":"Punctuation","value":"{"},
80+
{"type":"TextWhitespace","value":"\n "},
81+
{"type":"Keyword","value":"case"},
82+
{"type":"TextWhitespace","value":" "},
83+
{"type":"Name","value":"items"},
84+
{"type":"TextWhitespace","value":" "},
85+
{"type":"Punctuation","value":"{"},
86+
{"type":"TextWhitespace","value":"\n "},
87+
{"type":"Punctuation","value":"[]"},
88+
{"type":"TextWhitespace","value":" "},
89+
{"type":"Operator","value":"-\u003e"},
90+
{"type":"TextWhitespace","value":" "},
91+
{"type":"Name","value":"reversed"},
92+
{"type":"TextWhitespace","value":"\n "},
93+
{"type":"Punctuation","value":"["},
94+
{"type":"Name","value":"first"},
95+
{"type":"Punctuation","value":","},
96+
{"type":"TextWhitespace","value":" "},
97+
{"type":"Punctuation","value":".."},
98+
{"type":"Name","value":"rest"},
99+
{"type":"Punctuation","value":"]"},
100+
{"type":"TextWhitespace","value":" "},
101+
{"type":"Operator","value":"-\u003e"},
102+
{"type":"TextWhitespace","value":" "},
103+
{"type":"NameFunction","value":"tail_recursive_reverse"},
104+
{"type":"Punctuation","value":"("},
105+
{"type":"Name","value":"rest"},
106+
{"type":"Punctuation","value":","},
107+
{"type":"TextWhitespace","value":" "},
108+
{"type":"Punctuation","value":"["},
109+
{"type":"Name","value":"first"},
110+
{"type":"Punctuation","value":","},
111+
{"type":"TextWhitespace","value":" "},
112+
{"type":"Punctuation","value":".."},
113+
{"type":"Name","value":"reversed"},
114+
{"type":"Punctuation","value":"])"},
115+
{"type":"TextWhitespace","value":"\n "},
116+
{"type":"Punctuation","value":"}"},
117+
{"type":"TextWhitespace","value":"\n"},
118+
{"type":"Punctuation","value":"}"},
119+
{"type":"TextWhitespace","value":"\n\n"},
120+
{"type":"Keyword","value":"pub"},
121+
{"type":"TextWhitespace","value":" "},
122+
{"type":"Keyword","value":"fn"},
123+
{"type":"TextWhitespace","value":" "},
124+
{"type":"NameFunction","value":"main"},
125+
{"type":"Punctuation","value":"()"},
126+
{"type":"TextWhitespace","value":" "},
127+
{"type":"Punctuation","value":"{"},
128+
{"type":"TextWhitespace","value":"\n\t"},
129+
{"type":"KeywordDeclaration","value":"let"},
130+
{"type":"TextWhitespace","value":" "},
131+
{"type":"Name","value":"_thing"},
132+
{"type":"TextWhitespace","value":" "},
133+
{"type":"Operator","value":"="},
134+
{"type":"TextWhitespace","value":" "},
135+
{"type":"Punctuation","value":"\u003c\u003c"},
136+
{"type":"LiteralString","value":"\"Hello, Joe!\""},
137+
{"type":"Operator","value":":"},
138+
{"type":"Name","value":"utf8"},
139+
{"type":"Punctuation","value":"\u003e\u003e"},
140+
{"type":"TextWhitespace","value":"\n "},
141+
{"type":"Name","value":"io"},
142+
{"type":"Operator","value":"."},
143+
{"type":"NameFunction","value":"debug"},
144+
{"type":"Punctuation","value":"("},
145+
{"type":"NameFunction","value":"reverse_list"},
146+
{"type":"Punctuation","value":"(["},
147+
{"type":"LiteralNumberInteger","value":"1"},
148+
{"type":"Punctuation","value":","},
149+
{"type":"TextWhitespace","value":" "},
150+
{"type":"LiteralNumberInteger","value":"2"},
151+
{"type":"Punctuation","value":","},
152+
{"type":"TextWhitespace","value":" "},
153+
{"type":"LiteralNumberInteger","value":"3"},
154+
{"type":"Punctuation","value":","},
155+
{"type":"TextWhitespace","value":" "},
156+
{"type":"LiteralNumberInteger","value":"4"},
157+
{"type":"Punctuation","value":","},
158+
{"type":"TextWhitespace","value":" "},
159+
{"type":"LiteralNumberInteger","value":"5"},
160+
{"type":"Punctuation","value":"]))"},
161+
{"type":"TextWhitespace","value":"\n "},
162+
{"type":"Name","value":"io"},
163+
{"type":"Operator","value":"."},
164+
{"type":"NameFunction","value":"debug"},
165+
{"type":"Punctuation","value":"("},
166+
{"type":"NameFunction","value":"reverse_list"},
167+
{"type":"Punctuation","value":"(["},
168+
{"type":"LiteralString","value":"\"a\""},
169+
{"type":"Punctuation","value":","},
170+
{"type":"TextWhitespace","value":" "},
171+
{"type":"LiteralString","value":"\"b\""},
172+
{"type":"Punctuation","value":","},
173+
{"type":"TextWhitespace","value":" "},
174+
{"type":"LiteralString","value":"\"c\""},
175+
{"type":"Punctuation","value":","},
176+
{"type":"TextWhitespace","value":" "},
177+
{"type":"LiteralString","value":"\"d\""},
178+
{"type":"Punctuation","value":","},
179+
{"type":"TextWhitespace","value":" "},
180+
{"type":"LiteralString","value":"\"e\""},
181+
{"type":"Punctuation","value":"]))"},
182+
{"type":"TextWhitespace","value":"\n"},
183+
{"type":"Punctuation","value":"}"},
184+
{"type":"TextWhitespace","value":"\n"}
185+
]

0 commit comments

Comments
 (0)