This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgrammar.js
97 lines (76 loc) · 2.01 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
module.exports = grammar({
name: 'tsq',
extras: $ => [/\s/, $.comment],
word: $ => $._identifier,
rules: {
query: $ => repeat(choice($.pattern, $.predicate)),
pattern: $ => $._pattern,
comment: $ => token(seq(';', /.*/)),
_pattern: $ => seq(
field('pattern', choice(
$.alternation,
$.anonymous_leaf,
$.group,
$.named_node,
$.wildcard_node,
)),
optional(field('quantifier', $._quantifier)),
optional($.capture),
),
_quantifier: $ => choice(
$.one_or_more,
$.zero_or_one,
$.zero_or_more,
),
one_or_more: $ => '+',
zero_or_one: $ => '?',
zero_or_more: $ => '*',
capture: $ => /@[a-zA-Z0-9_-][a-zA-Z0-9.?!_-]*/,
alternation: $ => seq('[', repeat1(choice($.choice, $.predicate)), ']'),
choice: $ => $._pattern,
anonymous_leaf: $ => $._string,
_string: $ => seq(
'"',
repeat(choice(
token.immediate(prec(1, /[^"\n\\]+/)),
$.escape_sequence
)),
'"',
),
escape_sequence: $ => token.immediate(seq(
'\\',
choice('n', 'r', 't', '0', '\\'),
)),
_identifier: $ => /[a-zA-Z0-9_-][a-zA-Z0-9.?!_-]*/,
group: $ => seq('(', repeat1(choice($.pattern, $.predicate)), ')'),
named_node: $ => seq(
'(',
$.node_name,
optional(seq(
repeat1(seq(
optional($.anchor),
choice($.child, $.negated_child, $.predicate),
)),
optional($.anchor),
)),
')',
),
node_name: $ => $._identifier,
anchor: $ => '.',
child: $ => seq(
optional(seq($.field_name, ':')),
$._pattern,
),
field_name: $ => $._identifier,
negated_child: $ => seq('!', $.field_name),
predicate: $ => seq(
'(',
$.predicate_name,
repeat(choice($.capture, $.string)),
')',
),
predicate_name: $ => /#[a-zA-Z0-9_-][a-zA-Z0-9.?!_-]*/,
string: $ => $._string,
wildcard_node: $ => prec.right(choice('_', seq('(', '_', ')'))),
}
});