-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrammar.js
107 lines (90 loc) · 1.93 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const ATOM_CHAR = /[^;()"%\x00-\x20\x7f-\xff]/
const VAR_CHAR = /[^;()"\x00-\x20\x7f-\xff:{}]/
module.exports = grammar({
name: 'dune',
extras: $ => [
/\s/,
$.comment
],
inline: $ => [
$._value,
$._variable_trail
],
rules: {
configuration: $ => repeat($._value),
_value: $ => choice(
$.atom,
$.quoted_string,
$.block_string,
$.raw_string,
$.list
),
atom: $ => seq(
choice(
token(choice(repeat1(ATOM_CHAR), '%')),
$.variable
),
repeat(choice(
token.immediate(choice(repeat1(ATOM_CHAR), '%')),
alias($._immediate_variable, $.variable)
))
),
quoted_string: $ => seq(
'"',
repeat(choice(
$.escape_sequence,
alias($._immediate_variable, $.variable),
token.immediate(prec(1, /[^\\"%]+/)),
token.immediate('%')
)),
'"'
),
block_string: $ => seq(
'"\\|',
repeat(choice(
$.escape_sequence,
alias($._immediate_variable, $.variable),
token.immediate(prec(1, /[^\\"%\n\r]+/)),
token.immediate('%')
))
),
raw_string: $ => seq(
'"\\>',
token.immediate(prec(1, /.*/))
),
escape_sequence: $ => token.immediate(seq(
'\\',
choice(
/\n\s*/,
'%{',
/[\\'"ntbr]/,
/[0-9][0-9][0-9]/,
/x[A-Fa-f0-9][A-Fa-f0-9]/
)
)),
variable: $ => seq(
'%{',
$._variable_trail
),
_immediate_variable: $ => seq(
token.immediate('%{'),
$._variable_trail
),
_variable_trail: $ => seq(
$.variable_name,
optional(seq(
':',
optional($.variable_value)
)),
'}'
),
variable_name: $ => token(repeat1(VAR_CHAR)),
variable_value: $ => token(repeat1(choice(VAR_CHAR, ':'))),
list: $ => seq(
'(',
repeat($._value),
')'
),
comment: $ => /;.*/
}
})