@konomanoasa/tree-sitter-diagnostic-sh 0.3.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,
@@ -178,7 +179,7 @@ struct Owner {
178
179
  size_t first, limit;
179
180
  bool retained;
180
181
  };
181
- // Defer region extension until publication so rejected probes can discard it.
182
+ // Defer extensions so rejected probes leave no changes.
182
183
  struct Extension {
183
184
  size_t position, resume;
184
185
  };
@@ -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,7 +724,6 @@ enum {
723
724
  ARITHMETIC_RULE_COUNT = sizeof(arithmetic_rules) / sizeof(*arithmetic_rules)
724
725
  };
725
726
 
726
- // Only assignment needs lvalue tracking; operator results are non-lvalues.
727
727
  enum ArithmeticOperator {
728
728
  ARITHMETIC_OP_LEFT,
729
729
  ARITHMETIC_OP_QUESTION,
@@ -859,8 +859,7 @@ finish:
859
859
  return result;
860
860
  }
861
861
 
862
- // Holes leave the outer stack unknown; only later punctuation constrains it.
863
- // Check each outermost hole-free group once.
862
+ // Holes invalidate the outer stack.
864
863
  static enum ArithmeticValidation
865
864
  arithmetic_check_structure(const int32_t *source, size_t length) {
866
865
  struct ArithmeticGroup {
@@ -1506,7 +1505,6 @@ validate_arithmetic(const int32_t *source, size_t length) {
1506
1505
  result = ARITHMETIC_VALIDATION_VALID;
1507
1506
  goto finish;
1508
1507
  }
1509
- // Reading backward exposes assignment constraints before ambiguous prefixes.
1510
1508
  result = arithmetic_reverse_graph(&graph);
1511
1509
  if (result != ARITHMETIC_VALIDATION_VALID) goto finish;
1512
1510
  result = arithmetic_project_graph(&graph);
@@ -1763,6 +1761,7 @@ static const uint64_t fault_owners[TOKEN_TYPE_COUNT] = {
1763
1761
  OWNER_BIT(BEGIN_CASE_ITEM) |
1764
1762
  OWNER_BIT(BEGIN_FUNCTION_DEFINITION),
1765
1763
  [BRACE_GAP] = OWNER_BIT(BEGIN_BRACE_GROUP),
1764
+ [SPECIAL_BUILTIN_FUNCTION_NAME] = OWNER_BIT(BEGIN_FUNCTION_DEFINITION),
1766
1765
  [FUNCTION_BODY_GAP] = OWNER_BIT(BEGIN_FUNCTION_DEFINITION),
1767
1766
  [INVALID_FUNCTION_BODY] = OWNER_BIT(BEGIN_FUNCTION_DEFINITION),
1768
1767
  [FUNCTION_PARAMETER_ERROR] = OWNER_BIT(BEGIN_FUNCTION_DEFINITION),
@@ -1848,7 +1847,6 @@ static void owned_issue(
1848
1847
  };
1849
1848
  }
1850
1849
 
1851
- // Backquote faults rank by the lexical context they interrupt, not by owner.
1852
1850
  static size_t fault_context(const struct Parser *parser, struct Fault fault) {
1853
1851
  const struct Owner *owners = parser->plan->owners;
1854
1852
  switch (fault.kind) {
@@ -2708,7 +2706,7 @@ static struct Token word(struct Parser *parser) {
2708
2706
  prefix_name = false;
2709
2707
  }
2710
2708
  token.end = consumed_offset(input);
2711
- token.name = token.plain && prefix_name && count > 0;
2709
+ token.name = prefix_name && count > 0;
2712
2710
  if (
2713
2711
  token.plain &&
2714
2712
  digits_only(&token.text) &&
@@ -3044,7 +3042,7 @@ static bool here_document(
3044
3042
  here_document_lines(parser, here, &body, &at_line_start, &boundary);
3045
3043
  if (!here->quoted) here_body(parser, body, boundary);
3046
3044
  ts_free(body.units);
3047
- // 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.
3048
3046
  if (!found && !backquote_cut(parser, UNDEFINED_BACKQUOTE_HERE_DOCUMENT))
3049
3047
  report(
3050
3048
  parser,
@@ -3117,6 +3115,27 @@ static bool spelling(const struct Token *token, const char *value) {
3117
3115
  return i == token->text.count && !value[i];
3118
3116
  }
3119
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
+
3120
3139
  static bool reserved_colon(const struct Token *token) {
3121
3140
  return token->kind == WORD &&
3122
3141
  token->plain &&
@@ -3218,7 +3237,6 @@ static bool command_start(const struct Token *token) {
3218
3237
 
3219
3238
  static bool simple_command_start(const struct Token *token) {
3220
3239
  return (token->kind == WORD || redirect_kind(token->kind)) &&
3221
- !compound_start(token) &&
3222
3240
  !stop(token) &&
3223
3241
  !spelling(token, "!") &&
3224
3242
  !reserved_colon(token);
@@ -3229,6 +3247,10 @@ static bool stop_or_end(struct Parser *parser, uint32_t stops) {
3229
3247
  return token->kind == END || (stop(token) & (stops | parser->stops));
3230
3248
  }
3231
3249
 
3250
+ static bool command_boundary(struct Parser *parser) {
3251
+ return list_boundary(look(parser)->kind) || stop_or_end(parser, 0);
3252
+ }
3253
+
3232
3254
  // Decode faults precede word faults and are sorted by start.
3233
3255
  static size_t uncertain_position(
3234
3256
  const struct Plan *plan,
@@ -3365,9 +3387,7 @@ static struct Recovery recover(struct Parser *parser, unsigned boundaries) {
3365
3387
  static void invalid_target(struct Parser *parser, enum Lexeme kind) {
3366
3388
  bool here = kind == HERE || kind == HERE_STRIP;
3367
3389
  const struct Token *target = look(parser);
3368
- bool missing = list_boundary(target->kind) ||
3369
- redirect_kind(target->kind) ||
3370
- stop_or_end(parser, 0);
3390
+ bool missing = redirect_kind(target->kind) || command_boundary(parser);
3371
3391
  struct Fault fault = token_fault(
3372
3392
  parser,
3373
3393
  missing,
@@ -3740,9 +3760,17 @@ static void unexpected(struct Parser *parser) {
3740
3760
  report(parser, fault, recovery);
3741
3761
  }
3742
3762
 
3743
- static void function_definition(struct Parser *parser) {
3763
+ static void function_definition(struct Parser *parser, bool special_builtin) {
3744
3764
  if (parser->owner && !parser->opaque)
3745
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
+ );
3746
3774
  take(parser);
3747
3775
  bool damaged = false;
3748
3776
  if (look(parser)->kind != RIGHT && !stop_or_end(parser, 0)) {
@@ -3764,7 +3792,7 @@ static void function_definition(struct Parser *parser) {
3764
3792
  compound(parser);
3765
3793
  return;
3766
3794
  }
3767
- bool missing = list_boundary(look(parser)->kind) || stop_or_end(parser, 0);
3795
+ bool missing = command_boundary(parser);
3768
3796
  struct Fault fault = token_fault(
3769
3797
  parser,
3770
3798
  missing,
@@ -3774,20 +3802,13 @@ static void function_definition(struct Parser *parser) {
3774
3802
  report(parser, fault, recover(parser, RECOVER_CONNECTORS));
3775
3803
  }
3776
3804
 
3777
- static void command_contents(struct Parser *parser, bool simple) {
3778
- if (compound_start(look(parser))) {
3779
- compound(parser);
3780
- return;
3781
- }
3782
- if (!simple) {
3783
- unexpected(parser);
3784
- return;
3785
- }
3805
+ static void simple_command(struct Parser *parser) {
3786
3806
  if (look(parser)->kind == WORD) {
3787
3807
  bool name = look(parser)->name;
3808
+ bool special_builtin = name && special_builtin_name(look(parser));
3788
3809
  take(parser);
3789
3810
  if (name && look(parser)->kind == LEFT) {
3790
- function_definition(parser);
3811
+ function_definition(parser, special_builtin);
3791
3812
  return;
3792
3813
  }
3793
3814
  } else redirect(parser);
@@ -3799,15 +3820,13 @@ static void command_contents(struct Parser *parser, bool simple) {
3799
3820
 
3800
3821
  static void command(struct Parser *parser) {
3801
3822
  const struct Token *token = look(parser);
3802
- bool simple = simple_command_start(token);
3803
- size_t owner =
3804
- simple ? owner_begin(parser, BEGIN_SIMPLE_COMMAND, token->start) : 0;
3805
- command_contents(parser, simple);
3806
- owner_end(parser, owner, parser->consumed);
3807
- }
3808
-
3809
- static bool command_boundary(struct Parser *parser) {
3810
- 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
+ }
3811
3830
  }
3812
3831
 
3813
3832
  static void missing_command(struct Parser *parser) {
@@ -3849,18 +3868,19 @@ static void pipeline_contents(struct Parser *parser) {
3849
3868
  }
3850
3869
  }
3851
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
+
3852
3876
  static void pipeline(struct Parser *parser) {
3853
- size_t owner = 0;
3854
- if (command_start(look(parser)))
3855
- owner = owner_begin(parser, BEGIN_PIPELINE, look(parser)->start);
3877
+ size_t owner = command_owner(parser, BEGIN_PIPELINE);
3856
3878
  pipeline_contents(parser);
3857
3879
  owner_end(parser, owner, parser->consumed);
3858
3880
  }
3859
3881
 
3860
3882
  static void and_or(struct Parser *parser) {
3861
- size_t owner = 0;
3862
- if (command_start(look(parser)))
3863
- owner = owner_begin(parser, BEGIN_AND_OR, look(parser)->start);
3883
+ size_t owner = command_owner(parser, BEGIN_AND_OR);
3864
3884
  parser->resumed = false;
3865
3885
  if (list_boundary(look(parser)->kind)) unexpected(parser);
3866
3886
  else {
@@ -3905,13 +3925,16 @@ static bool commands(
3905
3925
  linebreak(parser);
3906
3926
  while (!parser->plan->failed && !stop_or_end(parser, 0)) {
3907
3927
  any = true;
3908
- if (!owner && command_start(look(parser)))
3909
- owner = owner_begin(parser, list, look(parser)->start);
3910
- and_or(parser);
3911
- command_separation(parser);
3912
- if (stop_or_end(parser, 0)) break;
3928
+ if (!owner) owner = command_owner(parser, list);
3913
3929
  enum Lexeme kind = look(parser)->kind;
3914
- 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
+ }
3915
3938
  if (look(parser)->kind == NEWLINE) {
3916
3939
  if (list == BEGIN_LIST) {
3917
3940
  owner_end(parser, owner, parser->consumed);
@@ -4189,7 +4212,7 @@ static bool later_child(const struct Plan *plan, size_t a, size_t b) {
4189
4212
  return a > b;
4190
4213
  }
4191
4214
 
4192
- // Sibling starts and ends are ordered, allowing backward search to stop early.
4215
+ // Ordered sibling ranges allow early termination.
4193
4216
  static size_t place_unowned(
4194
4217
  const struct Plan *plan,
4195
4218
  const struct PublicChild *children,