-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
45 lines (43 loc) · 1.19 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
module.exports = grammar({
name: "keyvalues",
extras: $ => [
/\s/,
$.comment
],
rules: {
document: $ => repeat($.pair),
pair: $ => seq(
field("key", $.key),
field("value", $._value),
optional(field("conditional", $.conditional))
),
conditional: $ => token(seq(
"[", token.immediate(/[^"{}\s]*/), "]"
)),
key: $ => choice(
seq("\"", $._string_content, "\""),
$._unquoted_string
),
_value: $ => choice(
$.subpairs,
$.value
),
value: $ => $._string_value,
_string_value: $ => choice(
seq("\"", optional($._string_content), "\""),
$._unquoted_string
),
subpairs: $ => seq("{", repeat($.pair), "}"),
_unquoted_string: $ => /[^\s{}"]+/,
_string_content: $ => repeat1(choice(
token.immediate(prec(1, /[^\\"]+/)),
$.escape_sequence,
"\\"
)),
escape_sequence: $ => token.immediate(seq(
"\\",
/(n|t|\\|\")/
)),
comment: $ => token(prec(1, seq("/", /.*/)))
}
});