-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscanner.c
More file actions
292 lines (264 loc) · 9.62 KB
/
Copy pathscanner.c
File metadata and controls
292 lines (264 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
#include "tree_sitter/parser.h"
#include <stdbool.h>
#include <stdint.h>
#if defined(__wasm__) || defined(__WASM__)
#define DBG(...) ((void)0)
#else
#include <stdio.h>
#include <stdlib.h>
#define DBG(...) do { if (getenv("TSDBG")) fprintf(stderr, __VA_ARGS__); } while (0)
#endif
// External tokens emitted by the SurrealQL scanner.
// Must stay in sync with the `externals:` declaration in grammar.js.
enum TokenType {
JS_FUNCTION_BODY,
OBJECT_OPEN,
};
// ---------------------------------------------------------------------------
// JS function body scanner (kept from previous grammar)
// ---------------------------------------------------------------------------
void *tree_sitter_surrealql_external_scanner_create(void) { return NULL; }
void tree_sitter_surrealql_external_scanner_destroy(void *payload) { (void)payload; }
void tree_sitter_surrealql_external_scanner_reset(void *payload) { (void)payload; }
unsigned tree_sitter_surrealql_external_scanner_serialize(void *payload, char *buffer) {
(void)payload;
(void)buffer;
return 0;
}
void tree_sitter_surrealql_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
(void)payload;
(void)buffer;
(void)length;
}
static void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
static void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
// Scan a JS single-line string delimited by `quote`. Handles \-escapes.
static void scan_js_string(TSLexer *lexer, int32_t quote) {
while (lexer->lookahead != 0 && lexer->lookahead != '\n') {
if (lexer->lookahead == '\\') {
advance(lexer);
if (lexer->lookahead != 0) advance(lexer);
} else if (lexer->lookahead == quote) {
advance(lexer);
return;
} else {
advance(lexer);
}
}
}
static void scan_template_literal(TSLexer *lexer) {
while (lexer->lookahead != 0) {
if (lexer->lookahead == '\\') {
advance(lexer);
if (lexer->lookahead != 0) advance(lexer);
} else if (lexer->lookahead == '`') {
advance(lexer);
return;
} else if (lexer->lookahead == '$') {
advance(lexer);
if (lexer->lookahead == '{') {
advance(lexer);
int depth = 1;
while (lexer->lookahead != 0 && depth > 0) {
int32_t c = lexer->lookahead;
advance(lexer);
if (c == '{') depth++;
else if (c == '}') depth--;
else if (c == '\'' || c == '"') scan_js_string(lexer, c);
else if (c == '`') scan_template_literal(lexer);
}
}
} else {
advance(lexer);
}
}
}
// Scan a JS function body — the entire `{...}` including its braces.
// The grammar's `JavaScriptBlock` rule simply references this token. We keep
// the leading `{` requirement so the scanner does not consume regular
// SurrealQL during error recovery at other `{`-adjacent positions.
static bool scan_js_function_body(TSLexer *lexer) {
while (lexer->lookahead == ' ' || lexer->lookahead == '\t' ||
lexer->lookahead == '\n' || lexer->lookahead == '\r') {
skip(lexer);
}
if (lexer->lookahead != '{') return false;
advance(lexer);
int depth = 1;
while (depth > 0 && lexer->lookahead != 0) {
int32_t c = lexer->lookahead;
advance(lexer);
if (c == '{') depth++;
else if (c == '}') depth--;
else if (c == '\'') scan_js_string(lexer, '\'');
else if (c == '"') scan_js_string(lexer, '"');
else if (c == '`') scan_template_literal(lexer);
else if (c == '/') {
if (lexer->lookahead == '/') {
advance(lexer);
while (lexer->lookahead != 0 && lexer->lookahead != '\n') advance(lexer);
} else if (lexer->lookahead == '*') {
advance(lexer);
while (lexer->lookahead != 0) {
if (lexer->lookahead == '*') {
advance(lexer);
if (lexer->lookahead == '/') { advance(lexer); break; }
} else {
advance(lexer);
}
}
}
}
}
lexer->result_symbol = JS_FUNCTION_BODY;
return true;
}
// ---------------------------------------------------------------------------
// Object-open scanner — mirrors lezer's tokens.js `objectToken`.
//
// Emits OBJECT_OPEN when `{` is followed by either:
// - another `{` is NOT next (otherwise it's a Block),
// - immediately `}` (empty object), or
// - an identifier/string key followed by `:` (looking past whitespace +
// comments).
// ---------------------------------------------------------------------------
static inline bool is_space(int32_t c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
static inline bool is_id_char(int32_t c) {
return c == '_' ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9');
}
// Skip whitespace and line comments after the opening `{`. Mirrors lezer's
// skipSpace in tokens.js (handles #, //, --).
static void skip_obj_whitespace(TSLexer *lexer) {
while (true) {
int32_t c = lexer->lookahead;
if (is_space(c)) {
skip(lexer);
continue;
}
if (c == '#') {
skip(lexer);
while (lexer->lookahead != 0 && lexer->lookahead != '\n') skip(lexer);
continue;
}
if ((c == '/' || c == '-') && lexer->lookahead != 0) {
// Peek the next char: we need lookahead == lookahead too. tree-sitter
// doesn't have a peek-2 API, so we eagerly advance and check.
skip(lexer);
if (lexer->lookahead == c) {
skip(lexer);
while (lexer->lookahead != 0 && lexer->lookahead != '\n') skip(lexer);
continue;
}
// Not a comment — we already consumed a char, so we cannot
// back-track. Bail out — caller will treat next non-space char as
// the object key (which is correct for our purposes since `/` and
// `-` are not valid identifier starts).
return;
}
break;
}
}
// Try to consume an identifier-like or string key starting at the current
// lookahead position. Returns true if a key was consumed.
static bool consume_obj_key(TSLexer *lexer) {
int32_t c = lexer->lookahead;
if (is_id_char(c) && !(c >= '0' && c <= '9')) {
// identifier
while (is_id_char(lexer->lookahead)) skip(lexer);
return true;
}
if (c >= '0' && c <= '9') {
// numeric key — used by some object literals; treat as identifier-like
while (is_id_char(lexer->lookahead)) skip(lexer);
return true;
}
if (c == '\'' || c == '"') {
int32_t quote = c;
skip(lexer);
while (lexer->lookahead != 0) {
if (lexer->lookahead == '\\') {
skip(lexer);
if (lexer->lookahead != 0) skip(lexer);
continue;
}
if (lexer->lookahead == quote) {
skip(lexer);
return true;
}
skip(lexer);
}
return false;
}
return false;
}
static bool scan_object_open(TSLexer *lexer) {
// Skip any leading whitespace/comments that come before the `{`. Tree-sitter
// sometimes invokes the external scanner before extras are processed, so
// we need to be tolerant here. Using skip() so they don't extend the token.
while (true) {
int32_t c = lexer->lookahead;
if (is_space(c)) {
skip(lexer);
continue;
}
if (c == '#') {
skip(lexer);
while (lexer->lookahead != 0 && lexer->lookahead != '\n') skip(lexer);
continue;
}
break;
}
if (lexer->lookahead != '{') return false;
// Consume the `{`. We must use advance() so the token includes it.
advance(lexer);
lexer->mark_end(lexer);
// Skip whitespace and comments — we use skip() so they don't extend the
// token, but we already marked the end past `{`.
skip_obj_whitespace(lexer);
int32_t c = lexer->lookahead;
if (c == '{') {
// Direct nested brace → this is a Block, not an Object.
return false;
}
if (c == '}') {
// Empty {} — treat as Object.
lexer->result_symbol = OBJECT_OPEN;
return true;
}
// Otherwise look for `key:`
if (!consume_obj_key(lexer)) return false;
skip_obj_whitespace(lexer);
if (lexer->lookahead == ':') {
lexer->result_symbol = OBJECT_OPEN;
return true;
}
return false;
}
// ---------------------------------------------------------------------------
// Dispatcher
// ---------------------------------------------------------------------------
bool tree_sitter_surrealql_external_scanner_scan(
void *payload,
TSLexer *lexer,
const bool *valid_symbols
) {
(void)payload;
DBG("scan: valid=[js=%d,obj=%d] la='%c'(%d)\n",
valid_symbols[JS_FUNCTION_BODY], valid_symbols[OBJECT_OPEN],
lexer->lookahead >= 32 && lexer->lookahead < 127 ? (char)lexer->lookahead : '?',
lexer->lookahead);
if (valid_symbols[OBJECT_OPEN]) {
bool ok = scan_object_open(lexer);
DBG(" object_open => %d\n", ok);
if (ok) return true;
}
if (valid_symbols[JS_FUNCTION_BODY]) {
if (scan_js_function_body(lexer)) return true;
}
return false;
}