@willbooster/tree-sitter-c 24.0.0 → 25.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 +20 -7
- package/grammar.js +9 -1
- package/package.json +3 -1
- package/src/conditionalMacro.h +23 -19
- package/src/emptyDirectives.h +439 -0
- package/src/functionMacro.h +159 -100
- package/src/macroProof.h +100 -0
- package/src/node-types.json +5 -0
- package/src/pragma.h +33 -5
- package/src/scanner.c +73 -23
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +3 -1
package/src/pragma.h
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// code-gauge-allow-file fileNcss 476 -- Preprocessor tokens and pragma operators share names, spacing, and literal readers.
|
|
1
2
|
#ifndef TREE_SITTER_C_PRAGMA_H_
|
|
2
3
|
#define TREE_SITTER_C_PRAGMA_H_
|
|
3
4
|
|
|
@@ -8,6 +9,11 @@
|
|
|
8
9
|
|
|
9
10
|
static const char pragma_word[] = "_Pragma";
|
|
10
11
|
|
|
12
|
+
typedef struct {
|
|
13
|
+
bool (*skip_empty)(TSLexer *, void *);
|
|
14
|
+
void *empty_context;
|
|
15
|
+
} PragmaTrivia;
|
|
16
|
+
|
|
11
17
|
typedef struct {
|
|
12
18
|
char *word;
|
|
13
19
|
unsigned capacity;
|
|
@@ -37,6 +43,9 @@ static bool scan_macro_name_ucn(TSLexer *lexer);
|
|
|
37
43
|
|
|
38
44
|
static bool scan_pragma_argument(TSLexer *lexer);
|
|
39
45
|
static bool scan_pragma_spacing(TSLexer *lexer);
|
|
46
|
+
static bool scan_pragma_spacing_with(TSLexer *lexer, const PragmaTrivia *trivia);
|
|
47
|
+
static bool scan_pragma_empty(TSLexer *lexer, const PragmaTrivia *trivia);
|
|
48
|
+
static bool scan_pragma_suffix_end_with(TSLexer *lexer, bool mark_end, const PragmaTrivia *trivia);
|
|
40
49
|
static bool scan_pragma_suffix(TSLexer *lexer);
|
|
41
50
|
static bool scan_pragma_suffix_end(TSLexer *lexer, bool mark_end);
|
|
42
51
|
static bool pragma_space(int32_t c);
|
|
@@ -48,12 +57,16 @@ static bool preproc_word(int32_t c, bool continuation);
|
|
|
48
57
|
static bool scan_preproc_quote(TSLexer *lexer, int32_t quote, bool escaped, bool directive_text);
|
|
49
58
|
static bool scan_preproc_raw_quote(TSLexer *lexer);
|
|
50
59
|
|
|
51
|
-
static bool
|
|
60
|
+
static bool scan_pragma_with(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
52
61
|
while (pragma_space(lexer->lookahead)) {
|
|
53
62
|
lexer->advance(lexer, true);
|
|
54
63
|
}
|
|
55
64
|
if (!scan_pragma_word(lexer)) return false;
|
|
56
|
-
return
|
|
65
|
+
return scan_pragma_suffix_end_with(lexer, true, trivia);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static bool scan_pragma(TSLexer *lexer) {
|
|
69
|
+
return scan_pragma_with(lexer, NULL);
|
|
57
70
|
}
|
|
58
71
|
|
|
59
72
|
static bool scan_pragma_suffix(TSLexer *lexer) {
|
|
@@ -61,11 +74,15 @@ static bool scan_pragma_suffix(TSLexer *lexer) {
|
|
|
61
74
|
}
|
|
62
75
|
|
|
63
76
|
static bool scan_pragma_suffix_end(TSLexer *lexer, bool mark_end) {
|
|
64
|
-
|
|
77
|
+
return scan_pragma_suffix_end_with(lexer, mark_end, NULL);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static bool scan_pragma_suffix_end_with(TSLexer *lexer, bool mark_end, const PragmaTrivia *trivia) {
|
|
81
|
+
if (!scan_pragma_spacing_with(lexer, trivia) || lexer->lookahead != '(') return false;
|
|
65
82
|
lexer->advance(lexer, false);
|
|
66
|
-
if (!
|
|
83
|
+
if (!scan_pragma_spacing_with(lexer, trivia)) return false;
|
|
67
84
|
if (!scan_pragma_argument(lexer)) return false;
|
|
68
|
-
if (!
|
|
85
|
+
if (!scan_pragma_spacing_with(lexer, trivia) || lexer->lookahead != ')') return false;
|
|
69
86
|
lexer->advance(lexer, false);
|
|
70
87
|
if (mark_end) lexer->mark_end(lexer);
|
|
71
88
|
return true;
|
|
@@ -98,6 +115,17 @@ static bool scan_pragma_argument(TSLexer *lexer) {
|
|
|
98
115
|
return true;
|
|
99
116
|
}
|
|
100
117
|
|
|
118
|
+
static bool scan_pragma_spacing_with(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
119
|
+
for (;;) {
|
|
120
|
+
if (!scan_pragma_spacing(lexer)) return false;
|
|
121
|
+
if (!scan_pragma_empty(lexer, trivia)) return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static bool scan_pragma_empty(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
126
|
+
return trivia && trivia->skip_empty && trivia->skip_empty(lexer, trivia->empty_context);
|
|
127
|
+
}
|
|
128
|
+
|
|
101
129
|
static bool scan_pragma_spacing(TSLexer *lexer) {
|
|
102
130
|
for (;;) {
|
|
103
131
|
if (pragma_space(lexer->lookahead)) {
|
package/src/scanner.c
CHANGED
|
@@ -1,39 +1,59 @@
|
|
|
1
1
|
#include "conditionalMacro.h"
|
|
2
2
|
#include "gnuRawString.h"
|
|
3
|
+
#include "emptyDirectives.h"
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
typedef struct {
|
|
6
|
+
EmptyDirectives directives;
|
|
7
|
+
MacroProofMemo proof_memo;
|
|
8
|
+
} CScanner;
|
|
5
9
|
|
|
6
|
-
|
|
10
|
+
enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, CONDITIONAL_MARKER_IFDEF, CONDITIONAL_MARKER_IFNDEF, LEADING_CONDITIONAL_MARKER_IFDEF, LEADING_CONDITIONAL_MARKER_IFNDEF, GNU_RAW_STRING_BODY, RAW_MACRO_TYPE_TAIL, BRACED_FUNCTION_ATTRIBUTE_START, BRACED_MACRO_TYPE_START, DIRECTIVE_CONTEXT, PREPROC_EMPTY };
|
|
11
|
+
|
|
12
|
+
static EmptyDirectives *scanner_directives(CScanner *scanner);
|
|
13
|
+
|
|
14
|
+
static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
|
|
15
|
+
static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
|
|
16
|
+
static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
|
|
7
17
|
|
|
8
18
|
void *tree_sitter_c_external_scanner_create(void) {
|
|
9
|
-
return
|
|
19
|
+
return calloc(1, sizeof(CScanner));
|
|
10
20
|
}
|
|
11
21
|
|
|
12
22
|
void tree_sitter_c_external_scanner_destroy(void *payload) {
|
|
13
|
-
|
|
23
|
+
CScanner *scanner = payload;
|
|
24
|
+
EmptyDirectives *state = scanner_directives(scanner);
|
|
25
|
+
if (!state) return;
|
|
26
|
+
free(state->starts);
|
|
27
|
+
free(state->ends);
|
|
28
|
+
free(state->line_ends);
|
|
29
|
+
macro_proof_destroy(&scanner->proof_memo);
|
|
30
|
+
free(scanner);
|
|
14
31
|
}
|
|
15
32
|
|
|
16
33
|
unsigned tree_sitter_c_external_scanner_serialize(void *payload, char *buffer) {
|
|
17
|
-
(
|
|
18
|
-
|
|
19
|
-
return
|
|
34
|
+
const EmptyDirectives *state = scanner_directives(payload);
|
|
35
|
+
buffer[0] = state && state->ready;
|
|
36
|
+
return 1;
|
|
20
37
|
}
|
|
21
38
|
|
|
22
39
|
void tree_sitter_c_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
|
|
23
|
-
|
|
24
|
-
(
|
|
25
|
-
(
|
|
40
|
+
CScanner *scanner = payload;
|
|
41
|
+
EmptyDirectives *state = scanner_directives(scanner);
|
|
42
|
+
if (state) state->ready = length && buffer[0];
|
|
26
43
|
}
|
|
27
44
|
|
|
28
45
|
bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
|
|
29
|
-
|
|
46
|
+
CScanner *scanner = payload;
|
|
47
|
+
EmptyDirectives *state = scanner_directives(scanner);
|
|
48
|
+
if (scan_directive_context(state, lexer, valid_symbols)) return true;
|
|
49
|
+
if (empty_directives_at_line_end(state, lexer)) return false;
|
|
30
50
|
if (valid_symbols[GNU_RAW_STRING_BODY] && !valid_symbols[PREPROC_LPAREN]) {
|
|
31
51
|
lexer->result_symbol = GNU_RAW_STRING_BODY;
|
|
32
52
|
return scan_gnu_raw_string_token(lexer);
|
|
33
53
|
}
|
|
34
54
|
if (valid_symbols[RAW_MACRO_TYPE_TAIL] && !valid_symbols[PREPROC_LPAREN]) {
|
|
35
55
|
lexer->result_symbol = RAW_MACRO_TYPE_TAIL;
|
|
36
|
-
return scan_raw_macro_type_tail(lexer);
|
|
56
|
+
return scan_raw_macro_type_tail(lexer, &(PragmaTrivia){empty_directives_skip, state});
|
|
37
57
|
}
|
|
38
58
|
if (valid_symbols[PREPROC_FUNCTION_NAME] && !valid_symbols[PREPROC_LPAREN]) {
|
|
39
59
|
lexer->result_symbol = PREPROC_FUNCTION_NAME;
|
|
@@ -41,23 +61,47 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
|
|
|
41
61
|
}
|
|
42
62
|
if (valid_symbols[PREPROC_LPAREN] || valid_symbols[PREPROC_NEWLINE] ||
|
|
43
63
|
valid_symbols[PREPROC_ARG] || valid_symbols[PREPROC_DIRECTIVE_ARG]) {
|
|
44
|
-
return scan_preproc_tokens(lexer, valid_symbols);
|
|
64
|
+
return scan_preproc_tokens(state, lexer, valid_symbols);
|
|
45
65
|
}
|
|
46
66
|
bool leading = valid_symbols[LEADING_CONDITIONAL_MARKER_IFDEF] || valid_symbols[LEADING_CONDITIONAL_MARKER_IFNDEF];
|
|
47
67
|
bool conditional = leading || valid_symbols[CONDITIONAL_MARKER_IFDEF] || valid_symbols[CONDITIONAL_MARKER_IFNDEF];
|
|
48
|
-
if (conditional)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
68
|
+
if (!conditional) return scan_declaration_tokens(scanner, lexer, valid_symbols);
|
|
69
|
+
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
70
|
+
const ConditionalMacroSymbols symbols = {
|
|
71
|
+
.ifdef = CONDITIONAL_MARKER_IFDEF, .ifndef = CONDITIONAL_MARKER_IFNDEF,
|
|
72
|
+
.leading_ifdef = LEADING_CONDITIONAL_MARKER_IFDEF, .leading_ifndef = LEADING_CONDITIONAL_MARKER_IFNDEF,
|
|
73
|
+
.skip_empty = empty_directives_skip, .empty_context = state,
|
|
74
|
+
};
|
|
75
|
+
if (lexer->lookahead == '#') return scan_conditional_macro(lexer, &symbols, valid_symbols);
|
|
76
|
+
return scan_declaration_tokens(scanner, lexer, valid_symbols);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
|
|
80
|
+
if (state && !state->ready && valid_symbols[DIRECTIVE_CONTEXT]) {
|
|
81
|
+
lexer->result_symbol = DIRECTIVE_CONTEXT;
|
|
82
|
+
return empty_directives_initialize(state, lexer);
|
|
83
|
+
}
|
|
84
|
+
bool directive_body = valid_symbols[PREPROC_NEWLINE] || valid_symbols[PREPROC_LPAREN] ||
|
|
85
|
+
valid_symbols[PREPROC_ARG] || valid_symbols[PREPROC_DIRECTIVE_ARG] || valid_symbols[GNU_RAW_STRING_BODY];
|
|
86
|
+
bool macro_name = valid_symbols[PREPROC_FUNCTION_NAME] && !valid_symbols[PREPROC_LPAREN];
|
|
87
|
+
if (valid_symbols[PREPROC_EMPTY] && !macro_name && (!directive_body || lexer->lookahead == '#') &&
|
|
88
|
+
empty_directives_consume(lexer, state, true)) {
|
|
89
|
+
lexer->mark_end(lexer);
|
|
90
|
+
lexer->result_symbol = PREPROC_EMPTY;
|
|
91
|
+
return true;
|
|
55
92
|
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
97
|
+
EmptyDirectives *state = scanner_directives(scanner);
|
|
56
98
|
static const TSSymbol attribute_tokens[] = {
|
|
57
99
|
FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, POST_TYPE_FUNCTION_ATTRIBUTE_START,
|
|
58
100
|
TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, BRACED_FUNCTION_ATTRIBUTE_START,
|
|
59
101
|
};
|
|
60
102
|
const MacroAttributeSymbols symbols = {
|
|
103
|
+
.trivia = {empty_directives_skip, state},
|
|
104
|
+
.proof_memo = scanner ? &scanner->proof_memo : NULL,
|
|
61
105
|
.pragma = PRAGMA_OPERATOR,
|
|
62
106
|
.object_start = TRAILING_OBJECT_DECLARATION_START,
|
|
63
107
|
.allow_pragma = valid_symbols[PRAGMA_OPERATOR],
|
|
@@ -77,10 +121,12 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
|
|
|
77
121
|
token == FUNCTION_ATTRIBUTE_START || token == POST_TYPE_FUNCTION_ATTRIBUTE_START,
|
|
78
122
|
token != FUNCTION_ATTRIBUTE_START, token == TRAILING_MACRO_ATTRIBUTE_NAME, &symbols);
|
|
79
123
|
}
|
|
80
|
-
return scan_preproc_tokens(lexer, valid_symbols);
|
|
124
|
+
return scan_preproc_tokens(state, lexer, valid_symbols);
|
|
81
125
|
}
|
|
82
126
|
|
|
83
|
-
|
|
127
|
+
// code-gauge-allow functionCognitiveComplexity 28 -- External-token validity selects one preprocessor lexing mode and preserves its token range.
|
|
128
|
+
// code-gauge-allow functionNcss 34 -- External-token validity selects one preprocessor lexing mode and preserves its token range.
|
|
129
|
+
static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
|
|
84
130
|
bool directive_text = !valid_symbols[PREPROC_ARG] && valid_symbols[PREPROC_DIRECTIVE_ARG];
|
|
85
131
|
PreprocArgumentMode argument_mode = preproc_argument_mode(
|
|
86
132
|
valid_symbols[PREPROC_ARG], valid_symbols[PREPROC_DIRECTIVE_ARG], valid_symbols[PREPROC_LPAREN]);
|
|
@@ -120,5 +166,9 @@ static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols) {
|
|
|
120
166
|
return scan_preproc_arg(lexer, false, argument_mode);
|
|
121
167
|
}
|
|
122
168
|
lexer->result_symbol = PRAGMA_OPERATOR;
|
|
123
|
-
return valid_symbols[PRAGMA_OPERATOR] &&
|
|
169
|
+
return valid_symbols[PRAGMA_OPERATOR] && scan_pragma_with(lexer, &(PragmaTrivia){empty_directives_skip, state});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static EmptyDirectives *scanner_directives(CScanner *scanner) {
|
|
173
|
+
return scanner ? &scanner->directives : NULL;
|
|
124
174
|
}
|
package/tree-sitter-c.wasm
CHANGED
|
Binary file
|
package/tree-sitter.json
CHANGED
|
@@ -16,13 +16,15 @@
|
|
|
16
16
|
"src/identifier.h",
|
|
17
17
|
"src/keywords.h",
|
|
18
18
|
"src/functionMacro.h",
|
|
19
|
+
"src/macroProof.h",
|
|
19
20
|
"src/conditionalMacro.h",
|
|
21
|
+
"src/emptyDirectives.h",
|
|
20
22
|
"src/gnuRawString.h"
|
|
21
23
|
]
|
|
22
24
|
}
|
|
23
25
|
],
|
|
24
26
|
"metadata": {
|
|
25
|
-
"version": "
|
|
27
|
+
"version": "25.0.0",
|
|
26
28
|
"license": "MIT",
|
|
27
29
|
"description": "C grammar for tree-sitter",
|
|
28
30
|
"authors": [
|