@mermanjs/tree-sitter-mermaid 0.3.4 → 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
@@ -29,6 +29,8 @@ enum TokenType {
29
29
  END_OF_INPUT,
30
30
  DIRECTIVE_BODY,
31
31
  CLASS_COMPACT_IDENTIFIER,
32
+ FLOWCHART_GROUP_SEPARATOR,
33
+ FLOW_DIRECTION_PREFIX,
32
34
  };
33
35
 
34
36
  enum ScannerMode {
@@ -192,6 +194,114 @@ static bool is_horizontal_space(int32_t lookahead) {
192
194
  lookahead == 0xa0;
193
195
  }
194
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
+
195
305
  static bool is_ascii_word_character(int32_t value) {
196
306
  return (value >= 'A' && value <= 'Z') ||
197
307
  (value >= 'a' && value <= 'z') ||
@@ -476,10 +586,18 @@ bool tree_sitter_mermaid_external_scanner_scan(void *payload, TSLexer *lexer,
476
586
  }
477
587
 
478
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
+ }
479
593
  if (valid_symbols[CLASS_COMPACT_IDENTIFIER] &&
480
594
  !valid_symbols[DIRECTIVE_BODY] && group == NULL) {
481
595
  return scan_class_compact_identifier(lexer);
482
596
  }
597
+ if (valid_symbols[FLOWCHART_GROUP_SEPARATOR] &&
598
+ !valid_symbols[DIRECTIVE_BODY] && group == NULL) {
599
+ return scan_flowchart_group_separator(lexer);
600
+ }
483
601
  if (group == NULL) {
484
602
  return false;
485
603
  }
Binary file
package/tree-sitter.json CHANGED
@@ -17,7 +17,7 @@
17
17
  }
18
18
  ],
19
19
  "metadata": {
20
- "version": "0.3.4",
20
+ "version": "0.3.5",
21
21
  "license": "MIT",
22
22
  "description": "Tolerant Tree-sitter grammar and editor queries for Mermaid",
23
23
  "authors": [