@konomanoasa/tree-sitter-lua 0.0.0-stage → 0.2.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/src/scanner.c ADDED
@@ -0,0 +1,339 @@
1
+ #include "tokens.h"
2
+ #include "tree_sitter/alloc.h"
3
+ #include "tree_sitter/parser.h"
4
+ #include <stdint.h>
5
+ #include <string.h>
6
+
7
+ typedef struct {
8
+ uint32_t delimiter_width;
9
+ uint32_t remaining_long_comments;
10
+ } Scanner;
11
+
12
+ static void advance(TSLexer *lexer) {
13
+ lexer->advance(lexer, false);
14
+ }
15
+
16
+ static bool emit(TSLexer *lexer, enum Token token) {
17
+ lexer->result_symbol = token;
18
+ return true;
19
+ }
20
+
21
+ static bool space(int32_t c) {
22
+ return c == ' ' || (c >= '\t' && c <= '\r');
23
+ }
24
+
25
+ static bool hash_line(TSLexer *lexer) {
26
+ while (!lexer->eof(lexer) && lexer->lookahead != '\n') {
27
+ if (lexer->lookahead == '\r') {
28
+ lexer->mark_end(lexer);
29
+ advance(lexer);
30
+ if (lexer->lookahead == '\n') return emit(lexer, HASH_LINE);
31
+ } else {
32
+ advance(lexer);
33
+ }
34
+ }
35
+ lexer->mark_end(lexer);
36
+ return emit(lexer, HASH_LINE);
37
+ }
38
+
39
+ static bool decimal(int32_t c) {
40
+ return c >= '0' && c <= '9';
41
+ }
42
+
43
+ static bool letter(int32_t c) {
44
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
45
+ }
46
+
47
+ static int digit_value(int32_t c) {
48
+ if (decimal(c)) return (int)(c - '0');
49
+ if (c >= 'a' && c <= 'f') return (int)(c - 'a' + 10);
50
+ if (c >= 'A' && c <= 'F') return (int)(c - 'A' + 10);
51
+ return -1;
52
+ }
53
+
54
+ static bool number_part(TSLexer *lexer, const bool *valid) {
55
+ int32_t c = lexer->lookahead;
56
+ if (valid[HEX_DIGITS] || valid[DECIMAL_DIGITS]) {
57
+ bool hex = valid[HEX_DIGITS];
58
+ bool any = false;
59
+ while (hex ? digit_value(c) >= 0 : decimal(c)) {
60
+ advance(lexer);
61
+ c = lexer->lookahead;
62
+ any = true;
63
+ }
64
+ if (any) return emit(lexer, hex ? HEX_DIGITS : DECIMAL_DIGITS);
65
+ }
66
+ enum Token marker;
67
+ if (c == 'e' || c == 'E')
68
+ marker = c == 'e' ? EXPONENT_LOWER_E : EXPONENT_UPPER_E;
69
+ else if (c == 'p' || c == 'P')
70
+ marker = c == 'p' ? EXPONENT_LOWER_P : EXPONENT_UPPER_P;
71
+ else
72
+ return valid[NUMBER_END] &&
73
+ c != '.' &&
74
+ c != '_' &&
75
+ !letter(c) &&
76
+ !decimal(c) &&
77
+ emit(lexer, NUMBER_END);
78
+ if (!valid[marker]) return false;
79
+ advance(lexer);
80
+ return emit(lexer, marker);
81
+ }
82
+
83
+ static bool code_point(TSLexer *lexer) {
84
+ int digit = digit_value(lexer->lookahead);
85
+ if (digit < 0) return false;
86
+ uint32_t value = 0;
87
+ do {
88
+ if (value > (uint32_t)INT32_MAX / 16) return false;
89
+ value = value * 16 + (unsigned)digit;
90
+ advance(lexer);
91
+ digit = digit_value(lexer->lookahead);
92
+ } while (digit >= 0);
93
+ return lexer->lookahead == '}' && emit(lexer, CODE_POINT);
94
+ }
95
+
96
+ static bool escape(TSLexer *lexer, const bool *valid) {
97
+ advance(lexer);
98
+ int32_t c = lexer->lookahead;
99
+ if (c == 'u') {
100
+ advance(lexer);
101
+ return valid[UNICODE_ESCAPE_START] &&
102
+ lexer->lookahead == '{' &&
103
+ emit(lexer, UNICODE_ESCAPE_START);
104
+ }
105
+ if (!valid[SIMPLE_ESCAPE]) return false;
106
+ if (c == 'z') {
107
+ advance(lexer);
108
+ while (space(lexer->lookahead)) advance(lexer);
109
+ } else if (c == '\n' || c == '\r') {
110
+ advance(lexer);
111
+ if (lexer->lookahead == (c == '\n' ? '\r' : '\n')) advance(lexer);
112
+ } else if (decimal(c)) {
113
+ unsigned value = 0;
114
+ for (unsigned i = 0; i < 3 && decimal(lexer->lookahead); ++i) {
115
+ value = 10 * value + (unsigned)(lexer->lookahead - '0');
116
+ advance(lexer);
117
+ }
118
+ if (value > 255) return false;
119
+ } else if (c == 'x') {
120
+ advance(lexer);
121
+ for (unsigned i = 0; i < 2; ++i) {
122
+ if (digit_value(lexer->lookahead) < 0) return false;
123
+ advance(lexer);
124
+ }
125
+ } else if (c > 0 && c < 128 && strchr("abfnrtv\\'\"", (int)c)) {
126
+ advance(lexer);
127
+ } else {
128
+ return false;
129
+ }
130
+ return emit(lexer, SIMPLE_ESCAPE);
131
+ }
132
+
133
+ static bool short_text(TSLexer *lexer, enum Token token) {
134
+ const int32_t quote = token == SINGLE_TEXT ? '\'' : '"';
135
+ bool any = false;
136
+ while (
137
+ !lexer->eof(lexer) &&
138
+ lexer->lookahead != quote &&
139
+ lexer->lookahead != '\\' &&
140
+ lexer->lookahead != '\r' &&
141
+ lexer->lookahead != '\n'
142
+ ) {
143
+ advance(lexer);
144
+ any = true;
145
+ }
146
+ return any && emit(lexer, token);
147
+ }
148
+
149
+ static uint32_t
150
+ take_long_bracket(TSLexer *lexer, int32_t bracket, uint32_t expected_width) {
151
+ if (lexer->lookahead != bracket) return 0;
152
+ advance(lexer);
153
+ uint32_t width = 2;
154
+ while (lexer->lookahead == '=') {
155
+ advance(lexer);
156
+ ++width;
157
+ }
158
+ if (
159
+ lexer->lookahead != bracket || (expected_width && width != expected_width)
160
+ )
161
+ return 0;
162
+ advance(lexer);
163
+ return width;
164
+ }
165
+
166
+ static bool take_comment_prefix(TSLexer *lexer) {
167
+ advance(lexer);
168
+ if (lexer->lookahead != '-') return false;
169
+ advance(lexer);
170
+ return true;
171
+ }
172
+
173
+ static void skip_line(TSLexer *lexer) {
174
+ while (
175
+ !lexer->eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '\r'
176
+ )
177
+ advance(lexer);
178
+ }
179
+
180
+ static void look_past_token_start(TSLexer *lexer) {
181
+ int32_t c = lexer->lookahead;
182
+ if (letter(c) || c == '_') {
183
+ do {
184
+ advance(lexer);
185
+ } while (
186
+ letter(lexer->lookahead) ||
187
+ decimal(lexer->lookahead) ||
188
+ lexer->lookahead == '_'
189
+ );
190
+ } else if (c == '[') {
191
+ take_long_bracket(lexer, '[', 0);
192
+ } else if (!lexer->eof(lexer)) {
193
+ advance(lexer);
194
+ if (c == '.' && lexer->lookahead == '.') advance(lexer);
195
+ }
196
+ }
197
+
198
+ static void
199
+ scan_to_long_close(TSLexer *lexer, uint32_t width, bool mark_content) {
200
+ while (lexer->lookahead != 0 || !lexer->eof(lexer)) {
201
+ if (lexer->lookahead == ']') {
202
+ if (mark_content) lexer->mark_end(lexer);
203
+ if (take_long_bracket(lexer, ']', width)) return;
204
+ } else {
205
+ advance(lexer);
206
+ }
207
+ }
208
+ if (mark_content) lexer->mark_end(lexer);
209
+ }
210
+
211
+ static uint32_t look_past_comments(TSLexer *lexer, uint32_t width) {
212
+ uint32_t remaining = 0;
213
+ for (;;) {
214
+ if (width) scan_to_long_close(lexer, width, false);
215
+ else skip_line(lexer);
216
+ while (space(lexer->lookahead)) advance(lexer);
217
+ if (lexer->lookahead != '-') {
218
+ look_past_token_start(lexer);
219
+ return remaining;
220
+ }
221
+ if (!take_comment_prefix(lexer)) return remaining;
222
+ width = take_long_bracket(lexer, '[', 0);
223
+ if (width) ++remaining;
224
+ }
225
+ }
226
+
227
+ static bool comment(TSLexer *lexer, Scanner *state, const bool *valid) {
228
+ if (!take_comment_prefix(lexer)) return false;
229
+ uint32_t width = take_long_bracket(lexer, '[', 0);
230
+ if (!width) {
231
+ if (!valid[SHORT_COMMENT]) return false;
232
+ skip_line(lexer);
233
+ return emit(lexer, SHORT_COMMENT);
234
+ }
235
+ if (!valid[LONG_COMMENT_OPEN]) return false;
236
+ if (state->remaining_long_comments) {
237
+ --state->remaining_long_comments;
238
+ } else {
239
+ lexer->mark_end(lexer);
240
+ state->remaining_long_comments = look_past_comments(lexer, width);
241
+ }
242
+ state->delimiter_width = width;
243
+ return emit(lexer, LONG_COMMENT_OPEN);
244
+ }
245
+
246
+ static bool long_body(TSLexer *lexer, Scanner *state, const bool *valid) {
247
+ if (lexer->eof(lexer)) return false;
248
+ if (take_long_bracket(lexer, ']', state->delimiter_width)) {
249
+ if (!valid[LONG_BRACKET_CLOSE]) return false;
250
+ state->delimiter_width = 0;
251
+ return emit(lexer, LONG_BRACKET_CLOSE);
252
+ }
253
+ if (!valid[LONG_CONTENT]) return false;
254
+ scan_to_long_close(lexer, state->delimiter_width, true);
255
+ return emit(lexer, LONG_CONTENT);
256
+ }
257
+
258
+ void *tree_sitter_lua_external_scanner_create(void) {
259
+ return ts_calloc(1, sizeof(Scanner));
260
+ }
261
+
262
+ void tree_sitter_lua_external_scanner_destroy(void *payload) {
263
+ ts_free(payload);
264
+ }
265
+
266
+ unsigned
267
+ tree_sitter_lua_external_scanner_serialize(void *payload, char *buffer) {
268
+ const Scanner *state = payload;
269
+ if (!state || (!state->delimiter_width && !state->remaining_long_comments))
270
+ return 0;
271
+ memcpy(buffer, state, sizeof(*state));
272
+ return sizeof(*state);
273
+ }
274
+
275
+ void tree_sitter_lua_external_scanner_deserialize(
276
+ void *payload,
277
+ const char *buffer,
278
+ unsigned length
279
+ ) {
280
+ Scanner *state = payload;
281
+ if (!state) return;
282
+ if (length == sizeof(*state)) memcpy(state, buffer, sizeof(*state));
283
+ else *state = (Scanner){0};
284
+ }
285
+
286
+ bool tree_sitter_lua_external_scanner_scan(
287
+ void *payload,
288
+ TSLexer *lexer,
289
+ const bool *valid
290
+ ) {
291
+ Scanner *state = payload;
292
+ if (valid[ERROR_SENTINEL] || !state) return false;
293
+ if (valid[HASH_LINE] && lexer->lookahead == '#') return hash_line(lexer);
294
+ if (valid[FILE_START]) return emit(lexer, FILE_START);
295
+ if (
296
+ state->delimiter_width && (valid[LONG_CONTENT] || valid[LONG_BRACKET_CLOSE])
297
+ )
298
+ return long_body(lexer, state, valid);
299
+ if (valid[SINGLE_TEXT] || valid[DOUBLE_TEXT]) {
300
+ if (lexer->lookahead == '\\') return escape(lexer, valid);
301
+ return short_text(lexer, valid[SINGLE_TEXT] ? SINGLE_TEXT : DOUBLE_TEXT);
302
+ }
303
+ if (valid[CODE_POINT]) return code_point(lexer);
304
+
305
+ if (valid[DECIMAL_DIGITS] || valid[HEX_DIGITS] || valid[NUMBER_END])
306
+ return number_part(lexer, valid);
307
+
308
+ if (valid[SPACE] && space(lexer->lookahead)) {
309
+ do {
310
+ advance(lexer);
311
+ } while (space(lexer->lookahead));
312
+ return emit(lexer, SPACE);
313
+ }
314
+ if (valid[CONCAT] && lexer->lookahead == '.') {
315
+ advance(lexer);
316
+ if (lexer->lookahead != '.') return false;
317
+ advance(lexer);
318
+ return lexer->lookahead != '.' && emit(lexer, CONCAT);
319
+ }
320
+ if (valid[GREATER] && lexer->lookahead == '>') {
321
+ advance(lexer);
322
+ return lexer->lookahead != '=' &&
323
+ lexer->lookahead != '>' &&
324
+ emit(lexer, GREATER);
325
+ }
326
+
327
+ if (
328
+ lexer->lookahead == '-' &&
329
+ (valid[SHORT_COMMENT] || valid[LONG_COMMENT_OPEN])
330
+ )
331
+ return comment(lexer, state, valid);
332
+ if (lexer->lookahead == '[' && valid[LONG_BRACKET_OPEN]) {
333
+ uint32_t width = take_long_bracket(lexer, '[', 0);
334
+ if (!width) return false;
335
+ state->delimiter_width = width;
336
+ return emit(lexer, LONG_BRACKET_OPEN);
337
+ }
338
+ return false;
339
+ }
package/src/tokens.h ADDED
@@ -0,0 +1,32 @@
1
+ /* Generated by npm run generate. */
2
+ #ifndef TREE_SITTER_LUA_TOKENS_H
3
+ #define TREE_SITTER_LUA_TOKENS_H
4
+
5
+ enum Token {
6
+ SPACE,
7
+ DECIMAL_DIGITS,
8
+ HEX_DIGITS,
9
+ EXPONENT_LOWER_E,
10
+ EXPONENT_UPPER_E,
11
+ EXPONENT_LOWER_P,
12
+ EXPONENT_UPPER_P,
13
+ NUMBER_END,
14
+ SHORT_COMMENT,
15
+ LONG_COMMENT_OPEN,
16
+ LONG_BRACKET_OPEN,
17
+ LONG_CONTENT,
18
+ LONG_BRACKET_CLOSE,
19
+ SINGLE_TEXT,
20
+ DOUBLE_TEXT,
21
+ SIMPLE_ESCAPE,
22
+ UNICODE_ESCAPE_START,
23
+ CODE_POINT,
24
+ CONCAT,
25
+ GREATER,
26
+ FILE_START,
27
+ HASH_LINE,
28
+ ERROR_SENTINEL,
29
+ TOKEN_COUNT
30
+ };
31
+
32
+ #endif
@@ -0,0 +1,54 @@
1
+ #ifndef TREE_SITTER_ALLOC_H_
2
+ #define TREE_SITTER_ALLOC_H_
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ #include <stdbool.h>
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+
12
+ // Allow clients to override allocation functions
13
+ #ifdef TREE_SITTER_REUSE_ALLOCATOR
14
+
15
+ extern void *(*ts_current_malloc)(size_t size);
16
+ extern void *(*ts_current_calloc)(size_t count, size_t size);
17
+ extern void *(*ts_current_realloc)(void *ptr, size_t size);
18
+ extern void (*ts_current_free)(void *ptr);
19
+
20
+ #ifndef ts_malloc
21
+ #define ts_malloc ts_current_malloc
22
+ #endif
23
+ #ifndef ts_calloc
24
+ #define ts_calloc ts_current_calloc
25
+ #endif
26
+ #ifndef ts_realloc
27
+ #define ts_realloc ts_current_realloc
28
+ #endif
29
+ #ifndef ts_free
30
+ #define ts_free ts_current_free
31
+ #endif
32
+
33
+ #else
34
+
35
+ #ifndef ts_malloc
36
+ #define ts_malloc malloc
37
+ #endif
38
+ #ifndef ts_calloc
39
+ #define ts_calloc calloc
40
+ #endif
41
+ #ifndef ts_realloc
42
+ #define ts_realloc realloc
43
+ #endif
44
+ #ifndef ts_free
45
+ #define ts_free free
46
+ #endif
47
+
48
+ #endif
49
+
50
+ #ifdef __cplusplus
51
+ }
52
+ #endif
53
+
54
+ #endif // TREE_SITTER_ALLOC_H_