-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
85 lines (67 loc) · 1.64 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module.exports = grammar({
name: 'd2',
rules: {
source_file: $ => repeat(choice(
// $.direction,
$.expression,
$._comment,
)),
extras: $ => choice(
$._comment,
/\s+/,
),
direction: _ => seq("direction", ":", choice(
"up",
"right",
"left",
"down"
)),
expression: $ => prec.right(seq(
optional("("),
repeat1(choice(
$.identifier,
$.connection,
)),
optional(")"),
optional(seq(
$.connection_identifier,
$.sub_identifier
)),
optional(seq(":", optional($.label), optional($.container))),
optional(choice(/\n+\s*/, ";")),
)),
label_block: $ => choice(
seq(":", optional($.label), optional($.container)),
// seq(":", $.container),
// seq(":", $.label),
),
container: $ => seq("{", repeat($.expression), "}"),
label: _ => /.+/,
// building blocks
identifier: $ => prec.right(repeat1(
choice(
$._ident_regex,
$.sub_identifier,
"-",
)
)),
sub_identifier: $ => seq(".", $.identifier),
// Trust me it's better that way than doing it via seq's/repeat's
connection: _ => choice(
/--+/,
/<-+/,
/-+>/,
/<-+>/,
// Multiline arrows
seq(/--+/, /\\\n+\s+/, /--+/),
seq(/<-+/, /\\\n+\s+/, /--+/),
seq(/--+/, /\\\n+\s+/, /-+>/),
seq(/<-+/, /\\\n+\s+/, /-+>/),
),
connection_identifier: _ => /\[\d+\]/,
// param_value: _ => /[\w\-_]+/i,
// const-like rules
_ident_regex: _ => /[\p{L}0-9_"' ]+/,
_comment: _ => token(seq('#', /.*\n/)),
}
});