@willbooster/tree-sitter-c 29.0.0 → 31.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/grammar.js CHANGED
@@ -806,6 +806,8 @@ module.exports = Object.assign(
806
806
  repeat(choice($._non_macro_declaration_modifiers, $._conditional_macro_specifier))
807
807
  ),
808
808
 
809
+ _declaration_declarators: ($) => declarationDeclarators($),
810
+
809
811
  declaration: ($) =>
810
812
  choice(
811
813
  declarationRule(
@@ -2442,11 +2444,11 @@ function functionDefinitionRule($, specifiers, oldStyle) {
2442
2444
  }
2443
2445
 
2444
2446
  /**
2445
- * @param {import('./types/treeSitterDsl').GrammarSymbols<'storage_class_specifier' | 'ms_call_modifier' | '_declaration_declarator' | '_declarator' | 'gnu_asm_expression' | 'init_declarator' | 'type_qualifier' | 'identifier'>} $
2447
+ * @param {import('./types/treeSitterDsl').GrammarSymbols<'storage_class_specifier' | 'ms_call_modifier' | '_declaration_declarators' | '_declaration_declarator' | '_declarator' | 'gnu_asm_expression' | 'init_declarator' | 'type_qualifier' | 'identifier'>} $
2446
2448
  * @param {import('./types/treeSitterDsl').Rule} specifiers
2447
2449
  * @param {import('./types/treeSitterDsl').Rule} modifiers
2448
2450
  * @param {boolean} allowInferredType
2449
- * @param {import('./types/treeSitterDsl').Rule} declarator
2451
+ * @param {import('./types/treeSitterDsl').Rule} [declarator]
2450
2452
  * @param {{guard?: import('./types/treeSitterDsl').Rule, bareMarkerInitializer?: boolean}} options
2451
2453
  */
2452
2454
  function declarationRule(
@@ -2454,26 +2456,14 @@ function declarationRule(
2454
2456
  specifiers,
2455
2457
  modifiers,
2456
2458
  allowInferredType = true,
2457
- declarator = $._declaration_declarator,
2458
- {
2459
- guard = choice(sym('_post_type_function_attribute_start'), sym('_braced_function_attribute_start')),
2460
- bareMarkerInitializer = false,
2461
- } = {}
2459
+ declarator,
2460
+ { guard, bareMarkerInitializer = false } = {}
2462
2461
  ) {
2463
- const initialized = choice(
2464
- bareMarkerInitializer
2465
- ? seq(optional(guard), $.init_declarator)
2466
- : choice($.init_declarator, seq(guard, alias(sym('_guarded_init_declarator'), $.init_declarator))),
2467
- seq(sym('_braced_function_attribute_start'), alias(sym('_braced_macro_init_declarator'), $.init_declarator))
2468
- );
2469
- const item = choice(
2470
- seq(optional($.ms_call_modifier), optional(guard), declarator, optional($.gnu_asm_expression)),
2471
- seq(sym('_braced_function_attribute_start'), alias(sym('_braced_macro_declarator'), $._declarator)),
2472
- initialized
2473
- );
2474
- const items = bareMarkerInitializer
2475
- ? seq(field('declarator', initialized), repeat(seq(',', field('declarator', item))))
2476
- : commaSep1(field('declarator', item));
2462
+ // The default declarator list is one shared rule so that each declaration form reuses its parse states.
2463
+ const items =
2464
+ declarator === undefined && guard === undefined && !bareMarkerInitializer
2465
+ ? $._declaration_declarators
2466
+ : declarationDeclarators($, declarator, guard, bareMarkerInitializer);
2477
2467
  return choice(
2478
2468
  seq(
2479
2469
  prec.dynamic(
@@ -2497,6 +2487,34 @@ function declarationRule(
2497
2487
  );
2498
2488
  }
2499
2489
 
2490
+ /**
2491
+ * @param {import('./types/treeSitterDsl').GrammarSymbols<'ms_call_modifier' | '_declaration_declarator' | '_declarator' | 'gnu_asm_expression' | 'init_declarator'>} $
2492
+ * @param {import('./types/treeSitterDsl').Rule} [declarator]
2493
+ * @param {import('./types/treeSitterDsl').Rule} [guard]
2494
+ * @param {boolean} [bareMarkerInitializer]
2495
+ */
2496
+ function declarationDeclarators(
2497
+ $,
2498
+ declarator = $._declaration_declarator,
2499
+ guard = choice(sym('_post_type_function_attribute_start'), sym('_braced_function_attribute_start')),
2500
+ bareMarkerInitializer = false
2501
+ ) {
2502
+ const initialized = choice(
2503
+ bareMarkerInitializer
2504
+ ? seq(optional(guard), $.init_declarator)
2505
+ : choice($.init_declarator, seq(guard, alias(sym('_guarded_init_declarator'), $.init_declarator))),
2506
+ seq(sym('_braced_function_attribute_start'), alias(sym('_braced_macro_init_declarator'), $.init_declarator))
2507
+ );
2508
+ const item = choice(
2509
+ seq(optional($.ms_call_modifier), optional(guard), declarator, optional($.gnu_asm_expression)),
2510
+ seq(sym('_braced_function_attribute_start'), alias(sym('_braced_macro_declarator'), $._declarator)),
2511
+ initialized
2512
+ );
2513
+ return bareMarkerInitializer
2514
+ ? seq(field('declarator', initialized), repeat(seq(',', field('declarator', item))))
2515
+ : commaSep1(field('declarator', item));
2516
+ }
2517
+
2500
2518
  /**
2501
2519
  * @param {import('./types/treeSitterDsl').GrammarSymbols<'initializer_list' | 'expression'>} $
2502
2520
  * @param {import('./types/treeSitterDsl').Rule} declarator
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "29.0.0",
3
+ "version": "31.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -1,4 +1,4 @@
1
- // code-gauge-allow-file fileNcss 510 -- One source-fact pass shares compact character storage, directive lexing and physical-position conversion.
1
+ // code-gauge-allow-file fileNcss 575 -- One source-fact pass shares compact character storage, directive lexing and physical-position conversion.
2
2
  #ifndef TREE_SITTER_C_EMPTY_DIRECTIVES_H_
3
3
  #define TREE_SITTER_C_EMPTY_DIRECTIVES_H_
4
4
 
@@ -15,6 +15,7 @@ typedef struct {
15
15
  uint32_t position_count, position_capacity, position_index, previous_byte, before_previous_byte;
16
16
  uint8_t *prefix_barriers;
17
17
  uint32_t length, index, byte, end, page_count;
18
+ uint32_t conditional_depth;
18
19
  bool utf16, encoding_known;
19
20
  } DirectiveInput;
20
21
 
@@ -24,13 +25,13 @@ typedef struct {
24
25
  bool built, ready;
25
26
  } EmptyDirectives;
26
27
 
27
- typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
28
+ typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_UNDEF, DIRECTIVE_LINE, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
28
29
 
29
30
  typedef struct {
30
- bool beginning, pending, operand, pragma_pending, pragma_newline;
31
+ bool beginning, pending, operand, pragma_pending, pragma_newline, pragma_name;
31
32
  unsigned pragma_depth, header;
32
33
  DirectiveKind kind;
33
- uint32_t start;
34
+ uint32_t start, required_end;
34
35
  } DirectiveLine;
35
36
 
36
37
  static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input);
@@ -44,6 +45,8 @@ static void directive_release(DirectiveInput *input);
44
45
  static unsigned directive_utf8_width(uint32_t c);
45
46
  static void directive_barrier(DirectiveInput *input);
46
47
  static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
48
+ static void directive_complete_required(EmptyDirectives *state, DirectiveLine *line);
49
+ static bool directive_requires_operand(DirectiveKind kind);
47
50
  static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
48
51
  static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
49
52
  static bool directive_pragma_part(DirectiveLine *line, int32_t c);
@@ -57,6 +60,9 @@ static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input)
57
60
  static int32_t directive_next(DirectiveInput *input);
58
61
  static int32_t directive_physical_next(DirectiveInput *input);
59
62
  static int32_t directive_peek(const DirectiveInput *input);
63
+ static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input);
64
+ static bool directive_pragma_operator(const DirectiveInput *input);
65
+ static int32_t directive_pragma_token(DirectiveInput *input);
60
66
  static bool directive_comment(DirectiveInput *input, bool *line);
61
67
  static bool directive_quoted(DirectiveInput *input, int32_t quote);
62
68
  static bool directive_raw(DirectiveInput *input);
@@ -67,7 +73,8 @@ typedef struct {
67
73
  } DirectiveWord;
68
74
 
69
75
  static uint32_t directive_keyword_end(const DirectiveInput *input);
70
- static DirectiveKind directive_word(DirectiveInput *input, int32_t first, bool raw);
76
+ static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, uint32_t end);
77
+ static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw);
71
78
  static DirectiveKind directive_word_kind(const DirectiveWord *word);
72
79
  static void directive_word_character(DirectiveWord *word, int32_t c);
73
80
  static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier);
@@ -280,6 +287,7 @@ static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input)
280
287
  if (c < 0) break;
281
288
  directive_line_character(state, input, &line, c);
282
289
  }
290
+ directive_complete_required(state, &line);
283
291
  if (line.pending) directive_record(state, line.start, input->end);
284
292
  }
285
293
 
@@ -307,7 +315,7 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
307
315
  directive_code(state, input, line, c);
308
316
  }
309
317
 
310
- // code-gauge-allow functionCognitiveComplexity 17 -- Header names, quoted tokens and distinct keyword boundaries update one logical directive's lexical state.
318
+ // code-gauge-allow functionCognitiveComplexity 19 -- Header names, quoted tokens and distinct keyword boundaries update one logical directive's lexical state.
311
319
  static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
312
320
  bool name = line->pending;
313
321
  bool header = directive_header_ready(line, c);
@@ -325,10 +333,9 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
325
333
  }
326
334
  if (!preproc_word(c, false) && !(c >= '0' && c <= '9')) return;
327
335
  uint32_t end = name ? directive_keyword_end(input) : 0;
328
- DirectiveKind kind = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC);
329
- if (name) {
330
- state->directive_ends[end / 8] |= 1u << (end % 8);
331
- }
336
+ DirectiveWord word = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC);
337
+ DirectiveKind kind = name ? directive_keyword(state, input, line, &word, end) : directive_word_kind(&word);
338
+ if (!name && !pragma && kind == DIRECTIVE_PRAGMA) directive_pragma_name(line, input);
332
339
  directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
333
340
  }
334
341
 
@@ -339,7 +346,7 @@ static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool na
339
346
  }
340
347
  if (pragma) return;
341
348
  if (kind == DIRECTIVE_PRAGMA) {
342
- bool required = line->kind == DIRECTIVE_REQUIRED || line->kind == DIRECTIVE_HEADER;
349
+ bool required = directive_requires_operand(line->kind) && line->kind != DIRECTIVE_UNDEF;
343
350
  if (required && !had_operand) {
344
351
  line->operand = false;
345
352
  line->pragma_pending = true;
@@ -394,11 +401,12 @@ static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input
394
401
  }
395
402
 
396
403
  static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line) {
404
+ directive_complete_required(state, line);
397
405
  if (line->pending) directive_record(state, line->start, directive_byte(input));
398
406
  line->beginning = true;
399
407
  line->pending = false;
400
408
  bool required = line->kind == DIRECTIVE_CONDITION ||
401
- ((line->kind == DIRECTIVE_REQUIRED || line->kind == DIRECTIVE_HEADER) && !line->operand && !line->pragma_pending);
409
+ (directive_requires_operand(line->kind) && !line->operand && !line->pragma_pending);
402
410
  if (required) directive_barrier(input);
403
411
  line->header = 0;
404
412
  if (line->pragma_pending) line->pragma_newline = true;
@@ -408,6 +416,21 @@ static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, Di
408
416
  }
409
417
  }
410
418
 
419
+ static void directive_complete_required(EmptyDirectives *state, DirectiveLine *line) {
420
+ bool pragma_name = line->pragma_name;
421
+ line->pragma_name = false;
422
+ if (!line->required_end) return;
423
+ if (line->operand || line->pragma_pending || pragma_name) {
424
+ uint32_t end = line->required_end;
425
+ state->directive_ends[end / 8] |= 1u << (end % 8);
426
+ }
427
+ line->required_end = 0;
428
+ }
429
+
430
+ static bool directive_requires_operand(DirectiveKind kind) {
431
+ return kind == DIRECTIVE_REQUIRED || kind == DIRECTIVE_UNDEF || kind == DIRECTIVE_LINE || kind == DIRECTIVE_HEADER;
432
+ }
433
+
411
434
  static void directive_barrier(DirectiveInput *input) {
412
435
  if (!input->index || !directive_newline(directive_character(input, input->index - 1))) return;
413
436
  uint32_t byte = input->previous_byte;
@@ -470,6 +493,35 @@ static int32_t directive_peek(const DirectiveInput *input) {
470
493
  return directive_next(&copy);
471
494
  }
472
495
 
496
+ // `_Pragma` is the macro name of `#define _Pragma(x)` or `#ifdef _Pragma`, but an operator before a string literal.
497
+ static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input) {
498
+ if (line->kind == DIRECTIVE_UNDEF || (line->kind == DIRECTIVE_REQUIRED && !directive_pragma_operator(input))) line->pragma_name = true;
499
+ }
500
+
501
+ // Mirrors the argument prefix accepted by `scan_pragma_argument` and the spacing a directive line can hold.
502
+ static bool directive_pragma_operator(const DirectiveInput *input) {
503
+ DirectiveInput copy = *input;
504
+ if (directive_pragma_token(&copy) != '(') return false;
505
+ int32_t c = directive_pragma_token(&copy);
506
+ if (c == 'L' || c == 'U') c = directive_next(&copy);
507
+ else if (c == 'u') {
508
+ c = directive_next(&copy);
509
+ if (c == '8') c = directive_next(&copy);
510
+ }
511
+ if (c == 'R') c = directive_next(&copy);
512
+ return c == '"';
513
+ }
514
+
515
+ static int32_t directive_pragma_token(DirectiveInput *input) {
516
+ for (;;) {
517
+ int32_t c = directive_next(input);
518
+ if (c == '/' && directive_peek(input) == '*') {
519
+ bool line = false;
520
+ directive_comment(input, &line);
521
+ } else if (!directive_horizontal(c)) return c;
522
+ }
523
+ }
524
+
473
525
  static bool directive_comment(DirectiveInput *input, bool *line) {
474
526
  *line = directive_next(input) == '/';
475
527
  int32_t previous = 0;
@@ -501,7 +553,19 @@ static uint32_t directive_keyword_end(const DirectiveInput *input) {
501
553
  return directive_byte(&name);
502
554
  }
503
555
 
504
- static DirectiveKind directive_word(DirectiveInput *input, int32_t first, bool raw) {
556
+ static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, uint32_t end) {
557
+ DirectiveKind kind = directive_word_kind(word);
558
+ // Directives in a group that may be skipped need no operand, and the parser cannot tell which groups are skipped.
559
+ if (directive_requires_operand(kind) && !input->conditional_depth) line->required_end = end;
560
+ else state->directive_ends[end / 8] |= 1u << (end % 8);
561
+ if (word->length < sizeof word->directive) {
562
+ if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "ifdef") == 0 || strcmp(word->directive, "ifndef") == 0) input->conditional_depth++;
563
+ else if (strcmp(word->directive, "endif") == 0 && input->conditional_depth) input->conditional_depth--;
564
+ }
565
+ return kind;
566
+ }
567
+
568
+ static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw) {
505
569
  DirectiveWord word = {0};
506
570
  directive_word_character(&word, first);
507
571
  bool number = first >= '0' && first <= '9';
@@ -515,7 +579,7 @@ static DirectiveKind directive_word(DirectiveInput *input, int32_t first, bool r
515
579
  directive_word_character(&word, previous);
516
580
  }
517
581
  if (raw && gnu_raw_string_prefix(&word.raw) && directive_peek(input) == '"') directive_raw(input);
518
- return directive_word_kind(&word);
582
+ return word;
519
583
  }
520
584
 
521
585
  static DirectiveKind directive_word_kind(const DirectiveWord *word) {
@@ -525,8 +589,10 @@ static DirectiveKind directive_word_kind(const DirectiveWord *word) {
525
589
  if (strcmp(word->directive, "include") == 0 || strcmp(word->directive, "include_next") == 0 ||
526
590
  strcmp(word->directive, "embed") == 0) return DIRECTIVE_HEADER;
527
591
  if (strcmp(word->directive, "warning") == 0 || strcmp(word->directive, "error") == 0) return DIRECTIVE_DIAGNOSTIC;
592
+ if (strcmp(word->directive, "undef") == 0) return DIRECTIVE_UNDEF;
593
+ if (strcmp(word->directive, "line") == 0) return DIRECTIVE_LINE;
528
594
  static const char *const required[] = {
529
- "define", "ifdef", "ifndef", "elifdef", "elifndef", "undef", "line",
595
+ "define", "ifdef", "ifndef", "elifdef", "elifndef",
530
596
  };
531
597
  for (unsigned i = 0; i < sizeof required / sizeof *required; i++) {
532
598
  if (strcmp(word->directive, required[i]) == 0) return DIRECTIVE_REQUIRED;
Binary file
package/tree-sitter.json CHANGED
@@ -24,7 +24,7 @@
24
24
  }
25
25
  ],
26
26
  "metadata": {
27
- "version": "29.0.0",
27
+ "version": "31.0.0",
28
28
  "license": "MIT",
29
29
  "description": "C grammar for tree-sitter",
30
30
  "authors": [