Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit d5d79a0

Browse files
committed
grammar: support for type/opaque/spec/callback definitions
1 parent b9b4a9a commit d5d79a0

File tree

5 files changed

+18970
-14269
lines changed

5 files changed

+18970
-14269
lines changed

grammar.js

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ const BRACE_RIGHT = "}";
1313
const BRACKET_LEFT = "[";
1414
const BRACKET_RIGHT = "]";
1515
const COLON = ":";
16+
const DOUBLE_COLON = "::";
1617
const COLON_EQUAL = ":=";
1718
const COMMA = ",";
1819
const DASH = "-";
1920
const DOT = ".";
21+
const DOT_DOT = "..";
22+
const DOT_DOT_DOT = "...";
2023
const EQUAL = "=";
2124
const FAT_ARROW = "=>";
2225
const REV_FAT_ARROW = "<=";
@@ -28,10 +31,12 @@ const DOUBLE_PIPE = "||";
2831
const QUESTION = "?";
2932
const SEMI = ";";
3033
const SLASH = "/";
34+
const UNDERSCORE = "_";
35+
const STAR = "*";
3136

3237
const OP1 = ["+", "-", "bnot", "not"];
3338
const OP2_LEFT_ASSOC = [
34-
"*",
39+
STAR,
3540
"+",
3641
"-",
3742
"/",
@@ -113,11 +118,13 @@ module.exports = grammar({
113118
prec(
114119
PREC.MODULE_DECLARATION,
115120
choice(
116-
$.expression,
121+
$.type_declaration,
122+
$.function_spec,
117123
$.function_declaration,
118124
$.module_attribute,
119125
$.module_name,
120-
$.module_export
126+
$.module_export,
127+
$.expression
121128
)
122129
),
123130

@@ -148,6 +155,121 @@ module.exports = grammar({
148155

149156
comment: ($) => /%.*\n/,
150157

158+
////////////////////////////////////////////////////////////////////////////
159+
//
160+
// Types
161+
//
162+
////////////////////////////////////////////////////////////////////////////
163+
164+
type_declaration: ($) =>
165+
seq(
166+
DASH,
167+
choice("type", "opaque"),
168+
field("name", $.atom),
169+
args($.variable),
170+
DOUBLE_COLON,
171+
field("def", $.type_expression),
172+
DOT
173+
),
174+
175+
function_spec: ($) =>
176+
seq(
177+
DASH,
178+
choice("spec", "callback"),
179+
field("name", $.atom),
180+
args($.type_expression),
181+
ARROW,
182+
field("def", $.type_expression),
183+
DOT
184+
),
185+
186+
type_expression: ($) =>
187+
sepBy(
188+
PIPE,
189+
choice(
190+
$.type_atom,
191+
$.type_application,
192+
$.type_bitstring,
193+
$.type_fun,
194+
$.type_integer,
195+
$.type_map,
196+
$.type_record,
197+
$.type_tuple,
198+
$.type_variable,
199+
$._type_list
200+
)
201+
),
202+
203+
type_variable: ($) => $.variable,
204+
205+
type_atom: ($) => $.atom,
206+
207+
type_application: ($) =>
208+
prec.right(PREC.MACRO_APPLICATION, seq($.atom, args($.type_expression))),
209+
210+
type_bitstring: ($) =>
211+
seq(
212+
BINARY_LEFT,
213+
choice(
214+
seq(UNDERSCORE, COLON, $.variable),
215+
seq(UNDERSCORE, COLON, UNDERSCORE, STAR, $.variable),
216+
seq(
217+
UNDERSCORE,
218+
COLON,
219+
$.variable,
220+
UNDERSCORE,
221+
COLON,
222+
UNDERSCORE,
223+
STAR,
224+
$.variable
225+
),
226+
BINARY_RIGHT
227+
)
228+
),
229+
230+
type_fun: ($) =>
231+
seq(
232+
"fun",
233+
parens(
234+
opt(
235+
choice(
236+
seq(parens(DOT_DOT_DOT), ARROW, $.type_expression),
237+
seq(args($.type_expression), ARROW, $.type_expression)
238+
)
239+
)
240+
)
241+
),
242+
type_integer: ($) => choice($.integer, seq($.integer, DOT_DOT, $.integer)),
243+
244+
_type_list: ($) => choice($.type_list, $.type_nonempty_list),
245+
246+
type_list: ($) => delim(BRACKET_LEFT, $.type_expression, BRACKET_RIGHT),
247+
248+
type_nonempty_list: ($) =>
249+
delim(
250+
BRACKET_LEFT,
251+
seq($.type_expression, COMMA, DOT_DOT_DOT),
252+
BRACKET_RIGHT
253+
),
254+
255+
type_tuple: ($) => tuple($.type_expression),
256+
257+
type_map: ($) =>
258+
seq(HASH, BRACE_LEFT, opt(sepBy(COMMA, $.type_map_entry)), BRACE_RIGHT),
259+
type_map_entry: ($) =>
260+
seq($.type_expression, choice(FAT_ARROW, COLON_EQUAL), $.type_expression),
261+
262+
type_record: ($) =>
263+
seq(
264+
HASH,
265+
$.atom,
266+
BRACE_LEFT,
267+
opt(sepBy(COMMA, $.type_record_field)),
268+
BRACE_RIGHT
269+
),
270+
type_record_field: ($) =>
271+
seq($.type_expression, DOUBLE_COLON, $.type_expression),
272+
151273
////////////////////////////////////////////////////////////////////////////
152274
//
153275
// Patterns

0 commit comments

Comments
 (0)