@@ -121,8 +121,61 @@ void lexer_reset(Lexer *lexer) {
121121 lexer -> processed_len = 0 ;
122122}
123123
124- // void lexer_to_js(const Lexer *lexer, ...)
125- // TODO
124+ /*
125+ * Convert the lexer state to a JavaScript array of tokens.
126+ */
127+ static inline void lexer_to_js (const Lexer * lexer , char * out , size_t out_size ) {
128+ if (!lexer || !out || out_size == 0 ) {
129+ if (out && out_size > 0 ) {
130+ out [0 ] = '\0' ;
131+ }
132+ return ;
133+ }
134+
135+ // Initialize an empty string in case there are no tokens
136+ out [0 ] = '\0' ;
137+ size_t current_len = 0 ;
138+ size_t remaining_size = out_size ;
139+
140+ // Start the JavaScript array
141+ int written = snprintf (out , remaining_size , "[\n" );
142+ if (written < 0 || (size_t )written >= remaining_size ) {
143+ // Not enough space, out is now truncated but null-terminated.
144+ return ;
145+ }
146+ current_len += written ;
147+ remaining_size -= written ;
148+
149+ // Iterate over each processed token
150+ for (size_t i = 0 ; i < lexer -> processed_len ; ++ i ) {
151+ // Convert the current token to its JS representation
152+ // We pass the pointer to the current end of the output string
153+ token_to_js (& lexer -> processed [i ], out + current_len , remaining_size );
154+
155+ // Update current length and remaining size
156+ size_t token_len = strlen (out + current_len );
157+ current_len += token_len ;
158+
159+ if (current_len >= out_size - 1 ) {
160+ // Buffer is full, we can't add a comma or the closing bracket.
161+ return ;
162+ }
163+ remaining_size = out_size - current_len ;
164+
165+ // Add a comma if this is not the last token
166+ if (i < lexer -> processed_len - 1 ) {
167+ written = snprintf (out + current_len , remaining_size , ",\n" );
168+ if (written < 0 || (size_t )written >= remaining_size ) {
169+ return ; // Not enough space for comma
170+ }
171+ current_len += written ;
172+ remaining_size -= written ;
173+ }
174+ }
175+
176+ // Close the JavaScript array
177+ snprintf (out + current_len , remaining_size , "\n]" );
178+ }
126179
127180/*
128181 * Transition to a new lexer state.
@@ -166,8 +219,8 @@ static void lexer__commit(Lexer *lexer) {
166219 last_pos - token -> pos_start , token -> pos_start , last_pos );
167220 } else {
168221 const char * name = token_name_utf8 (token );
169- printf ("[Lexer__commit] Commit token: name=%s, type : %d , pos: %zu-%zu\n" ,
170- name , token -> type , token -> pos_start , last_pos );
222+ printf ("[Lexer__commit] Commit token type: %d: name=%s, params : %zu , pos: %zu-%zu\n" ,
223+ token -> type , name , token -> param_len , token -> pos_start , last_pos );
171224 }
172225
173226 // Check if we need to reallocate the processed tokens array
@@ -181,27 +234,25 @@ static void lexer__commit(Lexer *lexer) {
181234 }
182235 }
183236
184- // Add the pending token to the processed tokens
185- // This creates a copy, so the pending token must
186- // be freed or reset later
237+ // Shallow copy the pending token to the processed tokens
187238 lexer -> processed [lexer -> processed_len ++ ] = * token ;
188- // Re-create the pending token
189- token_reset ( & lexer -> pendNode );
190- // lexer->pendNode = *token_create();
239+ // Create a new pending token for the next cycle
240+ // TODO :: check if this causes memory leaks !!
241+ lexer -> pendNode = * token_create ();
191242 lexer -> pendNode .pos_start = last_pos ;
192243 lexer -> pendNode .pos_end = last_pos ;
193244}
194245
195246static inline void lexer__commit_param (Lexer * lexer ) {
196247 token_param_append (& lexer -> pendNode , & lexer -> pendParam );
197248 // Re-create the pending parameter
198- // Maybe is should be reset instead?
249+ // TODO :: check if this causes memory leaks !!
199250 lexer -> pendParam = * param_create ();
200251}
201252
202253static inline void lexer__parse_one (Lexer * lexer , uint32_t curr , uint32_t prev ) {
203- // printf("i=%ld - STATE :: %u ;; new CHAR :: (%d) ;; prev CHAR :: (%d)\n",
204- // lexer->index, lexer->state, (int)curr, (int)prev);
254+ printf ("i=%ld - STATE :: %u ;; new CHAR :: (%d) ;; prev CHAR :: (%d)\n" ,
255+ lexer -> index , lexer -> state , (int )curr , (int )prev );
205256
206257 if (lexer -> state == STATE_RAW_TEXT ) {
207258 // Could this be the beginning of a new tag?
@@ -389,6 +440,7 @@ static inline void lexer__parse_one(Lexer *lexer, uint32_t curr, uint32_t prev)
389440 }
390441 // Is this a valid closing quote?
391442 else if (curr == value_0 && is_quote (curr ) && value_z != '\\' ) {
443+ param_val_append (& lexer -> pendParam , curr );
392444 lexer__commit_param (lexer );
393445 lexer__transition (lexer , STATE_INSIDE_TAG );
394446 }
0 commit comments