Skip to content

Commit 46c0df0

Browse files
committed
Update lua grammar
1 parent f68d0c8 commit 46c0df0

File tree

3 files changed

+28
-46
lines changed

3 files changed

+28
-46
lines changed

_automation/grammars.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
"scanner.c"
155155
],
156156
"reference": "master",
157-
"revision": "9ec837de6061c05d4f202047e3e9dcf644c779e8"
157+
"revision": "0e860f697901c9b610104eb61d3812755d5211fc"
158158
},
159159
{
160160
"language": "ocaml",

lua/parser.c

26.2 KB
Binary file not shown.

lua/scanner.c

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
// https://github.com/Azganoth/tree-sitter-lua/blob/master/src/scanner.cc
2-
// https://github.com/MunifTanjim/tree-sitter-lua/
3-
// Modified version from here.
4-
// Thanks to Azganoth and then MunifTanjim
5-
1+
#include "parser.h"
62
#include <wctype.h>
73
#include <stdio.h>
8-
#include "parser.h"
94

105
enum TokenType {
116
BLOCK_COMMENT_START,
@@ -47,32 +42,25 @@ static inline void skip_whitespaces(TSLexer *lexer) {
4742
void *tree_sitter_lua_external_scanner_create() { return NULL; }
4843
void tree_sitter_lua_external_scanner_destroy(void *payload) {}
4944

50-
enum InsideNode { INSIDE_NONE, INSIDE_COMMENT, INSIDE_STRING };
51-
52-
uint8_t inside_node = INSIDE_NONE;
5345
char ending_char = 0;
5446
uint8_t level_count = 0;
5547

5648
static inline void reset_state() {
57-
inside_node = INSIDE_NONE;
5849
ending_char = 0;
5950
level_count = 0;
6051
}
6152

6253
unsigned tree_sitter_lua_external_scanner_serialize(void *payload, char *buffer) {
63-
buffer[0] = inside_node;
64-
buffer[1] = ending_char;
65-
buffer[2] = level_count;
66-
return 3;
54+
buffer[0] = ending_char;
55+
buffer[1] = level_count;
56+
return 2;
6757
}
6858

6959
void tree_sitter_lua_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
7060
if (length == 0) return;
71-
inside_node = buffer[0];
61+
ending_char = buffer[0];
7262
if (length == 1) return;
73-
ending_char = buffer[1];
74-
if (length == 2) return;
75-
level_count = buffer[2];
63+
level_count = buffer[1];
7664
}
7765

7866
static bool scan_block_start(TSLexer *lexer) {
@@ -122,7 +110,6 @@ static bool scan_comment_start(TSLexer *lexer) {
122110

123111
if (scan_block_start(lexer)) {
124112
lexer->mark_end(lexer);
125-
inside_node = INSIDE_COMMENT;
126113
lexer->result_symbol = BLOCK_COMMENT_START;
127114
return true;
128115
}
@@ -156,14 +143,12 @@ static bool scan_comment_content(TSLexer *lexer) {
156143

157144
static bool scan_string_start(TSLexer *lexer) {
158145
if (lexer->lookahead == '"' || lexer->lookahead == '\'') {
159-
inside_node = INSIDE_STRING;
160146
ending_char = lexer->lookahead;
161147
consume(lexer);
162148
return true;
163149
}
164150

165151
if (scan_block_start(lexer)) {
166-
inside_node = INSIDE_STRING;
167152
return true;
168153
}
169154

@@ -188,7 +173,12 @@ static bool scan_string_content(TSLexer *lexer) {
188173
}
189174

190175
while (lexer->lookahead != '\n' && lexer->lookahead != 0 && lexer->lookahead != ending_char) {
191-
while (consume_char('\\', lexer) && consume_char('z', lexer)) continue;
176+
if (consume_char('\\', lexer) && consume_char('z', lexer)) {
177+
while (iswspace(lexer->lookahead)) {
178+
consume(lexer);
179+
}
180+
continue;
181+
};
192182

193183
if (lexer->lookahead == 0) {
194184
return true;
@@ -201,33 +191,25 @@ static bool scan_string_content(TSLexer *lexer) {
201191
}
202192

203193
bool tree_sitter_lua_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
204-
if (inside_node == INSIDE_STRING) {
205-
if (valid_symbols[STRING_END] && scan_string_end(lexer)) {
206-
reset_state();
207-
lexer->result_symbol = STRING_END;
208-
return true;
209-
}
210-
211-
if (valid_symbols[STRING_CONTENT] && scan_string_content(lexer)) {
212-
lexer->result_symbol = STRING_CONTENT;
213-
return true;
214-
}
215-
216-
return false;
194+
if (valid_symbols[STRING_END] && scan_string_end(lexer)) {
195+
reset_state();
196+
lexer->result_symbol = STRING_END;
197+
return true;
217198
}
218199

219-
if (inside_node == INSIDE_COMMENT) {
220-
if (valid_symbols[BLOCK_COMMENT_END] && ending_char == 0 && scan_block_end(lexer)) {
221-
reset_state();
222-
lexer->result_symbol = BLOCK_COMMENT_END;
223-
return true;
224-
}
200+
if (valid_symbols[STRING_CONTENT] && scan_string_content(lexer)) {
201+
lexer->result_symbol = STRING_CONTENT;
202+
return true;
203+
}
225204

226-
if (valid_symbols[BLOCK_COMMENT_CONTENT] && scan_comment_content(lexer)) {
227-
return true;
228-
}
205+
if (valid_symbols[BLOCK_COMMENT_END] && ending_char == 0 && scan_block_end(lexer)) {
206+
reset_state();
207+
lexer->result_symbol = BLOCK_COMMENT_END;
208+
return true;
209+
}
229210

230-
return false;
211+
if (valid_symbols[BLOCK_COMMENT_CONTENT] && scan_comment_content(lexer)) {
212+
return true;
231213
}
232214

233215
skip_whitespaces(lexer);

0 commit comments

Comments
 (0)