@willbooster/tree-sitter-c 43.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/README.md CHANGED
@@ -66,22 +66,25 @@ parser.setLanguage(await Language.load(c));
66
66
  The package also ships `grammar.js`, scanner sources under `src/`,
67
67
  and the node types in `src/node-types.json` for grammars extending C (such as C++).
68
68
  The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, `functionMacro.h`,
69
- `conditionalMacro.h`, `emptyDirectives.h`, `directiveBuffer.h`, `directiveScan.h`, `directiveUnits.h`, `macroProof.h`, and
70
- `gnuRawString.h`.
69
+ `conditionalMacro.h`, `emptyDirectives.h`, `directiveFacts.h`, `directiveBuffer.h`, `directiveScan.h`,
70
+ `directiveUnits.h`, `macroProof.h`, and `gnuRawString.h`.
71
71
 
72
72
  Derived grammars must port the inherited external-token dispatch from `src/scanner.c`.
73
73
  The headers call `peek_units` and `advance_units` of `TSLexer`, so generate the derived parser with the CLI of
74
74
  WillBooster/tree-sitter 23.1.0 or later, whose `src/tree_sitter/parser.h` declares them, and run it on that runtime.
75
75
  Append `_directive_context` and `preproc_empty` in their inherited external-token slots and retain them as extras.
76
- Append `_preproc_directive_start` in its inherited slot as a zero-width guard after every directive keyword token,
77
- including keywords in overridden directive rules. It is not an extra. Retain the phase-two splice handling in
78
- `preprocessorToken` when overriding keyword spelling.
79
- Port the scanner lifecycle functions together with `EmptyDirectives` from `emptyDirectives.h`; the source-boundary
80
- facts belong to one full parse and are freed by `empty_directives_release` in scanner destruction. When the scanner is ready and no external token
81
- other than `pragma_operator`, `_directive_context` and `preproc_empty` is valid, dispatch to `scan_extras` first;
82
- `only_extras_valid` checks the inherited slots only, so extend it to every external token the derived grammar adds,
83
- or the fast path drops those tokens. Otherwise run `scan_directive_context` first,
84
- emitting `_preproc_directive_start` only at `empty_directives_after_keyword` before null-directive consumption,
76
+ Append `_preproc_include`, `_preproc_define`, `_preproc_if`, `_preproc_ifdef`, `_preproc_ifndef`, `_preproc_else`,
77
+ `_preproc_elif`, `_preproc_elifdef`, `_preproc_elifndef`, `_preproc_endif`, `_preproc_pragma`, and `preproc_directive`
78
+ in their inherited slots, and scan `#` directives with `empty_directives_scan_word`, `empty_directives_has_keyword_end`,
79
+ and `empty_directives_select_symbol`, which check the logical-line start and keyword-end facts recorded in `EmptyDirectives`.
80
+ Port the scanner lifecycle functions together with `EmptyDirectives` from `directiveFacts.h` and `emptyDirectives.h`; the source-boundary
81
+ facts belong to one full parse and are freed by `empty_directives_release` in scanner destruction. The entry fast path in
82
+ `tree_sitter_c_external_scanner_scan` (together with `scan_c_token_spaced_fast`), `only_extras_valid`, `any_preproc_body_valid`,
83
+ `can_start_spaced_c_token`, and the `peek_after_ascii_spaces` precheck in `scan_active_tokens` inspect only the inherited external-token slots
84
+ and starter characters before `scan_c_token_slow` or token-specific lookahead runs; extend or bypass every one of those predicates for each
85
+ external token the derived grammar adds (including unspaced tokens, error-recovery slots, and spaced punctuation or identifier starters), or
86
+ those filters will reject the derived tokens before their scanner runs. When the scanner is ready and only extras, conditional markers,
87
+ `_trailing_object_declaration_start`, or external directive tokens are valid, dispatch to `scan_extras` first. Otherwise check null-directive consumption before directive-body tokens,
85
88
  then reject external scans at `empty_directives_at_line_end` before the other token dispatch. Pass `empty_directives_skip` and its state through `ConditionalMacroSymbols.skip_empty` and
86
89
  `empty_context` so declaration lookahead ignores null directives without moving the opening token's range.
87
90
  Keep preprocessor argument/newline handling ahead of annotation lookahead.
@@ -89,15 +92,15 @@ Port `preproc_argument_mode` selection together with `scan_preproc_arg` so token
89
92
  Append `_gnu_raw_string_body` and `_raw_macro_type_tail` in their inherited enum slots and port their dispatch from `src/scanner.c`.
90
93
  A derived grammar overriding `raw_string_literal` can keep its own body scanner, but must retain the inherited enum slots.
91
94
  Port `_conditional_marker_ifdef`, `_conditional_marker_ifndef`, `_leading_conditional_marker_ifdef`, and
92
- `_leading_conditional_marker_ifndef` with `scan_conditional_macro`. Require `empty_directives_at_start` at the opening
95
+ `_leading_conditional_marker_ifndef` with `scan_conditional_macro_after_keyword`. Require `empty_directives_at_start` at the opening
93
96
  `#` before this dispatch, including after skipped whitespace. The leading pair checks a following type prefix;
94
97
  when both contexts are valid, choose the leading role if its type proof succeeds, otherwise use the valid
95
98
  post-type role. Each token consumes only its opening directive keyword; the directive body, closing directive and following prefix are lookahead. Keep this dispatch ahead of
96
- the function-annotation guards and return immediately after a failed directive scan.
99
+ the function-annotation guards and fall back to the inherited directive token when the marker proof fails.
97
100
  The hidden `_function_attribute_start`, `_post_type_function_attribute_start`,
98
101
  `_post_type_function_attribute_suffix`, and `_braced_function_attribute_start` guards look ahead before the ordinary
99
102
  identifier token. Keep them outside the name field so identifier leaves and ordinary C lexing retain their shape.
100
- Port `scan_function_attribute_guard`, its `MacroAttributeSymbols` context, and the name-range handling in `pragma.h`.
103
+ Port `scan_function_attribute_guard_from`, its `MacroAttributeSymbols` context, and the name-range handling in `pragma.h`.
101
104
  Set `MacroAttributeSymbols.trivia` to `{empty_directives_skip, state}` so annotation, argument-group, and declaration-tail proofs skip the same null directives.
102
105
  Pass that `PragmaTrivia` context to the raw macro-type tail and balanced-group helpers, and use `scan_pragma_with` for the pragma-operator fallback.
103
106
  A derived scanner that omits this context cannot recognize null directives inside those lookahead paths.
@@ -105,7 +108,7 @@ Set `braced_start` to the inherited braced token and `allow_braced_start` from i
105
108
  Omitting these fields disables braced handoff in the current argument group and in prototype-tail lookahead.
106
109
  Preserve the inherited external-token order before adding tokens specific to the derived grammar.
107
110
 
108
- Port `_braced_macro_type_start` and `scan_braced_macro_type_start` before function-annotation lookahead.
111
+ Port `_braced_macro_type_start` and `scan_braced_macro_type_start` (or `scan_braced_macro_type_start_from` when a shared leading-trivia pass has already consumed an opening UCN backslash) before function-annotation lookahead.
109
112
  This separate zero-width guard proves marker words followed by a braced macro-type argument; the grammar still
110
113
  consumes each marker and validates its type. Keep `MacroName.preserve_end` in `pragma.h` so scanning subsequent words
111
114
  does not move the guard's end. Its default is false for ordinary name scans. Derived grammars can supply reserved
package/grammar.js CHANGED
@@ -95,7 +95,6 @@ const BLOCK_COMMENT = seq(
95
95
  COMMENT_SPLICES,
96
96
  '/'
97
97
  );
98
- const PREPROC_PREFIX = seq('#', repeat(choice(/[ \t\v\f]/, LINE_SPLICE, BLOCK_COMMENT)));
99
98
  const PRAGMA_SPACING = repeat(choice(/\s/, /\\(?:\r\n?|\n\r?)/));
100
99
  const PREPROC_ARGUMENT = /\S([^/\n]|\/[^*]|\\\r?\n)*/;
101
100
  const VA_ARG_KEYWORDS = choice('va_arg', '__builtin_va_arg');
@@ -541,7 +540,31 @@ module.exports = Object.assign(
541
540
  sym('_braced_macro_type_start'),
542
541
  sym('_directive_context'),
543
542
  sym('preproc_empty'),
544
- sym('_preproc_directive_start'),
543
+ sym('_preproc_include'),
544
+ sym('_preproc_define'),
545
+ sym('_preproc_if'),
546
+ sym('_preproc_ifdef'),
547
+ sym('_preproc_ifndef'),
548
+ sym('_preproc_else'),
549
+ sym('_preproc_elif'),
550
+ sym('_preproc_elifdef'),
551
+ sym('_preproc_elifndef'),
552
+ sym('_preproc_endif'),
553
+ sym('_preproc_pragma'),
554
+ sym('preproc_directive'),
555
+ ],
556
+
557
+ mergeableExternals: () => [
558
+ sym('_preproc_if'),
559
+ sym('_preproc_ifdef'),
560
+ sym('_preproc_ifndef'),
561
+ sym('_preproc_else'),
562
+ sym('_preproc_elif'),
563
+ sym('_preproc_elifdef'),
564
+ sym('_preproc_elifndef'),
565
+ sym('_preproc_endif'),
566
+ sym('_preproc_pragma'),
567
+ sym('preproc_directive'),
545
568
  ],
546
569
 
547
570
  extras: ($) => [
@@ -752,10 +775,10 @@ module.exports = Object.assign(
752
775
  choice(
753
776
  preprocCall(
754
777
  $,
755
- alias(preprocessorToken('pragma'), $.preproc_directive),
778
+ alias(sym('_preproc_pragma'), sym('preproc_directive')),
756
779
  choice($._preproc_directive_arg, $.preproc_arg)
757
780
  ),
758
- preprocCall($, $.preproc_directive, $._preproc_directive_arg)
781
+ preprocCall($, sym('preproc_directive'), $._preproc_directive_arg)
759
782
  ),
760
783
 
761
784
  ...preprocIf('_in_header', () => sym('_header_preproc_item')),
@@ -764,20 +787,43 @@ module.exports = Object.assign(
764
787
  ...preprocIf('_in_single_case', () => sym('_single_case_body'), 0, true),
765
788
  ...preprocIf('_in_field_declaration_list', () => sym('_field_declaration_list_item')),
766
789
  ...preprocIf('_in_enumerator_list', () => sym('_enumerator_list_item')),
790
+ ...preprocIf('_in_enumerator_directives', () => sym('_enumerator_directive')),
767
791
  ...preprocIf(
768
792
  '_in_enumerator_list_no_comma',
769
793
  () =>
770
794
  choice(
771
- seq(repeat(sym('_enumerator_list_item')), sym('enumerator'), optional(sym('_enumerator_directives'))),
795
+ seq(
796
+ repeat(sym('_enumerator_list_item')),
797
+ choice(
798
+ sym('enumerator'),
799
+ alias(sym('preproc_if_in_enumerator_list_no_comma'), sym('preproc_if')),
800
+ alias(sym('preproc_ifdef_in_enumerator_list_no_comma'), sym('preproc_ifdef'))
801
+ ),
802
+ optional(sym('_enumerator_directives'))
803
+ ),
772
804
  repeat1(sym('_enumerator_list_item'))
773
805
  ),
774
806
  0,
775
807
  false
776
808
  ),
777
809
 
778
- _enumerator_list_item: ($) => choice(seq($.enumerator, ','), $._enumerator_macro, $._enumerator_directives),
810
+ _enumerator_list_item: ($) =>
811
+ choice(
812
+ seq($.enumerator, ','),
813
+ $._enumerator_macro,
814
+ prec.right(repeat1($.preproc_call)),
815
+ alias(sym('preproc_if_in_enumerator_list'), sym('preproc_if')),
816
+ alias(sym('preproc_ifdef_in_enumerator_list'), sym('preproc_ifdef'))
817
+ ),
818
+
819
+ _enumerator_directive: ($) =>
820
+ choice(
821
+ $.preproc_call,
822
+ alias(sym('preproc_if_in_enumerator_directives'), sym('preproc_if')),
823
+ alias(sym('preproc_ifdef_in_enumerator_directives'), sym('preproc_ifdef'))
824
+ ),
779
825
 
780
- _enumerator_directives: ($) => prec.right(repeat1($.preproc_call)),
826
+ _enumerator_directives: ($) => prec.right(repeat1($._enumerator_directive)),
781
827
 
782
828
  _preproc_newline: () => token.immediate(/\r?\n/),
783
829
 
@@ -786,7 +832,6 @@ module.exports = Object.assign(
786
832
  preproc_arg: () => token(prec(-1, PREPROC_ARGUMENT)),
787
833
 
788
834
  _preproc_directive_arg: () => token(prec(-2, PREPROC_ARGUMENT)),
789
- preproc_directive: () => token(seq(PREPROC_PREFIX, /[a-zA-Z0-9]/, repeat(seq(repeat(LINE_SPLICE), /\w/)))),
790
835
 
791
836
  _preproc_expression: ($) =>
792
837
  choice(
@@ -1684,10 +1729,10 @@ module.exports = Object.assign(
1684
1729
  $.enumerator,
1685
1730
  alias(sym('preproc_if_in_enumerator_list_no_comma'), sym('preproc_if')),
1686
1731
  alias(sym('preproc_ifdef_in_enumerator_list_no_comma'), sym('preproc_ifdef'))
1687
- )
1732
+ ),
1733
+ optional($._enumerator_directives)
1688
1734
  )
1689
1735
  ),
1690
- repeat($.preproc_call),
1691
1736
  '}'
1692
1737
  ),
1693
1738
 
@@ -2689,7 +2734,6 @@ function declarationSpecifiers($, braced = false) {
2689
2734
  function preprocCall($, directive, argument) {
2690
2735
  return seq(
2691
2736
  field('directive', directive),
2692
- sym('_preproc_directive_start'),
2693
2737
  repeat(choice($.comment, $.pragma_operator)),
2694
2738
  optional(seq(field('argument', alias(argument, $.preproc_arg)), repeat(choice($.comment, $.pragma_operator)))),
2695
2739
  sym('_preproc_newline')
@@ -3103,18 +3147,10 @@ function preprocIf(suffix, content, precedence = 0, repeatContent = true) {
3103
3147
  *
3104
3148
  * @param {string} command
3105
3149
  *
3106
- * @returns {import('./types/treeSitterDsl').SeqRule}
3150
+ * @returns {import('./types/treeSitterDsl').AliasRule}
3107
3151
  */
3108
3152
  function preprocessor(command) {
3109
- return seq(alias(preprocessorToken(command), '#' + command), sym('_preproc_directive_start'));
3110
- }
3111
-
3112
- /** @param {string} command */
3113
- function preprocessorToken(command) {
3114
- const spelling = Array.from(command, (character, index) =>
3115
- index === 0 ? character : seq(repeat(LINE_SPLICE), character)
3116
- );
3117
- return token(seq(PREPROC_PREFIX, ...spelling));
3153
+ return alias(sym('_preproc_' + command), '#' + command);
3118
3154
  }
3119
3155
 
3120
3156
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "43.0.0",
3
+ "version": "45.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -26,6 +26,7 @@
26
26
  "src/functionMacro.h",
27
27
  "src/macroProof.h",
28
28
  "src/conditionalMacro.h",
29
+ "src/directiveFacts.h",
29
30
  "src/emptyDirectives.h",
30
31
  "src/directiveScan.h",
31
32
  "src/directiveUnits.h",
@@ -13,6 +13,7 @@ typedef struct {
13
13
  void *empty_context;
14
14
  } ConditionalMacroSymbols;
15
15
 
16
+ static bool scan_conditional_macro_after_keyword(TSLexer *lexer, bool ifdef, const ConditionalMacroSymbols *symbols, const bool *valid_symbols);
16
17
  static bool conditional_macro_body(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
17
18
  static bool conditional_macro_word(TSLexer *lexer, MacroName *name, bool escaped);
18
19
  static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped, bool directive);
@@ -27,18 +28,12 @@ static bool conditional_macro_reserved_word(const ConditionalMacroSymbols *symbo
27
28
  static bool conditional_macro_modifier_group(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const char *word, bool *escaped);
28
29
  static bool conditional_macro_attribute_declaration(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
29
30
 
30
- static bool scan_conditional_macro(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const bool *valid_symbols) {
31
- char directive[7] = {0};
32
- MacroName command = {.word = directive, .capacity = sizeof(directive) - 1, .ascii = true};
33
- if (!conditional_macro_directive(lexer, &command) || !command.ascii) return false;
34
- bool ifdef = strcmp(directive, "ifdef") == 0;
35
- if (!ifdef && strcmp(directive, "ifndef") != 0) return false;
31
+ static bool scan_conditional_macro_after_keyword(TSLexer *lexer, bool ifdef, const ConditionalMacroSymbols *symbols, const bool *valid_symbols) {
36
32
  TSSymbol leading_symbol = ifdef ? symbols->leading_ifdef : symbols->leading_ifndef;
37
33
  TSSymbol generic_symbol = ifdef ? symbols->ifdef : symbols->ifndef;
38
34
  bool leading = valid_symbols[leading_symbol];
39
35
  bool generic = valid_symbols[generic_symbol];
40
36
  if (!leading && !generic) return false;
41
- lexer->mark_end(lexer);
42
37
 
43
38
  bool escaped = false;
44
39
  if (!conditional_macro_body(lexer, symbols, &escaped)) return false;
@@ -39,6 +39,29 @@ typedef struct {
39
39
  bool spaced_splice;
40
40
  } DirectiveInput;
41
41
 
42
+ typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
43
+
44
+ typedef struct {
45
+ uint32_t value;
46
+ unsigned digits, limit;
47
+ bool named, braced;
48
+ } DirectiveUcn;
49
+
50
+ static void directive_shift(DirectiveBuffer *buffer, uint32_t start);
51
+ static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width);
52
+ static int32_t directive_next(DirectiveInput *input);
53
+ static int32_t directive_physical_next(DirectiveInput *input);
54
+ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c);
55
+ static bool directive_skip_splice(DirectiveInput *input);
56
+ static uint32_t directive_splice_end(const DirectiveInput *input);
57
+ static int32_t directive_peek(const DirectiveInput *input);
58
+ static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run);
59
+ static void directive_advance_plain(DirectiveInput *input, uint32_t length);
60
+ static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end);
61
+ static bool directive_run_continues(DirectiveRun run, uint16_t c);
62
+ static void directive_skip_plain(DirectiveInput *input);
63
+ static int32_t directive_plain(const DirectiveInput *input);
64
+ static uint32_t directive_byte(const DirectiveInput *input);
42
65
  static inline bool directive_has(const DirectiveInput *input, uint32_t index);
43
66
  static bool directive_fill(DirectiveBuffer *buffer, uint32_t index);
44
67
  static uint32_t directive_fill_units(DirectiveBuffer *buffer, uint32_t limit);
@@ -52,6 +75,205 @@ static uint16_t directive_character(const DirectiveInput *input, uint32_t index)
52
75
  static void directive_store(DirectiveBuffer *buffer, uint16_t c);
53
76
  static void directive_release(DirectiveBuffer *buffer);
54
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);
82
+
83
+ // Drops the characters before `start`, which `DirectiveScan` has consumed.
84
+ static void directive_shift(DirectiveBuffer *buffer, uint32_t start) {
85
+ if (!start) return;
86
+ uint32_t length = buffer->length - start;
87
+ for (uint32_t i = 0; i < length; i++) {
88
+ uint32_t from = start + i;
89
+ buffer->characters[i >> DIRECTIVE_PAGE_SHIFT][i & (DIRECTIVE_PAGE_SIZE - 1)] =
90
+ buffer->characters[from >> DIRECTIVE_PAGE_SHIFT][from & (DIRECTIVE_PAGE_SIZE - 1)];
91
+ }
92
+ buffer->length = length;
93
+ uint32_t count = 0;
94
+ for (uint32_t i = 0; i < buffer->position_count; i++) {
95
+ DirectivePosition position = buffer->positions[i];
96
+ if (position.index < start) continue;
97
+ position.index -= start;
98
+ buffer->positions[count++] = position;
99
+ }
100
+ buffer->position_count = count;
101
+ }
102
+
103
+ // Moves back to the character before `*index`, reading a surrogate pair as one character, and decodes it into `*c` and
104
+ // its byte width into `*width`; false when its width exceeds the bytes left.
105
+ static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width) {
106
+ const DirectiveBuffer *buffer = input->buffer;
107
+ uint32_t i = --*index;
108
+ if (i && directive_character(input, i) >= 0xdc00 && directive_character(input, i) <= 0xdfff &&
109
+ directive_character(input, i - 1) >= 0xd800 && directive_character(input, i - 1) <= 0xdbff) i = --*index;
110
+ *width = directive_expected_width(input, i, c);
111
+ if (*position && buffer->positions[*position - 1].index == i) {
112
+ DirectivePosition recorded = buffer->positions[--*position];
113
+ *byte = recorded.byte;
114
+ *width = recorded.width;
115
+ return true;
116
+ }
117
+ if (*width > *byte) return false;
118
+ *byte -= *width;
119
+ return true;
120
+ }
121
+
122
+ static int32_t directive_next(DirectiveInput *input) {
123
+ int32_t plain = directive_plain(input);
124
+ if (plain >= 0) {
125
+ directive_skip_plain(input);
126
+ return plain;
127
+ }
128
+ while (directive_has(input, input->index)) {
129
+ int32_t c = directive_physical_next(input);
130
+ if (c == '\\' && directive_skip_splice(input)) continue;
131
+ if (directive_newline(c)) {
132
+ if (directive_has(input, input->index) && directive_newline(directive_character(input, input->index)) &&
133
+ directive_character(input, input->index) != c) directive_physical_next(input);
134
+ return '\n';
135
+ }
136
+ return c;
137
+ }
138
+ return -1;
139
+ }
140
+
141
+ static int32_t directive_physical_next(DirectiveInput *input) {
142
+ const DirectiveBuffer *buffer = input->buffer;
143
+ uint32_t index = input->index;
144
+ int32_t c;
145
+ uint32_t width = directive_expected_width(input, index, &c);
146
+ input->index += c > 0xffff ? 2 : 1;
147
+ input->before_previous_byte = input->previous_byte;
148
+ input->previous_byte = input->byte;
149
+ if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index == index) {
150
+ DirectivePosition position = buffer->positions[input->position_index++];
151
+ input->previous_byte = position.byte;
152
+ input->byte = position.byte + position.width;
153
+ } else input->byte += width;
154
+ return c;
155
+ }
156
+
157
+ // Decodes the character at `index` into `*c` and returns its byte width when it has no recorded position.
158
+ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c) {
159
+ uint32_t unit = directive_character(input, index);
160
+ if (unit >= 0xd800 && unit <= 0xdbff && directive_has(input, index + 1)) {
161
+ uint32_t low = directive_character(input, index + 1);
162
+ if (low >= 0xdc00 && low <= 0xdfff) {
163
+ *c = (int32_t)(0x10000 + ((unit - 0xd800) << 10) + (low - 0xdc00));
164
+ return 4;
165
+ }
166
+ }
167
+ *c = (int32_t)unit;
168
+ return input->buffer->utf16 ? 2 : directive_utf8_width(unit);
169
+ }
170
+
171
+ // Consumes the rest of a line splice after its `\\`; false when none follows.
172
+ static bool directive_skip_splice(DirectiveInput *input) {
173
+ uint32_t after = directive_splice_end(input);
174
+ if (after == input->index) return false;
175
+ if (directive_horizontal(directive_character(input, input->index))) input->spaced_splice = true;
176
+ while (input->index < after) directive_physical_next(input);
177
+ return true;
178
+ }
179
+
180
+ static uint32_t directive_splice_end(const DirectiveInput *input) {
181
+ uint32_t after = input->index;
182
+ while (directive_has(input, after) && directive_horizontal(directive_character(input, after))) after++;
183
+ if (!directive_has(input, after) || !directive_newline(directive_character(input, after))) return input->index;
184
+ int32_t newline = directive_character(input, after++);
185
+ if (directive_has(input, after) && directive_newline(directive_character(input, after)) &&
186
+ directive_character(input, after) != newline) after++;
187
+ return after;
188
+ }
189
+
190
+ static int32_t directive_peek(const DirectiveInput *input) {
191
+ int32_t plain = directive_plain(input);
192
+ if (plain >= 0) return plain;
193
+ DirectiveInput copy = *input;
194
+ return directive_next(&copy);
195
+ }
196
+
197
+ // Consumes the longest run of plain characters that `run` accepts and returns its length.
198
+ static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run) {
199
+ const DirectiveBuffer *buffer = input->buffer;
200
+ uint32_t index = input->index;
201
+ // Scans the stored characters before the next recorded position and reads more only at their end, because reading
202
+ // may store a character with a recorded position, which ends the run.
203
+ while (directive_has(input, index)) {
204
+ uint32_t end = buffer->length;
205
+ if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index < end) {
206
+ end = buffer->positions[input->position_index].index;
207
+ }
208
+ uint32_t start = index;
209
+ index = directive_run_end(buffer, run, index, end);
210
+ if (index < end || index == start || end < buffer->length) break;
211
+ }
212
+ uint32_t length = index - input->index;
213
+ if (length) directive_advance_plain(input, length);
214
+ return length;
215
+ }
216
+
217
+ // Moves `input` over `length` plain characters, at least one.
218
+ static void directive_advance_plain(DirectiveInput *input, uint32_t length) {
219
+ uint32_t width = input->buffer->utf16 ? 2 : 1;
220
+ input->before_previous_byte = length > 1 ? input->byte + (length - 2) * width : input->previous_byte;
221
+ input->previous_byte = input->byte + (length - 1) * width;
222
+ input->byte += length * width;
223
+ input->index += length;
224
+ }
225
+
226
+ // The index of the first stored character from `index` to `end` that ends the run, or `end`.
227
+ static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end) {
228
+ while (index < end) {
229
+ const uint16_t *page = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT];
230
+ uint32_t page_end = (index | (DIRECTIVE_PAGE_SIZE - 1)) + 1;
231
+ if (page_end > end) page_end = end;
232
+ for (; index < page_end; index++) {
233
+ uint16_t c = page[index & (DIRECTIVE_PAGE_SIZE - 1)];
234
+ if (c >= 0x80 || c == '\\' || directive_newline(c) || !directive_run_continues(run, c)) return index;
235
+ }
236
+ }
237
+ return end;
238
+ }
239
+
240
+ // `c` is ASCII.
241
+ static bool directive_run_continues(DirectiveRun run, uint16_t c) {
242
+ bool identifier = (preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1;
243
+ switch (run) {
244
+ case DIRECTIVE_RUN_WORD:
245
+ return identifier;
246
+ case DIRECTIVE_RUN_SPACE:
247
+ return directive_horizontal(c);
248
+ case DIRECTIVE_RUN_LINE_COMMENT:
249
+ return true;
250
+ case DIRECTIVE_RUN_BLOCK_COMMENT:
251
+ return c != '*';
252
+ }
253
+ return false;
254
+ }
255
+
256
+ static void directive_skip_plain(DirectiveInput *input) {
257
+ input->index++;
258
+ input->before_previous_byte = input->previous_byte;
259
+ input->previous_byte = input->byte;
260
+ input->byte += input->buffer->utf16 ? 2 : 1;
261
+ }
262
+
263
+ // The next character when it is ASCII, cannot start a line splice or a newline, and has its expected width; otherwise -1.
264
+ static int32_t directive_plain(const DirectiveInput *input) {
265
+ if (!directive_has(input, input->index)) return -1;
266
+ uint16_t c = directive_character(input, input->index);
267
+ if (c >= 0x80 || c == '\\' || directive_newline(c)) return -1;
268
+ const DirectiveBuffer *buffer = input->buffer;
269
+ bool positioned = input->position_index < buffer->position_count &&
270
+ buffer->positions[input->position_index].index == input->index;
271
+ return positioned ? -1 : c;
272
+ }
273
+
274
+ static uint32_t directive_byte(const DirectiveInput *input) {
275
+ return input->byte;
276
+ }
55
277
 
56
278
  // Reads characters from the lexer until `index` is stored; false at the end of the input.
57
279
  static inline bool directive_has(const DirectiveInput *input, uint32_t index) {
@@ -216,4 +438,46 @@ static unsigned directive_utf8_width(uint32_t c) {
216
438
  return c < 0x80 || c > 0x10ffff ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
217
439
  }
218
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
+
219
483
  #endif