@konomanoasa/tree-sitter-posix-awk 0.18.0 → 0.19.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.
@@ -100,32 +100,54 @@ const LEAF_CASES: &[LeafCase] = &[
100
100
  LeafCase {
101
101
  name: "an ERE repetition count remains one leaf",
102
102
  source: "/a{12}/\n",
103
- kind: "dup_count",
103
+ kind: "duplication_count",
104
104
  range: 3..5,
105
105
  },
106
106
  ];
107
107
 
108
108
  const ERE_CASES: &[EreCase] = &[
109
+ EreCase {
110
+ name: "an empty ERE retains only its delimiter ranges",
111
+ source: "//",
112
+ leaves: &[("/", 0, 1), ("/", 1, 2)],
113
+ },
114
+ EreCase {
115
+ name: "empty groups retain delimiters within nested alternation and repetition",
116
+ source: "/(()|())*?/",
117
+ leaves: &[
118
+ ("/", 0, 1),
119
+ ("(", 1, 2),
120
+ ("(", 2, 3),
121
+ (")", 3, 4),
122
+ ("|", 4, 5),
123
+ ("(", 5, 6),
124
+ (")", 6, 7),
125
+ (")", 7, 8),
126
+ ("*", 8, 9),
127
+ ("repetition_modifier", 9, 10),
128
+ ("/", 10, 11),
129
+ ],
130
+ },
109
131
  EreCase {
110
132
  name: "ordinary characters and complete escapes retain their source ranges",
111
133
  source: r"/a é)}.\n\/\\\141\q(a*?)/",
112
134
  leaves: &[
113
135
  ("/", 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),
136
+ ("ordinary_character", 1, 2),
137
+ ("ordinary_character", 2, 3),
138
+ ("ordinary_character", 3, 5),
139
+ ("ordinary_character", 5, 6),
140
+ ("ordinary_character", 6, 7),
119
141
  (".", 7, 8),
120
142
  ("escape_sequence", 8, 10),
121
143
  ("escaped_delimiter", 10, 12),
122
- ("escape_sequence", 12, 14),
144
+ ("quoted_character", 12, 14),
123
145
  ("escape_sequence", 14, 18),
124
146
  ("escape_sequence", 18, 20),
125
147
  ("(", 20, 21),
126
- ("ordinary_character_content", 21, 22),
148
+ ("ordinary_character", 21, 22),
127
149
  ("*", 22, 23),
128
- ("?", 23, 24),
150
+ ("repetition_modifier", 23, 24),
129
151
  (")", 24, 25),
130
152
  ("/", 25, 26),
131
153
  ],
@@ -135,10 +157,10 @@ const ERE_CASES: &[EreCase] = &[
135
157
  source: r"/ \.[ \]][[. \..]]/",
136
158
  leaves: &[
137
159
  ("/", 0, 1),
138
- ("ordinary_character_content", 1, 2),
139
- ("escape_sequence", 2, 4),
160
+ ("ordinary_character", 1, 2),
161
+ ("quoted_character", 2, 4),
140
162
  ("[", 4, 5),
141
- ("collating_element_content", 5, 6),
163
+ ("collating_element_single", 5, 6),
142
164
  ("escape_sequence", 6, 8),
143
165
  ("]", 8, 9),
144
166
  ("[", 9, 10),
@@ -184,18 +206,14 @@ const ERE_CASES: &[EreCase] = &[
184
206
  ],
185
207
  },
186
208
  EreCase {
187
- name: "compound punctuation, spaces and Unicode retain individual leaves",
209
+ name: "raw compound punctuation, spaces and Unicode share one payload leaf",
188
210
  source: r"/[[. a-]🙂.]]/",
189
211
  leaves: &[
190
212
  ("/", 0, 1),
191
213
  ("[", 1, 2),
192
214
  ("[", 2, 3),
193
215
  (".", 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),
216
+ ("collating_element_multi", 4, 12),
199
217
  (".", 12, 13),
200
218
  ("]", 13, 14),
201
219
  ("]", 14, 15),
@@ -208,9 +226,9 @@ const ERE_CASES: &[EreCase] = &[
208
226
  leaves: &[
209
227
  ("/", 0, 1),
210
228
  ("[", 1, 2),
211
- ("collating_element_content", 2, 3),
212
- ("collating_element_content", 3, 4),
213
- ("collating_element_content", 4, 5),
229
+ ("collating_element_single", 2, 3),
230
+ ("collating_element_single", 3, 4),
231
+ ("collating_element_single", 4, 5),
214
232
  ("escaped_delimiter", 5, 7),
215
233
  ("escape_sequence", 7, 9),
216
234
  ("-", 9, 10),
@@ -219,15 +237,15 @@ const ERE_CASES: &[EreCase] = &[
219
237
  ],
220
238
  },
221
239
  EreCase {
222
- name: "range and list hyphens retain anonymous leaves",
240
+ name: "a leading hyphen is a named endpoint before range and list hyphens",
223
241
  source: r"/[--ac-]/",
224
242
  leaves: &[
225
243
  ("/", 0, 1),
226
244
  ("[", 1, 2),
227
- ("-", 2, 3),
245
+ ("collating_element_single", 2, 3),
228
246
  ("-", 3, 4),
229
- ("collating_element_content", 4, 5),
230
- ("collating_element_content", 5, 6),
247
+ ("collating_element_single", 4, 5),
248
+ ("collating_element_single", 5, 6),
231
249
  ("-", 6, 7),
232
250
  ("]", 7, 8),
233
251
  ("/", 8, 9),
@@ -405,6 +423,140 @@ fn ere_leaves_partition_complete_source_spellings() {
405
423
  }
406
424
  }
407
425
 
426
+ #[test]
427
+ fn empty_ere_and_groups_own_only_opening_and_closing_delimiters() {
428
+ for (kind, name, source, range) in [
429
+ ("ere", "pattern at EOF", "//", 0..2),
430
+ ("ere", "print expression", "BEGIN { print // }", 14..16),
431
+ ("ere", "match operand", "$0 ~ //", 5..7),
432
+ (
433
+ "ere",
434
+ "builtin argument",
435
+ "BEGIN { sub(//, \"x\") }",
436
+ 12..14,
437
+ ),
438
+ ("ere", "getline source", "{ getline < // }", 12..14),
439
+ ("ere", "division operand", "BEGIN { print 1 / // }", 18..20),
440
+ (
441
+ "ere",
442
+ "following division",
443
+ "BEGIN { print // / 2 }",
444
+ 14..16,
445
+ ),
446
+ ("ere_expression", "empty group", "/()/", 1..3),
447
+ ("ere_expression", "nested group", "/(())/", 2..4),
448
+ ("ere_expression", "concatenated group", "/a()b/", 2..4),
449
+ ("ere_expression", "left alternation group", "/()|a/", 1..3),
450
+ (
451
+ "ere_expression",
452
+ "right alternation group",
453
+ "/(a|())/",
454
+ 4..6,
455
+ ),
456
+ (
457
+ "ere_expression",
458
+ "group before a literal closer",
459
+ "/())/",
460
+ 1..3,
461
+ ),
462
+ ] {
463
+ let tree = parse(source, name);
464
+ let owner = tree
465
+ .root_node()
466
+ .descendant_for_byte_range(range.start, range.end)
467
+ .unwrap();
468
+ assert_eq!(owner.kind(), kind, "{name}");
469
+ assert_eq!(owner.byte_range(), range, "{name}");
470
+ assert_eq!(owner.child_count(), 2, "{name}");
471
+ assert_eq!(owner.named_child_count(), 0, "{name}");
472
+ assert!(owner.child_by_field_name("expression").is_none(), "{name}");
473
+ let (opening, closing) = if kind == "ere" {
474
+ ("/", "/")
475
+ } else {
476
+ ("(", ")")
477
+ };
478
+ for (field, token, expected) in [
479
+ ("opening", opening, range.start..range.start + 1),
480
+ ("closing", closing, range.end - 1..range.end),
481
+ ] {
482
+ let delimiter = owner.child_by_field_name(field).unwrap();
483
+ assert_eq!(delimiter.kind(), token, "{name}: {field}");
484
+ assert_eq!(delimiter.byte_range(), expected, "{name}: {field}");
485
+ assert!(!delimiter.is_named(), "{name}: {field}");
486
+ }
487
+ }
488
+ }
489
+
490
+ #[test]
491
+ fn ere_fields_select_direct_source_children_in_flat_expressions() {
492
+ let source = r"/(a|bc){2,3}?d{4}e{5,}()[[:alpha:][.ch.][=é=][.a\nb.]]/";
493
+ let tree = parse(source, "shared ERE fields");
494
+ for (kind, text, expected) in [
495
+ (
496
+ "ere_expression",
497
+ "(a|bc){2,3}?",
498
+ vec![("opening", "("), ("expression", "a|bc"), ("closing", ")")],
499
+ ),
500
+ (
501
+ "ere_expression",
502
+ "()",
503
+ vec![("opening", "("), ("closing", ")")],
504
+ ),
505
+ (
506
+ "ere_dupl_symbol",
507
+ "{2,3}?",
508
+ vec![("minimum", "2"), ("maximum", "3"), ("modifier", "?")],
509
+ ),
510
+ ("ere_dupl_symbol", "{4}", vec![("minimum", "4")]),
511
+ ("ere_dupl_symbol", "{5,}", vec![("minimum", "5")]),
512
+ ("character_class", "[:alpha:]", vec![("name", "alpha")]),
513
+ ("collating_symbol", "[.ch.]", vec![("element", "ch")]),
514
+ ("equivalence_class", "[=é=]", vec![("element", "é")]),
515
+ ("collating_symbol", r"[.a\nb.]", vec![("element", r"a\nb")]),
516
+ ] {
517
+ let start = source.find(text).unwrap();
518
+ let node = tree
519
+ .root_node()
520
+ .descendant_for_byte_range(start, start + text.len())
521
+ .unwrap();
522
+ assert_eq!(node.kind(), kind, "{text}");
523
+ let mut cursor = node.walk();
524
+ let actual: Vec<_> = node
525
+ .children(&mut cursor)
526
+ .enumerate()
527
+ .filter_map(|(index, child)| {
528
+ node
529
+ .field_name_for_child(index as u32)
530
+ .map(|field| (field, child.utf8_text(source.as_bytes()).unwrap()))
531
+ })
532
+ .collect();
533
+ assert_eq!(actual, expected, "{text}");
534
+ for (field, value) in expected {
535
+ let child = node.child_by_field_name(field).unwrap();
536
+ assert_eq!(child.parent(), Some(node), "{text}: {field}");
537
+ assert_eq!(
538
+ child.utf8_text(source.as_bytes()).unwrap(),
539
+ value,
540
+ "{text}: {field}"
541
+ );
542
+ }
543
+ }
544
+ let tree = parse("/()*?+?/", "repeated empty group");
545
+ let expression = tree.root_node().descendant_for_byte_range(1, 7).unwrap();
546
+ assert_eq!(expression.kind(), "ere_expression");
547
+ assert!(expression.child_by_field_name("expression").is_none());
548
+ let children: Vec<_> = expression
549
+ .children(&mut expression.walk())
550
+ .map(|node| node.kind())
551
+ .collect();
552
+ assert_eq!(children, ["(", ")", "ere_dupl_symbol", "ere_dupl_symbol"]);
553
+ for symbol in expression.named_children(&mut expression.walk()) {
554
+ let modifier = symbol.child_by_field_name("modifier").unwrap();
555
+ assert_eq!(modifier.kind(), "repetition_modifier");
556
+ assert_eq!(modifier.child_count(), 0);
557
+ }
558
+ }
559
+
408
560
  #[test]
409
561
  fn fields_belong_to_direct_children() {
410
562
  let language: tree_sitter::Language = grammar::LANGUAGE.into();
@@ -438,6 +590,11 @@ fn fields_belong_to_direct_children() {
438
590
  "{$-x++; $(-x)++; $$x++; $getline x++; $getline $-x++; $getline $(x)++}\n",
439
591
  ),
440
592
  ("group alternation", "/(a|b)c/"),
593
+ ("ordinary source escapes", r"/a\n\//"),
594
+ (
595
+ "intervals and compound payloads",
596
+ r"/()[[.a.][.ch.][=é=][.a\nb.][:alpha:]]{2,3}?/",
597
+ ),
441
598
  ("group concatenation", "/(ab)c/"),
442
599
  ("nested group repetition", "/((a|b)+?c)d/"),
443
600
  ("ERE boundary continuation", "/(a|b)/\\\n&& /c/"),
@@ -1032,6 +1189,14 @@ fn removing_an_invalid_bracket_before_getline_restores_statements() {
1032
1189
  #[test]
1033
1190
  fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1034
1191
  let seeds = [
1192
+ "{ $getline < a < getline x++ < y }",
1193
+ "{ $getline < a < getline x-- < y }",
1194
+ "{ $getline < a < $getline < b < getline x++++ < y }",
1195
+ "{ $getline < a < $getline < b < getline x++++++ < y }",
1196
+ "{ getline $getline < a < getline x++ < y }",
1197
+ "{ print ($getline < a < getline x++ < y) }",
1198
+ "{ f($getline < a < getline x++ < y) }",
1199
+ "{ x[$getline < a < getline x++ < y] }",
1035
1200
  "{ x = getline a + line b++ }",
1036
1201
  "{ a ? b : getline c-- ; (y) ? 1 : 2 }",
1037
1202
  "{ getline x++ }",
@@ -1043,6 +1208,10 @@ fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1043
1208
  "{ getline $getline x < source }",
1044
1209
  "{ print (getline x++ + y) }",
1045
1210
  "{ getline x\\\n++ }",
1211
+ "{ getline $getline < x++++ }",
1212
+ "{ getline $getline < x | getline ++ }",
1213
+ "{ getline $getline < a ? x : (y)++ }",
1214
+ "{ getline $getline < getline $getline < x++++ }",
1046
1215
  ];
1047
1216
  let mut random = 0x79d07b23u32;
1048
1217
  let mut next = || {
@@ -1099,3 +1268,417 @@ fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1099
1268
  );
1100
1269
  }
1101
1270
  }
1271
+
1272
+ #[test]
1273
+ fn getline_token_edit_histories_match_fresh_parses() {
1274
+ struct History {
1275
+ source: &'static str,
1276
+ edits: &'static [(usize, usize, &'static str)],
1277
+ }
1278
+ for History { source, edits } in [
1279
+ History {
1280
+ source: "{ getline $ getline < \"f\" < x }",
1281
+ edits: &[(10, 1, ""), (10, 0, "$"), (30, 0, "a[1]")],
1282
+ },
1283
+ History {
1284
+ source: "{ getline x < y ++ z }",
1285
+ edits: &[(10, 1, "getline"), (10, 7, "x")],
1286
+ },
1287
+ History {
1288
+ source: "{ getline < y ++ z }",
1289
+ edits: &[(10, 0, "a[1]")],
1290
+ },
1291
+ History {
1292
+ source: "{ print $ getline x ++ $ getline y ++ }",
1293
+ edits: &[(33, 2, "?"), (33, 1, "y ")],
1294
+ },
1295
+ History {
1296
+ source: "{ getline x + y getline z ++ w }",
1297
+ edits: &[(24, 2, "]"), (24, 1, "z ")],
1298
+ },
1299
+ History {
1300
+ source: "{ $getline < \"f\" ++ | getline }",
1301
+ edits: &[(0, 2, "tline"), (0, 5, "{ "), (20, 2, "$")],
1302
+ },
1303
+ History {
1304
+ source: "{ getline a[1/2] ++ $ getline x ++ }",
1305
+ edits: &[(18, 2, "a[1]"), (32, 0, ") "), (32, 2, "")],
1306
+ },
1307
+ History {
1308
+ source: "{ getline a[ b[i,\nj] ] ++ y -- z }",
1309
+ edits: &[(24, 2, "\"f\""), (29, 2, "]"), (29, 1, "--")],
1310
+ },
1311
+ History {
1312
+ source: "{ getline $1.5 ++ $ getline $1.5 ++ y }",
1313
+ edits: &[(16, 2, "\"f\""), (34, 2, "("), (34, 1, "++")],
1314
+ },
1315
+ ] {
1316
+ let mut reused = parser();
1317
+ let mut text = source.as_bytes().to_vec();
1318
+ let mut tree = parse(source, source);
1319
+ for (step, &(start, deleted, inserted)) in edits.iter().enumerate() {
1320
+ edit(&mut tree, &mut text, start, deleted, inserted);
1321
+ tree = reused.parse(&text, Some(&tree)).unwrap();
1322
+ let fresh = parser().parse(&text, None).unwrap();
1323
+ if !fresh.root_node().has_error() {
1324
+ assert_same_cst(
1325
+ tree.root_node(),
1326
+ fresh.root_node(),
1327
+ &format!("{source}: step {step}"),
1328
+ );
1329
+ }
1330
+ }
1331
+ assert!(!tree.root_node().has_error(), "{source}");
1332
+ }
1333
+ }
1334
+
1335
+ #[test]
1336
+ fn getline_postfix_concatenation_keeps_its_operands() {
1337
+ for (source, concatenation, left, right) in [
1338
+ ("{ -getline x++ }\n", "-getline x++", "-getline", "x++"),
1339
+ (
1340
+ "{ a + getline x++ }\n",
1341
+ "a + getline x++",
1342
+ "a + getline",
1343
+ "x++",
1344
+ ),
1345
+ ("{ a getline x++ }\n", "a getline x++", "a getline", "x++"),
1346
+ (
1347
+ "{ q = $-getline x++ }\n",
1348
+ "$-getline x++",
1349
+ "$-getline",
1350
+ "x++",
1351
+ ),
1352
+ (
1353
+ "{ a ^ getline x++ }\n",
1354
+ "a ^ getline x++",
1355
+ "a ^ getline",
1356
+ "x++",
1357
+ ),
1358
+ (
1359
+ "{ getline $-getline x++ }\n",
1360
+ "getline $-getline x++",
1361
+ "getline $-getline",
1362
+ "x++",
1363
+ ),
1364
+ (
1365
+ "{ ++$getline x-- }\n",
1366
+ "++$getline x--",
1367
+ "++$getline",
1368
+ "x--",
1369
+ ),
1370
+ (
1371
+ "{ print $-getline x++ }\n",
1372
+ "$-getline x++",
1373
+ "$-getline",
1374
+ "x++",
1375
+ ),
1376
+ ] {
1377
+ let tree = parse(source, source);
1378
+ let start = source.find(concatenation).unwrap();
1379
+ let expression = tree
1380
+ .root_node()
1381
+ .descendant_for_byte_range(start, start + concatenation.len())
1382
+ .unwrap();
1383
+ assert!(
1384
+ expression.child_by_field_name("operator").is_none(),
1385
+ "{source}"
1386
+ );
1387
+ for (field, text) in [("left", left), ("right", right)] {
1388
+ let child = expression.child_by_field_name(field).unwrap();
1389
+ assert_eq!(&source[child.byte_range()], text, "{source}: {field}");
1390
+ }
1391
+ let right = expression.child_by_field_name("right").unwrap();
1392
+ let operator = right.child_by_field_name("operator").unwrap();
1393
+ assert!(matches!(operator.kind(), "incr" | "decr"), "{source}");
1394
+ }
1395
+ }
1396
+
1397
+ #[test]
1398
+ fn redirected_getline_field_updates_keep_their_source_and_target() {
1399
+ for (source, expression, filename, target, operator) in [
1400
+ (
1401
+ "{ $getline < \"f\" ++ }\n",
1402
+ "$getline < \"f\" ++",
1403
+ "\"f\"",
1404
+ None,
1405
+ "++",
1406
+ ),
1407
+ (
1408
+ "{ $getline x < \"f\" -- }\n",
1409
+ "$getline x < \"f\" --",
1410
+ "\"f\"",
1411
+ Some("x"),
1412
+ "--",
1413
+ ),
1414
+ (
1415
+ "{ $getline $-x < \"f\" ++ }\n",
1416
+ "$getline $-x < \"f\" ++",
1417
+ "\"f\"",
1418
+ Some("$-x"),
1419
+ "++",
1420
+ ),
1421
+ (
1422
+ "{ print $getline < -1 ++ }\n",
1423
+ "$getline < -1 ++",
1424
+ "-1",
1425
+ None,
1426
+ "++",
1427
+ ),
1428
+ (
1429
+ "{ printf $getline < y++ -- }\n",
1430
+ "$getline < y++ --",
1431
+ "y++",
1432
+ None,
1433
+ "--",
1434
+ ),
1435
+ (
1436
+ "{ $getline < f() ++ }\n",
1437
+ "$getline < f() ++",
1438
+ "f()",
1439
+ None,
1440
+ "++",
1441
+ ),
1442
+ ] {
1443
+ let tree = parse(source, source);
1444
+ let start = source.find(expression).unwrap();
1445
+ let update = tree
1446
+ .root_node()
1447
+ .descendant_for_byte_range(start, start + expression.len())
1448
+ .unwrap();
1449
+ let update_operator = update.child_by_field_name("operator").unwrap();
1450
+ assert_eq!(&source[update_operator.byte_range()], operator, "{source}");
1451
+ let field = update.child_by_field_name("operand").unwrap();
1452
+ assert_eq!(field.kind(), "lvalue", "{source}");
1453
+ assert_eq!(field.start_byte(), start, "{source}");
1454
+ assert_eq!(
1455
+ field.end_byte(),
1456
+ update_operator.start_byte() - 1,
1457
+ "{source}"
1458
+ );
1459
+ let input = field
1460
+ .child_by_field_name("operand")
1461
+ .unwrap()
1462
+ .named_child(0)
1463
+ .unwrap()
1464
+ .named_child(0)
1465
+ .unwrap();
1466
+ assert_eq!(input.kind(), "non_unary_input_function", "{source}");
1467
+ let file = input.child_by_field_name("source").unwrap();
1468
+ assert_eq!(&source[file.byte_range()], filename, "{source}");
1469
+ let get = input.child_by_field_name("get").unwrap();
1470
+ assert_eq!(
1471
+ get
1472
+ .child_by_field_name("target")
1473
+ .map(|node| &source[node.byte_range()]),
1474
+ target,
1475
+ "{source}"
1476
+ );
1477
+ }
1478
+ }
1479
+
1480
+ #[test]
1481
+ fn getline_source_lookahead_preserves_target_ownership() {
1482
+ for (expression, target) in [
1483
+ ("getline $getline < \"f\"", Some("$getline < \"f\"")),
1484
+ ("getline $getline < \"f\" ++", None),
1485
+ ("getline $getline < x++", Some("$getline < x++")),
1486
+ ("getline $getline < a < getline x++ < y", None),
1487
+ ("getline $getline < x++++", None),
1488
+ ("getline $getline $-x < \"f\" ++", None),
1489
+ ("getline $getline < x | getline ++", None),
1490
+ (
1491
+ "getline $getline < a ? x : y++",
1492
+ Some("$getline < a ? x : y++"),
1493
+ ),
1494
+ ("getline $getline < a ? x : (y)++", None),
1495
+ ("getline $getline < 1e++", Some("$getline < 1e++")),
1496
+ ("getline x++ $getline $getline < 1 ++", Some("x")),
1497
+ ("getline $getline < $getline < 1 in a 1++", None),
1498
+ ("getline $getline < $getline < x in a < \"f\" ++", None),
1499
+ ("getline $getline < x < y < z | getline ++", None),
1500
+ ("getline $getline < x ~ y ~ 1 | getline ++", None),
1501
+ ("getline $getline < x ~ y | getline z ~ \"f\" ++", None),
1502
+ ("getline $getline $-x < x | getline ++", None),
1503
+ (
1504
+ "getline $getline < getline $getline < x++++",
1505
+ Some("$getline < getline $getline < x++++"),
1506
+ ),
1507
+ ] {
1508
+ let source = format!("{{ {expression} }}\n");
1509
+ let tree = parse(&source, expression);
1510
+ let keyword = tree.root_node().descendant_for_byte_range(2, 8).unwrap();
1511
+ assert_eq!(keyword.kind(), "getline_keyword", "{expression}");
1512
+ let get = keyword.parent().unwrap();
1513
+ assert_eq!(get.kind(), "simple_get", "{expression}");
1514
+ assert_eq!(
1515
+ get
1516
+ .child_by_field_name("target")
1517
+ .map(|node| &source[node.byte_range()]),
1518
+ target,
1519
+ "{expression}",
1520
+ );
1521
+ }
1522
+ }
1523
+
1524
+ #[test]
1525
+ fn source_getline_targets_can_leave_postfix_updates_for_enclosing_fields() {
1526
+ for (source, targets, operands) in [
1527
+ (
1528
+ "{ $getline < a < getline x++ < y }",
1529
+ vec![None, Some("x")],
1530
+ vec!["$getline < a < getline x"],
1531
+ ),
1532
+ (
1533
+ "{ $getline < a < getline x-- < y }",
1534
+ vec![None, Some("x")],
1535
+ vec!["$getline < a < getline x"],
1536
+ ),
1537
+ (
1538
+ "{ $getline < a < $getline < b < getline x++++ < y }",
1539
+ vec![None, None, Some("x")],
1540
+ vec![
1541
+ "$getline < a < $getline < b < getline x++",
1542
+ "$getline < b < getline x",
1543
+ ],
1544
+ ),
1545
+ (
1546
+ "{ getline $getline < a < getline x++ < y }",
1547
+ vec![None, None, Some("x")],
1548
+ vec!["$getline < a < getline x"],
1549
+ ),
1550
+ ("{ $getline < getline x++ }", vec![None, None], vec!["x"]),
1551
+ (
1552
+ "{ $getline < f(getline x++)++ < y }",
1553
+ vec![None, None],
1554
+ vec!["$getline < f(getline x++)", "x"],
1555
+ ),
1556
+ ] {
1557
+ let tree = parse(source, source);
1558
+ let mut pending = vec![tree.root_node()];
1559
+ let mut actual_targets = Vec::new();
1560
+ let mut actual_operands = Vec::new();
1561
+ while let Some(node) = pending.pop() {
1562
+ if node.kind() == "simple_get" {
1563
+ actual_targets.push(
1564
+ node
1565
+ .child_by_field_name("target")
1566
+ .map(|target| &source[target.byte_range()]),
1567
+ );
1568
+ }
1569
+ if let Some(operator) = node.child_by_field_name("operator")
1570
+ && matches!(operator.kind(), "incr" | "decr")
1571
+ {
1572
+ let operand = node.child_by_field_name("operand").unwrap();
1573
+ assert_eq!(operand.kind(), "lvalue", "{source}");
1574
+ actual_operands.push(&source[operand.byte_range()]);
1575
+ }
1576
+ for index in (0..node.child_count()).rev() {
1577
+ pending.push(node.child(index).unwrap());
1578
+ }
1579
+ }
1580
+ assert_eq!(actual_targets, targets, "{source}: targets");
1581
+ assert_eq!(actual_operands, operands, "{source}: update operands");
1582
+ }
1583
+ }
1584
+
1585
+ #[test]
1586
+ fn getline_expression_edit_histories_restore_fresh_csts() {
1587
+ let mut sources = vec![
1588
+ "{ print $-getline x++ tline y-- z }\n".to_string(),
1589
+ "{ x getline y++ | getline }\n".to_string(),
1590
+ "{ print $-getline a[1]-- ++y z }\n".to_string(),
1591
+ "{ getline $ getline < \"f\" < x a[1]}\n".to_string(),
1592
+ "{ getline $getline < \"f\" ++ }\n".to_string(),
1593
+ "{ getline $getline < x++ }\n".to_string(),
1594
+ "{ getline $getline < x++++ }\n".to_string(),
1595
+ "{ getline $getline < x | getline ++ }\n".to_string(),
1596
+ "{ getline $getline < a ? x : (y)++ }\n".to_string(),
1597
+ "{ getline $getline < a && # source\n x++ }\n".to_string(),
1598
+ "{ getline x++ $getline $getline < 1 ++ }\n".to_string(),
1599
+ "{ getline $getline < getline $getline < x++++ }\n".to_string(),
1600
+ "{ getline $getline < $getline < 1 in a 1++ }\n".to_string(),
1601
+ "{ getline $getline < x < y < z | getline ++ }\n".to_string(),
1602
+ ];
1603
+ for prefix in [
1604
+ "",
1605
+ "-",
1606
+ "+",
1607
+ "!",
1608
+ "a + ",
1609
+ "a - ",
1610
+ "a * ",
1611
+ "a / ",
1612
+ "a % ",
1613
+ "a ^ ",
1614
+ "a ",
1615
+ "q = ",
1616
+ "$",
1617
+ "$-",
1618
+ "$!",
1619
+ "++$",
1620
+ "--$",
1621
+ "getline $-",
1622
+ "print $",
1623
+ "print $-",
1624
+ "printf $-",
1625
+ ] {
1626
+ for updated in ["x++", "x--", "a[1]++", "$x--"] {
1627
+ for tail in ["", " y", " * n + z", " tline y-- z"] {
1628
+ sources.push(format!("{{ {prefix}getline {updated}{tail} }}\n"));
1629
+ }
1630
+ }
1631
+ }
1632
+ for context in ["", "print ", "printf ", "a + ", "a ", "q = "] {
1633
+ for target in ["", " x", " $-x"] {
1634
+ for source in ["\"f\"", "-1", "(f)", "f()", "/f/", "++y", "y++", "y + 1"]
1635
+ {
1636
+ for update in ["++", "--"] {
1637
+ for tail in ["", " z", " ++z", " | getline"] {
1638
+ sources.push(format!(
1639
+ "{{ {context}$getline{target} < {source} {update}{tail} }}\n"
1640
+ ));
1641
+ }
1642
+ }
1643
+ }
1644
+ }
1645
+ }
1646
+ let mut state = 0x917a_b065_u64;
1647
+ let mut next = || {
1648
+ state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
1649
+ state as usize
1650
+ };
1651
+ let mut histories = 0;
1652
+ for source in sources {
1653
+ let expected = parse(&source, &source);
1654
+ for history in 0..50 {
1655
+ let mut reused = parser();
1656
+ let mut text = source.as_bytes().to_vec();
1657
+ let mut tree = reused.parse(&text, None).unwrap();
1658
+ let mut reversals = Vec::new();
1659
+ for _ in 0..5 {
1660
+ let start = next() % (text.len() + 1);
1661
+ let removed = usize::from(start < text.len() && next() % 2 == 0);
1662
+ let inserted =
1663
+ ["", "x", "+", "-", "$", " ", "getline", "("][next() % 8];
1664
+ let deleted =
1665
+ String::from_utf8(text[start..start + removed].to_vec()).unwrap();
1666
+ edit(&mut tree, &mut text, start, removed, inserted);
1667
+ tree = reused.parse(&text, Some(&tree)).unwrap();
1668
+ reversals.push((start, inserted.len(), deleted));
1669
+ }
1670
+ for (start, removed, inserted) in reversals.into_iter().rev() {
1671
+ edit(&mut tree, &mut text, start, removed, &inserted);
1672
+ tree = reused.parse(&text, Some(&tree)).unwrap();
1673
+ }
1674
+ assert_eq!(text, source.as_bytes());
1675
+ assert_same_cst(
1676
+ tree.root_node(),
1677
+ expected.root_node(),
1678
+ &format!("{source}: history {history}"),
1679
+ );
1680
+ histories += 1;
1681
+ }
1682
+ }
1683
+ eprintln!("checked {histories} getline edit histories");
1684
+ }