-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.js
91 lines (77 loc) · 2.61 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
85
86
87
88
89
90
91
let newline = choice("\n", "\r", "\r\n");
let newline_or_eof = choice("\n", "\r", "\r\n", "\0");
const precs = {
non_string: 1,
key: 2,
above_key: 3,
};
module.exports = grammar({
name: "norg_meta",
extras: ($) => [],
supertypes: ($) => [$.value],
rules: {
metadata: ($) =>
seq(
repeat(newline),
repeat(
seq(
optional($._whitespace),
choice($.pair, $.delimiter),
repeat1(newline_or_eof)
)
)
),
_whitespace: (_) =>
token(prec(precs.non_string, /[\t ]+/)),
key: (_) => token(prec(precs.key, /[^\s:]+/)),
number: (_) => token(/\d+/),
array: ($) =>
seq(
token(prec(precs.non_string, "[")),
optional(token(prec(precs.non_string, /[\n\r\s]+/))),
optional(
seq(
$.value,
repeat(
prec.left(
seq(
repeat1(newline),
optional($._whitespace),
optional($.value)
)
)
)
)
),
token(prec(precs.non_string, "]"))
),
string: (_) => /[^\n]+/,
object: ($) =>
seq(
token(prec(precs.non_string, "{")),
optional(token(prec(precs.above_key, /[\n\r\s]+/))),
optional(
seq(
choice($.pair, $.delimiter),
repeat(
prec.left(
seq(
repeat1(newline),
optional($._whitespace),
optional(choice($.pair, $.delimiter))
)
)
)
)
),
token(prec(precs.above_key, "}"))
),
value: ($) => choice($.number, $.string, $.array, $.object),
pair: ($) =>
prec.right(
seq($.key, ":", optional(seq($._whitespace, optional($.value))))
),
delimiter: ($) =>
seq(token(prec(precs.above_key, /-+/)), optional(/.+/)),
},
});