@konomanoasa/tree-sitter-posix-awk 0.20.2 → 0.21.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 +3 -3
- package/README.md +0 -6
- package/bindings/python/__init__.py +9 -0
- package/bindings/python/binding.c +31 -0
- package/bindings/python/binding.pyi +3 -0
- package/bindings/python/py.typed +0 -0
- package/grammar.js +10 -3
- package/package.json +10 -8
- package/pyproject.toml +60 -0
- package/setup.py +28 -0
- package/src/grammar.json +124 -41
- package/src/node-types.json +1 -1
- package/src/parser.c +30105 -29949
- package/src/scanner.c +257 -33
- package/test/bindings.test.rs +11 -1403
- package/tree-sitter.json +2 -2
package/src/scanner.c
CHANGED
|
@@ -81,7 +81,9 @@ enum TokenType {
|
|
|
81
81
|
CONTINUATION_NEWLINE = 71,
|
|
82
82
|
LITERAL_BREAK = 72,
|
|
83
83
|
STRAY_BACKSLASH = 73,
|
|
84
|
-
|
|
84
|
+
ERE_INTERVAL_OPENING = 74,
|
|
85
|
+
ERE_LITERAL_BRACE = 75,
|
|
86
|
+
ERROR_SENTINEL = 76,
|
|
85
87
|
TOKEN_TYPE_COUNT,
|
|
86
88
|
};
|
|
87
89
|
|
|
@@ -392,6 +394,146 @@ scan_getline_pipe(TSLexer *lexer, const bool *valid_symbols, bool recovering) {
|
|
|
392
394
|
emit(lexer, GETLINE_PIPE);
|
|
393
395
|
}
|
|
394
396
|
|
|
397
|
+
enum { ERE_END = -1, ERE_NONCHARACTER = -2 };
|
|
398
|
+
|
|
399
|
+
typedef struct {
|
|
400
|
+
int32_t value;
|
|
401
|
+
bool quoted;
|
|
402
|
+
} EreCharacter;
|
|
403
|
+
|
|
404
|
+
typedef struct {
|
|
405
|
+
TSLexer *lexer;
|
|
406
|
+
EreCharacter unit;
|
|
407
|
+
bool buffered;
|
|
408
|
+
} EreReader;
|
|
409
|
+
|
|
410
|
+
static EreCharacter read_ere_unit(TSLexer *lexer) {
|
|
411
|
+
int32_t value = lexer->lookahead;
|
|
412
|
+
if (lexer->eof(lexer) || value == 0 || value == '\n' || value == '/')
|
|
413
|
+
return (EreCharacter){.value = ERE_END};
|
|
414
|
+
lexer->advance(lexer, false);
|
|
415
|
+
if (value != '\\') return (EreCharacter){.value = value};
|
|
416
|
+
value = lexer->lookahead;
|
|
417
|
+
if (lexer->eof(lexer) || value == 0 || value == '\n')
|
|
418
|
+
return (EreCharacter){.value = ERE_END};
|
|
419
|
+
lexer->advance(lexer, false);
|
|
420
|
+
if (is_octal_digit(value)) {
|
|
421
|
+
unsigned byte = (unsigned)(value - '0');
|
|
422
|
+
for (unsigned i = 1; i < 3 && is_octal_digit(lexer->lookahead); i++) {
|
|
423
|
+
byte = byte * 8 + (unsigned)(lexer->lookahead - '0');
|
|
424
|
+
lexer->advance(lexer, false);
|
|
425
|
+
}
|
|
426
|
+
byte &= UINT8_MAX;
|
|
427
|
+
return (EreCharacter){.value = (int32_t)byte};
|
|
428
|
+
}
|
|
429
|
+
static const char names[] = "abfnrtv/";
|
|
430
|
+
static const char values[] = "\a\b\f\n\r\t\v/";
|
|
431
|
+
for (unsigned i = 0; i < sizeof(names) - 1; i++)
|
|
432
|
+
if (value == names[i]) return (EreCharacter){.value = values[i]};
|
|
433
|
+
return (EreCharacter){.value = value, .quoted = true};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
static EreCharacter peek_ere_character(EreReader *reader) {
|
|
437
|
+
if (!reader->buffered) {
|
|
438
|
+
reader->unit = read_ere_unit(reader->lexer);
|
|
439
|
+
reader->buffered = true;
|
|
440
|
+
}
|
|
441
|
+
return reader->unit;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
static EreCharacter read_ere_character(EreReader *reader) {
|
|
445
|
+
EreCharacter character = peek_ere_character(reader);
|
|
446
|
+
reader->buffered = false;
|
|
447
|
+
return character;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
static EreCharacter read_ere_member(EreReader *reader) {
|
|
451
|
+
EreCharacter character = read_ere_character(reader);
|
|
452
|
+
if (character.value == '\\' && !character.quoted) {
|
|
453
|
+
EreCharacter next = peek_ere_character(reader);
|
|
454
|
+
if (next.quoted) {
|
|
455
|
+
// A decoded backslash quotes the retained backslash of the next escape.
|
|
456
|
+
reader->unit.quoted = false;
|
|
457
|
+
} else character = read_ere_character(reader);
|
|
458
|
+
character.quoted = true;
|
|
459
|
+
}
|
|
460
|
+
return character;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
static EreCharacter read_ere_form(EreReader *reader, int32_t marker) {
|
|
464
|
+
EreCharacter element = {.value = ERE_NONCHARACTER};
|
|
465
|
+
unsigned count = 0;
|
|
466
|
+
EreCharacter character = read_ere_character(reader);
|
|
467
|
+
while (character.value >= 0) {
|
|
468
|
+
EreCharacter next = read_ere_character(reader);
|
|
469
|
+
if (
|
|
470
|
+
character.value == marker &&
|
|
471
|
+
!character.quoted &&
|
|
472
|
+
next.value == ']' &&
|
|
473
|
+
!next.quoted
|
|
474
|
+
)
|
|
475
|
+
return count == 1 && marker == '.'
|
|
476
|
+
? element
|
|
477
|
+
: (EreCharacter){.value = ERE_NONCHARACTER};
|
|
478
|
+
if (count == 0) element = character;
|
|
479
|
+
if (count < 2) count += character.quoted ? 2 : 1;
|
|
480
|
+
character = next;
|
|
481
|
+
}
|
|
482
|
+
return character;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
static bool check_ere_bracket(EreReader *reader) {
|
|
486
|
+
EreCharacter previous = {.value = ERE_NONCHARACTER};
|
|
487
|
+
EreCharacter character = read_ere_member(reader);
|
|
488
|
+
bool pending = false, range_end = false, first = true, after_class = false;
|
|
489
|
+
if (character.value == '^' && !character.quoted)
|
|
490
|
+
character = read_ere_member(reader);
|
|
491
|
+
while (character.value >= 0) {
|
|
492
|
+
EreCharacter next = {.value = ERE_END};
|
|
493
|
+
bool have_next = false, class = false;
|
|
494
|
+
if (!character.quoted) {
|
|
495
|
+
if (character.value == ']' && !first) return true;
|
|
496
|
+
if (character.value == '[') {
|
|
497
|
+
next = read_ere_member(reader);
|
|
498
|
+
have_next = true;
|
|
499
|
+
if (!next.quoted && character_in(next.value, ".=:")) {
|
|
500
|
+
class = next.value != '.';
|
|
501
|
+
character = read_ere_form(reader, next.value);
|
|
502
|
+
have_next = false;
|
|
503
|
+
}
|
|
504
|
+
} else if (character.value == '-' && !first && !pending && !after_class) {
|
|
505
|
+
next = read_ere_member(reader);
|
|
506
|
+
if (next.value == ']' && !next.quoted) return true;
|
|
507
|
+
if (range_end) return false;
|
|
508
|
+
pending = true;
|
|
509
|
+
character = next;
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
if (pending && character.value >= 0 && previous.value > character.value)
|
|
514
|
+
return false;
|
|
515
|
+
range_end = pending;
|
|
516
|
+
pending = false;
|
|
517
|
+
previous = character;
|
|
518
|
+
after_class = class;
|
|
519
|
+
first = false;
|
|
520
|
+
character = have_next ? next : read_ere_member(reader);
|
|
521
|
+
}
|
|
522
|
+
return true;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
static bool check_ere_ranges(TSLexer *lexer) {
|
|
526
|
+
EreReader reader = {.lexer = lexer};
|
|
527
|
+
for (;;) {
|
|
528
|
+
EreCharacter character = read_ere_member(&reader);
|
|
529
|
+
if (character.value < 0) return true;
|
|
530
|
+
if (
|
|
531
|
+
character.value == '[' && !character.quoted && !check_ere_bracket(&reader)
|
|
532
|
+
)
|
|
533
|
+
return false;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
395
537
|
static bool scan_slash_start(
|
|
396
538
|
ScannerState *state,
|
|
397
539
|
TSLexer *lexer,
|
|
@@ -402,8 +544,10 @@ static bool scan_slash_start(
|
|
|
402
544
|
if (valid_symbols[DIVISION_SLASH]) {
|
|
403
545
|
return lexer->lookahead != '=' && emit(lexer, DIVISION_SLASH);
|
|
404
546
|
}
|
|
405
|
-
|
|
406
|
-
|
|
547
|
+
if (!valid_symbols[ERE_OPENING_SLASH]) return false;
|
|
548
|
+
const enum TokenType token =
|
|
549
|
+
check_ere_ranges(lexer) ? ERE_OPENING_SLASH : ERROR_SENTINEL;
|
|
550
|
+
return emit_mode(state, lexer, LEXICAL_MODE_ERE_BODY, token);
|
|
407
551
|
}
|
|
408
552
|
|
|
409
553
|
static bool scan_escape(const ScannerState *state, TSLexer *lexer) {
|
|
@@ -582,6 +726,37 @@ static bool scan_ere_compound_token(
|
|
|
582
726
|
return valid_symbols[delimiter] && emit_mode(state, lexer, mode, delimiter);
|
|
583
727
|
}
|
|
584
728
|
|
|
729
|
+
enum { ERE_DUP_MAX = 255 };
|
|
730
|
+
|
|
731
|
+
static bool scan_ere_count(TSLexer *lexer, unsigned *count) {
|
|
732
|
+
*count = 0;
|
|
733
|
+
do {
|
|
734
|
+
*count = *count * 10 + (unsigned)(lexer->lookahead - '0');
|
|
735
|
+
if (*count > ERE_DUP_MAX) return false;
|
|
736
|
+
lexer->advance(lexer, false);
|
|
737
|
+
} while (is_ascii_digit(lexer->lookahead));
|
|
738
|
+
return true;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
static bool scan_ere_brace(TSLexer *lexer, const bool *valid_symbols) {
|
|
742
|
+
lexer->advance(lexer, false);
|
|
743
|
+
lexer->mark_end(lexer);
|
|
744
|
+
if (!is_ascii_digit(lexer->lookahead))
|
|
745
|
+
return valid_symbols[ERE_LITERAL_BRACE] && emit(lexer, ERE_LITERAL_BRACE);
|
|
746
|
+
if (!valid_symbols[ERE_INTERVAL_OPENING]) return false;
|
|
747
|
+
unsigned minimum, maximum;
|
|
748
|
+
if (!scan_ere_count(lexer, &minimum)) return false;
|
|
749
|
+
if (lexer->lookahead == ',') {
|
|
750
|
+
lexer->advance(lexer, false);
|
|
751
|
+
if (
|
|
752
|
+
is_ascii_digit(lexer->lookahead) &&
|
|
753
|
+
(!scan_ere_count(lexer, &maximum) || maximum < minimum)
|
|
754
|
+
)
|
|
755
|
+
return false;
|
|
756
|
+
}
|
|
757
|
+
return lexer->lookahead == '}' && emit(lexer, ERE_INTERVAL_OPENING);
|
|
758
|
+
}
|
|
759
|
+
|
|
585
760
|
static bool scan_ere_context(
|
|
586
761
|
ScannerState *state,
|
|
587
762
|
TSLexer *lexer,
|
|
@@ -589,6 +764,11 @@ static bool scan_ere_context(
|
|
|
589
764
|
bool recovering
|
|
590
765
|
) {
|
|
591
766
|
if (!recovering) {
|
|
767
|
+
if (
|
|
768
|
+
lexer->lookahead == '{' &&
|
|
769
|
+
(valid_symbols[ERE_INTERVAL_OPENING] || valid_symbols[ERE_LITERAL_BRACE])
|
|
770
|
+
)
|
|
771
|
+
return scan_ere_brace(lexer, valid_symbols);
|
|
592
772
|
if (
|
|
593
773
|
(state->mode == LEXICAL_MODE_ERE_COLLATING ||
|
|
594
774
|
state->mode == LEXICAL_MODE_ERE_EQUIVALENCE) &&
|
|
@@ -934,6 +1114,7 @@ enum ExpressionPrecedence {
|
|
|
934
1114
|
EXPR_POSTFIX = 23,
|
|
935
1115
|
EXPR_FIELD = 24,
|
|
936
1116
|
EXPR_FILE_SOURCE = EXPR_ADDITIVE,
|
|
1117
|
+
EXPR_INPUT_PIPE = 13,
|
|
937
1118
|
};
|
|
938
1119
|
|
|
939
1120
|
static int expression_right_precedence(int precedence) {
|
|
@@ -1031,6 +1212,7 @@ enum {
|
|
|
1031
1212
|
SIGNATURE_UNARY = 1,
|
|
1032
1213
|
SIGNATURE_POSTFIX = 2,
|
|
1033
1214
|
SIGNATURE_LVALUE = 4,
|
|
1215
|
+
SIGNATURE_TARGET_END = 1 << 17,
|
|
1034
1216
|
};
|
|
1035
1217
|
|
|
1036
1218
|
static uint32_t boundary_signature(
|
|
@@ -1052,7 +1234,7 @@ static int signature_ceiling(uint32_t signature) {
|
|
|
1052
1234
|
}
|
|
1053
1235
|
|
|
1054
1236
|
static int signature_kind(uint32_t signature) {
|
|
1055
|
-
return (int)(signature >> 8);
|
|
1237
|
+
return (int)((signature & ~SIGNATURE_TARGET_END) >> 8);
|
|
1056
1238
|
}
|
|
1057
1239
|
|
|
1058
1240
|
static uint64_t make_descriptor(uint32_t signature, uint32_t length) {
|
|
@@ -1071,7 +1253,7 @@ static int descriptor_kind(uint64_t descriptor) {
|
|
|
1071
1253
|
return signature_kind(descriptor_signature(descriptor));
|
|
1072
1254
|
}
|
|
1073
1255
|
|
|
1074
|
-
enum { SAVED_SIGNATURE_BITS =
|
|
1256
|
+
enum { SAVED_SIGNATURE_BITS = 18 };
|
|
1075
1257
|
static const uint64_t SAVED_SIGNATURE_MASK =
|
|
1076
1258
|
(UINT64_C(1) << SAVED_SIGNATURE_BITS) - 1;
|
|
1077
1259
|
static const uint64_t SAVED_HIGH_MASK =
|
|
@@ -1113,10 +1295,31 @@ static int recognizer_token(const RecognizerInput *parser, size_t position) {
|
|
|
1113
1295
|
return position < parser->length ? parser->tokens[position] : PROBE_END;
|
|
1114
1296
|
}
|
|
1115
1297
|
|
|
1298
|
+
static bool fixed_getline_target(const RecognizerInput *input, size_t start) {
|
|
1299
|
+
const int target = recognizer_token(input, start + 1);
|
|
1300
|
+
const int operand =
|
|
1301
|
+
target == '$' ? recognizer_token(input, start + 2) : PROBE_END;
|
|
1302
|
+
return target == PROBE_NAME ||
|
|
1303
|
+
operand == PROBE_NAME ||
|
|
1304
|
+
operand == PROBE_VALUE;
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
static bool ends_at_fixed_target(
|
|
1308
|
+
const RecognizerInput *input,
|
|
1309
|
+
int kind,
|
|
1310
|
+
size_t start,
|
|
1311
|
+
bool target,
|
|
1312
|
+
bool last
|
|
1313
|
+
) {
|
|
1314
|
+
return kind != RECOGNIZED_POSTFIX &&
|
|
1315
|
+
((kind == RECOGNIZED_GET && target && fixed_getline_target(input, start)) ||
|
|
1316
|
+
last);
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1116
1319
|
typedef struct {
|
|
1117
1320
|
size_t start, end;
|
|
1118
1321
|
int kind, ceiling;
|
|
1119
|
-
bool lvalue, postfix, unary;
|
|
1322
|
+
bool lvalue, postfix, unary, target_end;
|
|
1120
1323
|
uint64_t children[2];
|
|
1121
1324
|
size_t child_indices[2];
|
|
1122
1325
|
} RecognizedValue;
|
|
@@ -1168,12 +1371,13 @@ typedef struct {
|
|
|
1168
1371
|
|
|
1169
1372
|
static uint32_t value_signature(RecognizedValue value) {
|
|
1170
1373
|
return boundary_signature(
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1374
|
+
value.kind,
|
|
1375
|
+
value.ceiling,
|
|
1376
|
+
value.lvalue,
|
|
1377
|
+
value.postfix,
|
|
1378
|
+
value.unary
|
|
1379
|
+
) |
|
|
1380
|
+
(value.target_end ? SIGNATURE_TARGET_END : 0u);
|
|
1177
1381
|
}
|
|
1178
1382
|
|
|
1179
1383
|
static size_t recognizer_context_hash(RecognizerContext value) {
|
|
@@ -1383,6 +1587,13 @@ static RecognizedValue recognizer_node(
|
|
|
1383
1587
|
.start = start,
|
|
1384
1588
|
.end = end,
|
|
1385
1589
|
.ceiling = EXPR_FIELD,
|
|
1590
|
+
.target_end = ends_at_fixed_target(
|
|
1591
|
+
parser->input,
|
|
1592
|
+
kind,
|
|
1593
|
+
start,
|
|
1594
|
+
right.end,
|
|
1595
|
+
right.end ? right.target_end : left.target_end
|
|
1596
|
+
),
|
|
1386
1597
|
};
|
|
1387
1598
|
}
|
|
1388
1599
|
|
|
@@ -1400,8 +1611,8 @@ static Continuation continuation(
|
|
|
1400
1611
|
const int token = recognizer_token(input, end);
|
|
1401
1612
|
const int ceiling = signature_ceiling(left);
|
|
1402
1613
|
const bool lvalue = left & SIGNATURE_LVALUE;
|
|
1403
|
-
const bool pipe = token == '|'
|
|
1404
|
-
const int precedence = pipe ?
|
|
1614
|
+
const bool pipe = token == '|';
|
|
1615
|
+
const int precedence = pipe ? EXPR_INPUT_PIPE : probe_precedence(token);
|
|
1405
1616
|
return (Continuation){
|
|
1406
1617
|
.token = token,
|
|
1407
1618
|
.precedence = precedence,
|
|
@@ -1409,12 +1620,12 @@ static Continuation continuation(
|
|
|
1409
1620
|
.postfix = token == PROBE_UPDATE &&
|
|
1410
1621
|
lvalue &&
|
|
1411
1622
|
(left & SIGNATURE_POSTFIX) &&
|
|
1623
|
+
!(left & SIGNATURE_TARGET_END) &&
|
|
1412
1624
|
minimum <= EXPR_POSTFIX &&
|
|
1413
1625
|
EXPR_POSTFIX <= ceiling,
|
|
1414
|
-
.eligible =
|
|
1415
|
-
minimum <= precedence &&
|
|
1626
|
+
.eligible = minimum <= precedence &&
|
|
1416
1627
|
precedence <= ceiling &&
|
|
1417
|
-
(
|
|
1628
|
+
(precedence != EXPR_ASSIGNMENT || lvalue),
|
|
1418
1629
|
};
|
|
1419
1630
|
}
|
|
1420
1631
|
|
|
@@ -1477,6 +1688,7 @@ static void recognizer_step(Recognizer *parser) {
|
|
|
1477
1688
|
const int next = recognizer_token(parser->input, frame->start + 1);
|
|
1478
1689
|
if (next == PROBE_NAME || next == '$') {
|
|
1479
1690
|
parser->alternatives[frame->start] =
|
|
1691
|
+
!fixed_getline_target(parser->input, frame->start) &&
|
|
1480
1692
|
parser->choices[frame->start] == 0;
|
|
1481
1693
|
if (!parser->choices[frame->start]) {
|
|
1482
1694
|
frame->stage = RECOGNIZER_TARGET;
|
|
@@ -1714,6 +1926,17 @@ static SharedNode shared_node(
|
|
|
1714
1926
|
.children = {left ? left : right, left ? right : 0},
|
|
1715
1927
|
.decision = UINT32_MAX,
|
|
1716
1928
|
};
|
|
1929
|
+
const uint32_t last = right ? right : left;
|
|
1930
|
+
if (
|
|
1931
|
+
ends_at_fixed_target(
|
|
1932
|
+
parser->input,
|
|
1933
|
+
kind,
|
|
1934
|
+
start,
|
|
1935
|
+
right,
|
|
1936
|
+
last && (shared_shape(parser->nodes[last - 1]) & SIGNATURE_TARGET_END)
|
|
1937
|
+
)
|
|
1938
|
+
)
|
|
1939
|
+
node.descriptor |= (uint64_t)SIGNATURE_TARGET_END << 32;
|
|
1717
1940
|
if (kind == RECOGNIZED_GET) {
|
|
1718
1941
|
node.decision = start;
|
|
1719
1942
|
node.rank = right ? 1 : 2;
|
|
@@ -1884,23 +2107,24 @@ static void shared_get(
|
|
|
1884
2107
|
uint32_t start,
|
|
1885
2108
|
bool redirected
|
|
1886
2109
|
) {
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
result,
|
|
1890
|
-
shared_node(
|
|
2110
|
+
if (!fixed_getline_target(parser->input, start))
|
|
2111
|
+
shared_add(
|
|
1891
2112
|
parser,
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
2113
|
+
result,
|
|
2114
|
+
shared_node(
|
|
2115
|
+
parser,
|
|
2116
|
+
RECOGNIZED_GET,
|
|
2117
|
+
start,
|
|
2118
|
+
start + 1,
|
|
2119
|
+
0,
|
|
2120
|
+
0,
|
|
2121
|
+
EXPR_FIELD,
|
|
2122
|
+
false,
|
|
2123
|
+
true,
|
|
2124
|
+
false
|
|
2125
|
+
),
|
|
2126
|
+
0
|
|
2127
|
+
);
|
|
1904
2128
|
SharedSet *targets = shared_expr(parser, start + 1, RECOGNIZED_LVALUE);
|
|
1905
2129
|
for (size_t i = 0; i < targets->length && !parser->failed; i++) {
|
|
1906
2130
|
uint32_t target = targets->items[i];
|