@konomanoasa/tree-sitter-posix-awk 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +1 -1
- package/bindings/rust/build.rs +3 -2
- package/bindings/rust/lib.rs +497 -73
- package/grammar.js +391 -273
- package/package.json +1 -1
- package/queries/highlights.scm +1 -1
- package/src/grammar.json +1658 -918
- package/src/node-types.json +73 -94
- package/src/parser.c +142728 -56204
- package/src/scanner.c +401 -408
- package/tree-sitter.json +1 -1
package/Cargo.toml
CHANGED
package/bindings/rust/build.rs
CHANGED
|
@@ -4,8 +4,9 @@ fn main() {
|
|
|
4
4
|
let mut c_config = cc::Build::new();
|
|
5
5
|
c_config.std("c17").include(src_dir);
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
if c_config.get_compiler().is_like_msvc() {
|
|
8
|
+
c_config.flag("-utf-8");
|
|
9
|
+
}
|
|
9
10
|
|
|
10
11
|
if std::env::var("TARGET").unwrap() == "wasm32-unknown-unknown" {
|
|
11
12
|
let Ok(wasm_headers) =
|
package/bindings/rust/lib.rs
CHANGED
|
@@ -44,6 +44,8 @@ pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
|
|
|
44
44
|
|
|
45
45
|
#[cfg(test)]
|
|
46
46
|
mod tests {
|
|
47
|
+
use std::ops::Range;
|
|
48
|
+
|
|
47
49
|
use tree_sitter::{
|
|
48
50
|
Node, Parser, Query, QueryCursor, StreamingIterator, Tree,
|
|
49
51
|
};
|
|
@@ -54,6 +56,100 @@ mod tests {
|
|
|
54
56
|
leaves: &'static [(&'static str, usize, usize)],
|
|
55
57
|
}
|
|
56
58
|
|
|
59
|
+
struct LeafCase {
|
|
60
|
+
name: &'static str,
|
|
61
|
+
source: &'static str,
|
|
62
|
+
kind: &'static str,
|
|
63
|
+
range: Range<usize>,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const LEAF_CASES: &[LeafCase] = &[
|
|
67
|
+
LeafCase {
|
|
68
|
+
name: "a complete name remains one leaf",
|
|
69
|
+
source: "foo\n",
|
|
70
|
+
kind: "name",
|
|
71
|
+
range: 0..3,
|
|
72
|
+
},
|
|
73
|
+
LeafCase {
|
|
74
|
+
name: "the name before consecutive continuations remains separate",
|
|
75
|
+
source: "fo\\\n\\\no\n",
|
|
76
|
+
kind: "name",
|
|
77
|
+
range: 0..2,
|
|
78
|
+
},
|
|
79
|
+
LeafCase {
|
|
80
|
+
name: "the name after consecutive continuations remains separate",
|
|
81
|
+
source: "fo\\\n\\\no\n",
|
|
82
|
+
kind: "name",
|
|
83
|
+
range: 6..7,
|
|
84
|
+
},
|
|
85
|
+
LeafCase {
|
|
86
|
+
name: "a keyword excludes its following continuation",
|
|
87
|
+
source: "BEGIN\\\n{}\n",
|
|
88
|
+
kind: "begin_keyword",
|
|
89
|
+
range: 0..5,
|
|
90
|
+
},
|
|
91
|
+
LeafCase {
|
|
92
|
+
name: "a builtin excludes its following continuation",
|
|
93
|
+
source: "{length\\\n(x)}\n",
|
|
94
|
+
kind: "builtin_func_name",
|
|
95
|
+
range: 1..7,
|
|
96
|
+
},
|
|
97
|
+
LeafCase {
|
|
98
|
+
name: "a function name excludes its adjacent parenthesis",
|
|
99
|
+
source: "{foo(x)}\n",
|
|
100
|
+
kind: "func_name",
|
|
101
|
+
range: 1..4,
|
|
102
|
+
},
|
|
103
|
+
LeafCase {
|
|
104
|
+
name: "a continuation before the parenthesis makes a variable name",
|
|
105
|
+
source: "{foo\\\n(x)}\n",
|
|
106
|
+
kind: "name",
|
|
107
|
+
range: 1..4,
|
|
108
|
+
},
|
|
109
|
+
LeafCase {
|
|
110
|
+
name: "a decimal constant includes its exponent and suffix",
|
|
111
|
+
source: "1.2e+3F\n",
|
|
112
|
+
kind: "number",
|
|
113
|
+
range: 0..7,
|
|
114
|
+
},
|
|
115
|
+
LeafCase {
|
|
116
|
+
name: "a logical operator excludes its following continuation",
|
|
117
|
+
source: "a||\\\nb\n",
|
|
118
|
+
kind: "or",
|
|
119
|
+
range: 1..3,
|
|
120
|
+
},
|
|
121
|
+
LeafCase {
|
|
122
|
+
name: "Unicode string content remains one leaf",
|
|
123
|
+
source: "\"é😀\"\n",
|
|
124
|
+
kind: "string_content",
|
|
125
|
+
range: 1..7,
|
|
126
|
+
},
|
|
127
|
+
LeafCase {
|
|
128
|
+
name: "an octal escape excludes the following string character",
|
|
129
|
+
source: "\"\\123x\"\n",
|
|
130
|
+
kind: "escape_sequence",
|
|
131
|
+
range: 1..5,
|
|
132
|
+
},
|
|
133
|
+
LeafCase {
|
|
134
|
+
name: "an escaped slash excludes the closing delimiter",
|
|
135
|
+
source: "/\\//\n",
|
|
136
|
+
kind: "escaped_delimiter",
|
|
137
|
+
range: 1..3,
|
|
138
|
+
},
|
|
139
|
+
LeafCase {
|
|
140
|
+
name: "an ERE class name excludes compound delimiters",
|
|
141
|
+
source: "/[[:alpha:]]/\n",
|
|
142
|
+
kind: "class_name",
|
|
143
|
+
range: 4..9,
|
|
144
|
+
},
|
|
145
|
+
LeafCase {
|
|
146
|
+
name: "an ERE repetition count remains one leaf",
|
|
147
|
+
source: "/a{12}/\n",
|
|
148
|
+
kind: "dup_count",
|
|
149
|
+
range: 3..5,
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
|
|
57
153
|
const ERE_CASES: &[EreCase] = &[
|
|
58
154
|
EreCase {
|
|
59
155
|
name: "ordinary characters and complete escapes retain their source ranges",
|
|
@@ -162,18 +258,66 @@ mod tests {
|
|
|
162
258
|
},
|
|
163
259
|
];
|
|
164
260
|
|
|
165
|
-
fn
|
|
261
|
+
fn parse(source: &str, name: &str) -> Tree {
|
|
166
262
|
let mut parser = Parser::new();
|
|
167
263
|
parser
|
|
168
264
|
.set_language(&super::LANGUAGE.into())
|
|
169
265
|
.expect("generated grammar must load");
|
|
170
266
|
let tree = parser
|
|
171
|
-
.parse(
|
|
267
|
+
.parse(source, None)
|
|
172
268
|
.expect("parser must return a tree");
|
|
173
|
-
assert!(!tree.root_node().has_error(), "{}"
|
|
269
|
+
assert!(!tree.root_node().has_error(), "{name}");
|
|
174
270
|
tree
|
|
175
271
|
}
|
|
176
272
|
|
|
273
|
+
fn assert_leaf_partition(
|
|
274
|
+
node: Node<'_>,
|
|
275
|
+
expected: &[(&str, usize, usize)],
|
|
276
|
+
name: &str,
|
|
277
|
+
) {
|
|
278
|
+
let mut leaves = Vec::new();
|
|
279
|
+
collect_leaves(node, &mut leaves);
|
|
280
|
+
let actual: Vec<_> = leaves
|
|
281
|
+
.iter()
|
|
282
|
+
.map(|leaf| (leaf.kind(), leaf.start_byte(), leaf.end_byte()))
|
|
283
|
+
.collect();
|
|
284
|
+
assert_eq!(actual, expected, "{name}");
|
|
285
|
+
let mut end = node.start_byte();
|
|
286
|
+
for leaf in leaves {
|
|
287
|
+
assert_eq!(leaf.start_byte(), end, "{name}");
|
|
288
|
+
assert!(leaf.end_byte() > end, "{name}");
|
|
289
|
+
end = leaf.end_byte();
|
|
290
|
+
}
|
|
291
|
+
assert_eq!(end, node.end_byte(), "{name}");
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
fn assert_leaf_highlights(
|
|
295
|
+
source: &str,
|
|
296
|
+
tree: &Tree,
|
|
297
|
+
ranges: impl IntoIterator<Item = Range<usize>>,
|
|
298
|
+
name: &str,
|
|
299
|
+
query: &Query,
|
|
300
|
+
) {
|
|
301
|
+
let mut cursor = QueryCursor::new();
|
|
302
|
+
let mut covered = vec![false; source.len()];
|
|
303
|
+
let mut captures =
|
|
304
|
+
cursor.captures(query, tree.root_node(), source.as_bytes());
|
|
305
|
+
while let Some((matched, index)) = captures.next() {
|
|
306
|
+
let node = matched.captures()[*index].node;
|
|
307
|
+
assert_eq!(
|
|
308
|
+
node.child_count(),
|
|
309
|
+
0,
|
|
310
|
+
"{name}: {} {:?} must be a leaf",
|
|
311
|
+
node.kind(),
|
|
312
|
+
node.byte_range(),
|
|
313
|
+
);
|
|
314
|
+
covered[node.byte_range()].fill(true);
|
|
315
|
+
}
|
|
316
|
+
for range in ranges {
|
|
317
|
+
assert!(covered[range].iter().all(|byte| *byte), "{name}");
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
177
321
|
fn collect_leaves<'tree>(node: Node<'tree>, leaves: &mut Vec<Node<'tree>>) {
|
|
178
322
|
if node.child_count() == 0 {
|
|
179
323
|
leaves.push(node);
|
|
@@ -187,94 +331,374 @@ mod tests {
|
|
|
187
331
|
#[test]
|
|
188
332
|
fn ere_leaves_partition_complete_source_spellings() {
|
|
189
333
|
for case in ERE_CASES {
|
|
190
|
-
let tree =
|
|
334
|
+
let tree = parse(&format!("{}\n", case.source), case.name);
|
|
191
335
|
let ere = tree
|
|
192
336
|
.root_node()
|
|
193
337
|
.descendant_for_byte_range(0, case.source.len())
|
|
194
338
|
.expect("ERE must span the entire fixture");
|
|
195
339
|
assert_eq!(ere.kind(), "ere", "{}", case.name);
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
340
|
+
assert_leaf_partition(ere, case.leaves, case.name);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
#[test]
|
|
345
|
+
fn fields_belong_to_direct_children() {
|
|
346
|
+
let language: tree_sitter::Language = super::LANGUAGE.into();
|
|
347
|
+
let fields: Vec<_> = (1..=language.field_count())
|
|
348
|
+
.map(|id| {
|
|
349
|
+
language
|
|
350
|
+
.field_name_for_id(id as u16)
|
|
351
|
+
.expect("declared field must have a name")
|
|
352
|
+
})
|
|
353
|
+
.collect();
|
|
354
|
+
for (name, source) in [
|
|
355
|
+
("unterminated action", "{}"),
|
|
356
|
+
("unterminated pattern", "a"),
|
|
357
|
+
("unterminated pattern and action", "a {}"),
|
|
358
|
+
("unterminated function", "function f(a,b) {}"),
|
|
359
|
+
("terminated item", "{}\n"),
|
|
360
|
+
("leading newline", "\n{}\n"),
|
|
361
|
+
("terminated and unterminated items", "BEGIN {}\nEND {}"),
|
|
362
|
+
(
|
|
363
|
+
"function name and parameter boundaries",
|
|
364
|
+
"function f\\\n(a,\\\nb) {}",
|
|
365
|
+
),
|
|
366
|
+
(
|
|
367
|
+
"nested statements and expressions",
|
|
368
|
+
"BEGIN { if (a) print a ? b : c > d; else for (i in a) { getline a[i] < file } }\n",
|
|
369
|
+
),
|
|
370
|
+
(
|
|
371
|
+
"shared direct input and pipe aliases",
|
|
372
|
+
"{print $-getline target < (a | getline); ($+$a=b) | getline target}\n",
|
|
373
|
+
),
|
|
374
|
+
("nested field after arithmetic unary", "{$+$a}"),
|
|
375
|
+
("nested field after logical unary in print", "{print $!$a}"),
|
|
376
|
+
(
|
|
377
|
+
"postfix field and getline target aliases",
|
|
378
|
+
"{$-x++; $(-x)++; $$x++; $getline x++; $getline $-x++; $getline $(x)++}\n",
|
|
379
|
+
),
|
|
380
|
+
("group alternation", "/(a|b)c/"),
|
|
381
|
+
("group concatenation", "/(ab)c/"),
|
|
382
|
+
("nested group repetition", "/((a|b)+?c)d/"),
|
|
383
|
+
("ERE boundary continuation", "/(a|b)/\\\n&& /c/"),
|
|
384
|
+
("ungrouped expression", "/a|bc+/"),
|
|
385
|
+
] {
|
|
386
|
+
let tree = parse(source, name);
|
|
387
|
+
let mut pending = vec![tree.root_node()];
|
|
388
|
+
while let Some(node) = pending.pop() {
|
|
389
|
+
for field in &fields {
|
|
390
|
+
if let Some(child) = node.child_by_field_name(field) {
|
|
391
|
+
assert_eq!(
|
|
392
|
+
child.parent(),
|
|
393
|
+
Some(node),
|
|
394
|
+
"{name}: {}.{field} must name a direct child",
|
|
395
|
+
node.kind(),
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
pending.extend(node.children(&mut node.walk()));
|
|
208
400
|
}
|
|
209
|
-
assert_eq!(end, case.source.len(), "{}", case.name);
|
|
210
401
|
}
|
|
211
402
|
}
|
|
212
403
|
|
|
213
404
|
#[test]
|
|
214
|
-
fn
|
|
405
|
+
fn unary_field_assignment_preserves_target_and_right_association() {
|
|
406
|
+
for (name, target, operand) in [
|
|
407
|
+
("positive name", "$+a", "+a"),
|
|
408
|
+
("negative name", "$-a", "-a"),
|
|
409
|
+
("negated array element", "$!a[b]", "!a[b]"),
|
|
410
|
+
("right associative exponentiation", "$-a^b^c", "-a^b^c"),
|
|
411
|
+
("nested field reference", "$+$a", "+$a"),
|
|
412
|
+
("postfix update inside unary operand", "$+a++", "+a++"),
|
|
413
|
+
("unary operand boundary", "$-\\\nname", "-\\\nname"),
|
|
414
|
+
] {
|
|
415
|
+
for operator in ["=", "+=", "-=", "*=", "/=", "%=", "^="] {
|
|
416
|
+
for (prefix, kind) in
|
|
417
|
+
[("{", "non_unary_expr"), ("{print ", "non_unary_print_expr")]
|
|
418
|
+
{
|
|
419
|
+
let source = format!("{prefix}{target}{operator}c=d}}\n");
|
|
420
|
+
let label = format!("{name}: {source:?}");
|
|
421
|
+
let tree = parse(&source, &label);
|
|
422
|
+
let assignment = tree
|
|
423
|
+
.root_node()
|
|
424
|
+
.descendant_for_byte_range(prefix.len(), source.len() - 2)
|
|
425
|
+
.unwrap();
|
|
426
|
+
assert_eq!(assignment.kind(), kind, "{label}");
|
|
427
|
+
let left = assignment.child_by_field_name("left").unwrap();
|
|
428
|
+
assert_eq!(left.kind(), "lvalue", "{label}");
|
|
429
|
+
assert_eq!(
|
|
430
|
+
left.utf8_text(source.as_bytes()).unwrap(),
|
|
431
|
+
target,
|
|
432
|
+
"{label}"
|
|
433
|
+
);
|
|
434
|
+
for (node, field, expected) in [
|
|
435
|
+
(assignment, "operator", operator),
|
|
436
|
+
(left, "operator", "$"),
|
|
437
|
+
(left, "operand", operand),
|
|
438
|
+
(assignment, "right", "c=d"),
|
|
439
|
+
] {
|
|
440
|
+
let child = node.child_by_field_name(field).unwrap();
|
|
441
|
+
assert_eq!(child.parent(), Some(node), "{label}: {field}");
|
|
442
|
+
assert_eq!(
|
|
443
|
+
child.utf8_text(source.as_bytes()).unwrap(),
|
|
444
|
+
expected,
|
|
445
|
+
"{label}: {field}",
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
let right = assignment.child_by_field_name("right").unwrap();
|
|
449
|
+
let nested = right.named_child(0).unwrap();
|
|
450
|
+
for (field, expected) in
|
|
451
|
+
[("left", "c"), ("operator", "="), ("right", "d")]
|
|
452
|
+
{
|
|
453
|
+
assert_eq!(
|
|
454
|
+
nested
|
|
455
|
+
.child_by_field_name(field)
|
|
456
|
+
.unwrap()
|
|
457
|
+
.utf8_text(source.as_bytes())
|
|
458
|
+
.unwrap(),
|
|
459
|
+
expected,
|
|
460
|
+
"{label}: nested {field}",
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
#[test]
|
|
469
|
+
fn unary_field_assignment_keeps_parenthesized_pipe_ownership() {
|
|
470
|
+
for (name, source, pipe_source) in [
|
|
471
|
+
(
|
|
472
|
+
"assignment supplies pipe",
|
|
473
|
+
"{($+a=b) | getline}\n",
|
|
474
|
+
"($+a=b)",
|
|
475
|
+
),
|
|
476
|
+
("pipe supplies assignment", "{$+a=(b | getline)}\n", "b"),
|
|
477
|
+
] {
|
|
478
|
+
let tree = parse(source, name);
|
|
479
|
+
let mut pending = vec![tree.root_node()];
|
|
480
|
+
let mut fields = Vec::new();
|
|
481
|
+
let mut sources = Vec::new();
|
|
482
|
+
while let Some(node) = pending.pop() {
|
|
483
|
+
if node.kind() == "lvalue"
|
|
484
|
+
&& node.child_by_field_name("operator").is_some()
|
|
485
|
+
{
|
|
486
|
+
fields.push(node.utf8_text(source.as_bytes()).unwrap());
|
|
487
|
+
}
|
|
488
|
+
if let Some(child) = node.child_by_field_name("source") {
|
|
489
|
+
sources.push(child.utf8_text(source.as_bytes()).unwrap());
|
|
490
|
+
}
|
|
491
|
+
pending.extend(node.children(&mut node.walk()));
|
|
492
|
+
}
|
|
493
|
+
assert_eq!(fields, ["$+a"], "{name}");
|
|
494
|
+
assert_eq!(sources, [pipe_source], "{name}");
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
#[test]
|
|
499
|
+
fn nested_unary_fields_complete_before_the_outer_assignment() {
|
|
500
|
+
for (name, atom, operator, right) in [
|
|
501
|
+
("name", "a", "=", "b"),
|
|
502
|
+
("number", "1", "+=", "b=c"),
|
|
503
|
+
("parenthesized pipe", "(cmd | getline)", "=", "b"),
|
|
504
|
+
("call argument pipe", "f(cmd | getline)", "=", "b"),
|
|
505
|
+
("subscript pipe", "a[cmd | getline]", "=", "b"),
|
|
506
|
+
] {
|
|
507
|
+
for depth in [10, 64] {
|
|
508
|
+
for prefix in ["{", "{print "] {
|
|
509
|
+
let target = format!("{}${atom}", "$+".repeat(depth));
|
|
510
|
+
let source = format!("{prefix}{target}{operator}{right}}}\n");
|
|
511
|
+
let label = format!("{name}, depth {depth}, {prefix}");
|
|
512
|
+
let tree = parse(&source, &label);
|
|
513
|
+
let assignment = tree
|
|
514
|
+
.root_node()
|
|
515
|
+
.descendant_for_byte_range(prefix.len(), source.len() - 2)
|
|
516
|
+
.unwrap();
|
|
517
|
+
for (field, expected) in [
|
|
518
|
+
("left", target.as_str()),
|
|
519
|
+
("operator", operator),
|
|
520
|
+
("right", right),
|
|
521
|
+
] {
|
|
522
|
+
assert_eq!(
|
|
523
|
+
assignment
|
|
524
|
+
.child_by_field_name(field)
|
|
525
|
+
.unwrap()
|
|
526
|
+
.utf8_text(source.as_bytes())
|
|
527
|
+
.unwrap(),
|
|
528
|
+
expected,
|
|
529
|
+
"{label}: {field}",
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
let mut lvalue = assignment.child_by_field_name("left").unwrap();
|
|
533
|
+
for level in 0..=depth {
|
|
534
|
+
assert_eq!(lvalue.kind(), "lvalue", "{label}: level {level}");
|
|
535
|
+
assert_eq!(lvalue.start_byte(), prefix.len() + level * 2);
|
|
536
|
+
assert_eq!(lvalue.end_byte(), prefix.len() + target.len());
|
|
537
|
+
assert_eq!(
|
|
538
|
+
lvalue.child_by_field_name("operator").unwrap().kind(),
|
|
539
|
+
"$"
|
|
540
|
+
);
|
|
541
|
+
let operand = lvalue.child_by_field_name("operand").unwrap();
|
|
542
|
+
let expression = operand.named_child(0).unwrap();
|
|
543
|
+
if level == depth {
|
|
544
|
+
assert_eq!(
|
|
545
|
+
operand.utf8_text(source.as_bytes()).unwrap(),
|
|
546
|
+
atom,
|
|
547
|
+
"{label}"
|
|
548
|
+
);
|
|
549
|
+
assert_eq!(expression.kind(), "non_unary_expr", "{label}");
|
|
550
|
+
} else {
|
|
551
|
+
assert_eq!(expression.kind(), "unary_expr", "{label}");
|
|
552
|
+
assert_eq!(
|
|
553
|
+
expression.child_by_field_name("operator").unwrap().kind(),
|
|
554
|
+
"+"
|
|
555
|
+
);
|
|
556
|
+
lvalue = expression
|
|
557
|
+
.child_by_field_name("operand")
|
|
558
|
+
.unwrap()
|
|
559
|
+
.named_child(0)
|
|
560
|
+
.unwrap()
|
|
561
|
+
.named_child(0)
|
|
562
|
+
.unwrap();
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
#[test]
|
|
571
|
+
fn highlights_capture_only_leaves_and_cover_ere_and_token_spellings() {
|
|
215
572
|
let query = Query::new(&super::LANGUAGE.into(), super::HIGHLIGHTS_QUERY)
|
|
216
573
|
.expect("highlight query must compile");
|
|
217
|
-
let mut cursor = QueryCursor::new();
|
|
218
574
|
for case in ERE_CASES {
|
|
219
|
-
let tree = parse_ere(case);
|
|
220
575
|
let source = format!("{}\n", case.source);
|
|
221
|
-
let
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
576
|
+
let tree = parse(&source, case.name);
|
|
577
|
+
assert_leaf_highlights(
|
|
578
|
+
&source,
|
|
579
|
+
&tree,
|
|
580
|
+
std::iter::once(0..case.source.len()),
|
|
581
|
+
case.name,
|
|
582
|
+
&query,
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
for case in LEAF_CASES {
|
|
586
|
+
let tree = parse(case.source, case.name);
|
|
587
|
+
assert_leaf_highlights(
|
|
588
|
+
case.source,
|
|
589
|
+
&tree,
|
|
590
|
+
std::iter::once(case.range.clone()),
|
|
591
|
+
case.name,
|
|
592
|
+
&query,
|
|
593
|
+
);
|
|
237
594
|
}
|
|
238
595
|
}
|
|
239
596
|
|
|
240
597
|
#[test]
|
|
241
|
-
fn
|
|
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
|
-
|
|
598
|
+
fn lexical_tokens_keep_complete_leaf_ranges() {
|
|
599
|
+
for case in LEAF_CASES {
|
|
600
|
+
let tree = parse(case.source, case.name);
|
|
601
|
+
let node = tree
|
|
602
|
+
.root_node()
|
|
603
|
+
.descendant_for_byte_range(case.range.start, case.range.end)
|
|
604
|
+
.expect("lexical node must span the expected range");
|
|
605
|
+
assert_eq!(node.kind(), case.kind, "{}", case.name);
|
|
606
|
+
assert_eq!(node.byte_range(), case.range, "{}", case.name);
|
|
607
|
+
assert_eq!(node.child_count(), 0, "{}", case.name);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
#[test]
|
|
612
|
+
fn continuation_markers_are_anonymous_and_exclude_newlines() {
|
|
613
|
+
struct Case {
|
|
614
|
+
name: &'static str,
|
|
615
|
+
source: &'static str,
|
|
616
|
+
markers: &'static [(usize, usize)],
|
|
617
|
+
}
|
|
618
|
+
assert!(!super::NODE_TYPES.contains("\"line_continuation\""));
|
|
619
|
+
let query = Query::new(&super::LANGUAGE.into(), super::HIGHLIGHTS_QUERY)
|
|
620
|
+
.expect("highlight query must compile");
|
|
621
|
+
for case in [
|
|
622
|
+
Case {
|
|
623
|
+
name: "a continuation alone creates no newline token",
|
|
624
|
+
source: "\\\n",
|
|
625
|
+
markers: &[(0, 1)],
|
|
626
|
+
},
|
|
627
|
+
Case {
|
|
628
|
+
name: "source and AWK expression boundaries",
|
|
629
|
+
source: "\\\n{a +\\\nb}\\\n",
|
|
630
|
+
markers: &[(0, 1), (6, 7), (10, 11)],
|
|
631
|
+
},
|
|
632
|
+
Case {
|
|
633
|
+
name: "consecutive continuations separate two names",
|
|
634
|
+
source: "fo\\\n\\\no\n",
|
|
635
|
+
markers: &[(2, 3), (4, 5)],
|
|
636
|
+
},
|
|
637
|
+
Case {
|
|
638
|
+
name: "a continuation separates two numbers",
|
|
639
|
+
source: "1\\\n2\n",
|
|
640
|
+
markers: &[(1, 2)],
|
|
641
|
+
},
|
|
642
|
+
Case {
|
|
643
|
+
name: "a continuation separates a definition name and parenthesis",
|
|
644
|
+
source: "function f\\\n(){}",
|
|
645
|
+
markers: &[(10, 11)],
|
|
646
|
+
},
|
|
647
|
+
Case {
|
|
648
|
+
name: "a continuation follows a parameter separator",
|
|
649
|
+
source: "function f(a,\\\nb){}",
|
|
650
|
+
markers: &[(13, 14)],
|
|
651
|
+
},
|
|
652
|
+
Case {
|
|
653
|
+
name: "a continuation separates complete string constants",
|
|
654
|
+
source: "\"a\"\\\n\"b\"\n",
|
|
655
|
+
markers: &[(3, 4)],
|
|
656
|
+
},
|
|
657
|
+
Case {
|
|
658
|
+
name: "a continuation follows a complete static ERE",
|
|
659
|
+
source: "/a/\\\n&& /b/\n",
|
|
660
|
+
markers: &[(3, 4)],
|
|
661
|
+
},
|
|
662
|
+
Case {
|
|
663
|
+
name: "string and ERE escapes remain distinct from continuations",
|
|
664
|
+
source: "\"\\n\"\\\n&& /\\n/\n",
|
|
665
|
+
markers: &[(4, 5)],
|
|
666
|
+
},
|
|
667
|
+
Case {
|
|
668
|
+
name: "a comment backslash does not become a continuation marker",
|
|
669
|
+
source: "# \\\n/a/\\\n&& /b/\n",
|
|
670
|
+
markers: &[(7, 8)],
|
|
671
|
+
},
|
|
672
|
+
] {
|
|
673
|
+
let tree = parse(case.source, case.name);
|
|
674
|
+
let mut leaves = Vec::new();
|
|
675
|
+
collect_leaves(tree.root_node(), &mut leaves);
|
|
676
|
+
let actual: Vec<_> = leaves
|
|
677
|
+
.iter()
|
|
678
|
+
.filter(|node| node.kind() == "\\")
|
|
679
|
+
.map(|node| {
|
|
680
|
+
assert!(!node.is_named(), "{}", case.name);
|
|
681
|
+
assert_eq!(node.utf8_text(case.source.as_bytes()).unwrap(), "\\");
|
|
682
|
+
(node.start_byte(), node.end_byte())
|
|
683
|
+
})
|
|
684
|
+
.collect();
|
|
685
|
+
assert_eq!(actual, case.markers, "{}", case.name);
|
|
686
|
+
for (_, end) in case.markers {
|
|
687
|
+
assert_eq!(case.source.as_bytes()[*end], b'\n');
|
|
688
|
+
assert!(
|
|
689
|
+
leaves.iter().all(|leaf| !leaf.byte_range().contains(end)),
|
|
690
|
+
"{}: a continuation newline must have no leaf",
|
|
691
|
+
case.name,
|
|
692
|
+
);
|
|
693
|
+
}
|
|
694
|
+
assert_leaf_highlights(
|
|
695
|
+
case.source,
|
|
696
|
+
&tree,
|
|
697
|
+
case.markers.iter().map(|(start, end)| *start..*end),
|
|
698
|
+
case.name,
|
|
699
|
+
&query,
|
|
700
|
+
);
|
|
701
|
+
}
|
|
278
702
|
}
|
|
279
703
|
|
|
280
704
|
#[test]
|