@willbooster/tree-sitter-javascript 21.0.0 → 21.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/README.md +2 -1
- package/package.json +2 -1
- package/src/literal.h +155 -0
- package/tree-sitter-javascript.wasm +0 -0
- package/tree-sitter-javascript_script.wasm +0 -0
- package/tree-sitter.json +1 -1
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.
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-javascript",
|
|
3
|
-
"version": "21.
|
|
3
|
+
"version": "21.1.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
|