forked from tree-sitter-grammars/tree-sitter-query
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.js
More file actions
150 lines (127 loc) · 3.58 KB
/
grammar.js
File metadata and controls
150 lines (127 loc) · 3.58 KB
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
/// <reference types="tree-sitter-cli/dsl" />
const PREC = {
IMMEDIATE_CHILD: 1,
// Prefer a string over a comment
COMMENT: 1,
STRING: 2,
WILDCARD_NODE: 1,
};
// Identifiers cannot start with `.`
const IDENTIFIER = /[a-zA-Z0-9\-_][a-zA-Z0-9.\-_]*/;
module.exports = grammar({
name: "query",
extras: $ => [
$.comment,
/\s+/,
],
supertypes: $ => [
$.definition,
],
rules: {
program: $ => repeat($.definition),
definition: $ => choice(
$.named_node,
$.anonymous_node,
$.missing_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/blob/4339b0fe05b264082bd159a77b21fc5d586c3a29/lib/src/query.c#L2056
escape_sequence: _ => token.immediate(seq('\\', /./)),
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),
token.immediate('"'),
),
string_content: $ => repeat1(choice(token.immediate(prec(PREC.STRING, /[^"\\\n]+/)), $.escape_sequence)),
parameters: $ => repeat1(choice($.capture, $.string, $._node_identifier)),
comment: _ => token(prec(PREC.COMMENT, seq(";", /.*/))),
list: $ => seq("[", repeat1($.definition), "]", quantifier($), captures($)),
grouping: $ => seq(
"(",
repeat1(seq($._group_expression, optional("."))),
")",
quantifier($),
captures($),
),
missing_node: ($) => seq(
"(",
"MISSING",
optional(field("name", choice($.identifier, $.string))),
")",
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(choice("#", "."), $._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),
);
}