-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscanner.c
More file actions
321 lines (294 loc) · 9.62 KB
/
Copy pathscanner.c
File metadata and controls
321 lines (294 loc) · 9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// External scanner for tree-sitter-jac (hand-written C).
//
// This is the ONE hand-authored native file in the project; the grammar itself
// is authored in Jac (grammar.jac -> src/grammar.json) and src/parser.c is
// generated. The scanner supplies tokens whose boundaries the context-free
// grammar cannot express, because they require unbounded, context-sensitive
// lexing:
//
// * PYINLINE -- a `::py:: ... ::py::` inline-Python block (PYNLINE).
// * JSX_TEXT -- raw character data between JSX tags (up to `<`/`{`/`}`).
// * FSTRING_START -- the prefix + opening quote of an f-string (`f"`, `rf'''`).
// * FSTRING_CONTENT-- a run of literal f-string text between interpolations.
// * FSTRING_END -- the closing quote of an f-string.
//
// The TokenType enum order MUST match the `externals` array in grammar.jac.
//
// f-strings carry state: the quote character and triple-ness of every f-string
// currently open (they nest: `f"{f'{x}'}"`). That stack is serialized so
// tree-sitter can restore it across speculative lexing.
#include "tree_sitter/parser.h"
#include <stdlib.h>
#include <wctype.h>
enum TokenType {
PYINLINE,
JSX_TEXT,
FSTRING_START,
FSTRING_CONTENT,
FSTRING_END,
};
#define MAX_FSTRING_DEPTH 24
typedef struct {
char quote; // '"' or '\''
bool triple; // """ / ''' delimited
} FString;
typedef struct {
FString stack[MAX_FSTRING_DEPTH];
uint8_t depth;
} Scanner;
// ---------------------------------------------------------------------------
// Lifecycle + serialization
// ---------------------------------------------------------------------------
void *tree_sitter_jac_external_scanner_create(void) {
Scanner *s = (Scanner *)malloc(sizeof(Scanner));
if (s) {
s->depth = 0;
}
return s;
}
void tree_sitter_jac_external_scanner_destroy(void *payload) { free(payload); }
unsigned tree_sitter_jac_external_scanner_serialize(void *payload, char *buffer) {
Scanner *s = (Scanner *)payload;
unsigned n = 0;
buffer[n++] = (char)s->depth;
for (uint8_t i = 0; i < s->depth; i++) {
buffer[n++] = s->stack[i].quote;
buffer[n++] = (char)(s->stack[i].triple ? 1 : 0);
}
return n;
}
void tree_sitter_jac_external_scanner_deserialize(void *payload, const char *buffer,
unsigned length) {
Scanner *s = (Scanner *)payload;
s->depth = 0;
if (length == 0) {
return;
}
unsigned n = 0;
s->depth = (uint8_t)buffer[n++];
for (uint8_t i = 0; i < s->depth; i++) {
s->stack[i].quote = buffer[n++];
s->stack[i].triple = buffer[n++] != 0;
}
}
// ---------------------------------------------------------------------------
// `::py::` inline-Python block
// ---------------------------------------------------------------------------
static const char PY_MARK[] = {':', ':', 'p', 'y', ':', ':'};
static const int PY_MARK_LEN = 6;
// KMP longest-proper-prefix-suffix table for "::py::" (fallback uses LPS[m-1]).
static const int PY_LPS[] = {0, 1, 0, 0, 1, 2};
static bool scan_pyinline(TSLexer *lexer) {
while (iswspace(lexer->lookahead)) {
lexer->advance(lexer, true);
}
for (int i = 0; i < PY_MARK_LEN; i++) {
if (lexer->lookahead != (int32_t)PY_MARK[i]) {
return false;
}
lexer->advance(lexer, false);
}
int m = 0;
while (!lexer->eof(lexer)) {
int32_t c = lexer->lookahead;
while (m > 0 && c != (int32_t)PY_MARK[m]) {
m = PY_LPS[m - 1];
}
if (c == (int32_t)PY_MARK[m]) {
m++;
}
lexer->advance(lexer, false);
if (m == PY_MARK_LEN) {
lexer->mark_end(lexer);
lexer->result_symbol = PYINLINE;
return true;
}
}
return false;
}
// ---------------------------------------------------------------------------
// JSX raw text
// ---------------------------------------------------------------------------
static bool scan_jsx_text(TSLexer *lexer) {
while (iswspace(lexer->lookahead)) {
lexer->advance(lexer, true);
}
bool has_text = false;
while (!lexer->eof(lexer)) {
int32_t c = lexer->lookahead;
if (c == '<' || c == '{' || c == '}') {
break;
}
lexer->advance(lexer, false);
has_text = true;
lexer->mark_end(lexer);
}
if (has_text) {
lexer->result_symbol = JSX_TEXT;
return true;
}
return false;
}
// ---------------------------------------------------------------------------
// f-strings
// ---------------------------------------------------------------------------
// `f"`, `F'`, `rf"""`, `fr'`, ... -- prefix letters that must include f/F,
// followed by a single or triple quote. Pushes the open f-string onto the stack.
static bool scan_fstring_start(TSLexer *lexer, Scanner *s) {
if (s->depth >= MAX_FSTRING_DEPTH) {
return false;
}
// The scanner is invoked before extras are stripped, so skip any leading
// whitespace (as extras) to reach the `f`/`r`/`b` prefix.
while (iswspace(lexer->lookahead)) {
lexer->advance(lexer, true);
}
bool has_f = false;
while (true) {
int32_t c = lexer->lookahead;
if (c == 'f' || c == 'F') {
has_f = true;
lexer->advance(lexer, false);
} else if (c == 'r' || c == 'R' || c == 'b' || c == 'B') {
lexer->advance(lexer, false);
} else {
break;
}
}
if (!has_f) {
return false;
}
int32_t q = lexer->lookahead;
if (q != '"' && q != '\'') {
return false;
}
lexer->advance(lexer, false); // opening quote
lexer->mark_end(lexer); // tentatively a single-quote start
bool triple = false;
if (lexer->lookahead == q) {
lexer->advance(lexer, false); // 2nd quote (beyond mark_end for now)
if (lexer->lookahead == q) {
lexer->advance(lexer, false); // 3rd quote -> triple
lexer->mark_end(lexer);
triple = true;
}
// else: an empty f-string `f""`; the 2nd quote is re-lexed as FSTRING_END.
}
s->stack[s->depth].quote = (char)q;
s->stack[s->depth].triple = triple;
s->depth++;
lexer->result_symbol = FSTRING_START;
return true;
}
// Scan the body of an open f-string, emitting EITHER one FSTRING_CONTENT run or
// the FSTRING_END quote. Done in a single pass (not two functions) because
// tree-sitter only resets the lexer position between scan() calls, not within
// one -- so distinguishing a lone quote (content) from the closing `"""` must
// not leave the cursor stranded for a following sub-scanner.
//
// `{{`/`}}` are escapes kept as content; a single `{`/`}` ends the content so
// the grammar can take the interpolation. `\x` escapes never close the string.
static bool scan_fstring_body(TSLexer *lexer, Scanner *s, const bool *valid) {
FString ctx = s->stack[s->depth - 1];
bool has = false;
while (!lexer->eof(lexer)) {
int32_t c = lexer->lookahead;
if (c == '{' || c == '}') {
lexer->advance(lexer, false); // past the first brace
if (lexer->lookahead == c) { // `{{` / `}}` -> literal content
lexer->advance(lexer, false);
has = true;
lexer->mark_end(lexer);
continue;
}
// single brace -> interpolation boundary; content (if any) ends before it.
if (has) {
lexer->result_symbol = FSTRING_CONTENT;
return true;
}
return false; // no content; reset -> the grammar lexes the `{`/`}`
}
if (c == '\\') {
lexer->advance(lexer, false);
if (!lexer->eof(lexer)) {
lexer->advance(lexer, false);
}
has = true;
lexer->mark_end(lexer);
continue;
}
if (c == (int32_t)ctx.quote) {
if (!ctx.triple) {
if (has) {
lexer->result_symbol = FSTRING_CONTENT;
return true;
}
if (!valid[FSTRING_END]) {
break; // quote where no end is expected -- bail to other tokens
}
lexer->advance(lexer, false);
lexer->mark_end(lexer);
lexer->result_symbol = FSTRING_END;
s->depth--;
return true;
}
// Triple: only a run of three quotes closes; 1 or 2 are content.
lexer->advance(lexer, false); // 1st quote
if (lexer->lookahead != (int32_t)ctx.quote) { // lone quote -> content
has = true;
lexer->mark_end(lexer);
continue;
}
lexer->advance(lexer, false); // 2nd quote
if (lexer->lookahead != (int32_t)ctx.quote) { // two quotes -> content
has = true;
lexer->mark_end(lexer);
continue;
}
// Three quotes -> the closing delimiter.
if (has) {
// Content ends before the run; the quotes (beyond mark_end) re-lex.
lexer->result_symbol = FSTRING_CONTENT;
return true;
}
if (!valid[FSTRING_END]) {
break;
}
lexer->advance(lexer, false); // 3rd quote
lexer->mark_end(lexer);
lexer->result_symbol = FSTRING_END;
s->depth--;
return true;
}
lexer->advance(lexer, false);
has = true;
lexer->mark_end(lexer);
}
if (has) {
lexer->result_symbol = FSTRING_CONTENT;
return true;
}
return false;
}
// ---------------------------------------------------------------------------
bool tree_sitter_jac_external_scanner_scan(void *payload, TSLexer *lexer,
const bool *valid_symbols) {
Scanner *s = (Scanner *)payload;
// Inside an open f-string, content/end take priority over everything else.
if (s->depth > 0 &&
(valid_symbols[FSTRING_CONTENT] || valid_symbols[FSTRING_END])) {
if (scan_fstring_body(lexer, s, valid_symbols)) {
return true;
}
}
if (valid_symbols[FSTRING_START] && scan_fstring_start(lexer, s)) {
return true;
}
if (valid_symbols[JSX_TEXT] && scan_jsx_text(lexer)) {
return true;
}
if (valid_symbols[PYINLINE]) {
return scan_pyinline(lexer);
}
return false;
}