@mermanjs/tree-sitter-mermaid 0.1.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/CMakeLists.txt +44 -0
- package/LICENSE +22 -0
- package/Makefile +80 -0
- package/README.md +201 -0
- package/THIRD_PARTY_LICENSES/mermaid/LICENSE +21 -0
- package/THIRD_PARTY_LICENSES/monaqa-tree-sitter-mermaid/LICENSE +21 -0
- package/THIRD_PARTY_LICENSES/pappasam-tree-sitter-mermaid/LICENSE +21 -0
- package/THIRD_PARTY_LICENSES/singularity-tree-sitter-mermaid/LICENSE +22 -0
- package/THIRD_PARTY_LICENSES/tree-sitter/LICENSE +21 -0
- package/THIRD_PARTY_LICENSES/zenuml-core/LICENSE +21 -0
- package/THIRD_PARTY_NOTICES.md +47 -0
- package/binding.gyp +56 -0
- package/bindings/c/tree-sitter-mermaid.pc.in +10 -0
- package/bindings/c/tree_sitter/tree-sitter-mermaid.h +16 -0
- package/bindings/node/binding.cc +25 -0
- package/bindings/node/index.d.ts +5 -0
- package/bindings/node/index.js +10 -0
- package/grammar/families/architecture.js +361 -0
- package/grammar/families/block.js +370 -0
- package/grammar/families/c4.js +394 -0
- package/grammar/families/class.js +441 -0
- package/grammar/families/cynefin.js +127 -0
- package/grammar/families/entity-relationship.js +408 -0
- package/grammar/families/event-modeling.js +307 -0
- package/grammar/families/flowchart.js +690 -0
- package/grammar/families/gantt.js +445 -0
- package/grammar/families/git-graph.js +326 -0
- package/grammar/families/info.js +77 -0
- package/grammar/families/ishikawa.js +103 -0
- package/grammar/families/journey.js +221 -0
- package/grammar/families/kanban.js +264 -0
- package/grammar/families/mindmap.js +221 -0
- package/grammar/families/packet.js +108 -0
- package/grammar/families/pie.js +118 -0
- package/grammar/families/quadrant-chart.js +385 -0
- package/grammar/families/radar.js +286 -0
- package/grammar/families/railroad-abnf.js +124 -0
- package/grammar/families/railroad-ebnf.js +179 -0
- package/grammar/families/railroad-peg.js +128 -0
- package/grammar/families/railroad-shared.js +114 -0
- package/grammar/families/railroad.js +143 -0
- package/grammar/families/requirement.js +475 -0
- package/grammar/families/sankey.js +139 -0
- package/grammar/families/sequence.js +437 -0
- package/grammar/families/state.js +400 -0
- package/grammar/families/swimlane.js +25 -0
- package/grammar/families/timeline.js +251 -0
- package/grammar/families/tree-view.js +189 -0
- package/grammar/families/treemap.js +146 -0
- package/grammar/families/venn.js +256 -0
- package/grammar/families/wardley.js +530 -0
- package/grammar/families/xy-chart.js +460 -0
- package/grammar/families/zenuml.js +725 -0
- package/grammar/shared/common.js +78 -0
- package/grammar/shared/header.js +16 -0
- package/grammar/shared/indentation.js +22 -0
- package/grammar/shared/langium.js +166 -0
- package/grammar/shared/preamble.js +26 -0
- package/grammar.js +188 -0
- package/metadata/derivations.json +519 -0
- package/metadata/provenance.json +98 -0
- package/package.json +107 -0
- package/prebuilds/darwin-arm64/@mermanjs+tree-sitter-mermaid.node +0 -0
- package/prebuilds/darwin-x64/@mermanjs+tree-sitter-mermaid.node +0 -0
- package/prebuilds/linux-x64/@mermanjs+tree-sitter-mermaid.node +0 -0
- package/prebuilds/win32-x64/@mermanjs+tree-sitter-mermaid.node +0 -0
- package/queries/helix/highlights.scm +960 -0
- package/queries/helix/indents.scm +45 -0
- package/queries/helix/injections.scm +15 -0
- package/queries/helix/locals.scm +134 -0
- package/queries/helix/textobjects.scm +264 -0
- package/queries/neovim/README.md +28 -0
- package/queries/neovim/brackets.scm +65 -0
- package/queries/neovim/folds.scm +53 -0
- package/queries/neovim/highlights.scm +121 -0
- package/queries/neovim/indents.scm +110 -0
- package/queries/neovim/injections.scm +86 -0
- package/queries/neovim/locals.scm +277 -0
- package/queries/neovim/outline.scm +117 -0
- package/queries/neovim/tags.scm +149 -0
- package/queries/neovim/textobjects.scm +97 -0
- package/queries/portable/highlights.scm +961 -0
- package/queries/portable/injections.scm +45 -0
- package/queries/portable/locals.scm +290 -0
- package/queries/portable/tags.scm +182 -0
- package/queries/zed/README.md +48 -0
- package/queries/zed/brackets.scm +111 -0
- package/queries/zed/highlights.scm +961 -0
- package/queries/zed/indents.scm +111 -0
- package/queries/zed/injections.scm +11 -0
- package/queries/zed/outline.scm +167 -0
- package/queries/zed/textobjects.scm +68 -0
- package/src/grammar.json +71138 -0
- package/src/node-types.json +34871 -0
- package/src/parser.c +321353 -0
- package/src/scanner.c +462 -0
- package/src/tree_sitter/alloc.h +54 -0
- package/src/tree_sitter/array.h +336 -0
- package/src/tree_sitter/parser.h +286 -0
- package/tree-sitter-mermaid.wasm +0 -0
- package/tree-sitter.json +40 -0
package/src/scanner.c
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
#include "tree_sitter/parser.h"
|
|
2
|
+
|
|
3
|
+
#include <stdbool.h>
|
|
4
|
+
#include <stdint.h>
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
#include <string.h>
|
|
7
|
+
|
|
8
|
+
enum TokenType {
|
|
9
|
+
MINDMAP_START,
|
|
10
|
+
MINDMAP_INDENT,
|
|
11
|
+
MINDMAP_REINDENT,
|
|
12
|
+
MINDMAP_DEDENT,
|
|
13
|
+
MINDMAP_INDENTATION_OVERFLOW,
|
|
14
|
+
TREEMAP_START,
|
|
15
|
+
TREEMAP_INDENT,
|
|
16
|
+
TREEMAP_REINDENT,
|
|
17
|
+
TREEMAP_DEDENT,
|
|
18
|
+
TREEMAP_INDENTATION_OVERFLOW,
|
|
19
|
+
TREE_VIEW_START,
|
|
20
|
+
TREE_VIEW_INDENT,
|
|
21
|
+
TREE_VIEW_REINDENT,
|
|
22
|
+
TREE_VIEW_DEDENT,
|
|
23
|
+
TREE_VIEW_INDENTATION_OVERFLOW,
|
|
24
|
+
KANBAN_START,
|
|
25
|
+
KANBAN_INDENT,
|
|
26
|
+
KANBAN_REINDENT,
|
|
27
|
+
KANBAN_DEDENT,
|
|
28
|
+
KANBAN_INDENTATION_OVERFLOW,
|
|
29
|
+
END_OF_INPUT,
|
|
30
|
+
DIRECTIVE_BODY,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
enum ScannerMode {
|
|
34
|
+
MODE_NONE = 0,
|
|
35
|
+
MODE_MINDMAP = 1,
|
|
36
|
+
MODE_TREEMAP = 2,
|
|
37
|
+
MODE_TREE_VIEW = 3,
|
|
38
|
+
MODE_KANBAN = 4,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
enum {
|
|
42
|
+
STATE_VERSION = 1,
|
|
43
|
+
MAX_DEPTH = 256,
|
|
44
|
+
MAX_INDENTATION = UINT16_MAX - 1,
|
|
45
|
+
STATE_PREFIX_SIZE = 10,
|
|
46
|
+
STATE_CHECKSUM_SIZE = 4,
|
|
47
|
+
MAX_SERIALIZED_SIZE =
|
|
48
|
+
STATE_PREFIX_SIZE + (MAX_DEPTH * 2) + STATE_CHECKSUM_SIZE,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
_Static_assert(MAX_SERIALIZED_SIZE <= TREE_SITTER_SERIALIZATION_BUFFER_SIZE,
|
|
52
|
+
"scanner state exceeds Tree-sitter's serialization buffer");
|
|
53
|
+
|
|
54
|
+
typedef struct {
|
|
55
|
+
uint8_t mode;
|
|
56
|
+
uint16_t count;
|
|
57
|
+
uint16_t levels[MAX_DEPTH];
|
|
58
|
+
} Scanner;
|
|
59
|
+
|
|
60
|
+
typedef struct {
|
|
61
|
+
enum TokenType start;
|
|
62
|
+
enum TokenType indent;
|
|
63
|
+
enum TokenType reindent;
|
|
64
|
+
enum TokenType dedent;
|
|
65
|
+
enum TokenType overflow;
|
|
66
|
+
enum ScannerMode mode;
|
|
67
|
+
} TokenGroup;
|
|
68
|
+
|
|
69
|
+
static const TokenGroup TOKEN_GROUPS[] = {
|
|
70
|
+
{MINDMAP_START, MINDMAP_INDENT, MINDMAP_REINDENT, MINDMAP_DEDENT,
|
|
71
|
+
MINDMAP_INDENTATION_OVERFLOW, MODE_MINDMAP},
|
|
72
|
+
{TREEMAP_START, TREEMAP_INDENT, TREEMAP_REINDENT, TREEMAP_DEDENT,
|
|
73
|
+
TREEMAP_INDENTATION_OVERFLOW, MODE_TREEMAP},
|
|
74
|
+
{TREE_VIEW_START, TREE_VIEW_INDENT, TREE_VIEW_REINDENT, TREE_VIEW_DEDENT,
|
|
75
|
+
TREE_VIEW_INDENTATION_OVERFLOW, MODE_TREE_VIEW},
|
|
76
|
+
{KANBAN_START, KANBAN_INDENT, KANBAN_REINDENT, KANBAN_DEDENT,
|
|
77
|
+
KANBAN_INDENTATION_OVERFLOW, MODE_KANBAN},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
static void scanner_reset(Scanner *scanner) {
|
|
81
|
+
scanner->mode = MODE_NONE;
|
|
82
|
+
scanner->count = 0;
|
|
83
|
+
memset(scanner->levels, 0, sizeof(scanner->levels));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static uint16_t read_u16(const char *buffer) {
|
|
87
|
+
return (uint16_t)(uint8_t)buffer[0] |
|
|
88
|
+
(uint16_t)((uint16_t)(uint8_t)buffer[1] << 8);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
static uint32_t read_u32(const char *buffer) {
|
|
92
|
+
return (uint32_t)(uint8_t)buffer[0] |
|
|
93
|
+
((uint32_t)(uint8_t)buffer[1] << 8) |
|
|
94
|
+
((uint32_t)(uint8_t)buffer[2] << 16) |
|
|
95
|
+
((uint32_t)(uint8_t)buffer[3] << 24);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static void write_u16(char *buffer, uint16_t value) {
|
|
99
|
+
buffer[0] = (char)(value & 0xff);
|
|
100
|
+
buffer[1] = (char)((value >> 8) & 0xff);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static void write_u32(char *buffer, uint32_t value) {
|
|
104
|
+
buffer[0] = (char)(value & 0xff);
|
|
105
|
+
buffer[1] = (char)((value >> 8) & 0xff);
|
|
106
|
+
buffer[2] = (char)((value >> 16) & 0xff);
|
|
107
|
+
buffer[3] = (char)((value >> 24) & 0xff);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static uint32_t checksum(const char *buffer, unsigned length) {
|
|
111
|
+
uint32_t hash = UINT32_C(2166136261);
|
|
112
|
+
for (unsigned index = 0; index < length; index++) {
|
|
113
|
+
hash ^= (uint8_t)buffer[index];
|
|
114
|
+
hash *= UINT32_C(16777619);
|
|
115
|
+
}
|
|
116
|
+
return hash;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static bool mode_is_valid(uint8_t mode) {
|
|
120
|
+
return mode >= MODE_MINDMAP && mode <= MODE_KANBAN;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static bool token_group_requested(const bool *valid_symbols,
|
|
124
|
+
const TokenGroup *group) {
|
|
125
|
+
return valid_symbols[group->start] || valid_symbols[group->indent] ||
|
|
126
|
+
valid_symbols[group->reindent] || valid_symbols[group->dedent] ||
|
|
127
|
+
valid_symbols[group->overflow];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static const TokenGroup *requested_group(const bool *valid_symbols) {
|
|
131
|
+
const TokenGroup *selected = NULL;
|
|
132
|
+
for (unsigned index = 0;
|
|
133
|
+
index < sizeof(TOKEN_GROUPS) / sizeof(TOKEN_GROUPS[0]); index++) {
|
|
134
|
+
const TokenGroup *candidate = &TOKEN_GROUPS[index];
|
|
135
|
+
if (!token_group_requested(valid_symbols, candidate)) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (selected != NULL) {
|
|
139
|
+
return NULL;
|
|
140
|
+
}
|
|
141
|
+
selected = candidate;
|
|
142
|
+
}
|
|
143
|
+
return selected;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
static bool directive_body_is_unambiguous(const bool *valid_symbols) {
|
|
147
|
+
if (!valid_symbols[DIRECTIVE_BODY] || valid_symbols[END_OF_INPUT]) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
for (unsigned index = 0;
|
|
151
|
+
index < sizeof(TOKEN_GROUPS) / sizeof(TOKEN_GROUPS[0]); index++) {
|
|
152
|
+
if (token_group_requested(valid_symbols, &TOKEN_GROUPS[index])) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static bool is_box_drawing_prefix(int32_t lookahead) {
|
|
160
|
+
return lookahead == 0x2502 || lookahead == 0x251c || lookahead == 0x2514 ||
|
|
161
|
+
lookahead == 0x2503 || lookahead == 0x2523 || lookahead == 0x2517 ||
|
|
162
|
+
lookahead == '|';
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static bool is_hierarchy_row(enum ScannerMode mode, int32_t lookahead) {
|
|
166
|
+
if (lookahead == 0 || lookahead == '\n' || lookahead == '\r' ||
|
|
167
|
+
lookahead == '%') {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
if (mode == MODE_MINDMAP || mode == MODE_KANBAN) {
|
|
171
|
+
return lookahead != ':';
|
|
172
|
+
}
|
|
173
|
+
if (mode == MODE_TREEMAP) {
|
|
174
|
+
return lookahead == '"' || lookahead == '\'';
|
|
175
|
+
}
|
|
176
|
+
return !is_box_drawing_prefix(lookahead);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
static bool consume_literal(TSLexer *lexer, const char *literal) {
|
|
180
|
+
for (; *literal != '\0'; literal++) {
|
|
181
|
+
if (lexer->lookahead != (unsigned char)*literal) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
lexer->advance(lexer, false);
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
static bool is_horizontal_space(int32_t lookahead) {
|
|
190
|
+
return lookahead == ' ' || lookahead == '\t' || lookahead == '\f' ||
|
|
191
|
+
lookahead == 0xa0;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
static bool scan_directive_body(TSLexer *lexer) {
|
|
195
|
+
bool consumed = false;
|
|
196
|
+
|
|
197
|
+
while (!lexer->eof(lexer)) {
|
|
198
|
+
if (lexer->lookahead == '}') {
|
|
199
|
+
lexer->mark_end(lexer);
|
|
200
|
+
lexer->advance(lexer, false);
|
|
201
|
+
if (lexer->lookahead == '%') {
|
|
202
|
+
lexer->advance(lexer, false);
|
|
203
|
+
if (lexer->lookahead == '%') {
|
|
204
|
+
if (!consumed) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
lexer->result_symbol = DIRECTIVE_BODY;
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
consumed = true;
|
|
212
|
+
lexer->mark_end(lexer);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
lexer->advance(lexer, false);
|
|
217
|
+
consumed = true;
|
|
218
|
+
lexer->mark_end(lexer);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (!consumed) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
lexer->result_symbol = DIRECTIVE_BODY;
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
static bool is_tree_view_metadata_row(TSLexer *lexer) {
|
|
229
|
+
if (lexer->lookahead == 't') {
|
|
230
|
+
if (!consume_literal(lexer, "title")) {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
return lexer->lookahead == 0 || lexer->lookahead == '\n' ||
|
|
234
|
+
lexer->lookahead == '\r' || lexer->lookahead == '%' ||
|
|
235
|
+
is_horizontal_space(lexer->lookahead);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (!consume_literal(lexer, "acc")) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
if (lexer->lookahead == 'T') {
|
|
242
|
+
if (!consume_literal(lexer, "Title")) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
while (lexer->lookahead == ' ' || lexer->lookahead == '\t') {
|
|
246
|
+
lexer->advance(lexer, false);
|
|
247
|
+
}
|
|
248
|
+
return lexer->lookahead == ':';
|
|
249
|
+
}
|
|
250
|
+
if (lexer->lookahead == 'D') {
|
|
251
|
+
if (!consume_literal(lexer, "Descr")) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
while (is_horizontal_space(lexer->lookahead)) {
|
|
255
|
+
lexer->advance(lexer, false);
|
|
256
|
+
}
|
|
257
|
+
return lexer->lookahead == ':' || lexer->lookahead == '{' ||
|
|
258
|
+
lexer->lookahead == '\n' || lexer->lookahead == '\r';
|
|
259
|
+
}
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
static bool emit_transition(Scanner *scanner, TSLexer *lexer,
|
|
264
|
+
const bool *valid_symbols, const TokenGroup *group,
|
|
265
|
+
uint16_t indentation) {
|
|
266
|
+
Scanner next = *scanner;
|
|
267
|
+
|
|
268
|
+
if (scanner->mode != group->mode || scanner->count == 0) {
|
|
269
|
+
if (!valid_symbols[group->start]) {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
scanner_reset(&next);
|
|
273
|
+
next.mode = (uint8_t)group->mode;
|
|
274
|
+
next.count = 1;
|
|
275
|
+
next.levels[0] = indentation;
|
|
276
|
+
*scanner = next;
|
|
277
|
+
lexer->result_symbol = group->start;
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const uint16_t top = scanner->levels[scanner->count - 1];
|
|
282
|
+
if (indentation == top) {
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (indentation > top) {
|
|
287
|
+
if (scanner->count == MAX_DEPTH) {
|
|
288
|
+
if (!valid_symbols[group->overflow]) {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
lexer->result_symbol = group->overflow;
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
if (!valid_symbols[group->indent]) {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
next.levels[next.count] = indentation;
|
|
298
|
+
next.count++;
|
|
299
|
+
*scanner = next;
|
|
300
|
+
lexer->result_symbol = group->indent;
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const uint16_t parent =
|
|
305
|
+
scanner->count > 1 ? scanner->levels[scanner->count - 2] : 0;
|
|
306
|
+
if (scanner->count > 1 && indentation > parent) {
|
|
307
|
+
if (!valid_symbols[group->reindent]) {
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
next.levels[next.count - 1] = indentation;
|
|
311
|
+
*scanner = next;
|
|
312
|
+
lexer->result_symbol = group->reindent;
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (!valid_symbols[group->dedent]) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
uint16_t keep = 0;
|
|
321
|
+
while (keep < scanner->count && scanner->levels[keep] < indentation) {
|
|
322
|
+
keep++;
|
|
323
|
+
}
|
|
324
|
+
if (keep < scanner->count && scanner->levels[keep] == indentation) {
|
|
325
|
+
next.count = (uint16_t)(keep + 1);
|
|
326
|
+
} else if (keep == 0) {
|
|
327
|
+
next.count = 1;
|
|
328
|
+
next.levels[0] = indentation;
|
|
329
|
+
} else {
|
|
330
|
+
next.count = (uint16_t)(keep + 1);
|
|
331
|
+
next.levels[keep] = indentation;
|
|
332
|
+
}
|
|
333
|
+
*scanner = next;
|
|
334
|
+
lexer->result_symbol = group->dedent;
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
void *tree_sitter_mermaid_external_scanner_create(void) {
|
|
339
|
+
Scanner *scanner = (Scanner *)calloc(1, sizeof(Scanner));
|
|
340
|
+
return scanner;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
void tree_sitter_mermaid_external_scanner_destroy(void *payload) {
|
|
344
|
+
free(payload);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
unsigned tree_sitter_mermaid_external_scanner_serialize(void *payload,
|
|
348
|
+
char *buffer) {
|
|
349
|
+
const Scanner *scanner = (const Scanner *)payload;
|
|
350
|
+
if (scanner == NULL || scanner->mode == MODE_NONE || scanner->count == 0) {
|
|
351
|
+
return 0;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const unsigned body_length = STATE_PREFIX_SIZE + scanner->count * 2;
|
|
355
|
+
const unsigned total_length = body_length + STATE_CHECKSUM_SIZE;
|
|
356
|
+
buffer[0] = 'M';
|
|
357
|
+
buffer[1] = 'M';
|
|
358
|
+
buffer[2] = STATE_VERSION;
|
|
359
|
+
buffer[3] = (char)scanner->mode;
|
|
360
|
+
buffer[4] = 0;
|
|
361
|
+
buffer[5] = 0;
|
|
362
|
+
write_u16(buffer + 6, scanner->count);
|
|
363
|
+
write_u16(buffer + 8, scanner->levels[scanner->count - 1]);
|
|
364
|
+
for (uint16_t index = 0; index < scanner->count; index++) {
|
|
365
|
+
write_u16(buffer + STATE_PREFIX_SIZE + index * 2, scanner->levels[index]);
|
|
366
|
+
}
|
|
367
|
+
write_u32(buffer + body_length, checksum(buffer, body_length));
|
|
368
|
+
return total_length;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
void tree_sitter_mermaid_external_scanner_deserialize(void *payload,
|
|
372
|
+
const char *buffer,
|
|
373
|
+
unsigned length) {
|
|
374
|
+
Scanner *scanner = (Scanner *)payload;
|
|
375
|
+
if (scanner == NULL) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
scanner_reset(scanner);
|
|
379
|
+
if (length == 0) {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (buffer == NULL || length < STATE_PREFIX_SIZE + STATE_CHECKSUM_SIZE ||
|
|
383
|
+
buffer[0] != 'M' || buffer[1] != 'M' ||
|
|
384
|
+
(uint8_t)buffer[2] != STATE_VERSION || !mode_is_valid(buffer[3]) ||
|
|
385
|
+
buffer[4] != 0 || buffer[5] != 0) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const uint16_t count = read_u16(buffer + 6);
|
|
390
|
+
const unsigned body_length = STATE_PREFIX_SIZE + count * 2;
|
|
391
|
+
if (count == 0 || count > MAX_DEPTH ||
|
|
392
|
+
length != body_length + STATE_CHECKSUM_SIZE ||
|
|
393
|
+
checksum(buffer, body_length) != read_u32(buffer + body_length)) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
Scanner decoded = {0};
|
|
398
|
+
decoded.mode = (uint8_t)buffer[3];
|
|
399
|
+
decoded.count = count;
|
|
400
|
+
for (uint16_t index = 0; index < count; index++) {
|
|
401
|
+
const uint16_t level =
|
|
402
|
+
read_u16(buffer + STATE_PREFIX_SIZE + index * 2);
|
|
403
|
+
if (level > MAX_INDENTATION ||
|
|
404
|
+
(index > 0 && level <= decoded.levels[index - 1])) {
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
decoded.levels[index] = level;
|
|
408
|
+
}
|
|
409
|
+
if (read_u16(buffer + 8) != decoded.levels[count - 1]) {
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
*scanner = decoded;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
bool tree_sitter_mermaid_external_scanner_scan(void *payload, TSLexer *lexer,
|
|
416
|
+
const bool *valid_symbols) {
|
|
417
|
+
Scanner *scanner = (Scanner *)payload;
|
|
418
|
+
if (scanner == NULL) {
|
|
419
|
+
return false;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (valid_symbols[END_OF_INPUT] && lexer->eof(lexer)) {
|
|
423
|
+
lexer->mark_end(lexer);
|
|
424
|
+
lexer->result_symbol = END_OF_INPUT;
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (directive_body_is_unambiguous(valid_symbols)) {
|
|
429
|
+
return scan_directive_body(lexer);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const TokenGroup *group = requested_group(valid_symbols);
|
|
433
|
+
if (group == NULL) {
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
uint32_t indentation = 0;
|
|
438
|
+
while ((lexer->lookahead == ' ' || lexer->lookahead == '\t') &&
|
|
439
|
+
indentation <= MAX_INDENTATION) {
|
|
440
|
+
lexer->advance(lexer, false);
|
|
441
|
+
indentation++;
|
|
442
|
+
}
|
|
443
|
+
lexer->mark_end(lexer);
|
|
444
|
+
|
|
445
|
+
if (indentation > MAX_INDENTATION) {
|
|
446
|
+
if (!valid_symbols[group->overflow]) {
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
lexer->result_symbol = group->overflow;
|
|
450
|
+
return true;
|
|
451
|
+
}
|
|
452
|
+
if (!is_hierarchy_row(group->mode, lexer->lookahead)) {
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
if (group->mode == MODE_TREE_VIEW &&
|
|
456
|
+
is_tree_view_metadata_row(lexer)) {
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
return emit_transition(scanner, lexer, valid_symbols, group,
|
|
461
|
+
(uint16_t)indentation);
|
|
462
|
+
}
|
|
@@ -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_
|