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
use rowan::TextSize;
use crate::{rules::Rule, syntax_kind::SyntaxKind, SyntaxNode};
pub fn continuations_at(root: &SyntaxNode, mut offset: TextSize) -> Option<Vec<SyntaxKind>> {
let token = if offset < root.text_range().start() {
return None;
} else if offset >= root.text_range().end() {
let last_token = root.last_token()?;
offset = last_token.text_range().end();
last_token
} else {
root.token_at_offset(offset).left_biased()?
};
let mut parent = token.parent()?;
let mut children_stack: Vec<_> = parent.children_with_tokens().collect();
children_stack.reverse();
let mut rule_stack = vec![Rule::from_node_kind(parent.kind())?];
let mut result = vec![];
loop {
if let Some(child) = children_stack.last() {
if let Some(rule) = rule_stack.last() {
if child.text_range().start() >= offset {
// NOTE: This child is behind the "cursor"
// -> Add remaining stack to solution
// -> if the stack is nullable, move up in the tree
while let Some(rule) = rule_stack.pop() {
result.append(&mut rule.first());
if !rule.is_nullable() {
return Some(result);
}
}
// NOTE: The stack was nullable, move up the tree
if let Some(grand_parent) = parent.parent() {
parent = grand_parent;
children_stack = parent.children_with_tokens().collect();
children_stack.reverse();
rule_stack.push(Rule::from_node_kind(parent.kind())?);
continue;
} else {
return Some(result);
}
}
// NOTE: Skip comments & whitespace.
if child.kind().is_trivia() {
children_stack.pop();
} else if child.kind() == SyntaxKind::Error {
return Some(result);
} else {
match &rule {
Rule::Node(syntax_kind) | Rule::Token(syntax_kind) => {
if *syntax_kind == child.kind() {
rule_stack.pop();
children_stack.pop();
} else if rule.is_nullable() {
rule_stack.pop();
} else {
return Some(result);
}
if children_stack.is_empty() && rule_stack.is_empty() {
// NOTE: Position could not be found in this rule, move up the tree
if let Some(grand_parent) = parent.parent() {
parent = grand_parent;
children_stack.extend(parent.children_with_tokens());
children_stack.reverse();
rule_stack.push(Rule::from_node_kind(parent.kind())?);
} else {
return Some(result);
}
}
}
Rule::Seq(rules) => {
let x = rules.clone();
rule_stack.pop();
rule_stack.extend(x.into_iter().rev());
}
Rule::Alt(rules) => {
if let Some(rule) = rules
.iter()
.find(|rule| rule.first().iter().any(|kind| *kind == child.kind()))
{
let x = rule.clone();
rule_stack.pop();
rule_stack.push(x);
} else if rules.iter().any(|rule| rule.is_nullable()) {
rule_stack.pop();
} else {
return None;
}
}
Rule::Opt(rule) => {
let x = *rule.clone();
rule_stack.pop();
if x.first().contains(&child.kind()) {
rule_stack.push(x);
}
}
Rule::Rep(rule) => {
if rule.first().iter().any(|kind| *kind == child.kind()) {
rule_stack.push(*rule.clone());
} else {
rule_stack.pop();
}
}
}
}
} else {
// NOTE: The rule stack is empty ->
return Some(result);
}
} else {
// NOTE: The childrens stack is empty
// -> Add remaining rule stack to solution
// -> if the stack is nullable, move up in the tree
while let Some(rule) = rule_stack.pop() {
result.append(&mut rule.first());
if !rule.is_nullable() {
return Some(result);
}
}
if let Some(grand_parent) = parent.parent() {
parent = grand_parent;
children_stack.extend(parent.children_with_tokens());
children_stack.reverse();
rule_stack.push(Rule::from_node_kind(parent.kind())?);
} else {
return Some(result);
}
}
}
}
#[cfg(test)]
mod test {
use crate::{parse_query, rules::Rule, syntax_kind::SyntaxKind};
use super::continuations_at;
#[test]
fn nullablity() {
assert!(Rule::from_node_kind(SyntaxKind::GroupGraphPatternSub)
.unwrap()
.is_nullable());
}
#[test]
fn first() {
assert_eq!(
Rule::from_node_kind(SyntaxKind::GroupGraphPatternSub)
.unwrap()
.first(),
vec![SyntaxKind::TriplesBlock, SyntaxKind::GraphPatternNotTriples]
);
}
#[test]
fn continueations_failure() {
// 0123456789012345678
let input = "SELECT WHERE { }";
let root = parse_query(input);
assert_eq!(continuations_at(&root, 12.into()), None);
}
#[test]
fn continueations_triplesblock() {
// 012345678901234567890123456
let input = "SELECT WHERE { ?a ?b ?c . }";
let root = parse_query(input);
assert_eq!(
continuations_at(&root, 25.into()),
vec![
SyntaxKind::TriplesBlock,
SyntaxKind::GraphPatternNotTriples,
SyntaxKind::RCurly
]
.into()
);
}
#[test]
fn continueations_where_clause() {
// 0123456789012345678
let input = "SELECT * WHERE { }";
let root = parse_query(input);
assert_eq!(
continuations_at(&root, 14.into()),
vec![SyntaxKind::GroupGraphPattern].into()
);
}
#[test]
fn continueations_triple() {
// 01234567890123456789012345678901234
let input = "SELECT * FROM <> WHERE { ?a ?s ?c } ";
let root = parse_query(input);
assert_eq!(
continuations_at(&root, 24.into()),
vec![
SyntaxKind::SubSelect,
SyntaxKind::GroupGraphPatternSub,
SyntaxKind::RCurly
]
.into()
);
// assert_eq!(
// continuations_at(&root, 27.into()),
// vec![SyntaxKind::PropertyListPathNotEmpty].into()
// );
// assert_eq!(
// continuations_at(&root, 30.into()),
// vec![SyntaxKind::ObjectListPath].into()
// );
}
#[test]
fn continueations_select_query() {
// 012345678901234567890123456789
let input = "SELECT * FROM <> WHERE {}\n";
let root = parse_query(input);
assert_eq!(
continuations_at(&root, 8.into()),
vec![SyntaxKind::DatasetClause, SyntaxKind::WhereClause].into()
);
assert_eq!(
continuations_at(&root, 16.into()),
vec![SyntaxKind::DatasetClause, SyntaxKind::WhereClause].into()
);
assert_eq!(
continuations_at(&root, 25.into()),
vec![SyntaxKind::SolutionModifier, SyntaxKind::ValuesClause].into()
);
// Maybe this would be reasonable?:
// assert_eq!(
// continuations_at(&root, 27.into()),
// vec![SyntaxKind::SolutionModifier, SyntaxKind::ValuesClause].into()
// );
}
#[test]
fn continueations_values_clause() {
// 0123456789012345678901234567890
let input = "SELECT * WHERE { VALUES ?a {}}";
let root = parse_query(input);
assert_eq!(
continuations_at(&root, 27.into()),
vec![SyntaxKind::LCurly].into()
);
assert_eq!(
continuations_at(&root, 28.into()),
vec![SyntaxKind::DataBlockValue, SyntaxKind::RCurly].into()
);
}
#[test]
fn continueations_solution_modifier() {
// 0123456789012345678901234567890
let input = "SELECT * WHERE {#c\n} \n\n";
let root = parse_query(input);
assert_eq!(
continuations_at(&root, 20.into()),
vec![SyntaxKind::SolutionModifier, SyntaxKind::ValuesClause].into()
);
}
}