@mermanjs/tree-sitter-mermaid 0.3.3 → 0.3.5

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 CHANGED
@@ -28,6 +28,9 @@ enum TokenType {
28
28
  KANBAN_INDENTATION_OVERFLOW,
29
29
  END_OF_INPUT,
30
30
  DIRECTIVE_BODY,
31
+ CLASS_COMPACT_IDENTIFIER,
32
+ FLOWCHART_GROUP_SEPARATOR,
33
+ FLOW_DIRECTION_PREFIX,
31
34
  };
32
35
 
33
36
  enum ScannerMode {
@@ -191,6 +194,159 @@ static bool is_horizontal_space(int32_t lookahead) {
191
194
  lookahead == 0xa0;
192
195
  }
193
196
 
197
+ static bool is_flowchart_group_space(int32_t lookahead) {
198
+ return is_horizontal_space(lookahead) || lookahead == '\v' ||
199
+ lookahead == 0x1680 || (lookahead >= 0x2000 && lookahead <= 0x200a) ||
200
+ lookahead == 0x2028 || lookahead == 0x2029 || lookahead == 0x202f ||
201
+ lookahead == 0x205f || lookahead == 0x3000 || lookahead == 0xfeff;
202
+ }
203
+
204
+ static bool scan_flowchart_group_separator(TSLexer *lexer) {
205
+ // The grouping token requires whitespace on both sides. Leaving this to the
206
+ // ordinary lexer makes it absorb a failed whitespace prefix into the arrow.
207
+ if (!is_flowchart_group_space(lexer->lookahead)) {
208
+ return false;
209
+ }
210
+ do {
211
+ lexer->advance(lexer, true);
212
+ } while (is_flowchart_group_space(lexer->lookahead));
213
+ if (lexer->lookahead != '&') {
214
+ return false;
215
+ }
216
+ lexer->advance(lexer, false);
217
+ if (!is_flowchart_group_space(lexer->lookahead)) {
218
+ return false;
219
+ }
220
+ do {
221
+ lexer->advance(lexer, false);
222
+ } while (is_flowchart_group_space(lexer->lookahead));
223
+ lexer->mark_end(lexer);
224
+ lexer->result_symbol = FLOWCHART_GROUP_SEPARATOR;
225
+ return true;
226
+ }
227
+
228
+ static bool is_line_ending(int32_t c) { return c == '\r' || c == '\n'; }
229
+
230
+ // Only the whitespace after a keyword can cross lines in flow.jison. A later
231
+ // keyword at the end of this line may get its value after stripped comments.
232
+ static unsigned flow_direction_rank(TSLexer *lexer, bool *crossed_line) {
233
+ bool spaced = false;
234
+ *crossed_line = false;
235
+ while (is_horizontal_space(lexer->lookahead) ||
236
+ is_line_ending(lexer->lookahead)) {
237
+ spaced = true;
238
+ if (is_line_ending(lexer->lookahead)) {
239
+ *crossed_line = true;
240
+ }
241
+ lexer->advance(lexer, false);
242
+ if (*crossed_line && lexer->lookahead == '%') {
243
+ if (!consume_literal(lexer, "%%") || lexer->lookahead == '{' ||
244
+ is_line_ending(lexer->lookahead)) {
245
+ return 0;
246
+ }
247
+ while (!lexer->eof(lexer) && !is_line_ending(lexer->lookahead)) {
248
+ lexer->advance(lexer, false);
249
+ }
250
+ }
251
+ }
252
+
253
+ const int32_t first = lexer->lookahead;
254
+ if (!spaced ||
255
+ (first != 'T' && first != 'B' && first != 'R' && first != 'L')) {
256
+ return 0;
257
+ }
258
+ lexer->advance(lexer, false);
259
+ // Larger ranks win. flow.jison tries TB first.
260
+ static const char values[][3] = {"TD", "LR", "RL", "BT", "TB"};
261
+ for (unsigned i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
262
+ if (first == values[i][0] && lexer->lookahead == values[i][1]) {
263
+ return i + 1;
264
+ }
265
+ }
266
+ return 0;
267
+ }
268
+
269
+ static bool scan_flow_direction_prefix(TSLexer *lexer) {
270
+ while (is_horizontal_space(lexer->lookahead)) {
271
+ lexer->advance(lexer, true);
272
+ }
273
+ if (!consume_literal(lexer, "direction")) {
274
+ return false;
275
+ }
276
+ bool crossed_line;
277
+ const unsigned rank = flow_direction_rank(lexer, &crossed_line);
278
+ if (rank == 0 || crossed_line) {
279
+ return false;
280
+ }
281
+
282
+ // Stop at the first equal or stronger candidate. Repeating this token leaves
283
+ // the last winner to the grammar without rescanning the rest for every pair.
284
+ while (!lexer->eof(lexer) && !is_line_ending(lexer->lookahead) &&
285
+ lexer->lookahead != 0x2028 && lexer->lookahead != 0x2029 &&
286
+ lexer->lookahead != ';') {
287
+ if (lexer->lookahead != 'd') {
288
+ lexer->advance(lexer, false);
289
+ continue;
290
+ }
291
+ lexer->mark_end(lexer);
292
+ if (consume_literal(lexer, "direction")) {
293
+ if (flow_direction_rank(lexer, &crossed_line) >= rank) {
294
+ lexer->result_symbol = FLOW_DIRECTION_PREFIX;
295
+ return true;
296
+ }
297
+ if (crossed_line) {
298
+ return false;
299
+ }
300
+ }
301
+ }
302
+ return false;
303
+ }
304
+
305
+ static bool is_ascii_word_character(int32_t value) {
306
+ return (value >= 'A' && value <= 'Z') ||
307
+ (value >= 'a' && value <= 'z') ||
308
+ (value >= '0' && value <= '9') || value == '_';
309
+ }
310
+
311
+ static bool is_class_identifier_character(int32_t value) {
312
+ return is_ascii_word_character(value) ||
313
+ (value >= 0xc0 && value <= 0xffff);
314
+ }
315
+
316
+ // Jison recognizes LINE and DOTTED_LINE before the single MINUS and DOT
317
+ // tokens in class names. Preserve that boundary without changing identifier
318
+ // leaves or rejecting names containing an ordinary or trailing hyphen.
319
+ static bool scan_class_compact_identifier(TSLexer *lexer) {
320
+ while (is_horizontal_space(lexer->lookahead)) {
321
+ lexer->advance(lexer, true);
322
+ }
323
+ if (!is_class_identifier_character(lexer->lookahead)) {
324
+ return false;
325
+ }
326
+
327
+ bool have_name = false;
328
+ bool previous_is_word = false;
329
+ bool aggregation_prefix = false;
330
+ while (is_class_identifier_character(lexer->lookahead) ||
331
+ lexer->lookahead == '-' || lexer->lookahead == '.') {
332
+ int32_t current = lexer->lookahead;
333
+ if (!aggregation_prefix) {
334
+ lexer->mark_end(lexer);
335
+ }
336
+ lexer->advance(lexer, false);
337
+ if ((current == '-' || current == '.') && lexer->lookahead == current) {
338
+ lexer->result_symbol = CLASS_COMPACT_IDENTIFIER;
339
+ return true;
340
+ }
341
+ // An o starting a new Jison word can own the following line operator.
342
+ // Retain the end before it until the next character resolves that choice.
343
+ aggregation_prefix = have_name && !previous_is_word && current == 'o';
344
+ previous_is_word = is_ascii_word_character(current);
345
+ have_name = true;
346
+ }
347
+ return false;
348
+ }
349
+
194
350
  static bool scan_directive_body(TSLexer *lexer) {
195
351
  bool consumed = false;
196
352
 
@@ -430,6 +586,18 @@ bool tree_sitter_mermaid_external_scanner_scan(void *payload, TSLexer *lexer,
430
586
  }
431
587
 
432
588
  const TokenGroup *group = requested_group(valid_symbols);
589
+ if (valid_symbols[FLOW_DIRECTION_PREFIX] &&
590
+ !valid_symbols[DIRECTIVE_BODY] && group == NULL) {
591
+ return scan_flow_direction_prefix(lexer);
592
+ }
593
+ if (valid_symbols[CLASS_COMPACT_IDENTIFIER] &&
594
+ !valid_symbols[DIRECTIVE_BODY] && group == NULL) {
595
+ return scan_class_compact_identifier(lexer);
596
+ }
597
+ if (valid_symbols[FLOWCHART_GROUP_SEPARATOR] &&
598
+ !valid_symbols[DIRECTIVE_BODY] && group == NULL) {
599
+ return scan_flowchart_group_separator(lexer);
600
+ }
433
601
  if (group == NULL) {
434
602
  return false;
435
603
  }
Binary file
package/tree-sitter.json CHANGED
@@ -17,7 +17,7 @@
17
17
  }
18
18
  ],
19
19
  "metadata": {
20
- "version": "0.3.3",
20
+ "version": "0.3.5",
21
21
  "license": "MIT",
22
22
  "description": "Tolerant Tree-sitter grammar and editor queries for Mermaid",
23
23
  "authors": [