-
Notifications
You must be signed in to change notification settings - Fork 14
/
grammar.js
148 lines (126 loc) · 3.45 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
/// <reference types="tree-sitter-cli/dsl" />
const PREC = {
IMMEDIATE_CHILD: 1,
// Prefer a string over a comment
COMMENT: 1,
STRING: 2,
WILDCARD_NODE: 1,
};
const IDENTIFIER = /[a-zA-Z0-9.\-_\$]+/;
module.exports = grammar({
name: "query",
extras: $ => [
$.comment,
/\s+/,
],
supertypes: $ => [
$.definition,
],
rules: {
program: $ => repeat($.definition),
definition: $ => choice(
$.named_node,
$.anonymous_node,
$.grouping,
$.predicate,
$.list,
$.field_definition,
),
// Expressions that are valid inside a group.
_group_expression: $ => choice(
$.definition,
immediate_child($._group_expression),
),
// Expressions that are valid inside a named node.
_named_node_expression: $ => choice(
$.definition,
$.negated_field,
immediate_child($._named_node_expression),
),
// Taken from https://github.com/tree-sitter/tree-sitter-javascript/blob/3f8b62f9befd3cb3b4cb0de22f6595a0aadf76ca/grammar.js#L827
escape_sequence: _ => token.immediate(seq(
'\\',
choice(
/[^xu0-7]/,
/[0-7]{1,3}/,
/x[0-9a-fA-F]{2}/,
/u[0-9a-fA-F]{4}/,
/u\{[0-9a-fA-F]+\}/
)
)),
quantifier: _ => choice("*", "+", "?"),
identifier: _ => IDENTIFIER,
_immediate_identifier: $ => alias(token.immediate(IDENTIFIER), $.identifier),
_node_identifier: $ => choice($.identifier, prec(PREC.WILDCARD_NODE, "_")),
capture: $ => seq("@", field("name", $._immediate_identifier)),
string: $ => seq(
'"',
optional($.string_content),
'"',
),
string_content: $ => repeat1(choice(token.immediate(prec(PREC.STRING, /[^"\\]+/)), $.escape_sequence)),
parameters: $ => repeat1(choice($.capture, $.string, $._node_identifier)),
comment: _ => token(prec(PREC.COMMENT, seq(";", /.*/))),
list: $ => seq("[", repeat($.definition), "]", quantifier($), captures($)),
grouping: $ => seq(
"(",
repeat(seq($._group_expression, optional("."))),
")",
quantifier($),
captures($),
),
anonymous_node: $ => seq(
field("name", choice($.string, "_")),
quantifier($),
captures($),
),
named_node: $ => seq(
"(",
choice(
field("name", $._node_identifier),
seq(field("supertype", $.identifier), token.immediate('/'), field("name", $._immediate_identifier)),
),
optional(
seq(
optional("."),
choice(
repeat1($._named_node_expression),
seq(
repeat($._named_node_expression),
seq($._named_node_expression, "."),
)
),
),
),
")",
quantifier($),
captures($),
),
_field_name: $ => seq($.identifier, ":"),
field_definition: $ => seq(
field("name", $._field_name),
$.definition,
),
negated_field: $ => seq("!", $.identifier),
predicate: $ =>
seq(
"(",
field("name", seq("#", $._immediate_identifier, field("type", $.predicate_type))),
field("parameters", $.parameters),
")"
),
predicate_type: _ => token.immediate(choice("?", "!")),
}
});
function captures($) {
return repeat($.capture);
}
function quantifier($) {
return optional(field("quantifier", $.quantifier));
}
function immediate_child(expression) {
return prec.left(
PREC.IMMEDIATE_CHILD,
seq(expression, ".", expression),
);
}