@@ -33,13 +33,20 @@ static bool consume_if(TSLexer *lexer, const int32_t character)
3333const char SQ_STRING_DELIMITER = '\'' ;
3434const char DQ_STRING_DELIMITER = '"' ;
3535
36+ struct ScannerState {
37+ unsigned short started ;
38+ unsigned int depth ;
39+ };
40+
3641void * tree_sitter_lua_external_scanner_create ()
3742{
38- return NULL ;
43+ struct ScannerState * state = malloc (sizeof (struct ScannerState ));
44+ return state ;
3945}
4046
4147void tree_sitter_lua_external_scanner_destroy (void * payload )
4248{
49+ free (payload );
4350}
4451
4552enum StartedToken
@@ -51,22 +58,21 @@ enum StartedToken
5158 LONG_STRING ,
5259};
5360
54- unsigned short started = 0 ;
55- unsigned int depth = 0 ;
56-
5761unsigned int tree_sitter_lua_external_scanner_serialize (void * payload , char * buffer )
5862{
59- buffer [0 ] = started ;
60- buffer [1 ] = depth ;
63+ struct ScannerState * state = (struct ScannerState * )payload ;
64+ buffer [0 ] = state -> started ;
65+ buffer [1 ] = state -> depth ;
6166 return 2 ;
6267}
6368
6469void tree_sitter_lua_external_scanner_deserialize (void * payload , const char * buffer , unsigned int length )
6570{
6671 if (length == 2 )
6772 {
68- started = buffer [0 ];
69- depth = buffer [1 ];
73+ struct ScannerState * state = (struct ScannerState * )payload ;
74+ state -> started = buffer [0 ];
75+ state -> depth = buffer [1 ];
7076 }
7177}
7278
@@ -81,9 +87,9 @@ static unsigned int get_depth(TSLexer *lexer)
8187 return current_depth ;
8288}
8389
84- static bool scan_depth (TSLexer * lexer )
90+ static bool scan_depth (TSLexer * lexer , struct ScannerState * state )
8591{
86- unsigned int remaining_depth = depth ;
92+ unsigned int remaining_depth = state -> depth ;
8793 while (remaining_depth > 0 && consume_if (lexer , '=' ))
8894 {
8995 remaining_depth -= 1 ;
@@ -94,7 +100,8 @@ static bool scan_depth(TSLexer *lexer)
94100
95101bool tree_sitter_lua_external_scanner_scan (void * payload , TSLexer * lexer , const bool * valid_symbols )
96102{
97- switch (started )
103+ struct ScannerState * state = (struct ScannerState * )payload ;
104+ switch (state -> started )
98105 {
99106 case SHORT_COMMENT :
100107 {
@@ -103,7 +110,7 @@ bool tree_sitter_lua_external_scanner_scan(void *payload, TSLexer *lexer, const
103110 {
104111 if (valid_symbols [COMMENT_END ])
105112 {
106- started = 0 ;
113+ state -> started = 0 ;
107114
108115 lexer -> result_symbol = COMMENT_END ;
109116 return true;
@@ -127,14 +134,14 @@ bool tree_sitter_lua_external_scanner_scan(void *payload, TSLexer *lexer, const
127134 case SHORT_DQ_STRING :
128135 {
129136 // define the short string's delimiter
130- const char delimiter = started == SHORT_SQ_STRING ? SQ_STRING_DELIMITER : DQ_STRING_DELIMITER ;
137+ const char delimiter = state -> started == SHORT_SQ_STRING ? SQ_STRING_DELIMITER : DQ_STRING_DELIMITER ;
131138
132139 // try to match the short string's end (" or ')
133140 if (consume_if (lexer , delimiter ))
134141 {
135142 if (valid_symbols [STRING_END ])
136143 {
137- started = 0 ;
144+ state -> started = 0 ;
138145
139146 lexer -> result_symbol = STRING_END ;
140147 return true;
@@ -163,18 +170,18 @@ bool tree_sitter_lua_external_scanner_scan(void *payload, TSLexer *lexer, const
163170 case LONG_COMMENT :
164171 case LONG_STRING :
165172 {
166- const bool is_inside_a_comment = started == LONG_COMMENT ;
173+ const bool is_inside_a_comment = state -> started == LONG_COMMENT ;
167174
168175 bool some_characters_were_consumed = false;
169176 if (is_inside_a_comment ? valid_symbols [COMMENT_END ] : valid_symbols [STRING_END ])
170177 {
171178 // try to match the long comment's/string's end (]=*])
172179 if (consume_if (lexer , ']' ))
173180 {
174- if (scan_depth (lexer ) && consume_if (lexer , ']' ))
181+ if (scan_depth (lexer , state ) && consume_if (lexer , ']' ))
175182 {
176- started = 0 ;
177- depth = 0 ;
183+ state -> started = 0 ;
184+ state -> depth = 0 ;
178185
179186 lexer -> result_symbol = is_inside_a_comment ? COMMENT_END : STRING_END ;
180187 return true;
@@ -203,7 +210,7 @@ bool tree_sitter_lua_external_scanner_scan(void *payload, TSLexer *lexer, const
203210 lexer -> mark_end (lexer );
204211 if (consume_if (lexer , ']' ))
205212 {
206- if (scan_depth (lexer ))
213+ if (scan_depth (lexer , state ))
207214 {
208215 if (consume_if (lexer , ']' ))
209216 {
@@ -245,7 +252,7 @@ bool tree_sitter_lua_external_scanner_scan(void *payload, TSLexer *lexer, const
245252 {
246253 if (consume_if (lexer , '-' ))
247254 {
248- started = SHORT_COMMENT ;
255+ state -> started = SHORT_COMMENT ;
249256
250257 // try to match a long comment's start (--[=*[)
251258 lexer -> mark_end (lexer );
@@ -255,8 +262,8 @@ bool tree_sitter_lua_external_scanner_scan(void *payload, TSLexer *lexer, const
255262
256263 if (consume_if (lexer , '[' ))
257264 {
258- started = LONG_COMMENT ;
259- depth = possible_depth ;
265+ state -> started = LONG_COMMENT ;
266+ state -> depth = possible_depth ;
260267
261268 lexer -> mark_end (lexer );
262269 }
@@ -275,12 +282,12 @@ bool tree_sitter_lua_external_scanner_scan(void *payload, TSLexer *lexer, const
275282 // try to match a short single-quoted string's start (")
276283 if (consume_if (lexer , SQ_STRING_DELIMITER ))
277284 {
278- started = SHORT_SQ_STRING ;
285+ state -> started = SHORT_SQ_STRING ;
279286 }
280287 // try to match a short double-quoted string's start (')
281288 else if (consume_if (lexer , DQ_STRING_DELIMITER ))
282289 {
283- started = SHORT_DQ_STRING ;
290+ state -> started = SHORT_DQ_STRING ;
284291 }
285292 // try to match a long string's start ([=*[)
286293 else if (consume_if (lexer , '[' ))
@@ -289,12 +296,12 @@ bool tree_sitter_lua_external_scanner_scan(void *payload, TSLexer *lexer, const
289296
290297 if (consume_if (lexer , '[' ))
291298 {
292- started = LONG_STRING ;
293- depth = possible_depth ;
299+ state -> started = LONG_STRING ;
300+ state -> depth = possible_depth ;
294301 }
295302 }
296303
297- if (started )
304+ if (state -> started )
298305 {
299306 lexer -> result_symbol = STRING_START ;
300307 return true;
0 commit comments