@konomanoasa/tree-sitter-posix-awk 0.18.0 → 0.20.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.
@@ -8,13 +8,11 @@ use tree_sitter::{
8
8
  struct EreCase {
9
9
  name: &'static str,
10
10
  source: &'static str,
11
- leaves: &'static [(&'static str, usize, usize)],
12
11
  }
13
12
 
14
13
  struct LeafCase {
15
14
  name: &'static str,
16
15
  source: &'static str,
17
- kind: &'static str,
18
16
  range: Range<usize>,
19
17
  }
20
18
 
@@ -22,216 +20,107 @@ const LEAF_CASES: &[LeafCase] = &[
22
20
  LeafCase {
23
21
  name: "a complete name remains one leaf",
24
22
  source: "foo\n",
25
- kind: "name",
26
23
  range: 0..3,
27
24
  },
28
25
  LeafCase {
29
26
  name: "the name before consecutive continuations remains separate",
30
27
  source: "fo\\\n\\\no\n",
31
- kind: "name",
32
28
  range: 0..2,
33
29
  },
34
30
  LeafCase {
35
31
  name: "the name after consecutive continuations remains separate",
36
32
  source: "fo\\\n\\\no\n",
37
- kind: "name",
38
33
  range: 6..7,
39
34
  },
40
35
  LeafCase {
41
36
  name: "a keyword excludes its following continuation",
42
37
  source: "BEGIN\\\n{}\n",
43
- kind: "begin_keyword",
44
38
  range: 0..5,
45
39
  },
46
40
  LeafCase {
47
41
  name: "a builtin excludes its following continuation",
48
42
  source: "{length\\\n(x)}\n",
49
- kind: "builtin_func_name",
50
43
  range: 1..7,
51
44
  },
52
45
  LeafCase {
53
46
  name: "a function name excludes its adjacent parenthesis",
54
47
  source: "{foo(x)}\n",
55
- kind: "func_name",
56
48
  range: 1..4,
57
49
  },
58
50
  LeafCase {
59
51
  name: "a continuation before the parenthesis makes a variable name",
60
52
  source: "{foo\\\n(x)}\n",
61
- kind: "name",
62
53
  range: 1..4,
63
54
  },
64
55
  LeafCase {
65
56
  name: "a decimal constant includes its exponent and suffix",
66
57
  source: "1.2e+3F\n",
67
- kind: "number",
68
58
  range: 0..7,
69
59
  },
70
60
  LeafCase {
71
61
  name: "a logical operator excludes its following continuation",
72
62
  source: "a||\\\nb\n",
73
- kind: "or",
74
63
  range: 1..3,
75
64
  },
76
65
  LeafCase {
77
66
  name: "Unicode string content remains one leaf",
78
67
  source: "\"é😀\"\n",
79
- kind: "string_content",
80
68
  range: 1..7,
81
69
  },
82
70
  LeafCase {
83
71
  name: "an octal escape excludes the following string character",
84
72
  source: "\"\\123x\"\n",
85
- kind: "escape_sequence",
86
73
  range: 1..5,
87
74
  },
88
75
  LeafCase {
89
76
  name: "an escaped slash excludes the closing delimiter",
90
77
  source: "/\\//\n",
91
- kind: "escaped_delimiter",
92
78
  range: 1..3,
93
79
  },
94
80
  LeafCase {
95
81
  name: "an ERE class name excludes compound delimiters",
96
82
  source: "/[[:alpha:]]/\n",
97
- kind: "class_name",
98
83
  range: 4..9,
99
84
  },
100
85
  LeafCase {
101
86
  name: "an ERE repetition count remains one leaf",
102
87
  source: "/a{12}/\n",
103
- kind: "dup_count",
104
88
  range: 3..5,
105
89
  },
106
90
  ];
107
91
 
108
92
  const ERE_CASES: &[EreCase] = &[
93
+ EreCase {
94
+ name: "an empty ERE retains only its delimiter ranges",
95
+ source: "//",
96
+ },
97
+ EreCase {
98
+ name: "empty groups retain delimiters within nested alternation and repetition",
99
+ source: "/(()|())*?/",
100
+ },
109
101
  EreCase {
110
102
  name: "ordinary characters and complete escapes retain their source ranges",
111
103
  source: r"/a é)}.\n\/\\\141\q(a*?)/",
112
- leaves: &[
113
- ("/", 0, 1),
114
- ("ordinary_character_content", 1, 2),
115
- ("ordinary_character_content", 2, 3),
116
- ("ordinary_character_content", 3, 5),
117
- (")", 5, 6),
118
- ("}", 6, 7),
119
- (".", 7, 8),
120
- ("escape_sequence", 8, 10),
121
- ("escaped_delimiter", 10, 12),
122
- ("escape_sequence", 12, 14),
123
- ("escape_sequence", 14, 18),
124
- ("escape_sequence", 18, 20),
125
- ("(", 20, 21),
126
- ("ordinary_character_content", 21, 22),
127
- ("*", 22, 23),
128
- ("?", 23, 24),
129
- (")", 24, 25),
130
- ("/", 25, 26),
131
- ],
132
104
  },
133
105
  EreCase {
134
106
  name: "a blank before an escape remains content in every literal position",
135
107
  source: r"/ \.[ \]][[. \..]]/",
136
- leaves: &[
137
- ("/", 0, 1),
138
- ("ordinary_character_content", 1, 2),
139
- ("escape_sequence", 2, 4),
140
- ("[", 4, 5),
141
- ("collating_element_content", 5, 6),
142
- ("escape_sequence", 6, 8),
143
- ("]", 8, 9),
144
- ("[", 9, 10),
145
- ("[", 10, 11),
146
- (".", 11, 12),
147
- ("collating_element_content", 12, 13),
148
- ("escape_sequence", 13, 15),
149
- (".", 15, 16),
150
- ("]", 16, 17),
151
- ("]", 17, 18),
152
- ("/", 18, 19),
153
- ],
154
108
  },
155
109
  EreCase {
156
110
  name: "collating forms separate raw fragments from adjacent escapes",
157
111
  source: r"/[[.a\nb.][=é\t犬=][.\/\141\q.]]/",
158
- leaves: &[
159
- ("/", 0, 1),
160
- ("[", 1, 2),
161
- ("[", 2, 3),
162
- (".", 3, 4),
163
- ("collating_element_content", 4, 5),
164
- ("escape_sequence", 5, 7),
165
- ("collating_element_content", 7, 8),
166
- (".", 8, 9),
167
- ("]", 9, 10),
168
- ("[", 10, 11),
169
- ("=", 11, 12),
170
- ("collating_element_content", 12, 14),
171
- ("escape_sequence", 14, 16),
172
- ("collating_element_content", 16, 19),
173
- ("=", 19, 20),
174
- ("]", 20, 21),
175
- ("[", 21, 22),
176
- (".", 22, 23),
177
- ("escaped_delimiter", 23, 25),
178
- ("escape_sequence", 25, 29),
179
- ("escape_sequence", 29, 31),
180
- (".", 31, 32),
181
- ("]", 32, 33),
182
- ("]", 33, 34),
183
- ("/", 34, 35),
184
- ],
185
112
  },
186
113
  EreCase {
187
- name: "compound punctuation, spaces and Unicode retain individual leaves",
114
+ name: "raw compound punctuation, spaces and Unicode share one payload leaf",
188
115
  source: r"/[[. a-]🙂.]]/",
189
- leaves: &[
190
- ("/", 0, 1),
191
- ("[", 1, 2),
192
- ("[", 2, 3),
193
- (".", 3, 4),
194
- ("collating_element_content", 4, 5),
195
- ("collating_element_content", 5, 6),
196
- ("collating_element_content", 6, 7),
197
- ("collating_element_content", 7, 8),
198
- ("collating_element_content", 8, 12),
199
- (".", 12, 13),
200
- ("]", 13, 14),
201
- ("]", 14, 15),
202
- ("/", 15, 16),
203
- ],
204
116
  },
205
117
  EreCase {
206
118
  name: "bracket literals distinguish raw brackets and a trailing hyphen",
207
119
  source: r"/[]a[\/\n-]/",
208
- leaves: &[
209
- ("/", 0, 1),
210
- ("[", 1, 2),
211
- ("collating_element_content", 2, 3),
212
- ("collating_element_content", 3, 4),
213
- ("collating_element_content", 4, 5),
214
- ("escaped_delimiter", 5, 7),
215
- ("escape_sequence", 7, 9),
216
- ("-", 9, 10),
217
- ("]", 10, 11),
218
- ("/", 11, 12),
219
- ],
220
120
  },
221
121
  EreCase {
222
- name: "range and list hyphens retain anonymous leaves",
122
+ name: "a leading hyphen is a named endpoint before range and list hyphens",
223
123
  source: r"/[--ac-]/",
224
- leaves: &[
225
- ("/", 0, 1),
226
- ("[", 1, 2),
227
- ("-", 2, 3),
228
- ("-", 3, 4),
229
- ("collating_element_content", 4, 5),
230
- ("collating_element_content", 5, 6),
231
- ("-", 6, 7),
232
- ("]", 7, 8),
233
- ("/", 8, 9),
234
- ],
235
124
  },
236
125
  ];
237
126
 
@@ -279,27 +168,6 @@ fn edit(
279
168
  });
280
169
  }
281
170
 
282
- fn assert_leaf_partition(
283
- node: Node<'_>,
284
- expected: &[(&str, usize, usize)],
285
- name: &str,
286
- ) {
287
- let mut leaves = Vec::new();
288
- collect_leaves(node, &mut leaves);
289
- let actual: Vec<_> = leaves
290
- .iter()
291
- .map(|leaf| (leaf.kind(), leaf.start_byte(), leaf.end_byte()))
292
- .collect();
293
- assert_eq!(actual, expected, "{name}");
294
- let mut end = node.start_byte();
295
- for leaf in leaves {
296
- assert_eq!(leaf.start_byte(), end, "{name}");
297
- assert!(leaf.end_byte() > end, "{name}");
298
- end = leaf.end_byte();
299
- }
300
- assert_eq!(end, node.end_byte(), "{name}");
301
- }
302
-
303
171
  fn assert_leaf_highlights(
304
172
  source: &str,
305
173
  tree: &Tree,
@@ -360,6 +228,35 @@ fn assert_same_cst(actual: Node<'_>, expected: Node<'_>, context: &str) {
360
228
  }
361
229
  }
362
230
 
231
+ fn assert_changed_scopes(old: &Tree, new: &Tree, context: &str) {
232
+ let scopes = |tree: &Tree| {
233
+ let mut scopes = vec![Vec::new(); tree.root_node().end_byte()];
234
+ let mut pending = vec![tree.root_node()];
235
+ while let Some(node) = pending.pop() {
236
+ for scope in &mut scopes[node.byte_range()] {
237
+ scope.push(node.kind_id());
238
+ }
239
+ for index in (0..node.child_count()).rev() {
240
+ pending.push(node.child(index).unwrap());
241
+ }
242
+ }
243
+ scopes
244
+ };
245
+ let old_scopes = scopes(old);
246
+ let new_scopes = scopes(new);
247
+ let ranges: Vec<_> = old.changed_ranges(new).collect();
248
+ for (byte, scope) in new_scopes.iter().enumerate() {
249
+ if old_scopes.get(byte) != Some(scope) {
250
+ assert!(
251
+ ranges
252
+ .iter()
253
+ .any(|range| range.start_byte <= byte && byte < range.end_byte),
254
+ "{context}: unreported scope change at byte {byte}: {ranges:?}",
255
+ );
256
+ }
257
+ }
258
+ }
259
+
363
260
  fn assert_fresh_cst(
364
261
  reused: &mut Parser,
365
262
  source: &[u8],
@@ -393,15 +290,136 @@ fn parses_valid_source() {
393
290
  }
394
291
 
395
292
  #[test]
396
- fn ere_leaves_partition_complete_source_spellings() {
397
- for case in ERE_CASES {
398
- let tree = parse(&format!("{}\n", case.source), case.name);
399
- let ere = tree
293
+ fn empty_ere_and_groups_own_only_opening_and_closing_delimiters() {
294
+ for (kind, name, source, range) in [
295
+ ("ere", "pattern at EOF", "//", 0..2),
296
+ ("ere", "print expression", "BEGIN { print // }", 14..16),
297
+ ("ere", "match operand", "$0 ~ //", 5..7),
298
+ (
299
+ "ere",
300
+ "builtin argument",
301
+ "BEGIN { sub(//, \"x\") }",
302
+ 12..14,
303
+ ),
304
+ ("ere", "getline source", "{ getline < // }", 12..14),
305
+ ("ere", "division operand", "BEGIN { print 1 / // }", 18..20),
306
+ (
307
+ "ere",
308
+ "following division",
309
+ "BEGIN { print // / 2 }",
310
+ 14..16,
311
+ ),
312
+ ("ere_expression", "empty group", "/()/", 1..3),
313
+ ("ere_expression", "nested group", "/(())/", 2..4),
314
+ ("ere_expression", "concatenated group", "/a()b/", 2..4),
315
+ ("ere_expression", "left alternation group", "/()|a/", 1..3),
316
+ (
317
+ "ere_expression",
318
+ "right alternation group",
319
+ "/(a|())/",
320
+ 4..6,
321
+ ),
322
+ (
323
+ "ere_expression",
324
+ "group before a literal closer",
325
+ "/())/",
326
+ 1..3,
327
+ ),
328
+ ] {
329
+ let tree = parse(source, name);
330
+ let owner = tree
400
331
  .root_node()
401
- .descendant_for_byte_range(0, case.source.len())
332
+ .descendant_for_byte_range(range.start, range.end)
402
333
  .unwrap();
403
- assert_eq!(ere.kind(), "ere", "{}", case.name);
404
- assert_leaf_partition(ere, case.leaves, case.name);
334
+ assert_eq!(owner.kind(), kind, "{name}");
335
+ assert_eq!(owner.byte_range(), range, "{name}");
336
+ assert_eq!(owner.child_count(), 2, "{name}");
337
+ assert_eq!(owner.named_child_count(), 0, "{name}");
338
+ assert!(owner.child_by_field_name("expression").is_none(), "{name}");
339
+ let (opening, closing) = if kind == "ere" {
340
+ ("/", "/")
341
+ } else {
342
+ ("(", ")")
343
+ };
344
+ for (field, token, expected) in [
345
+ ("opening", opening, range.start..range.start + 1),
346
+ ("closing", closing, range.end - 1..range.end),
347
+ ] {
348
+ let delimiter = owner.child_by_field_name(field).unwrap();
349
+ assert_eq!(delimiter.kind(), token, "{name}: {field}");
350
+ assert_eq!(delimiter.byte_range(), expected, "{name}: {field}");
351
+ assert!(!delimiter.is_named(), "{name}: {field}");
352
+ }
353
+ }
354
+ }
355
+
356
+ #[test]
357
+ fn ere_fields_select_direct_source_children_in_flat_expressions() {
358
+ let source = r"/(a|bc){2,3}?d{4}e{5,}()[[:alpha:][.ch.][=é=][.a\nb.]]/";
359
+ let tree = parse(source, "shared ERE fields");
360
+ for (kind, text, expected) in [
361
+ (
362
+ "ere_expression",
363
+ "(a|bc){2,3}?",
364
+ vec![("opening", "("), ("expression", "a|bc"), ("closing", ")")],
365
+ ),
366
+ (
367
+ "ere_expression",
368
+ "()",
369
+ vec![("opening", "("), ("closing", ")")],
370
+ ),
371
+ (
372
+ "ere_dupl_symbol",
373
+ "{2,3}?",
374
+ vec![("minimum", "2"), ("maximum", "3"), ("modifier", "?")],
375
+ ),
376
+ ("ere_dupl_symbol", "{4}", vec![("minimum", "4")]),
377
+ ("ere_dupl_symbol", "{5,}", vec![("minimum", "5")]),
378
+ ("character_class", "[:alpha:]", vec![("name", "alpha")]),
379
+ ("collating_symbol", "[.ch.]", vec![("element", "ch")]),
380
+ ("equivalence_class", "[=é=]", vec![("element", "é")]),
381
+ ("collating_symbol", r"[.a\nb.]", vec![("element", r"a\nb")]),
382
+ ] {
383
+ let start = source.find(text).unwrap();
384
+ let node = tree
385
+ .root_node()
386
+ .descendant_for_byte_range(start, start + text.len())
387
+ .unwrap();
388
+ assert_eq!(node.kind(), kind, "{text}");
389
+ let mut cursor = node.walk();
390
+ let actual: Vec<_> = node
391
+ .children(&mut cursor)
392
+ .enumerate()
393
+ .filter_map(|(index, child)| {
394
+ node
395
+ .field_name_for_child(index as u32)
396
+ .map(|field| (field, child.utf8_text(source.as_bytes()).unwrap()))
397
+ })
398
+ .collect();
399
+ assert_eq!(actual, expected, "{text}");
400
+ for (field, value) in expected {
401
+ let child = node.child_by_field_name(field).unwrap();
402
+ assert_eq!(child.parent(), Some(node), "{text}: {field}");
403
+ assert_eq!(
404
+ child.utf8_text(source.as_bytes()).unwrap(),
405
+ value,
406
+ "{text}: {field}"
407
+ );
408
+ }
409
+ }
410
+ let tree = parse("/()*?+?/", "repeated empty group");
411
+ let expression = tree.root_node().descendant_for_byte_range(1, 7).unwrap();
412
+ assert_eq!(expression.kind(), "ere_expression");
413
+ assert!(expression.child_by_field_name("expression").is_none());
414
+ let children: Vec<_> = expression
415
+ .children(&mut expression.walk())
416
+ .map(|node| node.kind())
417
+ .collect();
418
+ assert_eq!(children, ["(", ")", "ere_dupl_symbol", "ere_dupl_symbol"]);
419
+ for symbol in expression.named_children(&mut expression.walk()) {
420
+ let modifier = symbol.child_by_field_name("modifier").unwrap();
421
+ assert_eq!(modifier.kind(), "repetition_modifier");
422
+ assert_eq!(modifier.child_count(), 0);
405
423
  }
406
424
  }
407
425
 
@@ -433,11 +451,28 @@ fn fields_belong_to_direct_children() {
433
451
  ),
434
452
  ("nested field after arithmetic unary", "{$+$a}"),
435
453
  ("nested field after logical unary in print", "{print $!$a}"),
454
+ (
455
+ "ordinary updated fields without getline",
456
+ "{$$x++; $(-x)++; $-x++; ++$!x; $!$a}",
457
+ ),
458
+ (
459
+ "ordinary fields inside operators and arguments",
460
+ "{$a + $b * $!$c; $d ? $e : $f; f($1, ($a,$b) in arr)}",
461
+ ),
462
+ (
463
+ "nested getline targets without a source",
464
+ "{getline $(getline $(-$a)); print $+$a}",
465
+ ),
436
466
  (
437
467
  "postfix field and getline target aliases",
438
468
  "{$-x++; $(-x)++; $$x++; $getline x++; $getline $-x++; $getline $(x)++}\n",
439
469
  ),
440
470
  ("group alternation", "/(a|b)c/"),
471
+ ("ordinary source escapes", r"/a\n\//"),
472
+ (
473
+ "intervals and compound payloads",
474
+ r"/()[[.a.][.ch.][=é=][.a\nb.][:alpha:]]{2,3}?/",
475
+ ),
441
476
  ("group concatenation", "/(ab)c/"),
442
477
  ("nested group repetition", "/((a|b)+?c)d/"),
443
478
  ("ERE boundary continuation", "/(a|b)/\\\n&& /c/"),
@@ -525,36 +560,6 @@ fn unary_field_assignment_preserves_target_and_right_association() {
525
560
  }
526
561
  }
527
562
 
528
- #[test]
529
- fn unary_field_assignment_keeps_parenthesized_pipe_ownership() {
530
- for (name, source, pipe_source) in [
531
- (
532
- "assignment supplies pipe",
533
- "{($+a=b) | getline}\n",
534
- "($+a=b)",
535
- ),
536
- ("pipe supplies assignment", "{$+a=(b | getline)}\n", "b"),
537
- ] {
538
- let tree = parse(source, name);
539
- let mut pending = vec![tree.root_node()];
540
- let mut fields = Vec::new();
541
- let mut sources = Vec::new();
542
- while let Some(node) = pending.pop() {
543
- if node.kind() == "lvalue"
544
- && node.child_by_field_name("operator").is_some()
545
- {
546
- fields.push(node.utf8_text(source.as_bytes()).unwrap());
547
- }
548
- if let Some(child) = node.child_by_field_name("source") {
549
- sources.push(child.utf8_text(source.as_bytes()).unwrap());
550
- }
551
- pending.extend(node.children(&mut node.walk()));
552
- }
553
- assert_eq!(fields, ["$+a"], "{name}");
554
- assert_eq!(sources, [pipe_source], "{name}");
555
- }
556
- }
557
-
558
563
  #[test]
559
564
  fn nested_unary_fields_complete_before_the_outer_assignment() {
560
565
  for (name, atom, operator, right) in [
@@ -655,25 +660,10 @@ fn highlights_capture_only_leaves_and_cover_ere_and_token_spellings() {
655
660
  }
656
661
 
657
662
  #[test]
658
- fn lexical_tokens_keep_complete_leaf_ranges() {
659
- for case in LEAF_CASES {
660
- let tree = parse(case.source, case.name);
661
- let node = tree
662
- .root_node()
663
- .descendant_for_byte_range(case.range.start, case.range.end)
664
- .unwrap();
665
- assert_eq!(node.kind(), case.kind, "{}", case.name);
666
- assert_eq!(node.byte_range(), case.range, "{}", case.name);
667
- assert_eq!(node.child_count(), 0, "{}", case.name);
668
- }
669
- }
670
-
671
- #[test]
672
- fn continuation_markers_are_anonymous_and_exclude_newlines() {
663
+ fn continuation_markers_exclude_newlines_and_receive_leaf_highlights() {
673
664
  struct Case {
674
665
  name: &'static str,
675
666
  source: &'static str,
676
- markers: &'static [(usize, usize)],
677
667
  }
678
668
  assert!(!grammar::NODE_TYPES.contains("\"line_continuation\""));
679
669
  let query =
@@ -682,52 +672,42 @@ fn continuation_markers_are_anonymous_and_exclude_newlines() {
682
672
  Case {
683
673
  name: "a continuation alone creates no newline token",
684
674
  source: "\\\n",
685
- markers: &[(0, 1)],
686
675
  },
687
676
  Case {
688
677
  name: "source and AWK expression boundaries",
689
678
  source: "\\\n{a +\\\nb}\\\n",
690
- markers: &[(0, 1), (6, 7), (10, 11)],
691
679
  },
692
680
  Case {
693
681
  name: "consecutive continuations separate two names",
694
682
  source: "fo\\\n\\\no\n",
695
- markers: &[(2, 3), (4, 5)],
696
683
  },
697
684
  Case {
698
685
  name: "a continuation separates two numbers",
699
686
  source: "1\\\n2\n",
700
- markers: &[(1, 2)],
701
687
  },
702
688
  Case {
703
689
  name: "a continuation separates a definition name and parenthesis",
704
690
  source: "function f\\\n(){}",
705
- markers: &[(10, 11)],
706
691
  },
707
692
  Case {
708
693
  name: "a continuation follows a parameter separator",
709
694
  source: "function f(a,\\\nb){}",
710
- markers: &[(13, 14)],
711
695
  },
712
696
  Case {
713
697
  name: "a continuation separates complete string constants",
714
698
  source: "\"a\"\\\n\"b\"\n",
715
- markers: &[(3, 4)],
716
699
  },
717
700
  Case {
718
701
  name: "a continuation follows a complete static ERE",
719
702
  source: "/a/\\\n&& /b/\n",
720
- markers: &[(3, 4)],
721
703
  },
722
704
  Case {
723
705
  name: "string and ERE escapes remain distinct from continuations",
724
706
  source: "\"\\n\"\\\n&& /\\n/\n",
725
- markers: &[(4, 5)],
726
707
  },
727
708
  Case {
728
709
  name: "a comment backslash does not become a continuation marker",
729
710
  source: "# \\\n/a/\\\n&& /b/\n",
730
- markers: &[(7, 8)],
731
711
  },
732
712
  ] {
733
713
  let tree = parse(case.source, case.name);
@@ -742,8 +722,7 @@ fn continuation_markers_are_anonymous_and_exclude_newlines() {
742
722
  (node.start_byte(), node.end_byte())
743
723
  })
744
724
  .collect();
745
- assert_eq!(actual, case.markers, "{}", case.name);
746
- for (_, end) in case.markers {
725
+ for (_, end) in &actual {
747
726
  assert_eq!(case.source.as_bytes()[*end], b'\n');
748
727
  assert!(
749
728
  leaves.iter().all(|leaf| !leaf.byte_range().contains(end)),
@@ -754,7 +733,7 @@ fn continuation_markers_are_anonymous_and_exclude_newlines() {
754
733
  assert_leaf_highlights(
755
734
  case.source,
756
735
  &tree,
757
- case.markers.iter().map(|(start, end)| *start..*end),
736
+ actual.iter().map(|(start, end)| *start..*end),
758
737
  case.name,
759
738
  &query,
760
739
  );
@@ -1029,9 +1008,54 @@ fn removing_an_invalid_bracket_before_getline_restores_statements() {
1029
1008
  );
1030
1009
  }
1031
1010
 
1011
+ #[test]
1012
+ fn editing_a_field_getline_lookahead_during_recovery_restores_its_target() {
1013
+ let mut parser = parser();
1014
+ let source = "{ getline x $ getline y ++ $ getline z ++ }";
1015
+ let mut text = source.as_bytes().to_vec();
1016
+ let mut tree = parser.parse(&text, None).unwrap();
1017
+ assert!(!tree.root_node().has_error());
1018
+ for (start, deleted, inserted) in [
1019
+ (0, 1, "( "),
1020
+ (25, 3, ""),
1021
+ (3, 0, "++ "),
1022
+ (26, 1, ","),
1023
+ (26, 1, "y"),
1024
+ (3, 3, ""),
1025
+ (25, 0, "++ "),
1026
+ (0, 2, "{"),
1027
+ ] {
1028
+ edit(&mut tree, &mut text, start, deleted, inserted);
1029
+ tree = parser.parse(&text, Some(&tree)).unwrap();
1030
+ }
1031
+ assert_eq!(text, source.as_bytes());
1032
+ let expected = parser.parse(&text, None).unwrap();
1033
+ assert_same_cst(
1034
+ tree.root_node(),
1035
+ expected.root_node(),
1036
+ "repair after the token behind a field getline changed",
1037
+ );
1038
+ }
1039
+
1032
1040
  #[test]
1033
1041
  fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1034
1042
  let seeds = [
1043
+ "{ (getline line) + y++ }",
1044
+ "{ (getline line) < y }",
1045
+ "{ getline line <= y }",
1046
+ "{ f(getline line, ++y) }",
1047
+ "{ a[getline line, i++] }",
1048
+ "{ getline a[i++] }",
1049
+ "{ (getline a) + (getline b) + i++ }",
1050
+ "{ print (getline line) > out }",
1051
+ "{ $getline < (a < getline x++) < y }",
1052
+ "{ $getline < (a < getline x--) < y }",
1053
+ "{ $getline < $getline < getline x++++ < y }",
1054
+ "{ $getline < $getline < $getline < getline x++++++ < y }",
1055
+ "{ getline $getline < (a < getline x++) < y }",
1056
+ "{ print ($getline < (a < getline x++) < y) }",
1057
+ "{ f($getline < (a < getline x++) < y) }",
1058
+ "{ x[$getline < (a < getline x++) < y] }",
1035
1059
  "{ x = getline a + line b++ }",
1036
1060
  "{ a ? b : getline c-- ; (y) ? 1 : 2 }",
1037
1061
  "{ getline x++ }",
@@ -1043,6 +1067,10 @@ fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1043
1067
  "{ getline $getline x < source }",
1044
1068
  "{ print (getline x++ + y) }",
1045
1069
  "{ getline x\\\n++ }",
1070
+ "{ getline $getline < x++++ }",
1071
+ "{ getline $getline < (x | getline) ++ }",
1072
+ "{ getline $getline < (a ? x : (y))++ }",
1073
+ "{ getline $getline < getline $getline < x++++ }",
1046
1074
  ];
1047
1075
  let mut random = 0x79d07b23u32;
1048
1076
  let mut next = || {
@@ -1080,7 +1108,7 @@ fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1080
1108
  };
1081
1109
  steps.push((start, deleted, inserted.clone()));
1082
1110
  edit(&mut tree, &mut text, start, deleted, &inserted);
1083
- tree = parser.parse(&text, Some(&tree)).unwrap();
1111
+ let next = parser.parse(&text, Some(&tree)).unwrap();
1084
1112
  let fresh = parser.parse(&text, None).unwrap();
1085
1113
  let expected = independent.parse(&text, None).unwrap();
1086
1114
  let context = format!(
@@ -1089,8 +1117,12 @@ fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1089
1117
  );
1090
1118
  assert_same_cst(fresh.root_node(), expected.root_node(), &context);
1091
1119
  if !fresh.root_node().has_error() {
1092
- assert_same_cst(tree.root_node(), fresh.root_node(), &context);
1120
+ assert_same_cst(next.root_node(), fresh.root_node(), &context);
1121
+ if !tree.root_node().has_error() {
1122
+ assert_changed_scopes(&tree, &next, &context);
1123
+ }
1093
1124
  }
1125
+ tree = next;
1094
1126
  }
1095
1127
  assert_eq!(
1096
1128
  text,
@@ -1099,3 +1131,289 @@ fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1099
1131
  );
1100
1132
  }
1101
1133
  }
1134
+
1135
+ #[test]
1136
+ fn getline_token_edit_histories_match_fresh_parses() {
1137
+ struct History {
1138
+ source: &'static str,
1139
+ edits: &'static [(usize, usize, &'static str)],
1140
+ }
1141
+ for History { source, edits } in [
1142
+ History {
1143
+ source: "{ getline $ getline < \"f\" < x }",
1144
+ edits: &[(10, 1, ""), (10, 0, "$"), (30, 0, "a[1]")],
1145
+ },
1146
+ History {
1147
+ source: "{ getline x < y ++ z }",
1148
+ edits: &[(10, 1, "getline"), (10, 7, "x")],
1149
+ },
1150
+ History {
1151
+ source: "{ getline < y ++ z }",
1152
+ edits: &[(10, 0, "a[1]")],
1153
+ },
1154
+ History {
1155
+ source: "{ print $ getline x ++ $ getline y ++ }",
1156
+ edits: &[(33, 2, "?"), (33, 1, "y ")],
1157
+ },
1158
+ History {
1159
+ source: "{ getline x + y getline z ++ w }",
1160
+ edits: &[(24, 2, "]"), (24, 1, "z ")],
1161
+ },
1162
+ History {
1163
+ source: "{ $getline < \"f\" ++ | getline }",
1164
+ edits: &[(0, 2, "tline"), (0, 5, "{ "), (20, 2, "$")],
1165
+ },
1166
+ History {
1167
+ source: "{ getline a[1/2] ++ $ getline x ++ }",
1168
+ edits: &[(18, 2, "a[1]"), (32, 0, ") "), (32, 2, "")],
1169
+ },
1170
+ History {
1171
+ source: "{ getline a[ b[i,\nj] ] ++ y -- z }",
1172
+ edits: &[(24, 2, "\"f\""), (29, 2, "]"), (29, 1, "--")],
1173
+ },
1174
+ History {
1175
+ source: "{ getline $1.5 ++ $ getline $1.5 ++ y }",
1176
+ edits: &[(16, 2, "\"f\""), (34, 2, "("), (34, 1, "++")],
1177
+ },
1178
+ ] {
1179
+ let mut reused = parser();
1180
+ let mut text = source.as_bytes().to_vec();
1181
+ let mut tree = parse(source, source);
1182
+ for (step, &(start, deleted, inserted)) in edits.iter().enumerate() {
1183
+ edit(&mut tree, &mut text, start, deleted, inserted);
1184
+ tree = reused.parse(&text, Some(&tree)).unwrap();
1185
+ let fresh = parser().parse(&text, None).unwrap();
1186
+ if !fresh.root_node().has_error() {
1187
+ assert_same_cst(
1188
+ tree.root_node(),
1189
+ fresh.root_node(),
1190
+ &format!("{source}: step {step}"),
1191
+ );
1192
+ }
1193
+ }
1194
+ assert!(!tree.root_node().has_error(), "{source}");
1195
+ }
1196
+ }
1197
+
1198
+ #[test]
1199
+ fn changed_ranges_include_getline_ownership_changes() {
1200
+ for (name, source, removed, inserted) in [
1201
+ (
1202
+ "file source moves inward",
1203
+ "{ $getline $-getline < 1 ++ }",
1204
+ "++",
1205
+ "",
1206
+ ),
1207
+ (
1208
+ "file source crosses two targets",
1209
+ "{ $getline $!getline $!getline < 1 ++ }",
1210
+ "++",
1211
+ "",
1212
+ ),
1213
+ (
1214
+ "grouped source",
1215
+ "{ $getline $-getline < (1) ++ }",
1216
+ "++",
1217
+ "",
1218
+ ),
1219
+ ("call source", "{ $getline $-getline < f(1) ++ }", "++", ""),
1220
+ (
1221
+ "subscript source",
1222
+ "{ $getline $-getline < a[1] ++ }",
1223
+ "++",
1224
+ "",
1225
+ ),
1226
+ (
1227
+ "source inside call arguments",
1228
+ "{ f($getline $-getline < 1 ++, x) }",
1229
+ "++",
1230
+ "",
1231
+ ),
1232
+ (
1233
+ "source inside a subscript",
1234
+ "{ a[$getline $-getline < 1 ++, x] }",
1235
+ "++",
1236
+ "",
1237
+ ),
1238
+ (
1239
+ "source inside print arguments",
1240
+ "{ print ($getline $-getline < 1 ++), x }",
1241
+ "++",
1242
+ "",
1243
+ ),
1244
+ (
1245
+ "multiline source after Unicode",
1246
+ "{ print \"é\"; $getline $-getline < \\\n1 ++; print 2 }\n{ print 3 }",
1247
+ "++",
1248
+ "",
1249
+ ),
1250
+ (
1251
+ "target and update change owners",
1252
+ "{ getline x++ }",
1253
+ "++",
1254
+ "++ y",
1255
+ ),
1256
+ (
1257
+ "grammar path changes to selected path",
1258
+ "{ getline x }",
1259
+ "x",
1260
+ "x++",
1261
+ ),
1262
+ (
1263
+ "removing a group makes the comparison a file source",
1264
+ "{ (getline x) < file }",
1265
+ "(getline x)",
1266
+ "getline x",
1267
+ ),
1268
+ (
1269
+ "a comparison changes to a file source operator",
1270
+ "{ getline x <= file }",
1271
+ "<=",
1272
+ "<",
1273
+ ),
1274
+ (
1275
+ "merging arguments changes getline ownership",
1276
+ "{ f(getline x, y++) }",
1277
+ ",",
1278
+ "+",
1279
+ ),
1280
+ (
1281
+ "merging subscripts changes getline ownership",
1282
+ "{ a[getline x, y++] }",
1283
+ ",",
1284
+ "+",
1285
+ ),
1286
+ (
1287
+ "an update after a target subscript needs selection",
1288
+ "{ getline a[i++] }",
1289
+ "]",
1290
+ "]++",
1291
+ ),
1292
+ ] {
1293
+ let mut parser = parser();
1294
+ let mut text = source.as_bytes().to_vec();
1295
+ let mut tree = parse(source, name);
1296
+ let start = source.find(removed).unwrap();
1297
+ for (deleted, inserted) in [
1298
+ (removed.len(), inserted),
1299
+ (inserted.len(), removed),
1300
+ (removed.len(), inserted),
1301
+ ] {
1302
+ edit(&mut tree, &mut text, start, deleted, inserted);
1303
+ let next = parser.parse(&text, Some(&tree)).unwrap();
1304
+ let fresh = parser.parse(&text, None).unwrap();
1305
+ assert!(!fresh.root_node().has_error(), "{name}");
1306
+ assert_same_cst(next.root_node(), fresh.root_node(), name);
1307
+ assert_changed_scopes(&tree, &next, name);
1308
+ tree = next;
1309
+ }
1310
+ }
1311
+ }
1312
+
1313
+ #[test]
1314
+ fn getline_expression_edit_histories_restore_fresh_csts() {
1315
+ let mut sources = vec![
1316
+ "{ print $-getline x++ tline y-- z }\n".to_string(),
1317
+ "{ x getline y++ | getline }\n".to_string(),
1318
+ "{ print $-getline a[1]-- ++y z }\n".to_string(),
1319
+ "{ getline $ getline < \"f\" < x a[1]}\n".to_string(),
1320
+ "{ getline $getline < \"f\" ++ }\n".to_string(),
1321
+ "{ getline $getline < x++ }\n".to_string(),
1322
+ "{ getline $getline < x++++ }\n".to_string(),
1323
+ "{ getline $getline < (x | getline) ++ }\n".to_string(),
1324
+ "{ getline $getline < (a ? x : (y))++ }\n".to_string(),
1325
+ "{ getline $getline < a && # source\n x++ }\n".to_string(),
1326
+ "{ getline x++ $getline $getline < 1 ++ }\n".to_string(),
1327
+ "{ getline $getline < getline $getline < x++++ }\n".to_string(),
1328
+ "{ getline $getline < (($getline < 1 in a) 1)++ }\n".to_string(),
1329
+ "{ getline $getline < (x < (y < z | getline)) ++ }\n".to_string(),
1330
+ "{ $-getline $-getline < a }\n".to_string(),
1331
+ "{ $-getline $-getline y < a }\n".to_string(),
1332
+ "{ $-getline $-getline < a ++ }\n".to_string(),
1333
+ "{ $getline $-getline $-getline < a }\n".to_string(),
1334
+ "{ $getline $-getline < 1 ++ }\n".to_string(),
1335
+ "{ $getline $!getline < x -- ++ }\n".to_string(),
1336
+ "{ $getline $--$getline < 1 ++ }\n".to_string(),
1337
+ ];
1338
+ for prefix in [
1339
+ "",
1340
+ "-",
1341
+ "+",
1342
+ "!",
1343
+ "a + ",
1344
+ "a - ",
1345
+ "a * ",
1346
+ "a / ",
1347
+ "a % ",
1348
+ "a ^ ",
1349
+ "a ",
1350
+ "q = ",
1351
+ "$",
1352
+ "$-",
1353
+ "$!",
1354
+ "++$",
1355
+ "--$",
1356
+ "getline $-",
1357
+ "print $",
1358
+ "print $-",
1359
+ "printf $-",
1360
+ ] {
1361
+ for updated in ["x++", "x--", "a[1]++", "$x--"] {
1362
+ for tail in ["", " y", " * n + z", " tline y-- z"] {
1363
+ sources.push(format!("{{ {prefix}getline {updated}{tail} }}\n"));
1364
+ }
1365
+ }
1366
+ }
1367
+ for context in ["", "print ", "printf ", "a + ", "a ", "q = "] {
1368
+ for target in ["", " x", " $-x"] {
1369
+ for source in ["\"f\"", "-1", "(f)", "f()", "/f/", "++y", "y++", "y + 1"]
1370
+ {
1371
+ for update in ["++", "--"] {
1372
+ for tail in ["", " z", " ++z", " | getline"] {
1373
+ sources.push(format!(
1374
+ "{{ {context}$getline{target} < {source} {update}{tail} }}\n"
1375
+ ));
1376
+ }
1377
+ }
1378
+ }
1379
+ }
1380
+ }
1381
+ let mut state = 0x917a_b065_u64;
1382
+ let mut next = || {
1383
+ state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
1384
+ state as usize
1385
+ };
1386
+ let mut histories = 0;
1387
+ for source in sources {
1388
+ let expected = parse(&source, &source);
1389
+ for history in 0..50 {
1390
+ let mut reused = parser();
1391
+ let mut text = source.as_bytes().to_vec();
1392
+ let mut tree = reused.parse(&text, None).unwrap();
1393
+ let mut reversals = Vec::new();
1394
+ for _ in 0..5 {
1395
+ let start = next() % (text.len() + 1);
1396
+ let removed = usize::from(start < text.len() && next() % 2 == 0);
1397
+ let inserted =
1398
+ ["", "x", "+", "-", "$", " ", "getline", "("][next() % 8];
1399
+ let deleted =
1400
+ String::from_utf8(text[start..start + removed].to_vec()).unwrap();
1401
+ edit(&mut tree, &mut text, start, removed, inserted);
1402
+ tree = reused.parse(&text, Some(&tree)).unwrap();
1403
+ reversals.push((start, inserted.len(), deleted));
1404
+ }
1405
+ for (start, removed, inserted) in reversals.into_iter().rev() {
1406
+ edit(&mut tree, &mut text, start, removed, &inserted);
1407
+ tree = reused.parse(&text, Some(&tree)).unwrap();
1408
+ }
1409
+ assert_eq!(text, source.as_bytes());
1410
+ assert_same_cst(
1411
+ tree.root_node(),
1412
+ expected.root_node(),
1413
+ &format!("{source}: history {history}"),
1414
+ );
1415
+ histories += 1;
1416
+ }
1417
+ }
1418
+ eprintln!("checked {histories} getline edit histories");
1419
+ }