@willbooster/tree-sitter-c 44.0.0 → 45.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": "44.0.0",
3
+ "version": "45.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -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
@@ -42,7 +42,12 @@ typedef struct {
42
42
  } PreprocDirectiveSymbols;
43
43
 
44
44
  static bool empty_directives_skip_prefix(TSLexer *lexer);
45
+ static bool empty_directives_scan_word_step(TSLexer *lexer, char *word, unsigned capacity, unsigned *length, bool *trailing_splice, TSLexerPosition *position);
46
+ static bool empty_directives_ucn_follows(TSLexer *lexer, bool continuation);
45
47
  static inline bool empty_directives_name_character(int32_t c, bool continuation);
48
+ static bool directive_word_continues(DirectiveInput *input, bool number, bool strict_identifier, bool *in_identifier, int32_t *previous);
49
+ static bool directive_consume_ucn(DirectiveInput *input);
50
+ static bool directive_ucn(DirectiveInput *input, bool continuation);
46
51
  static TSSymbol empty_directives_keyword_symbol(const char *word, const PreprocDirectiveSymbols *symbols);
47
52
  static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte);
48
53
  static bool directive_offset_has(const DirectiveOffsets *offsets, uint32_t byte);
@@ -69,35 +74,58 @@ static bool empty_directives_has_keyword_end(const EmptyDirectives *state, uint3
69
74
 
70
75
  // Scans `#`, any directive prefix spacing or block comments, and the ASCII directive name into `word`, marking the
71
76
  // 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
77
  static bool empty_directives_scan_word(TSLexer *lexer, char *word, unsigned capacity, uint32_t *keyword_end) {
74
78
  lexer->advance(lexer, false);
75
79
  if (!empty_directives_skip_prefix(lexer) || !empty_directives_name_character(lexer->lookahead, false)) return false;
76
80
  unsigned length = 0;
77
81
  bool trailing_splice = false;
78
82
  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
- }
83
+ while (empty_directives_scan_word_step(lexer, word, capacity, &length, &trailing_splice, &position)) {}
95
84
  if (!trailing_splice) lexer->get_position(lexer, &position);
96
85
  word[length < capacity ? length : 0] = '\0';
97
86
  *keyword_end = position.bytes;
98
87
  return true;
99
88
  }
100
89
 
90
+ static bool empty_directives_scan_word_step(TSLexer *lexer, char *word, unsigned capacity, unsigned *length, bool *trailing_splice, TSLexerPosition *position) {
91
+ int32_t c = lexer->lookahead;
92
+ if (empty_directives_name_character(c, true)) {
93
+ if (*length < capacity) word[(*length)++] = (char)c;
94
+ lexer->advance(lexer, false);
95
+ lexer->mark_end(lexer);
96
+ *trailing_splice = false;
97
+ return true;
98
+ }
99
+ if (c != '\\') {
100
+ if (preproc_identifier_character(c, true)) *length = capacity;
101
+ return false;
102
+ }
103
+ if (!*trailing_splice) lexer->get_position(lexer, position);
104
+ *trailing_splice = true;
105
+ lexer->advance(lexer, false);
106
+ if (scan_preproc_newline(lexer, false)) return true;
107
+ if (empty_directives_ucn_follows(lexer, true)) *length = capacity;
108
+ return false;
109
+ }
110
+
111
+ static bool empty_directives_ucn_follows(TSLexer *lexer, bool continuation) {
112
+ if (!scan_preproc_splices(lexer)) return false;
113
+ int32_t prefix = lexer->lookahead;
114
+ if (prefix != 'u' && prefix != 'U' && prefix != 'N') return false;
115
+ lexer->advance(lexer, false);
116
+ if (!scan_preproc_splices(lexer)) return false;
117
+ bool braced = prefix != 'U' && lexer->lookahead == '{';
118
+ DirectiveUcn ucn;
119
+ if (!directive_ucn_start(&ucn, prefix, braced)) return false;
120
+ if (braced) lexer->advance(lexer, false);
121
+ for (bool done = false;;) {
122
+ if (!scan_preproc_splices(lexer)) return false;
123
+ if (!directive_ucn_feed(&ucn, lexer->lookahead, continuation, &done)) return false;
124
+ lexer->advance(lexer, false);
125
+ if (done) return true;
126
+ }
127
+ }
128
+
101
129
  static inline bool empty_directives_name_character(int32_t c, bool continuation) {
102
130
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (continuation && c == '_');
103
131
  }
@@ -360,4 +388,49 @@ static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages) {
360
388
  return true;
361
389
  }
362
390
 
391
+ static bool directive_word_continues(DirectiveInput *input, bool number, bool strict_identifier, bool *in_identifier, int32_t *previous) {
392
+ if (strict_identifier && *in_identifier && directive_consume_ucn(input)) {
393
+ *previous = 0x80;
394
+ return true;
395
+ }
396
+ int32_t c = directive_peek(input);
397
+ bool exponent_sign = number && (c == '+' || c == '-') &&
398
+ (*previous == 'e' || *previous == 'E' || *previous == 'p' || *previous == 'P');
399
+ if (!preproc_word(c, true) && !(number && (c == '.' || c == '\'' || exponent_sign))) return false;
400
+ if (strict_identifier && !(c != '\\' && preproc_identifier_character(c, true))) *in_identifier = false;
401
+ *previous = directive_next(input);
402
+ return true;
403
+ }
404
+
405
+ static bool directive_consume_ucn(DirectiveInput *input) {
406
+ if (directive_peek(input) != '\\') return false;
407
+ bool had_spaced = input->spaced_splice;
408
+ DirectiveInput copy = *input;
409
+ copy.spaced_splice = false;
410
+ directive_next(&copy);
411
+ if (copy.spaced_splice || !directive_ucn(&copy, true)) return false;
412
+ copy.spaced_splice = had_spaced;
413
+ *input = copy;
414
+ return true;
415
+ }
416
+
417
+ static bool directive_ucn(DirectiveInput *input, bool continuation) {
418
+ bool had_spaced = input->spaced_splice;
419
+ DirectiveInput copy = *input;
420
+ copy.spaced_splice = false;
421
+ int32_t prefix = directive_next(&copy);
422
+ bool braced = prefix != 'U' && (prefix == 'u' || prefix == 'N') && directive_peek(&copy) == '{';
423
+ DirectiveUcn ucn;
424
+ if (!directive_ucn_start(&ucn, prefix, braced)) return false;
425
+ if (braced) directive_next(&copy);
426
+ for (bool done = false;;) {
427
+ if (!directive_ucn_feed(&ucn, directive_next(&copy), continuation, &done)) return false;
428
+ if (done) break;
429
+ }
430
+ if (copy.spaced_splice) return false;
431
+ copy.spaced_splice = had_spaced;
432
+ *input = copy;
433
+ return true;
434
+ }
435
+
363
436
  #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);
@@ -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.0",
32
32
  "license": "MIT",
33
33
  "description": "C grammar for tree-sitter",
34
34
  "authors": [