@willbooster/tree-sitter-javascript 21.0.0 → 22.0.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/README.md CHANGED
@@ -77,7 +77,8 @@ parser.setLanguage(await Language.load(javascript));
77
77
 
78
78
  The package also ships the node types in `src/node-types.json`, `tree-sitter.json`, and `grammar.js` for grammars
79
79
  that extend this one (e.g. `require('@willbooster/tree-sitter-javascript/grammar')`). Its JSDoc describes the
80
- array/formal and object-rest extension hooks. The crate ships `tree-sitter.json` too.
80
+ array/formal and object-rest extension hooks. Their external scanners can include `src/literal.h`, which scans template
81
+ characters and escapes and JSX text for the tokens they pass it. The crate ships `tree-sitter.json` too.
81
82
 
82
83
  In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-javascript) and the
83
84
  [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter) runtime with the matching ABI. The tested version is 17.0.1:
package/grammar.js CHANGED
@@ -135,6 +135,10 @@ module.exports = grammar({
135
135
  sym('_class_heritage_yield_identifier'),
136
136
  sym('_class_heritage_await_identifier'),
137
137
  sym('_template_escape_sequence'),
138
+ // The `function` and `class` keywords where a declaration may start, so that an expression statement cannot begin
139
+ // with them there, as ECMAScript's ExpressionStatement lookahead restriction requires.
140
+ sym('_declaration_function_keyword'),
141
+ sym('_declaration_class_keyword'),
138
142
  ],
139
143
 
140
144
  extras: ($) => [$.comment, sym('html_comment'), /[\s\p{Zs}\uFEFF\u2028\u2029\u2060\u200B]/u],
@@ -579,8 +583,10 @@ module.exports = grammar({
579
583
  seq(
580
584
  'try',
581
585
  field('body', $.statement_block),
582
- optional(field('handler', $.catch_clause)),
583
- optional(field('finalizer', $.finally_clause))
586
+ choice(
587
+ seq(field('handler', $.catch_clause), optional(field('finalizer', $.finally_clause))),
588
+ field('finalizer', $.finally_clause)
589
+ )
584
590
  ),
585
591
 
586
592
  with_statement: ($) => seq('with', field('object', $.parenthesized_expression), field('body', $._single_statement)),
@@ -670,17 +676,12 @@ module.exports = grammar({
670
676
  'object',
671
677
  seq(
672
678
  '{',
673
- commaSep(
674
- optional(
675
- choice(
676
- $.pair,
677
- $.spread_element,
678
- $.method_definition,
679
- aliasWithAsyncKeyword(
680
- choice($.identifier, $._reserved_identifier),
681
- sym('shorthand_property_identifier')
682
- )
683
- )
679
+ trailingCommaSep(
680
+ choice(
681
+ $.pair,
682
+ $.spread_element,
683
+ $.method_definition,
684
+ aliasWithAsyncKeyword(choice($.identifier, $._reserved_identifier), sym('shorthand_property_identifier'))
684
685
  )
685
686
  ),
686
687
  '}'
@@ -696,14 +697,10 @@ module.exports = grammar({
696
697
  sym('shorthand_property_identifier_pattern')
697
698
  )
698
699
  );
700
+ const rest = alias($._object_rest_pattern, $.rest_pattern);
699
701
  return prec(
700
702
  'object',
701
- seq(
702
- '{',
703
- repeat(seq(optional(property), ',')),
704
- optional(choice(property, alias($._object_rest_pattern, $.rest_pattern))),
705
- '}'
706
- )
703
+ seq('{', optional(choice(seq(commaSep1(property), optional(seq(',', optional(rest)))), rest)), '}')
707
704
  );
708
705
  },
709
706
 
@@ -845,7 +842,11 @@ module.exports = grammar({
845
842
  'class',
846
843
  field('name', optional($.identifier))
847
844
  ),
848
- seq(repeat(field('decorator', $.decorator)), 'class', field('name', $.identifier))
845
+ seq(
846
+ repeat(field('decorator', $.decorator)),
847
+ choice('class', alias(sym('_declaration_class_keyword'), 'class')),
848
+ field('name', $.identifier)
849
+ )
849
850
  ),
850
851
  optional($.class_heritage),
851
852
  field('body', $.class_body)
@@ -903,7 +904,11 @@ module.exports = grammar({
903
904
  'function',
904
905
  field('name', optional($.identifier))
905
906
  ),
906
- seq(...(async ? [alias(sym('_async_keyword'), 'async')] : []), 'function', field('name', $.identifier))
907
+ seq(
908
+ ...(async ? [alias(sym('_async_keyword'), 'async')] : []),
909
+ choice('function', alias(sym('_declaration_function_keyword'), 'function')),
910
+ field('name', $.identifier)
911
+ )
907
912
  ),
908
913
  functionScope(async, seq($._call_signature, field('body', $.statement_block)))
909
914
  )
@@ -939,7 +944,7 @@ module.exports = grammar({
939
944
  ),
940
945
  seq(
941
946
  ...(async ? [alias(sym('_async_keyword'), 'async')] : []),
942
- 'function',
947
+ choice('function', alias(sym('_declaration_function_keyword'), 'function')),
943
948
  '*',
944
949
  field('name', $.identifier)
945
950
  )
@@ -1476,7 +1481,7 @@ module.exports = grammar({
1476
1481
  undefined: () => 'undefined',
1477
1482
 
1478
1483
  arguments: ($) =>
1479
- seq(choice('(', sym('_call_arguments_context')), commaSep(optional(choice($.expression, $.spread_element))), ')'),
1484
+ seq(choice('(', sym('_call_arguments_context')), trailingCommaSep(choice($.expression, $.spread_element)), ')'),
1480
1485
 
1481
1486
  decorator: ($) =>
1482
1487
  seq(
@@ -1850,6 +1855,15 @@ function commaSep(rule) {
1850
1855
  return optional(commaSep1(rule));
1851
1856
  }
1852
1857
 
1858
+ /**
1859
+ * @param {import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} rule
1860
+ *
1861
+ * @returns {import('@willbooster/web-tree-sitter/dsl').ChoiceRule}
1862
+ */
1863
+ function trailingCommaSep(rule) {
1864
+ return optional(seq(commaSep1(rule), optional(',')));
1865
+ }
1866
+
1853
1867
  /** @param {(isAsync: boolean) => import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} makeRule */
1854
1868
  function functionContext(makeRule) {
1855
1869
  return choice(makeRule(false), makeRule(true));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-javascript",
3
- "version": "21.0.0",
3
+ "version": "22.0.0",
4
4
  "description": "JavaScript grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
@@ -16,6 +16,7 @@
16
16
  },
17
17
  "files": [
18
18
  "grammar.js",
19
+ "src/literal.h",
19
20
  "grammars/script/grammar.js",
20
21
  "grammars/script/src/node-types.json",
21
22
  "tree-sitter.json",
package/src/literal.h ADDED
@@ -0,0 +1,155 @@
1
+ #ifndef TREE_SITTER_JAVASCRIPT_LITERAL_H_
2
+ #define TREE_SITTER_JAVASCRIPT_LITERAL_H_
3
+
4
+ #include "tree_sitter/parser.h"
5
+
6
+ static bool scan_character_reference(TSLexer *lexer);
7
+ static bool scan_template_escape_value(TSLexer *lexer);
8
+ static bool scan_fixed_hex_digits(TSLexer *lexer, unsigned length);
9
+ static bool scan_template_code_point(TSLexer *lexer);
10
+ static int hex_digit_value(int32_t c);
11
+ static bool is_ascii_letter(int32_t c);
12
+ static bool is_ascii_digit(int32_t c);
13
+ static bool reference_is_digit(int32_t c, bool hex);
14
+
15
+ static bool scan_template_chars(TSLexer *lexer, TSSymbol symbol) {
16
+ lexer->result_symbol = symbol;
17
+ for (bool has_content = false;; has_content = true) {
18
+ lexer->mark_end(lexer);
19
+ if (lexer->eof(lexer)) return false;
20
+ switch (lexer->lookahead) {
21
+ case '`':
22
+ case '\\':
23
+ return has_content;
24
+ case '$':
25
+ lexer->advance(lexer, false);
26
+ if (lexer->lookahead == '{') return has_content;
27
+ break;
28
+ default:
29
+ lexer->advance(lexer, false);
30
+ }
31
+ }
32
+ }
33
+
34
+ static bool scan_jsx_text(TSLexer *lexer, TSSymbol symbol) {
35
+ bool saw_text = false;
36
+ bool at_newline = false;
37
+ lexer->result_symbol = symbol;
38
+ for (;;) {
39
+ lexer->mark_end(lexer);
40
+ if (lexer->eof(lexer)) return saw_text;
41
+ switch (lexer->lookahead) {
42
+ case '<': case '>': case '{': case '}':
43
+ return saw_text;
44
+ case '&':
45
+ if (scan_character_reference(lexer)) return saw_text;
46
+ saw_text = true;
47
+ at_newline = false;
48
+ continue;
49
+ default:
50
+ break;
51
+ }
52
+ // Use ASCII classes here because Babel retains Unicode spaces in JSX text.
53
+ bool is_space = (lexer->lookahead >= '\t' && lexer->lookahead <= '\r') || lexer->lookahead == ' ';
54
+ // Babel splits JSX text into lines at CR/LF; U+2028 and U+2029 stay text.
55
+ if (lexer->lookahead == '\n' || lexer->lookahead == '\r') {
56
+ at_newline = true;
57
+ } else {
58
+ at_newline &= is_space;
59
+ saw_text |= !at_newline;
60
+ }
61
+ lexer->advance(lexer, false);
62
+ }
63
+ }
64
+
65
+ static bool scan_character_reference(TSLexer *lexer) {
66
+ lexer->advance(lexer, false);
67
+ unsigned length = 0;
68
+ if (lexer->lookahead == '#') {
69
+ lexer->advance(lexer, false);
70
+ bool hex = lexer->lookahead == 'x' || lexer->lookahead == 'X';
71
+ if (hex) lexer->advance(lexer, false);
72
+ unsigned max_length = hex ? 6 : 5;
73
+ while (length < max_length && reference_is_digit(lexer->lookahead, hex)) {
74
+ lexer->advance(lexer, false);
75
+ length++;
76
+ }
77
+ } else {
78
+ while (length < 30 && is_ascii_letter(lexer->lookahead)) {
79
+ lexer->advance(lexer, false);
80
+ length++;
81
+ }
82
+ }
83
+ return length > 0 && lexer->lookahead == ';';
84
+ }
85
+
86
+ static bool scan_template_escape(TSLexer *lexer, TSSymbol symbol) {
87
+ lexer->advance(lexer, false);
88
+ if (lexer->eof(lexer) || !scan_template_escape_value(lexer)) return false;
89
+ lexer->mark_end(lexer);
90
+ lexer->result_symbol = symbol;
91
+ return true;
92
+ }
93
+
94
+ static bool scan_template_escape_value(TSLexer *lexer) {
95
+ int32_t c = lexer->lookahead;
96
+ lexer->advance(lexer, false);
97
+ switch (c) {
98
+ case '0':
99
+ return !is_ascii_digit(lexer->lookahead);
100
+ case 'x':
101
+ return scan_fixed_hex_digits(lexer, 2);
102
+ case 'u':
103
+ return lexer->lookahead == '{' ? scan_template_code_point(lexer) : scan_fixed_hex_digits(lexer, 4);
104
+ case '\r':
105
+ if (lexer->lookahead == '\n') lexer->advance(lexer, false);
106
+ return true;
107
+ default:
108
+ return !is_ascii_digit(c);
109
+ }
110
+ }
111
+
112
+ static bool scan_fixed_hex_digits(TSLexer *lexer, unsigned length) {
113
+ for (unsigned i = 0; i < length; i++) {
114
+ if (hex_digit_value(lexer->lookahead) < 0) return false;
115
+ lexer->advance(lexer, false);
116
+ }
117
+ return true;
118
+ }
119
+
120
+ static bool scan_template_code_point(TSLexer *lexer) {
121
+ lexer->advance(lexer, false);
122
+ uint32_t value = 0;
123
+ bool has_digit = false;
124
+ int digit;
125
+ while ((digit = hex_digit_value(lexer->lookahead)) >= 0) {
126
+ has_digit = true;
127
+ value = value * 16 + (unsigned)digit;
128
+ if (value > 0x10FFFF) return false;
129
+ lexer->advance(lexer, false);
130
+ }
131
+ if (!has_digit || lexer->lookahead != '}') return false;
132
+ lexer->advance(lexer, false);
133
+ return true;
134
+ }
135
+
136
+ static int hex_digit_value(int32_t c) {
137
+ if (is_ascii_digit(c)) return c - '0';
138
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
139
+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
140
+ return -1;
141
+ }
142
+
143
+ static bool is_ascii_letter(int32_t c) {
144
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
145
+ }
146
+
147
+ static bool reference_is_digit(int32_t c, bool hex) {
148
+ return hex ? hex_digit_value(c) >= 0 : is_ascii_digit(c);
149
+ }
150
+
151
+ static bool is_ascii_digit(int32_t c) {
152
+ return c >= '0' && c <= '9';
153
+ }
154
+
155
+ #endif
Binary file
Binary file
package/tree-sitter.json CHANGED
@@ -34,7 +34,7 @@
34
34
  }
35
35
  ],
36
36
  "metadata": {
37
- "version": "21.0.0",
37
+ "version": "22.0.0",
38
38
  "license": "MIT",
39
39
  "description": "JavaScript grammar for tree-sitter",
40
40
  "authors": [