-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.js
37 lines (28 loc) · 913 Bytes
/
grammar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const WHITE_SPACE = /[ \f\t\v]+/;
const NEWLINE = /\r?\n/;
const ANYTHING = /[^\r\n]+/;
module.exports = grammar({
name: "iex",
extras: ($) => [WHITE_SPACE],
rules: {
source: ($) => repeat(choice($.evaluation_block, NEWLINE)),
evaluation_block: ($) =>
seq(
alias($._default_prompt_line, $.prompt_line),
repeat(alias($._cont_prompt_line, $.prompt_line)),
optional($.result)
),
_default_prompt_line: ($) =>
seq(alias(/iex(\([^\)]+\)\d*)?>/, $.prompt), optional($.expression)),
_cont_prompt_line: ($) =>
seq(alias(/\.\.\.(\([^\)]+\)\d*)?>/, $.prompt), optional($.expression)),
expression: ($) => seq(ANYTHING, NEWLINE),
result: ($) =>
prec.right(
seq(sep1(token(prec(-1, ANYTHING)), NEWLINE), optional(NEWLINE))
),
},
});
function sep1(rule, separator) {
return seq(rule, repeat(seq(separator, rule)));
}