-
Notifications
You must be signed in to change notification settings - Fork 5
/
grammar.js
298 lines (269 loc) · 10.8 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* @file KDL grammar for tree-sitter
* @author Amaan Qureshi <[email protected]>
* @license MIT
* @see {@link https://kdl.dev|official website}
* @see {@link https://github.com/kdl-org/kdl/blob/main/SPEC.md|official syntax spec}
*/
// deno-lint-ignore-file no-control-regex
/* eslint-disable arrow-parens */
/* eslint-disable camelcase */
/* eslint-disable-next-line spaced-comment */
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
const ANNOTATION_BUILTINS = [
'i8',
'i16',
'i32',
'i64',
'u8',
'u16',
'u32',
'u64',
'isize',
'usize',
'f32',
'f64',
'decimal64',
'decimal128',
'date-time',
'time',
'date',
'duration',
'decimal',
'currency',
'country-2',
'country-3',
'country-subdivision',
'email',
'idn-email',
'hostname',
'idn-hostname',
'ipv4',
'ipv6',
'url',
'url-reference',
'irl',
'iri-reference',
'url-template',
'uuid',
'regex',
'base64',
];
module.exports = grammar({
name: 'kdl',
conflicts: $ => [
[$.document],
[$._node_space],
[$.node_children],
],
externals: $ => [$._eof, $.multi_line_comment],
extras: $ => [$.multi_line_comment],
word: $ => $._normal_bare_identifier,
rules: {
// nodes := linespace* (node nodes?)? linespace*
document: $ =>
seq(
repeat($._linespace),
optional(seq(
$.node,
repeat(seq(
repeat($._linespace),
$.node,
)),
)),
repeat($._linespace),
),
// node := ('/-' node-space*)? type? identifier (node-space+ node-prop-or-arg)* (node-space* node-children ws*)? node-space* node-terminator
node: $ => prec(1,
seq(
alias(optional(seq('/-', repeat($._node_space))), $.node_comment),
optional($.type),
$.identifier,
repeat(seq(repeat1($._node_space), $.node_field)),
optional(seq(repeat($._node_space), field('children', $.node_children), repeat($._ws))),
repeat($._node_space),
$._node_terminator,
),
),
// node-prop-or-arg (field) := ('/-' node-space*)? (prop | value)
// _node_prop_or_arg: $ =>
// seq(
// alias(optional(seq('/-', repeat($._node_space))), $.node_prop_or_arg_slash_dash),
// field('node_prop_or_arg', choice($.prop, $.value)),
// ),
node_field: $ => choice($._node_field_comment, $._node_field),
_node_field_comment: $ => alias(seq('/-', repeat($._node_space), $._node_field), $.node_field_comment),
_node_field: $ => choice($.prop, $.value),
// node-children := ('/-' node-space*)? '{' nodes '}'
node_children: $ =>
seq(
optional(seq(alias('/-', $.node_children_comment), repeat($._node_space))),
'{',
seq(
repeat($._linespace),
optional(seq($.node, repeat(seq(repeat($._linespace), $.node)))),
repeat($._linespace),
),
'}',
),
// node-space := ws* escline ws* | ws+
_node_space: $ =>
choice(
seq(repeat($._ws), $._escline, repeat($._ws)),
repeat1($._ws),
),
// node-terminator := single-line-comment | newline | ';' | eof
_node_terminator: $ =>
choice($.single_line_comment, $._newline, ';', $._eof),
// identifier := string | bare-identifier
identifier: $ => choice($.string, $._bare_identifier),
// bare-identifier := ((identifier-char - digit - sign) identifier-char* | sign ((identifier-char - digit) identifier-char*)?) - keyword
_bare_identifier: $ =>
choice(
$._normal_bare_identifier,
seq($._sign, optional(seq($.__identifier_char_no_digit, repeat($._identifier_char)))),
),
// _normal_bare_identifier: $ => $.__identifier_char_no_digit_sign,
_normal_bare_identifier: _ => token(
seq(
/[\u4E00-\u9FFF\p{L}\p{M}\p{N}\p{Emoji}_~!@#\$%\^&\*.:'\|\?&&[^\s\d\/(){}<>;\[\]=,"]]/,
/[\u4E00-\u9FFF\p{L}\p{M}\p{N}\p{Emoji}\-_~!@#\$%\^&\*.:'\|\?+&&[^\s\/(){}<>;\[\]=,"]]*/,
),
),
// identifier-char := unicode - linespace - [\/(){}<>;[]=,"]
_identifier_char: _ => token(
/[\u4E00-\u9FFF\p{L}\p{M}\p{N}\-_~!@#\$%\^&\*.:'\|\?+&&[^\s\/(){}<>;\[\]=,"]]/,
),
// can't start with a digit
__identifier_char_no_digit: _ => token(
/[\u4E00-\u9FFF\p{L}\p{M}\p{N}\-_~!@#\$%\^&\*.:'\|\?+&&[^\s\d\/(){}<>;\[\]=,"]]/,
),
// can't start with a digit or sign
__identifier_char_no_digit_sign: _ => token(
/[\u4E00-\u9FFF\p{L}\p{M}\p{N}\-_~!@#\$%\^&\*.:'\|\?&&[^\s\d\+\-\/(){}<>;\[\]=,"]]/,
),
// keyword := boolean | 'null'
keyword: $ => choice($.boolean, 'null'),
// type annotations
annotation_type: _ => choice(...ANNOTATION_BUILTINS),
// prop := identifier '=' value
prop: $ => seq($.identifier, '=', $.value),
// value := type? (string | number | keyword)
value: $ => seq(optional($.type), choice($.string, $.number, $.keyword)),
// type := '(' identifier ')'
type: $ => seq('(', choice($.identifier, $.annotation_type), ')'),
// String
// string := raw-string | escaped-string
string: $ => choice($._raw_string, $._escaped_string),
// escaped-string := '"' character* '"'
_escaped_string: $ => seq('"', alias(repeat(choice($.escape, /[^"]/)), $.string_fragment), '"'),
// character := '\' escape | [^\"]
_character: $ => choice($.escape, /[^"]/),
// escape := ["\\/bfnrt] | 'u{' hex-digit{1, 6} '}'
escape: _ =>
token.immediate(/\\\\|\\"|\\\/|\\b|\\f|\\n|\\r|\\t|\\u\{[0-9a-fA-F]{1,6}\}/),
// hex-digit := [0-9a-fA-F]
_hex_digit: _ => /[0-9a-fA-F]/,
// // raw-string := 'r' raw-string-hash
// raw_string: $ => seq('r', $._raw_string_hash),
// // raw-string-hash := '#' raw-string-hash '#' | raw-string-quotes
// _raw_string_hash: $ => choice(seq('#', $._raw_string_hash, '#'), $._raw_string_quotes),
// // raw-string-quotes := '"' .* '"'
// _raw_string_quotes: _ => seq('"', /.*/, '"'),
_raw_string: _ =>
seq(
choice(
// raw-string-hash := '#' raw-string-hash '#' | raw-string-quotes
// yes this isn't perfect but it works, ideally this should be handled in an external scanner
seq(token.immediate(seq('r', repeat1('#'))), /[^#]*/, repeat1('#')),
// raw-string-quotes := '"' . '"'
seq(token.immediate(seq('r', '"')), /[^"]*/, '"'),
),
),
// number := decimal | hex | octal | binary
number: $ => choice($._decimal, $._hex, $._octal, $._binary),
// decimal := sign? integer ('.' integer)? exponent?
_decimal: $ =>
seq(
optional($._sign),
$._integer,
optional(seq('.', alias($._integer, $.decimal))),
optional(alias($._exponent, $.exponent)),
),
// exponent := ('e' | 'E') sign? integer
_exponent: $ => seq(choice('e', 'E'), optional($._sign), $._integer),
// integer := digit (digit | '_')*
_integer: $ => seq($._digit, repeat(choice($._digit, '_'))),
// digit := [0-9]
_digit: _ => /[0-9]/,
// sign := '+' | '-'
_sign: _ => choice('+', '-'),
// hex := sign? '0x' hex-digit (hex-digit | '_')*
_hex: $ => seq(optional($._sign), '0x', $._hex_digit, repeat(choice($._hex_digit, '_'))),
// octal := sign? '0o' [0-7] [0-7_]*
_octal: $ => seq(optional($._sign), '0o', /[0-7]/, repeat(choice(/[0-7]/, '_'))),
// binary := sign? '0b' ('0' | '1') ('0' | '1' | '_')*
_binary: $ => seq(optional($._sign), '0b', choice('0', '1'), repeat(choice('0', '1', '_'))),
// boolean := 'true' | 'false'
boolean: _ => choice('true', 'false'),
// escline := '\\' ws* (single-line-comment | newline)
_escline: $ => seq('\\', repeat($._ws), choice($.single_line_comment, $._newline)),
// linespace := newline | ws | single-line-comment
_linespace: $ => choice($._newline, $._ws, $.single_line_comment),
// newline := See Table (All line-break white_space)
// Newline
// The following characters should be treated as new lines:
//
// ╭──────────────────────────────────────────────────────────╮
// │ Acronym Name Code Pt │
// │ CR Carriage Return U+000D │
// │ LF Line Feed U+000A │
// │ CRLF Carriage Return and Line Feed U+000D + U+000A │
// │ NEL Next Line U+0085 │
// │ FF Form Feed U+000C │
// │ LS Line Separator U+2028 │
// │ PS Paragraph Separator U+2029 │
// ╰──────────────────────────────────────────────────────────╯
// Note that for the purpose of new lines, CRLF is considered a single newline.
_newline: _ => choice(/\r'/, /\n/, /\r\n/, /\u0085/, /\u000C/, /\u2028/, /\u2029/),
// ws := bom | unicode-space | multi-line-comment
_ws: $ => choice($._bom, $._unicode_space, $.multi_line_comment),
// bom := '\u{FEFF}'
_bom: _ => /\u{FEFF}/,
// unicode-space := See Table (All White_Space unicode characters which are not `newline`)
// Whitespace
// The following characters should be treated as non-Newline white space:
//
// ╭────────────────────────────────────╮
// │ Name Code Pt │
// │ Character Tabulation U+0009 │
// │ Space U+0020 │
// │ No-Break Space U+00A0 │
// │ Ogham Space Mark U+1680 │
// │ En Quad U+2000 │
// │ Em Quad U+2001 │
// │ En Space U+2002 │
// │ Em Space U+2003 │
// │ Three-Per-Em Space U+2004 │
// │ Four-Per-Em Space U+2005 │
// │ Six-Per-Em Space U+2006 │
// │ Figure Space U+2007 │
// │ Punctuation Space U+2008 │
// │ Thin Space U+2009 │
// │ Hair Space U+200A │
// │ Narrow No-Break Space U+202F │
// │ Medium Mathematical Space U+205F │
// │ Ideographic Space U+3000 │
// ╰────────────────────────────────────╯
_unicode_space: _ =>
/[\u0009\u0020\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000]/,
// single-line-comment := '//' ^newline+ (newline | eof)
single_line_comment: $ =>
seq(
'//',
repeat(/[^\r\n\u0085\u000C\u2028\u2029]/),
choice($._newline, $._eof),
),
},
});