@willbooster/tree-sitter-zig 18.0.0 → 20.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 +13 -10
- package/grammar.js +134 -241
- package/package.json +1 -1
- package/src/node-types.json +2654 -523
- package/src/scanner.c +11 -121
- package/tree-sitter-zig.wasm +0 -0
- package/tree-sitter.json +1 -1
package/src/scanner.c
CHANGED
|
@@ -5,12 +5,6 @@ enum TokenType {
|
|
|
5
5
|
EMPTY_DOC_COMMENT_CONTENT,
|
|
6
6
|
ERROR_SENTINEL,
|
|
7
7
|
DECLARATION_NAME_COLON,
|
|
8
|
-
UNION_BACKING_OPEN,
|
|
9
|
-
COMMENT,
|
|
10
|
-
STRING_CONTENT_GUARD,
|
|
11
|
-
DIVISION,
|
|
12
|
-
DIVISION_ASSIGNMENT,
|
|
13
|
-
DECLARATION_DOC_OPEN,
|
|
14
8
|
};
|
|
15
9
|
|
|
16
10
|
void *tree_sitter_zig_external_scanner_create(void) { return NULL; }
|
|
@@ -31,47 +25,24 @@ void tree_sitter_zig_external_scanner_deserialize(void *payload, const char *buf
|
|
|
31
25
|
|
|
32
26
|
static inline bool at_eof(const TSLexer *lexer) { return lexer->lookahead == 0 && lexer->eof(lexer); }
|
|
33
27
|
|
|
34
|
-
static bool is_whitespace(int32_t character) {
|
|
35
|
-
return character == ' ' || character == '\t' || character == '\n' || character == '\r' ||
|
|
36
|
-
character == '\f';
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
static bool is_identifier_continue(int32_t character) {
|
|
40
|
-
return (character >= 'A' && character <= 'Z') || (character >= 'a' && character <= 'z') ||
|
|
41
|
-
(character >= '0' && character <= '9') || character == '_';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
28
|
static bool scan_declaration_name_colon(TSLexer *lexer) {
|
|
29
|
+
while (lexer->lookahead == ' ' || lexer->lookahead == '\t' || lexer->lookahead == '\n' ||
|
|
30
|
+
lexer->lookahead == '\r' || lexer->lookahead == '\f') {
|
|
31
|
+
lexer->advance(lexer, true);
|
|
32
|
+
}
|
|
33
|
+
if (lexer->lookahead != ':') return false;
|
|
45
34
|
lexer->advance(lexer, false);
|
|
46
35
|
lexer->result_symbol = DECLARATION_NAME_COLON;
|
|
47
36
|
return true;
|
|
48
37
|
}
|
|
49
38
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
while (lexer->lookahead != '\n' && !at_eof(lexer)) lexer->advance(lexer, false);
|
|
57
|
-
}
|
|
58
|
-
for (const char *character = "enum"; *character != '\0'; character++) {
|
|
59
|
-
if (lexer->lookahead != *character) return false;
|
|
60
|
-
lexer->advance(lexer, false);
|
|
39
|
+
bool tree_sitter_zig_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
|
|
40
|
+
(void)payload;
|
|
41
|
+
if (valid_symbols[ERROR_SENTINEL]) return false;
|
|
42
|
+
if (valid_symbols[DECLARATION_NAME_COLON]) return scan_declaration_name_colon(lexer);
|
|
43
|
+
if (!valid_symbols[DOC_COMMENT_CONTENT] && !valid_symbols[EMPTY_DOC_COMMENT_CONTENT]) {
|
|
44
|
+
return false;
|
|
61
45
|
}
|
|
62
|
-
return !is_identifier_continue(lexer->lookahead);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
static bool scan_union_backing_open(TSLexer *lexer) {
|
|
66
|
-
lexer->advance(lexer, false);
|
|
67
|
-
lexer->mark_end(lexer);
|
|
68
|
-
// A leading enum selects the tagged-union header before Zig parses an expression.
|
|
69
|
-
if (union_header_starts_with_enum(lexer)) return false;
|
|
70
|
-
lexer->result_symbol = UNION_BACKING_OPEN;
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
static bool scan_doc_content(TSLexer *lexer, const bool *valid_symbols) {
|
|
75
46
|
if (at_eof(lexer)) {
|
|
76
47
|
if (!valid_symbols[EMPTY_DOC_COMMENT_CONTENT]) return false;
|
|
77
48
|
lexer->result_symbol = EMPTY_DOC_COMMENT_CONTENT;
|
|
@@ -86,84 +57,3 @@ static bool scan_doc_content(TSLexer *lexer, const bool *valid_symbols) {
|
|
|
86
57
|
lexer->result_symbol = DOC_COMMENT_CONTENT;
|
|
87
58
|
return true;
|
|
88
59
|
}
|
|
89
|
-
|
|
90
|
-
typedef struct {
|
|
91
|
-
bool starts_line;
|
|
92
|
-
bool may_follow_bom;
|
|
93
|
-
uint32_t whitespace_columns;
|
|
94
|
-
} DocumentationLine;
|
|
95
|
-
|
|
96
|
-
static bool scan_declaration_doc(TSLexer *lexer, const bool *valid_symbols, DocumentationLine line) {
|
|
97
|
-
if (!valid_symbols[DECLARATION_DOC_OPEN] || valid_symbols[ERROR_SENTINEL]) return false;
|
|
98
|
-
// The adjusted column excludes an initial BOM and includes the three marker characters already read.
|
|
99
|
-
if (!line.starts_line &&
|
|
100
|
-
(!line.may_follow_bom || lexer->get_column(lexer) != line.whitespace_columns + 3)) return false;
|
|
101
|
-
lexer->result_symbol = DECLARATION_DOC_OPEN;
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
static bool scan_line_comment(TSLexer *lexer, const bool *valid_symbols, DocumentationLine line) {
|
|
106
|
-
lexer->advance(lexer, false);
|
|
107
|
-
if (lexer->lookahead == '!') return false;
|
|
108
|
-
if (lexer->lookahead == '/') {
|
|
109
|
-
lexer->advance(lexer, false);
|
|
110
|
-
if (lexer->lookahead != '/') return scan_declaration_doc(lexer, valid_symbols, line);
|
|
111
|
-
}
|
|
112
|
-
if (!valid_symbols[COMMENT]) return false;
|
|
113
|
-
while (lexer->lookahead != '\n' && !at_eof(lexer)) lexer->advance(lexer, false);
|
|
114
|
-
lexer->result_symbol = COMMENT;
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
static bool scan_slash(TSLexer *lexer, const bool *valid_symbols, DocumentationLine line) {
|
|
119
|
-
if (lexer->lookahead != '/') return false;
|
|
120
|
-
lexer->advance(lexer, false);
|
|
121
|
-
if (lexer->lookahead == '/') return scan_line_comment(lexer, valid_symbols, line);
|
|
122
|
-
if (valid_symbols[ERROR_SENTINEL]) return false;
|
|
123
|
-
if (lexer->lookahead == '=') {
|
|
124
|
-
if (!valid_symbols[DIVISION_ASSIGNMENT]) return false;
|
|
125
|
-
lexer->advance(lexer, false);
|
|
126
|
-
lexer->result_symbol = DIVISION_ASSIGNMENT;
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
if (!valid_symbols[DIVISION]) return false;
|
|
130
|
-
lexer->result_symbol = DIVISION;
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
static DocumentationLine skip_whitespace(TSLexer *lexer, bool may_start_documentation) {
|
|
135
|
-
DocumentationLine line = {0};
|
|
136
|
-
if (may_start_documentation) {
|
|
137
|
-
TSLexerPosition position;
|
|
138
|
-
lexer->get_position(lexer, &position);
|
|
139
|
-
line.starts_line = position.column == 0;
|
|
140
|
-
line.may_follow_bom = position.row == 0 && position.bytes <= 3;
|
|
141
|
-
}
|
|
142
|
-
while (is_whitespace(lexer->lookahead)) {
|
|
143
|
-
line.starts_line |= lexer->lookahead == '\n';
|
|
144
|
-
line.whitespace_columns++;
|
|
145
|
-
lexer->advance(lexer, true);
|
|
146
|
-
}
|
|
147
|
-
return line;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
bool tree_sitter_zig_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
|
|
151
|
-
(void)payload;
|
|
152
|
-
if (!valid_symbols[ERROR_SENTINEL] &&
|
|
153
|
-
(valid_symbols[DOC_COMMENT_CONTENT] || valid_symbols[EMPTY_DOC_COMMENT_CONTENT])) {
|
|
154
|
-
return scan_doc_content(lexer, valid_symbols);
|
|
155
|
-
}
|
|
156
|
-
// Literal string contents take priority over comment prefixes.
|
|
157
|
-
if (valid_symbols[STRING_CONTENT_GUARD] && !valid_symbols[ERROR_SENTINEL] &&
|
|
158
|
-
lexer->lookahead != '\n' && lexer->lookahead != '\r') return false;
|
|
159
|
-
DocumentationLine line = skip_whitespace(lexer, valid_symbols[DECLARATION_DOC_OPEN]);
|
|
160
|
-
if (!valid_symbols[ERROR_SENTINEL]) {
|
|
161
|
-
if (lexer->lookahead == ':' && valid_symbols[DECLARATION_NAME_COLON]) {
|
|
162
|
-
return scan_declaration_name_colon(lexer);
|
|
163
|
-
}
|
|
164
|
-
if (lexer->lookahead == '(' && valid_symbols[UNION_BACKING_OPEN]) {
|
|
165
|
-
return scan_union_backing_open(lexer);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return scan_slash(lexer, valid_symbols, line);
|
|
169
|
-
}
|
package/tree-sitter-zig.wasm
CHANGED
|
Binary file
|