@willbooster/tree-sitter-c 44.0.0 → 45.0.1

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": "44.0.0",
3
+ "version": "45.0.1",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -27,7 +27,7 @@ typedef struct {
27
27
  uint64_t recent;
28
28
  // Whether the current segment recorded a null directive or a line-end barrier, which the reverse pass carries, and
29
29
  // whether the characters stored or scanned hold `pragma_word`.
30
- bool utf16, encoding_known, failed, carries, pragma;
30
+ bool utf16, encoding_known, failed, carries, pragma, spaced_splices;
31
31
  uint8_t classes[UNIT_CLASS_COUNT];
32
32
  } DirectiveBuffer;
33
33
 
@@ -41,6 +41,12 @@ typedef struct {
41
41
 
42
42
  typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
43
43
 
44
+ typedef struct {
45
+ uint32_t value;
46
+ unsigned digits, limit;
47
+ bool named, braced;
48
+ } DirectiveUcn;
49
+
44
50
  static void directive_shift(DirectiveBuffer *buffer, uint32_t start);
45
51
  static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width);
46
52
  static int32_t directive_next(DirectiveInput *input);
@@ -69,6 +75,10 @@ static uint16_t directive_character(const DirectiveInput *input, uint32_t index)
69
75
  static void directive_store(DirectiveBuffer *buffer, uint16_t c);
70
76
  static void directive_release(DirectiveBuffer *buffer);
71
77
  static unsigned directive_utf8_width(uint32_t c);
78
+ static bool directive_ucn_start(DirectiveUcn *ucn, int32_t prefix, bool braced);
79
+ static bool directive_ucn_feed(DirectiveUcn *ucn, int32_t c, bool continuation, bool *done);
80
+ static int directive_hex_digit(int32_t c);
81
+ static bool directive_valid_ucn_scalar(uint32_t value, bool continuation);
72
82
 
73
83
  // Drops the characters before `start`, which `DirectiveScan` has consumed.
74
84
  static void directive_shift(DirectiveBuffer *buffer, uint32_t start) {
@@ -428,4 +438,46 @@ static unsigned directive_utf8_width(uint32_t c) {
428
438
  return c < 0x80 || c > 0x10ffff ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
429
439
  }
430
440
 
441
+ static bool directive_ucn_start(DirectiveUcn *ucn, int32_t prefix, bool braced) {
442
+ if (prefix == 'N') {
443
+ if (!braced) return false;
444
+ *ucn = (DirectiveUcn){.named = true, .braced = true};
445
+ return true;
446
+ }
447
+ if ((prefix != 'u' && prefix != 'U') || (prefix == 'U' && braced)) return false;
448
+ *ucn = (DirectiveUcn){.limit = prefix == 'U' ? 8u : 4u, .braced = braced};
449
+ return true;
450
+ }
451
+
452
+ static bool directive_ucn_feed(DirectiveUcn *ucn, int32_t c, bool continuation, bool *done) {
453
+ *done = false;
454
+ if (ucn->braced && c == '}') {
455
+ *done = true;
456
+ return ucn->digits > 0 && (ucn->named || directive_valid_ucn_scalar(ucn->value, continuation));
457
+ }
458
+ if (ucn->named) {
459
+ if (!((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ' ' || c == '-')) return false;
460
+ ucn->digits++;
461
+ return true;
462
+ }
463
+ int digit = directive_hex_digit(c);
464
+ if (digit < 0) return false;
465
+ ucn->value = ucn->value <= 0x10ffff ? (ucn->value << 4) | (uint32_t)digit : 0x110000;
466
+ if (++ucn->digits == ucn->limit && !ucn->braced) {
467
+ *done = true;
468
+ return directive_valid_ucn_scalar(ucn->value, continuation);
469
+ }
470
+ return true;
471
+ }
472
+
473
+ static int directive_hex_digit(int32_t c) {
474
+ if (c >= '0' && c <= '9') return c - '0';
475
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
476
+ return c >= 'A' && c <= 'F' ? c - 'A' + 10 : -1;
477
+ }
478
+
479
+ static bool directive_valid_ucn_scalar(uint32_t value, bool continuation) {
480
+ return value >= 0xa0 && value <= 0x10ffff && preproc_identifier_character((int32_t)value, continuation);
481
+ }
482
+
431
483
  #endif
@@ -33,8 +33,9 @@ typedef struct {
33
33
  DirectiveSpans line_ends;
34
34
  // `anchored` and `ready` belong to the scanner state: whether the token at the start of the input is out, and
35
35
  // whether the facts, which `built` says exist, are in use. `pragmas` is the fact that the input read for them holds
36
- // `pragma_word`, without which no scan has to look for a `_Pragma` operator.
37
- bool built, anchored, ready, pragmas, failed;
36
+ // `pragma_word`, without which no scan has to look for a `_Pragma` operator. `spaced_splices` enables C++23
37
+ // `[lex.phases]/2` horizontal-whitespace line splices inside directive universal character names.
38
+ bool built, anchored, ready, pragmas, failed, spaced_splices;
38
39
  } EmptyDirectives;
39
40
 
40
41
  typedef struct {
@@ -42,7 +43,15 @@ typedef struct {
42
43
  } PreprocDirectiveSymbols;
43
44
 
44
45
  static bool empty_directives_skip_prefix(TSLexer *lexer);
46
+ static bool empty_directives_scan_word_step(TSLexer *lexer, char *word, unsigned capacity, unsigned *length, bool *trailing_splice, TSLexerPosition *position);
47
+ static bool empty_directives_ucn_follows(TSLexer *lexer, bool continuation);
48
+ static bool empty_directives_ucn_follows_splices(TSLexer *lexer, bool continuation, bool spaced_splices);
49
+ static bool empty_directives_skip_splices(TSLexer *lexer, bool spaced_splices);
50
+ static bool empty_directives_skip_line_splice(TSLexer *lexer, bool spaced_splices);
45
51
  static inline bool empty_directives_name_character(int32_t c, bool continuation);
52
+ static bool directive_word_continues(DirectiveInput *input, bool number, bool strict_identifier, bool *in_identifier, int32_t *previous);
53
+ static bool directive_consume_ucn(DirectiveInput *input);
54
+ static bool directive_ucn(DirectiveInput *input, bool continuation);
46
55
  static TSSymbol empty_directives_keyword_symbol(const char *word, const PreprocDirectiveSymbols *symbols);
47
56
  static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte);
48
57
  static bool directive_offset_has(const DirectiveOffsets *offsets, uint32_t byte);
@@ -69,35 +78,77 @@ static bool empty_directives_has_keyword_end(const EmptyDirectives *state, uint3
69
78
 
70
79
  // Scans `#`, any directive prefix spacing or block comments, and the ASCII directive name into `word`, marking the
71
80
  // token end at the name's last character and storing the post-keyword cursor byte in `*keyword_end`.
72
- // code-gauge-allow functionCognitiveComplexity -- Tracks trailing line splices while marking the keyword's physical token end.
73
81
  static bool empty_directives_scan_word(TSLexer *lexer, char *word, unsigned capacity, uint32_t *keyword_end) {
74
82
  lexer->advance(lexer, false);
75
83
  if (!empty_directives_skip_prefix(lexer) || !empty_directives_name_character(lexer->lookahead, false)) return false;
76
84
  unsigned length = 0;
77
85
  bool trailing_splice = false;
78
86
  TSLexerPosition position = {0};
79
- for (;;) {
80
- int32_t c = lexer->lookahead;
81
- if (empty_directives_name_character(c, true)) {
82
- if (length < capacity) word[length++] = (char)c;
83
- lexer->advance(lexer, false);
84
- lexer->mark_end(lexer);
85
- trailing_splice = false;
86
- } else if (c == '\\') {
87
- if (!trailing_splice) lexer->get_position(lexer, &position);
88
- trailing_splice = true;
89
- lexer->advance(lexer, false);
90
- if (!scan_preproc_newline(lexer, false)) break;
91
- } else {
92
- break;
93
- }
94
- }
87
+ while (empty_directives_scan_word_step(lexer, word, capacity, &length, &trailing_splice, &position)) {}
95
88
  if (!trailing_splice) lexer->get_position(lexer, &position);
96
89
  word[length < capacity ? length : 0] = '\0';
97
90
  *keyword_end = position.bytes;
98
91
  return true;
99
92
  }
100
93
 
94
+ static bool empty_directives_scan_word_step(TSLexer *lexer, char *word, unsigned capacity, unsigned *length, bool *trailing_splice, TSLexerPosition *position) {
95
+ int32_t c = lexer->lookahead;
96
+ if (empty_directives_name_character(c, true)) {
97
+ if (*length < capacity) word[(*length)++] = (char)c;
98
+ lexer->advance(lexer, false);
99
+ lexer->mark_end(lexer);
100
+ *trailing_splice = false;
101
+ return true;
102
+ }
103
+ if (c != '\\') {
104
+ if (preproc_identifier_character(c, true)) *length = capacity;
105
+ return false;
106
+ }
107
+ if (!*trailing_splice) lexer->get_position(lexer, position);
108
+ *trailing_splice = true;
109
+ lexer->advance(lexer, false);
110
+ if (scan_preproc_newline(lexer, false)) return true;
111
+ if (empty_directives_ucn_follows(lexer, true)) *length = capacity;
112
+ return false;
113
+ }
114
+
115
+ static bool empty_directives_ucn_follows(TSLexer *lexer, bool continuation) {
116
+ return empty_directives_ucn_follows_splices(lexer, continuation, false);
117
+ }
118
+
119
+ static bool empty_directives_ucn_follows_splices(TSLexer *lexer, bool continuation, bool spaced_splices) {
120
+ if (!empty_directives_skip_splices(lexer, spaced_splices)) return false;
121
+ int32_t prefix = lexer->lookahead;
122
+ if (prefix != 'u' && prefix != 'U' && prefix != 'N') return false;
123
+ lexer->advance(lexer, false);
124
+ if (!empty_directives_skip_splices(lexer, spaced_splices)) return false;
125
+ bool braced = prefix != 'U' && lexer->lookahead == '{';
126
+ DirectiveUcn ucn;
127
+ if (!directive_ucn_start(&ucn, prefix, braced)) return false;
128
+ if (braced) lexer->advance(lexer, false);
129
+ for (bool done = false;;) {
130
+ if (!empty_directives_skip_splices(lexer, spaced_splices)) return false;
131
+ if (!directive_ucn_feed(&ucn, lexer->lookahead, continuation, &done)) return false;
132
+ lexer->advance(lexer, false);
133
+ if (done) return true;
134
+ }
135
+ }
136
+
137
+ static bool empty_directives_skip_splices(TSLexer *lexer, bool spaced_splices) {
138
+ while (lexer->lookahead == '\\') {
139
+ lexer->advance(lexer, false);
140
+ if (!empty_directives_skip_line_splice(lexer, spaced_splices)) return false;
141
+ }
142
+ return true;
143
+ }
144
+
145
+ static bool empty_directives_skip_line_splice(TSLexer *lexer, bool spaced_splices) {
146
+ if (spaced_splices) {
147
+ while (directive_horizontal(lexer->lookahead)) lexer->advance(lexer, false);
148
+ }
149
+ return scan_preproc_newline(lexer, false);
150
+ }
151
+
101
152
  static inline bool empty_directives_name_character(int32_t c, bool continuation) {
102
153
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (continuation && c == '_');
103
154
  }
@@ -360,4 +411,49 @@ static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages) {
360
411
  return true;
361
412
  }
362
413
 
414
+ static bool directive_word_continues(DirectiveInput *input, bool number, bool strict_identifier, bool *in_identifier, int32_t *previous) {
415
+ if (strict_identifier && *in_identifier && directive_consume_ucn(input)) {
416
+ *previous = 0x80;
417
+ return true;
418
+ }
419
+ int32_t c = directive_peek(input);
420
+ bool exponent_sign = number && (c == '+' || c == '-') &&
421
+ (*previous == 'e' || *previous == 'E' || *previous == 'p' || *previous == 'P');
422
+ if (!preproc_word(c, true) && !(number && (c == '.' || c == '\'' || exponent_sign))) return false;
423
+ if (strict_identifier && !(c != '\\' && preproc_identifier_character(c, true))) *in_identifier = false;
424
+ *previous = directive_next(input);
425
+ return true;
426
+ }
427
+
428
+ static bool directive_consume_ucn(DirectiveInput *input) {
429
+ if (directive_peek(input) != '\\') return false;
430
+ bool had_spaced = input->spaced_splice;
431
+ DirectiveInput copy = *input;
432
+ copy.spaced_splice = false;
433
+ directive_next(&copy);
434
+ if ((!copy.buffer->spaced_splices && copy.spaced_splice) || !directive_ucn(&copy, true)) return false;
435
+ copy.spaced_splice = had_spaced || copy.spaced_splice;
436
+ *input = copy;
437
+ return true;
438
+ }
439
+
440
+ static bool directive_ucn(DirectiveInput *input, bool continuation) {
441
+ bool had_spaced = input->spaced_splice;
442
+ DirectiveInput copy = *input;
443
+ copy.spaced_splice = false;
444
+ int32_t prefix = directive_next(&copy);
445
+ bool braced = prefix != 'U' && (prefix == 'u' || prefix == 'N') && directive_peek(&copy) == '{';
446
+ DirectiveUcn ucn;
447
+ if (!directive_ucn_start(&ucn, prefix, braced)) return false;
448
+ if (braced) directive_next(&copy);
449
+ for (bool done = false;;) {
450
+ if (!directive_ucn_feed(&ucn, directive_next(&copy), continuation, &done)) return false;
451
+ if (done) break;
452
+ }
453
+ if (!copy.buffer->spaced_splices && copy.spaced_splice) return false;
454
+ copy.spaced_splice = had_spaced || copy.spaced_splice;
455
+ *input = copy;
456
+ return true;
457
+ }
458
+
363
459
  #endif
@@ -102,7 +102,7 @@ static bool directive_raw(DirectiveInput *input);
102
102
 
103
103
  static uint32_t directive_keyword_end(const DirectiveInput *input);
104
104
  static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, uint32_t end);
105
- static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw, bool classify);
105
+ static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw, bool classify, bool strict_identifier);
106
106
  static void directive_word_run(DirectiveInput *input, DirectiveWord *word, bool classify);
107
107
  static DirectiveKind directive_word_kind(const DirectiveWord *word);
108
108
  static void directive_word_character(DirectiveWord *word, int32_t c);
@@ -139,7 +139,7 @@ static bool empty_directives_initialize(EmptyDirectives *state, TSLexer *lexer,
139
139
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
140
140
  TSLexerPosition position;
141
141
  lexer->get_position(lexer, &position);
142
- DirectiveBuffer buffer = {.lexer = lexer, .fill_byte = position.bytes};
142
+ DirectiveBuffer buffer = {.lexer = lexer, .fill_byte = position.bytes, .spaced_splices = state->spaced_splices};
143
143
  DirectiveInput input = {.buffer = &buffer, .byte = position.bytes};
144
144
  // `run` holds the run before a segment's `#`, and `tail` the segment's trailing run.
145
145
  DirectiveSpans run = {0}, tail = {0};
@@ -743,20 +743,18 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
743
743
  bool operand = line->operand;
744
744
  if (!name && !pragma) line->operand = true;
745
745
  line->beginning = line->pending = false;
746
- if (header) {
747
- if (directive_header(input, c == '<' ? '>' : '"')) directive_line_end(state, input, line);
746
+ if (header || c == '"' || c == '\'') {
747
+ bool ended = header ? directive_header(input, c == '<' ? '>' : '"') : directive_quoted(input, c);
748
+ if (ended) directive_line_end(state, input, line);
748
749
  return;
749
750
  }
750
- if (c == '"' || c == '\'') {
751
- if (directive_quoted(input, c)) directive_line_end(state, input, line);
752
- return;
753
- }
754
- if (!preproc_word(c, false) && !(c >= '0' && c <= '9')) return;
751
+ bool ucn_start = name && c == '\\' && directive_ucn(input, false);
752
+ if (!ucn_start && !preproc_word(c, false) && !(c >= '0' && c <= '9')) return;
755
753
  uint32_t end = name ? directive_keyword_end(input) : 0;
756
- DirectiveWord word = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC, true);
754
+ DirectiveWord word = directive_word(input, ucn_start ? 0x80 : c, line->kind != DIRECTIVE_DIAGNOSTIC, true, name);
757
755
  DirectiveKind kind = name ? directive_keyword(state, input, line, &word, end) : directive_word_kind(&word);
758
756
  if (!name && !pragma && kind == DIRECTIVE_PRAGMA) directive_pragma_name(line, input);
759
- directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
757
+ directive_word_role(line, kind, name, pragma, operand, ucn_start || preproc_word(c, false));
760
758
  }
761
759
 
762
760
  // Outside a directive name, a line with no `_Pragma` or header operand in progress keeps its state through every token
@@ -823,7 +821,7 @@ static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input
823
821
  return;
824
822
  }
825
823
  } else if (preproc_word(c, false) || (c >= '0' && c <= '9')) {
826
- directive_word(input, c, true, false);
824
+ directive_word(input, c, true, false, false);
827
825
  }
828
826
  directive_neutral_units(input);
829
827
  }
@@ -1031,19 +1029,16 @@ static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *i
1031
1029
  }
1032
1030
 
1033
1031
  // Without `classify`, only `raw` of the result is complete.
1034
- static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw, bool classify) {
1032
+ static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw, bool classify, bool strict_identifier) {
1035
1033
  DirectiveWord word = {0};
1036
1034
  directive_word_character(&word, first);
1037
1035
  bool number = first >= '0' && first <= '9';
1038
1036
  int32_t previous = first;
1039
1037
  if (!number) directive_word_run(input, &word, classify);
1040
- for (;;) {
1041
- int32_t c = directive_peek(input);
1042
- bool exponent_sign = number && (c == '+' || c == '-') &&
1043
- (previous == 'e' || previous == 'E' || previous == 'p' || previous == 'P');
1044
- if (!preproc_word(c, true) && !(number && (c == '.' || c == '\'' || exponent_sign))) break;
1045
- previous = directive_next(input);
1046
- directive_word_character(&word, previous);
1038
+ bool in_identifier = classify;
1039
+ while (directive_word_continues(input, number, strict_identifier, &in_identifier, &previous)) {
1040
+ if (in_identifier) directive_word_character(&word, previous);
1041
+ else gnu_raw_string_prefix_character(&word.raw, previous);
1047
1042
  }
1048
1043
  if (raw && gnu_raw_string_prefix(&word.raw) && directive_peek(input) == '"') directive_raw(input);
1049
1044
  return word;
Binary file
package/tree-sitter.json CHANGED
@@ -28,7 +28,7 @@
28
28
  }
29
29
  ],
30
30
  "metadata": {
31
- "version": "44.0.0",
31
+ "version": "45.0.1",
32
32
  "license": "MIT",
33
33
  "description": "C grammar for tree-sitter",
34
34
  "authors": [