@willbooster/tree-sitter-c 40.0.5 → 41.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "40.0.5",
3
+ "version": "41.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -31,6 +31,8 @@ typedef struct {
31
31
  typedef struct {
32
32
  DirectiveBuffer *buffer;
33
33
  uint32_t index, byte, position_index, previous_byte, before_previous_byte;
34
+ // Whether a line splice with spaces before its newline was read, which the lexer's tokens do not take as a splice.
35
+ bool spaced_splice;
34
36
  } DirectiveInput;
35
37
 
36
38
  #define DIRECTIVE_FACT_PAGE_SHIFT 15
@@ -41,7 +43,7 @@ typedef struct {
41
43
  // holds no fact; source without facts, such as an excluded range, costs one pointer per page of each directory.
42
44
  // `empty_directives_release` frees them.
43
45
  typedef struct {
44
- uint8_t **starts, **ends, **line_ends, **directive_starts, **directive_ends;
46
+ uint8_t **starts, **ends, **comments, **line_ends, **directive_starts, **directive_ends;
45
47
  uint32_t pages;
46
48
  bool built, ready;
47
49
  } EmptyDirectives;
@@ -58,7 +60,8 @@ typedef struct {
58
60
  bool beginning, pending, operand, pragma_pending, pragma_newline, pragma_name;
59
61
  unsigned pragma_depth, header;
60
62
  DirectiveKind kind;
61
- uint32_t start, required_end;
63
+ // `comment_start` is the first comment after a pending `#`, or 0.
64
+ uint32_t start, comment_start, required_end;
62
65
  } DirectiveLine;
63
66
 
64
67
  static bool directive_scan_buffered(DirectiveScan *scan, DirectiveInput *input);
@@ -104,6 +107,7 @@ static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input
104
107
  static bool directive_header_ready(DirectiveLine *line, int32_t c);
105
108
  static bool directive_header(DirectiveInput *input, int32_t close);
106
109
  static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
110
+ static bool directive_skip_splice(DirectiveInput *input);
107
111
  static uint32_t directive_splice_end(const DirectiveInput *input);
108
112
 
109
113
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer);
@@ -141,7 +145,7 @@ static DirectiveKind directive_word_kind(const DirectiveWord *word);
141
145
  static void directive_word_character(DirectiveWord *word, int32_t c);
142
146
  static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier);
143
147
  static uint32_t directive_byte(const DirectiveInput *input);
144
- static void directive_record(EmptyDirectives *state, DirectiveInput *input, uint32_t start, uint32_t end);
148
+ static void directive_record(EmptyDirectives *state, DirectiveInput *input, const DirectiveLine *line, uint32_t end);
145
149
 
146
150
  static bool empty_directives_initialize(EmptyDirectives *state, TSLexer *lexer) {
147
151
  TSLexerCheckpoint start;
@@ -202,14 +206,19 @@ static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byt
202
206
  }
203
207
 
204
208
  // Consumes the rest of a null directive whose start was confirmed at a position the lexer has not left except over
205
- // prefix characters.
209
+ // prefix characters: as a token (`skip_prefix`) up to its first recorded end, which `directive_record` places before
210
+ // the comments or after the line, and otherwise the whole logical line, since some scanner lookahead dispatches on
211
+ // the character after the skip without reading comments.
206
212
  static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
207
213
  TSLexerPosition position;
208
214
  while (lexer->lookahead != '#' && !pragma_eof(lexer)) lexer->advance(lexer, skip_prefix);
209
- do {
210
- lexer->advance(lexer, false);
211
- lexer->get_position(lexer, &position);
212
- } while (!pragma_eof(lexer) && !directive_bit(state, state->ends, position.bytes));
215
+ for (bool whole = !skip_prefix;; whole = false) {
216
+ do {
217
+ lexer->advance(lexer, false);
218
+ lexer->get_position(lexer, &position);
219
+ } while (!pragma_eof(lexer) && !directive_bit(state, state->ends, position.bytes));
220
+ if (!whole || pragma_eof(lexer) || !directive_bit(state, state->comments, position.bytes)) return;
221
+ }
213
222
  }
214
223
 
215
224
  // Ordinary code goes through `DirectiveScan` without being stored, and only directive segments, each from a directive's
@@ -316,7 +325,7 @@ static bool directive_segment(EmptyDirectives *state, DirectiveInput *input) {
316
325
  if (directive_line_clean(&line) && directive_segment_ends(input)) return true;
317
326
  }
318
327
  directive_complete_required(state, input, &line);
319
- if (line.pending) directive_record(state, input, line.start, input->buffer->fill_byte);
328
+ if (line.pending) directive_record(state, input, &line, input->buffer->fill_byte);
320
329
  return false;
321
330
  }
322
331
 
@@ -378,13 +387,13 @@ static bool directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer) {
378
387
  }
379
388
 
380
389
  static void empty_directives_release(EmptyDirectives *state) {
381
- uint8_t **directories[] = {state->starts, state->ends, state->line_ends, state->directive_starts, state->directive_ends};
390
+ uint8_t **directories[] = {state->starts, state->ends, state->comments, state->line_ends, state->directive_starts, state->directive_ends};
382
391
  for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
383
392
  if (!directories[i]) continue;
384
393
  for (uint32_t page = 0; page < state->pages; page++) free(directories[i][page]);
385
394
  free(directories[i]);
386
395
  }
387
- state->starts = state->ends = state->line_ends = state->directive_starts = state->directive_ends = NULL;
396
+ state->starts = state->ends = state->comments = state->line_ends = state->directive_starts = state->directive_ends = NULL;
388
397
  state->pages = 0;
389
398
  }
390
399
 
@@ -636,7 +645,7 @@ static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, u
636
645
  }
637
646
 
638
647
  static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages) {
639
- uint8_t ***directories[] = {&state->starts, &state->ends, &state->line_ends, &state->directive_starts, &state->directive_ends};
648
+ uint8_t ***directories[] = {&state->starts, &state->ends, &state->comments, &state->line_ends, &state->directive_starts, &state->directive_ends};
640
649
  if ((size_t)pages > SIZE_MAX / sizeof(uint8_t *)) return false;
641
650
  for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
642
651
  if (!*directories[i]) continue;
@@ -655,6 +664,10 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
655
664
  return;
656
665
  }
657
666
  if (c == '/' && (directive_peek(input) == '/' || directive_peek(input) == '*')) {
667
+ if (line->pending && !line->comment_start) {
668
+ line->comment_start = input->previous_byte;
669
+ input->spaced_splice = false;
670
+ }
658
671
  directive_line_comment(state, input, line);
659
672
  return;
660
673
  }
@@ -665,6 +678,7 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
665
678
  line->kind = DIRECTIVE_ORDINARY;
666
679
  line->operand = false;
667
680
  line->start = input->previous_byte;
681
+ line->comment_start = 0;
668
682
  directive_set(state, input->buffer, &state->directive_starts, line->start);
669
683
  line->beginning = false;
670
684
  line->pending = true;
@@ -787,7 +801,7 @@ static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input
787
801
 
788
802
  static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line) {
789
803
  directive_complete_required(state, input, line);
790
- if (line->pending) directive_record(state, input, line->start, directive_byte(input));
804
+ if (line->pending) directive_record(state, input, line, directive_byte(input));
791
805
  line->beginning = true;
792
806
  line->pending = false;
793
807
  bool required = line->kind == DIRECTIVE_CONDITION ||
@@ -834,11 +848,7 @@ static int32_t directive_next(DirectiveInput *input) {
834
848
  }
835
849
  while (directive_has(input, input->index)) {
836
850
  int32_t c = directive_physical_next(input);
837
- uint32_t after = c == '\\' ? directive_splice_end(input) : input->index;
838
- if (after != input->index) {
839
- while (input->index < after) directive_physical_next(input);
840
- continue;
841
- }
851
+ if (c == '\\' && directive_skip_splice(input)) continue;
842
852
  if (directive_newline(c)) {
843
853
  if (directive_has(input, input->index) && directive_newline(directive_character(input, input->index)) &&
844
854
  directive_character(input, input->index) != c) directive_physical_next(input);
@@ -879,6 +889,15 @@ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t i
879
889
  return input->buffer->utf16 ? 2 : directive_utf8_width(unit);
880
890
  }
881
891
 
892
+ // Consumes the rest of a line splice after its `\\`; false when none follows.
893
+ static bool directive_skip_splice(DirectiveInput *input) {
894
+ uint32_t after = directive_splice_end(input);
895
+ if (after == input->index) return false;
896
+ if (directive_horizontal(directive_character(input, input->index))) input->spaced_splice = true;
897
+ while (input->index < after) directive_physical_next(input);
898
+ return true;
899
+ }
900
+
882
901
  static uint32_t directive_splice_end(const DirectiveInput *input) {
883
902
  uint32_t after = input->index;
884
903
  while (directive_has(input, after) && directive_horizontal(directive_character(input, after))) after++;
@@ -1131,10 +1150,15 @@ static uint32_t directive_byte(const DirectiveInput *input) {
1131
1150
  return input->byte;
1132
1151
  }
1133
1152
 
1134
- static void directive_record(EmptyDirectives *state, DirectiveInput *input, uint32_t start, uint32_t end) {
1153
+ // A null directive with comments has two ends: before its first comment, marked in `comments` too, and after its line.
1154
+ // It keeps its comments when a spaced splice follows their start, where the lexer would read them differently.
1155
+ static void directive_record(EmptyDirectives *state, DirectiveInput *input, const DirectiveLine *line, uint32_t end) {
1135
1156
  input->buffer->carries = true;
1136
- directive_set(state, input->buffer, &state->starts, start);
1157
+ directive_set(state, input->buffer, &state->starts, line->start);
1137
1158
  directive_set(state, input->buffer, &state->ends, end);
1159
+ if (!line->comment_start || input->spaced_splice) return;
1160
+ directive_set(state, input->buffer, &state->ends, line->comment_start);
1161
+ directive_set(state, input->buffer, &state->comments, line->comment_start);
1138
1162
  }
1139
1163
 
1140
1164
  #endif
Binary file
package/tree-sitter.json CHANGED
@@ -25,7 +25,7 @@
25
25
  }
26
26
  ],
27
27
  "metadata": {
28
- "version": "40.0.5",
28
+ "version": "41.0.0",
29
29
  "license": "MIT",
30
30
  "description": "C grammar for tree-sitter",
31
31
  "authors": [