@konomanoasa/tree-sitter-diagnostic-sh 0.2.0 → 0.4.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/src/scanner.c CHANGED
@@ -94,6 +94,7 @@ enum TokenType {
94
94
  CASE_PATTERN_SEPARATOR_ERROR,
95
95
  PARENTHESIS_GAP,
96
96
  BRACE_GAP,
97
+ SPECIAL_BUILTIN_FUNCTION_NAME,
97
98
  FUNCTION_BODY_GAP,
98
99
  INVALID_FUNCTION_BODY,
99
100
  FUNCTION_PARAMETER_ERROR,
@@ -147,6 +148,7 @@ struct Capture {
147
148
  size_t count, capacity;
148
149
  bool quoted, failed, newlines_only;
149
150
  struct Unit newline;
151
+ size_t uncertain;
150
152
  struct Capture *parent;
151
153
  };
152
154
  struct Rejected {
@@ -177,10 +179,9 @@ struct Owner {
177
179
  size_t first, limit;
178
180
  bool retained;
179
181
  };
180
- // Defer region extension until publication so rejected probes can discard it.
182
+ // Defer extensions so rejected probes leave no changes.
181
183
  struct Extension {
182
- size_t issue;
183
- struct Recovery recovery;
184
+ size_t position, resume;
184
185
  };
185
186
  struct Plan {
186
187
  struct Issue *issues;
@@ -669,7 +670,7 @@ static bool arithmetic_build_graph(struct ArithmeticGraph *graph) {
669
670
  if (!arithmetic_step(graph, origin, graph->pending[i])) return false;
670
671
  if (graph->valid) return true;
671
672
  }
672
- // Clearing only the visited positions keeps each origin's lexing linear.
673
+ // Avoid rescanning the full source for each origin.
673
674
  for (size_t i = 0; i < graph->pending_count; i++)
674
675
  graph->visited[graph->pending[i].position] = 0;
675
676
  for (size_t i = 0; i < graph->endpoint_count; i++) {
@@ -723,6 +724,211 @@ enum {
723
724
  ARITHMETIC_RULE_COUNT = sizeof(arithmetic_rules) / sizeof(*arithmetic_rules)
724
725
  };
725
726
 
727
+ enum ArithmeticOperator {
728
+ ARITHMETIC_OP_LEFT,
729
+ ARITHMETIC_OP_QUESTION,
730
+ ARITHMETIC_OP_ASSIGN,
731
+ ARITHMETIC_OP_CONDITIONAL,
732
+ ARITHMETIC_OP_BINARY,
733
+ ARITHMETIC_OP_UNARY,
734
+ };
735
+
736
+ static unsigned arithmetic_precedence(enum ArithmeticOperator operation) {
737
+ switch (operation) {
738
+ case ARITHMETIC_OP_ASSIGN:
739
+ return 1;
740
+ case ARITHMETIC_OP_QUESTION:
741
+ case ARITHMETIC_OP_CONDITIONAL:
742
+ return 2;
743
+ case ARITHMETIC_OP_BINARY:
744
+ return 3;
745
+ case ARITHMETIC_OP_UNARY:
746
+ return 4;
747
+ default:
748
+ return 0;
749
+ }
750
+ }
751
+
752
+ static bool arithmetic_reduce(
753
+ enum ArithmeticOperator operation,
754
+ uint8_t *values,
755
+ size_t *count
756
+ ) {
757
+ size_t operands = operation == ARITHMETIC_OP_UNARY ? 1
758
+ : operation == ARITHMETIC_OP_CONDITIONAL ? 3
759
+ : 2;
760
+ if (
761
+ operation == ARITHMETIC_OP_LEFT ||
762
+ operation == ARITHMETIC_OP_QUESTION ||
763
+ *count < operands
764
+ )
765
+ return false;
766
+ *count -= operands;
767
+ if (operation == ARITHMETIC_OP_ASSIGN && !values[*count]) return false;
768
+ values[(*count)++] = false;
769
+ return true;
770
+ }
771
+
772
+ static enum ArithmeticValidation
773
+ arithmetic_literal(const int32_t *source, size_t length) {
774
+ uint8_t *values = ts_malloc((length + 1) * sizeof(*values));
775
+ uint8_t *operations = ts_malloc((length + 1) * sizeof(*operations));
776
+ enum ArithmeticValidation result = ARITHMETIC_VALIDATION_RESOURCE_FAILURE;
777
+ if (!values || !operations) goto finish;
778
+ result = ARITHMETIC_VALIDATION_INVALID;
779
+ size_t value_count = 0, operation_count = 0, position = 0;
780
+ bool operand = true;
781
+ while (position < length) {
782
+ if (arithmetic_whitespace(source[position])) {
783
+ position++;
784
+ continue;
785
+ }
786
+ int state = LEX_START;
787
+ while (position < length) {
788
+ int next = arithmetic_lex((uint8_t)state, source[position]);
789
+ if (next == LEX_INVALID) goto finish;
790
+ if (next == LEX_STOP) break;
791
+ state = next;
792
+ position++;
793
+ }
794
+ uint16_t token = arithmetic_tokens[state];
795
+ if (!token) goto finish;
796
+ if (operand) {
797
+ if (token & (1u << ARITHMETIC_UNARY_OPERATOR))
798
+ operations[operation_count++] = ARITHMETIC_OP_UNARY;
799
+ else if (token & (1u << ARITHMETIC_LEFT))
800
+ operations[operation_count++] = ARITHMETIC_OP_LEFT;
801
+ else if (token & ((1u << ARITHMETIC_NAME) | (1u << ARITHMETIC_NUMBER))) {
802
+ values[value_count++] = (token & (1u << ARITHMETIC_NAME)) != 0;
803
+ operand = false;
804
+ } else goto finish;
805
+ continue;
806
+ }
807
+ if (token & ((1u << ARITHMETIC_RIGHT) | (1u << ARITHMETIC_COLON))) {
808
+ enum ArithmeticOperator stop = token & (1u << ARITHMETIC_RIGHT)
809
+ ? ARITHMETIC_OP_LEFT
810
+ : ARITHMETIC_OP_QUESTION;
811
+ while (operation_count && operations[operation_count - 1] != stop) {
812
+ if (!arithmetic_reduce(
813
+ operations[--operation_count],
814
+ values,
815
+ &value_count
816
+ ))
817
+ goto finish;
818
+ }
819
+ if (!operation_count) goto finish;
820
+ if (stop == ARITHMETIC_OP_LEFT) operation_count--;
821
+ else {
822
+ operations[operation_count - 1] = ARITHMETIC_OP_CONDITIONAL;
823
+ operand = true;
824
+ }
825
+ continue;
826
+ }
827
+ enum ArithmeticOperator operation;
828
+ if (token & (1u << ARITHMETIC_ASSIGNMENT_OPERATOR))
829
+ operation = ARITHMETIC_OP_ASSIGN;
830
+ else if (token & (1u << ARITHMETIC_QUESTION))
831
+ operation = ARITHMETIC_OP_QUESTION;
832
+ else if (token & (1u << ARITHMETIC_BINARY_OPERATOR))
833
+ operation = ARITHMETIC_OP_BINARY;
834
+ else goto finish;
835
+ while (operation_count) {
836
+ enum ArithmeticOperator previous = operations[operation_count - 1];
837
+ if (previous == ARITHMETIC_OP_LEFT || previous == ARITHMETIC_OP_QUESTION)
838
+ break;
839
+ if (
840
+ arithmetic_precedence(previous) < arithmetic_precedence(operation) ||
841
+ (arithmetic_precedence(previous) == arithmetic_precedence(operation) &&
842
+ operation != ARITHMETIC_OP_BINARY)
843
+ )
844
+ break;
845
+ operation_count--;
846
+ if (!arithmetic_reduce(previous, values, &value_count)) goto finish;
847
+ }
848
+ operations[operation_count++] = operation;
849
+ operand = true;
850
+ }
851
+ if (operand) goto finish;
852
+ while (operation_count)
853
+ if (!arithmetic_reduce(operations[--operation_count], values, &value_count))
854
+ goto finish;
855
+ if (value_count == 1) result = ARITHMETIC_VALIDATION_VALID;
856
+ finish:
857
+ ts_free(values);
858
+ ts_free(operations);
859
+ return result;
860
+ }
861
+
862
+ // Holes invalidate the outer stack.
863
+ static enum ArithmeticValidation
864
+ arithmetic_check_structure(const int32_t *source, size_t length) {
865
+ struct ArithmeticGroup {
866
+ size_t start, end;
867
+ };
868
+ struct ArithmeticGroup *groups = NULL;
869
+ size_t *stack = NULL;
870
+ size_t group_count = 0, group_capacity = 0, depth = 0, capacity = 0;
871
+ bool unknown = false;
872
+ enum ArithmeticValidation result = ARITHMETIC_VALIDATION_VALID;
873
+ for (size_t i = 0; i < length; i++) {
874
+ int32_t c = source[i];
875
+ if (c == -1) {
876
+ depth = 0;
877
+ unknown = true;
878
+ } else if (c == '(' || c == '?') {
879
+ size_t *grown =
880
+ diagnostic_grow(stack, &capacity, depth + 1, sizeof(*grown));
881
+ if (!grown) {
882
+ result = ARITHMETIC_VALIDATION_RESOURCE_FAILURE;
883
+ goto finish;
884
+ }
885
+ stack = grown;
886
+ stack[depth++] = i;
887
+ } else if (c == ')' || c == ':') {
888
+ if (!depth) {
889
+ if (unknown) continue;
890
+ result = ARITHMETIC_VALIDATION_INVALID;
891
+ goto finish;
892
+ }
893
+ size_t start = stack[--depth];
894
+ if (source[start] != (c == ')' ? '(' : '?')) {
895
+ result = ARITHMETIC_VALIDATION_INVALID;
896
+ goto finish;
897
+ }
898
+ if (c != ')') continue;
899
+ while (group_count && groups[group_count - 1].start > start)
900
+ group_count--;
901
+ struct ArithmeticGroup *grown = diagnostic_grow(
902
+ groups,
903
+ &group_capacity,
904
+ group_count + 1,
905
+ sizeof(*grown)
906
+ );
907
+ if (!grown) {
908
+ result = ARITHMETIC_VALIDATION_RESOURCE_FAILURE;
909
+ goto finish;
910
+ }
911
+ groups = grown;
912
+ groups[group_count++] = (struct ArithmeticGroup){start, i + 1};
913
+ }
914
+ }
915
+ if (depth) {
916
+ result = ARITHMETIC_VALIDATION_INVALID;
917
+ goto finish;
918
+ }
919
+ for (size_t i = 0; i < group_count; i++) {
920
+ result = arithmetic_literal(
921
+ source + groups[i].start,
922
+ groups[i].end - groups[i].start
923
+ );
924
+ if (result != ARITHMETIC_VALIDATION_VALID) break;
925
+ }
926
+ finish:
927
+ ts_free(groups);
928
+ ts_free(stack);
929
+ return result;
930
+ }
931
+
726
932
  static uint8_t
727
933
  arithmetic_symbol(const struct ArithmeticRule *rule, size_t dot) {
728
934
  return rule->right[rule->length - dot - 1];
@@ -1279,6 +1485,12 @@ validate_arithmetic(const int32_t *source, size_t length) {
1279
1485
  ) {
1280
1486
  return result;
1281
1487
  }
1488
+ bool dynamic = false;
1489
+ for (size_t i = 0; i < length; i++) dynamic |= source[i] == -1;
1490
+ if (!dynamic) return arithmetic_literal(source, length);
1491
+ result = arithmetic_check_structure(source, length);
1492
+ if (result != ARITHMETIC_VALIDATION_VALID) return result;
1493
+ result = ARITHMETIC_VALIDATION_RESOURCE_FAILURE;
1282
1494
  graph.vertex_ids = ts_malloc((length + 1) * sizeof(*graph.vertex_ids));
1283
1495
  graph.visited = ts_calloc(length + 1, sizeof(*graph.visited));
1284
1496
  graph.emitted = ts_calloc(length + 1, sizeof(*graph.emitted));
@@ -1293,7 +1505,6 @@ validate_arithmetic(const int32_t *source, size_t length) {
1293
1505
  result = ARITHMETIC_VALIDATION_VALID;
1294
1506
  goto finish;
1295
1507
  }
1296
- // Reading backward exposes assignment constraints before ambiguous prefixes.
1297
1508
  result = arithmetic_reverse_graph(&graph);
1298
1509
  if (result != ARITHMETIC_VALIDATION_VALID) goto finish;
1299
1510
  result = arithmetic_project_graph(&graph);
@@ -1382,6 +1593,17 @@ static void capture_change(
1382
1593
  }
1383
1594
  }
1384
1595
 
1596
+ static void capture_uncertain(struct Input *input, size_t position) {
1597
+ for (
1598
+ struct Capture *capture = input->capture; capture; capture = capture->parent
1599
+ )
1600
+ if (
1601
+ !capture->newlines_only &&
1602
+ (!capture->uncertain || position < capture->uncertain - 1)
1603
+ )
1604
+ capture->uncertain = position + 1;
1605
+ }
1606
+
1385
1607
  static void capture_newline(struct Input *input, struct Unit unit) {
1386
1608
  if (unit.value == '\n')
1387
1609
  for (
@@ -1445,7 +1667,6 @@ static bool expansion_escape(int32_t c) {
1445
1667
  return c == '$' || c == '`' || c == '\\';
1446
1668
  }
1447
1669
 
1448
- // Bound recursion to protect the C stack.
1449
1670
  enum { NESTING_LIMIT = 512 };
1450
1671
 
1451
1672
  static bool nest(struct Parser *parser) {
@@ -1540,6 +1761,7 @@ static const uint64_t fault_owners[TOKEN_TYPE_COUNT] = {
1540
1761
  OWNER_BIT(BEGIN_CASE_ITEM) |
1541
1762
  OWNER_BIT(BEGIN_FUNCTION_DEFINITION),
1542
1763
  [BRACE_GAP] = OWNER_BIT(BEGIN_BRACE_GROUP),
1764
+ [SPECIAL_BUILTIN_FUNCTION_NAME] = OWNER_BIT(BEGIN_FUNCTION_DEFINITION),
1543
1765
  [FUNCTION_BODY_GAP] = OWNER_BIT(BEGIN_FUNCTION_DEFINITION),
1544
1766
  [INVALID_FUNCTION_BODY] = OWNER_BIT(BEGIN_FUNCTION_DEFINITION),
1545
1767
  [FUNCTION_PARAMETER_ERROR] = OWNER_BIT(BEGIN_FUNCTION_DEFINITION),
@@ -1625,7 +1847,6 @@ static void owned_issue(
1625
1847
  };
1626
1848
  }
1627
1849
 
1628
- // Backquote faults rank by the lexical context they interrupt, not by owner.
1629
1850
  static size_t fault_context(const struct Parser *parser, struct Fault fault) {
1630
1851
  const struct Owner *owners = parser->plan->owners;
1631
1852
  switch (fault.kind) {
@@ -1660,7 +1881,8 @@ static void issue(
1660
1881
  report(parser, fault, recovery);
1661
1882
  }
1662
1883
 
1663
- static void extend_issue(struct Parser *parser, size_t index, size_t resume) {
1884
+ static void
1885
+ extend_region(struct Parser *parser, size_t position, size_t resume) {
1664
1886
  struct Plan *plan = parser->plan;
1665
1887
  if (plan->failed) return;
1666
1888
  struct Extension *grown = diagnostic_grow(
@@ -1675,7 +1897,7 @@ static void extend_issue(struct Parser *parser, size_t index, size_t resume) {
1675
1897
  }
1676
1898
  plan->extensions = grown;
1677
1899
  plan->extensions[plan->extension_count++] =
1678
- (struct Extension){index, {resume, false}};
1900
+ (struct Extension){position, resume};
1679
1901
  }
1680
1902
 
1681
1903
  static bool backquote_cut(struct Parser *parser, enum TokenType kind) {
@@ -1976,6 +2198,19 @@ static void parameter(struct Parser *parser, bool double_quoted) {
1976
2198
  parser->closer = parent_closer;
1977
2199
  }
1978
2200
 
2201
+ static bool undecidable_delimiter_fault(enum TokenType kind) {
2202
+ switch (kind) {
2203
+ case UNSPECIFIED_DOLLAR_QUOTE_ESCAPE:
2204
+ case UNSPECIFIED_EMPTY_HEX_ESCAPE:
2205
+ case UNSPECIFIED_CONTROL_ESCAPE:
2206
+ case UNSPECIFIED_HEX_ESCAPE:
2207
+ case UNSPECIFIED_OCTAL_ESCAPE:
2208
+ return true;
2209
+ default:
2210
+ return false;
2211
+ }
2212
+ }
2213
+
1979
2214
  static int hex_value(int32_t c) {
1980
2215
  if (is_decimal_digit(c)) return c - '0';
1981
2216
  if (c >= 'a' && c <= 'f') return c - 'a' + 10;
@@ -2079,6 +2314,7 @@ dollar_quote_escape(struct Parser *parser, struct Unit *unit) {
2079
2314
  unit->end = input->units[input->position - 1].end;
2080
2315
  capture_change(input, *unit, unit->value, true);
2081
2316
  }
2317
+ if (undecidable_delimiter_fault(fault)) capture_uncertain(input, unit->start);
2082
2318
  if (!end) end = unit->end;
2083
2319
  owner_end(parser, owner, end);
2084
2320
  return (struct Fault){fault, unit->start, end, owner};
@@ -2088,28 +2324,26 @@ static void dollar_quote(struct Parser *parser, struct Text *text) {
2088
2324
  struct Input *input = &parser->input;
2089
2325
  struct Unit opening = pop(input);
2090
2326
  capture_change(input, opening, -1, true);
2091
- struct Fault fault = {.kind = TOKEN_TYPE_COUNT};
2092
- size_t boundary = input->end;
2093
2327
  bool closed = false;
2094
2328
  while (!parser->plan->failed && input->position < input->count) {
2095
2329
  struct Unit unit = read_raw(input);
2096
2330
  if (unit.value == '\'') {
2097
2331
  capture_change(input, unit, -1, true);
2098
- boundary = unit.start;
2099
2332
  closed = true;
2100
2333
  break;
2101
2334
  }
2102
2335
  if (unit.value == '\\') {
2103
2336
  struct Fault escape = dollar_quote_escape(parser, &unit);
2104
- if (fault.kind == TOKEN_TYPE_COUNT && escape.kind != TOKEN_TYPE_COUNT)
2105
- fault = escape;
2337
+ if (escape.kind != TOKEN_TYPE_COUNT)
2338
+ report(
2339
+ parser,
2340
+ escape,
2341
+ (struct Recovery){escape.end, escape.kind != INCOMPLETE_ESCAPE}
2342
+ );
2106
2343
  }
2107
2344
  text_push(parser, text, unit);
2108
2345
  }
2109
- if (fault.kind != TOKEN_TYPE_COUNT)
2110
- report(parser, fault, (struct Recovery){boundary, closed});
2111
- else if (!closed)
2112
- unclosed(parser, UNDEFINED_BACKQUOTE_QUOTE, DOLLAR_QUOTE_GAP);
2346
+ if (!closed) unclosed(parser, UNDEFINED_BACKQUOTE_QUOTE, DOLLAR_QUOTE_GAP);
2113
2347
  }
2114
2348
 
2115
2349
  struct BackquoteInput {
@@ -2288,6 +2522,8 @@ arithmetic_valid(struct Plan *plan, const struct Text *content, bool open) {
2288
2522
  static void
2289
2523
  replay_capture(struct Parser *parser, const struct Capture *capture) {
2290
2524
  if (capture->newline.end) capture_newline(&parser->input, capture->newline);
2525
+ if (capture->uncertain)
2526
+ capture_uncertain(&parser->input, capture->uncertain - 1);
2291
2527
  for (size_t i = 0; i < capture->count; i++) {
2292
2528
  struct Change change = capture->changes[i];
2293
2529
  capture_change(
@@ -2470,7 +2706,7 @@ static struct Token word(struct Parser *parser) {
2470
2706
  prefix_name = false;
2471
2707
  }
2472
2708
  token.end = consumed_offset(input);
2473
- token.name = token.plain && prefix_name && count > 0;
2709
+ token.name = prefix_name && count > 0;
2474
2710
  if (
2475
2711
  token.plain &&
2476
2712
  digits_only(&token.text) &&
@@ -2560,7 +2796,6 @@ static struct Token lex(struct Parser *parser) {
2560
2796
  return token;
2561
2797
  }
2562
2798
 
2563
- // A newline inside a saved token cannot start the pending here-document body.
2564
2799
  static void saved_token_newline(struct Parser *parser, struct Unit newline) {
2565
2800
  if (!newline.end || parser->token.kind == NEWLINE) return;
2566
2801
  report(
@@ -2807,7 +3042,7 @@ static bool here_document(
2807
3042
  here_document_lines(parser, here, &body, &at_line_start, &boundary);
2808
3043
  if (!here->quoted) here_body(parser, body, boundary);
2809
3044
  ts_free(body.units);
2810
- // An unstarted body has no owner, so its gap must not climb to an outer body.
3045
+ // An unstarted body's gap must not belong to an outer body.
2811
3046
  if (!found && !backquote_cut(parser, UNDEFINED_BACKQUOTE_HERE_DOCUMENT))
2812
3047
  report(
2813
3048
  parser,
@@ -2824,15 +3059,15 @@ static void documents(struct Parser *parser, bool at_line_start) {
2824
3059
  for (size_t document = 0; document < parser->document_count; document++) {
2825
3060
  struct Document *here = &parser->documents[document];
2826
3061
  if (here->uncertain) {
2827
- extend_issue(parser, here->uncertain - 1, input->end);
3062
+ extend_region(parser, here->uncertain - 1, input->end);
2828
3063
  input->position = input->count;
2829
- at_line_start = false;
2830
- } else {
2831
- at_line_start = here_document(parser, here, at_line_start, body_start);
2832
- body_start = consumed_offset(input);
3064
+ break;
2833
3065
  }
2834
- ts_free(here->delimiter.units);
3066
+ at_line_start = here_document(parser, here, at_line_start, body_start);
3067
+ body_start = consumed_offset(input);
2835
3068
  }
3069
+ for (size_t document = 0; document < parser->document_count; document++)
3070
+ ts_free(parser->documents[document].delimiter.units);
2836
3071
  parser->document_count = 0;
2837
3072
  }
2838
3073
 
@@ -2880,6 +3115,27 @@ static bool spelling(const struct Token *token, const char *value) {
2880
3115
  return i == token->text.count && !value[i];
2881
3116
  }
2882
3117
 
3118
+ static bool special_builtin_name(const struct Token *token) {
3119
+ static const char *const names[] = {
3120
+ "break",
3121
+ "continue",
3122
+ "eval",
3123
+ "exec",
3124
+ "exit",
3125
+ "export",
3126
+ "readonly",
3127
+ "return",
3128
+ "set",
3129
+ "shift",
3130
+ "times",
3131
+ "trap",
3132
+ "unset",
3133
+ };
3134
+ for (size_t i = 0; i < sizeof(names) / sizeof(*names); i++)
3135
+ if (spelling(token, names[i])) return true;
3136
+ return false;
3137
+ }
3138
+
2883
3139
  static bool reserved_colon(const struct Token *token) {
2884
3140
  return token->kind == WORD &&
2885
3141
  token->plain &&
@@ -2981,7 +3237,6 @@ static bool command_start(const struct Token *token) {
2981
3237
 
2982
3238
  static bool simple_command_start(const struct Token *token) {
2983
3239
  return (token->kind == WORD || redirect_kind(token->kind)) &&
2984
- !compound_start(token) &&
2985
3240
  !stop(token) &&
2986
3241
  !spelling(token, "!") &&
2987
3242
  !reserved_colon(token);
@@ -2992,23 +3247,14 @@ static bool stop_or_end(struct Parser *parser, uint32_t stops) {
2992
3247
  return token->kind == END || (stop(token) & (stops | parser->stops));
2993
3248
  }
2994
3249
 
2995
- static bool undecidable_delimiter_fault(enum TokenType kind) {
2996
- switch (kind) {
2997
- case UNSPECIFIED_DOLLAR_QUOTE_ESCAPE:
2998
- case UNSPECIFIED_EMPTY_HEX_ESCAPE:
2999
- case UNSPECIFIED_CONTROL_ESCAPE:
3000
- case UNSPECIFIED_HEX_ESCAPE:
3001
- case UNSPECIFIED_OCTAL_ESCAPE:
3002
- return true;
3003
- default:
3004
- return false;
3005
- }
3250
+ static bool command_boundary(struct Parser *parser) {
3251
+ return list_boundary(look(parser)->kind) || stop_or_end(parser, 0);
3006
3252
  }
3007
3253
 
3008
3254
  // Decode faults precede word faults and are sorted by start.
3009
- static size_t uncertain_delimiter(
3255
+ static size_t uncertain_position(
3010
3256
  const struct Plan *plan,
3011
- size_t since,
3257
+ const struct Capture *capture,
3012
3258
  size_t start,
3013
3259
  size_t end
3014
3260
  ) {
@@ -3018,18 +3264,13 @@ static size_t uncertain_delimiter(
3018
3264
  if (plan->issues[middle].start < start) low = middle + 1;
3019
3265
  else high = middle;
3020
3266
  }
3021
- size_t uncertain =
3022
- low < plan->decode_issues && plan->issues[low].start < end ? low + 1 : 0;
3023
- for (size_t i = since; i < plan->issue_count; i++) {
3024
- const struct Issue *found = &plan->issues[i];
3025
- if (
3026
- found->start >= start &&
3027
- found->start < end &&
3028
- undecidable_delimiter_fault(found->kind) &&
3029
- (!uncertain || found->start < plan->issues[uncertain - 1].start)
3030
- )
3031
- uncertain = i + 1;
3032
- }
3267
+ size_t uncertain = capture->uncertain;
3268
+ if (
3269
+ low < plan->decode_issues &&
3270
+ plan->issues[low].start < end &&
3271
+ (!uncertain || plan->issues[low].start < uncertain - 1)
3272
+ )
3273
+ uncertain = plan->issues[low].start + 1;
3033
3274
  return uncertain;
3034
3275
  }
3035
3276
 
@@ -3038,7 +3279,6 @@ static bool declare_here_document(
3038
3279
  bool strip,
3039
3280
  const struct Capture *capture,
3040
3281
  size_t begin,
3041
- size_t since,
3042
3282
  const struct Token *target
3043
3283
  ) {
3044
3284
  struct Document *grown = diagnostic_grow(
@@ -3065,7 +3305,7 @@ static bool declare_here_document(
3065
3305
  .quoted = capture->quoted,
3066
3306
  .strip = strip,
3067
3307
  .uncertain =
3068
- uncertain_delimiter(parser->plan, since, target->start, target->end)
3308
+ uncertain_position(parser->plan, capture, target->start, target->end)
3069
3309
  };
3070
3310
  return true;
3071
3311
  }
@@ -3078,7 +3318,7 @@ enum RecoveryBoundary {
3078
3318
  static bool redirect_operand(struct Parser *parser, enum Lexeme kind) {
3079
3319
  bool here = kind == HERE || kind == HERE_STRIP;
3080
3320
  struct Capture capture = {.parent = parser->input.capture};
3081
- size_t begin = parser->input.position, since = parser->plan->issue_count;
3321
+ size_t begin = parser->input.position;
3082
3322
  bool quoted_delimiter = parser->quoted_delimiter;
3083
3323
  if (here) {
3084
3324
  parser->input.capture = &capture;
@@ -3097,7 +3337,6 @@ static bool redirect_operand(struct Parser *parser, enum Lexeme kind) {
3097
3337
  kind == HERE_STRIP,
3098
3338
  &capture,
3099
3339
  begin,
3100
- since,
3101
3340
  target
3102
3341
  ))
3103
3342
  )
@@ -3148,9 +3387,7 @@ static struct Recovery recover(struct Parser *parser, unsigned boundaries) {
3148
3387
  static void invalid_target(struct Parser *parser, enum Lexeme kind) {
3149
3388
  bool here = kind == HERE || kind == HERE_STRIP;
3150
3389
  const struct Token *target = look(parser);
3151
- bool missing = list_boundary(target->kind) ||
3152
- redirect_kind(target->kind) ||
3153
- stop_or_end(parser, 0);
3390
+ bool missing = redirect_kind(target->kind) || command_boundary(parser);
3154
3391
  struct Fault fault = token_fault(
3155
3392
  parser,
3156
3393
  missing,
@@ -3523,9 +3760,17 @@ static void unexpected(struct Parser *parser) {
3523
3760
  report(parser, fault, recovery);
3524
3761
  }
3525
3762
 
3526
- static void function_definition(struct Parser *parser) {
3763
+ static void function_definition(struct Parser *parser, bool special_builtin) {
3527
3764
  if (parser->owner && !parser->opaque)
3528
3765
  parser->plan->owners[parser->owner].kind = BEGIN_FUNCTION_DEFINITION;
3766
+ if (special_builtin)
3767
+ issue(
3768
+ parser,
3769
+ SPECIAL_BUILTIN_FUNCTION_NAME,
3770
+ parser->consumed_start,
3771
+ parser->consumed,
3772
+ (struct Recovery){parser->consumed, true}
3773
+ );
3529
3774
  take(parser);
3530
3775
  bool damaged = false;
3531
3776
  if (look(parser)->kind != RIGHT && !stop_or_end(parser, 0)) {
@@ -3547,7 +3792,7 @@ static void function_definition(struct Parser *parser) {
3547
3792
  compound(parser);
3548
3793
  return;
3549
3794
  }
3550
- bool missing = list_boundary(look(parser)->kind) || stop_or_end(parser, 0);
3795
+ bool missing = command_boundary(parser);
3551
3796
  struct Fault fault = token_fault(
3552
3797
  parser,
3553
3798
  missing,
@@ -3557,20 +3802,13 @@ static void function_definition(struct Parser *parser) {
3557
3802
  report(parser, fault, recover(parser, RECOVER_CONNECTORS));
3558
3803
  }
3559
3804
 
3560
- static void command_contents(struct Parser *parser, bool simple) {
3561
- if (compound_start(look(parser))) {
3562
- compound(parser);
3563
- return;
3564
- }
3565
- if (!simple) {
3566
- unexpected(parser);
3567
- return;
3568
- }
3805
+ static void simple_command(struct Parser *parser) {
3569
3806
  if (look(parser)->kind == WORD) {
3570
3807
  bool name = look(parser)->name;
3808
+ bool special_builtin = name && special_builtin_name(look(parser));
3571
3809
  take(parser);
3572
3810
  if (name && look(parser)->kind == LEFT) {
3573
- function_definition(parser);
3811
+ function_definition(parser, special_builtin);
3574
3812
  return;
3575
3813
  }
3576
3814
  } else redirect(parser);
@@ -3582,15 +3820,13 @@ static void command_contents(struct Parser *parser, bool simple) {
3582
3820
 
3583
3821
  static void command(struct Parser *parser) {
3584
3822
  const struct Token *token = look(parser);
3585
- bool simple = simple_command_start(token);
3586
- size_t owner =
3587
- simple ? owner_begin(parser, BEGIN_SIMPLE_COMMAND, token->start) : 0;
3588
- command_contents(parser, simple);
3589
- owner_end(parser, owner, parser->consumed);
3590
- }
3591
-
3592
- static bool command_boundary(struct Parser *parser) {
3593
- return list_boundary(look(parser)->kind) || stop_or_end(parser, 0);
3823
+ if (compound_start(token)) compound(parser);
3824
+ else if (!simple_command_start(token)) unexpected(parser);
3825
+ else {
3826
+ size_t owner = owner_begin(parser, BEGIN_SIMPLE_COMMAND, token->start);
3827
+ simple_command(parser);
3828
+ owner_end(parser, owner, parser->consumed);
3829
+ }
3594
3830
  }
3595
3831
 
3596
3832
  static void missing_command(struct Parser *parser) {
@@ -3632,18 +3868,19 @@ static void pipeline_contents(struct Parser *parser) {
3632
3868
  }
3633
3869
  }
3634
3870
 
3871
+ static size_t command_owner(struct Parser *parser, enum TokenType kind) {
3872
+ const struct Token *token = look(parser);
3873
+ return command_start(token) ? owner_begin(parser, kind, token->start) : 0;
3874
+ }
3875
+
3635
3876
  static void pipeline(struct Parser *parser) {
3636
- size_t owner = 0;
3637
- if (command_start(look(parser)))
3638
- owner = owner_begin(parser, BEGIN_PIPELINE, look(parser)->start);
3877
+ size_t owner = command_owner(parser, BEGIN_PIPELINE);
3639
3878
  pipeline_contents(parser);
3640
3879
  owner_end(parser, owner, parser->consumed);
3641
3880
  }
3642
3881
 
3643
3882
  static void and_or(struct Parser *parser) {
3644
- size_t owner = 0;
3645
- if (command_start(look(parser)))
3646
- owner = owner_begin(parser, BEGIN_AND_OR, look(parser)->start);
3883
+ size_t owner = command_owner(parser, BEGIN_AND_OR);
3647
3884
  parser->resumed = false;
3648
3885
  if (list_boundary(look(parser)->kind)) unexpected(parser);
3649
3886
  else {
@@ -3688,13 +3925,16 @@ static bool commands(
3688
3925
  linebreak(parser);
3689
3926
  while (!parser->plan->failed && !stop_or_end(parser, 0)) {
3690
3927
  any = true;
3691
- if (!owner && command_start(look(parser)))
3692
- owner = owner_begin(parser, list, look(parser)->start);
3693
- and_or(parser);
3694
- command_separation(parser);
3695
- if (stop_or_end(parser, 0)) break;
3928
+ if (!owner) owner = command_owner(parser, list);
3696
3929
  enum Lexeme kind = look(parser)->kind;
3697
- if (kind == SEMICOLON || kind == AMPERSAND) take(parser);
3930
+ if (kind == SEMICOLON || kind == AMPERSAND) unexpected(parser);
3931
+ else {
3932
+ and_or(parser);
3933
+ command_separation(parser);
3934
+ if (stop_or_end(parser, 0)) break;
3935
+ kind = look(parser)->kind;
3936
+ if (kind == SEMICOLON || kind == AMPERSAND) take(parser);
3937
+ }
3698
3938
  if (look(parser)->kind == NEWLINE) {
3699
3939
  if (list == BEGIN_LIST) {
3700
3940
  owner_end(parser, owner, parser->consumed);
@@ -3773,6 +4013,11 @@ static int compare_issues(const void *left, const void *right) {
3773
4013
  return a->start < b->start ? -1 : a->start != b->start;
3774
4014
  }
3775
4015
 
4016
+ static int compare_extensions(const void *left, const void *right) {
4017
+ const struct Extension *a = left, *b = right;
4018
+ return a->position < b->position ? -1 : a->position != b->position;
4019
+ }
4020
+
3776
4021
  static bool event(struct Scanner *scanner, enum TokenType kind, size_t end) {
3777
4022
  struct Event *grown = diagnostic_grow(
3778
4023
  scanner->events,
@@ -3967,7 +4212,7 @@ static bool later_child(const struct Plan *plan, size_t a, size_t b) {
3967
4212
  return a > b;
3968
4213
  }
3969
4214
 
3970
- // Sibling starts and ends are ordered, allowing backward search to stop early.
4215
+ // Ordered sibling ranges allow early termination.
3971
4216
  static size_t place_unowned(
3972
4217
  const struct Plan *plan,
3973
4218
  const struct PublicChild *children,
@@ -4003,7 +4248,6 @@ static size_t place_unowned(
4003
4248
  return best;
4004
4249
  }
4005
4250
 
4006
- // Stages are compared only after ownership and lexical context agree.
4007
4251
  static unsigned requirement_order(enum TokenType kind) {
4008
4252
  switch (kind) {
4009
4253
  case INVALID_FOR_HEADER:
@@ -4053,15 +4297,17 @@ static bool ancestor(const struct Plan *plan, size_t outer, size_t inner) {
4053
4297
  return false;
4054
4298
  }
4055
4299
 
4300
+ static bool input_fault(enum TokenType kind) {
4301
+ return kind == INVALID_ENCODING || kind == NUL_CHARACTER;
4302
+ }
4303
+
4056
4304
  static bool replaces_issue(
4057
4305
  const struct Plan *plan,
4058
4306
  const struct Issue *candidate,
4059
4307
  const struct Issue *current
4060
4308
  ) {
4061
- bool current_input =
4062
- current->kind == INVALID_ENCODING || current->kind == NUL_CHARACTER;
4063
- bool candidate_input =
4064
- candidate->kind == INVALID_ENCODING || candidate->kind == NUL_CHARACTER;
4309
+ bool current_input = input_fault(current->kind);
4310
+ bool candidate_input = input_fault(candidate->kind);
4065
4311
  if (current_input != candidate_input) return candidate_input;
4066
4312
  if (!!candidate->owner != !!current->owner) return candidate->owner != 0;
4067
4313
  if (candidate->owner && current->owner) {
@@ -4113,13 +4359,29 @@ static size_t select_regions(struct Plan *plan, size_t end) {
4113
4359
  sizeof(*plan->issues),
4114
4360
  compare_issues
4115
4361
  );
4116
- size_t count = 0;
4362
+ if (plan->extension_count > 1)
4363
+ qsort(
4364
+ plan->extensions,
4365
+ plan->extension_count,
4366
+ sizeof(*plan->extensions),
4367
+ compare_extensions
4368
+ );
4369
+ size_t count = 0, input = 0, extension = 0;
4117
4370
  for (size_t i = 0; i < plan->issue_count;) {
4371
+ if (input < i) input = i;
4372
+ while (input < plan->issue_count && !input_fault(plan->issues[input].kind))
4373
+ input++;
4118
4374
  size_t start = plan->issues[i].start;
4119
4375
  struct Issue current = {0};
4120
4376
  bool found = false;
4121
4377
  for (; i < plan->issue_count && plan->issues[i].start == start; i++) {
4122
4378
  const struct Issue *candidate = &plan->issues[i];
4379
+ if (
4380
+ !input_fault(candidate->kind) &&
4381
+ input < plan->issue_count &&
4382
+ plan->issues[input].start < candidate->end
4383
+ )
4384
+ continue;
4123
4385
  if (
4124
4386
  covered(plan, candidate, count ? &plan->issues[count - 1] : NULL, end)
4125
4387
  )
@@ -4130,6 +4392,14 @@ static size_t select_regions(struct Plan *plan, size_t end) {
4130
4392
  }
4131
4393
  }
4132
4394
  if (!found) continue;
4395
+ for (; extension < plan->extension_count; extension++) {
4396
+ size_t position = plan->extensions[extension].position;
4397
+ size_t resume = plan->extensions[extension].resume;
4398
+ if (position > current.start && position >= current.recovery.position)
4399
+ break;
4400
+ if (position >= current.start && resume >= current.recovery.position)
4401
+ current.recovery = (struct Recovery){resume, false};
4402
+ }
4133
4403
  plan->issues[count++] = current;
4134
4404
  if (
4135
4405
  current.owner &&
@@ -4206,9 +4476,6 @@ static bool append_unowned(
4206
4476
 
4207
4477
  static bool publish(struct Scanner *scanner, struct Plan *plan, size_t end) {
4208
4478
  if (plan->failed) return false;
4209
- for (size_t i = 0; i < plan->extension_count; i++)
4210
- plan->issues[plan->extensions[i].issue].recovery =
4211
- plan->extensions[i].recovery;
4212
4479
  plan->issue_count = select_regions(plan, end);
4213
4480
  extend_ancestors(plan);
4214
4481
  size_t count = plan->issue_count;