11#include <string.h>
2- #include <stdio.h>
32#include <stdlib.h>
43
5- #include <helpers/characters .h>
4+ #include <utils/chardef .h>
65
7- #include <lexer/actions.h>
8-
9- /**
10- * Creates a new lexer object for tokenizing the input string.
11- *
12- * @param input A pointer to the input string to be tokenized
13- *
14- * @return A pointer to the newly created lexer object
15- */
16- struct Lexer * new_lexer (char * input ) {
17- struct Lexer * lex = malloc (sizeof (struct Lexer ));
18-
19- lex -> input = input ;
20- lex -> position = 0 ;
21- lex -> read_position = 0 ;
22- lex -> ch = 0 ;
23- lex -> input_length = strlen (input );
24- lex -> line = 1 ;
25- lex -> column = 0 ;
26-
27- next_char (lex );
28-
29- return lex ;
30- }
6+ #include <lexer/lib.h>
317
328/**
339 * Advances the lexer to the next character in the input stream.
@@ -39,7 +15,7 @@ struct Lexer* new_lexer(char* input) {
3915 *
4016 * @return void
4117 */
42- void next_char (struct Lexer * lex ) {
18+ void __next_char (struct Lexer * lex ) {
4319 if (lex -> read_position >= lex -> input_length ) {
4420 lex -> ch = '\0' ;
4521 }
@@ -57,7 +33,15 @@ void next_char(struct Lexer* lex) {
5733 ++ lex -> read_position ;
5834}
5935
60- byte peek_next_char (struct Lexer * lexer ) {
36+ byte __peek_prev_char (struct Lexer * lexer ) {
37+ if (lexer -> read_position >= lexer -> input_length ) {
38+ return 0 ;
39+ }
40+
41+ return lexer -> input [lexer -> read_position - 2 ];
42+ }
43+
44+ byte __peek_next_char (struct Lexer * lexer ) {
6145 if (lexer -> read_position >= lexer -> input_length ) {
6246 return 0 ;
6347 }
@@ -72,11 +56,13 @@ byte peek_next_char(struct Lexer *lexer) {
7256 *
7357 * @return A dynamically allocated string containing the read sequence, or NULL if there was an error.
7458 */
75- char * read_sequence (struct Lexer * lex ) {
59+ char * __read_sequence (struct Lexer * lex ) {
7660 int position = lex -> position ;
7761
78- while (is_letter (lex -> ch ) || is_numeric (lex -> ch )) {
79- next_char (lex );
62+ while ((is_letter (lex -> ch ) || is_numeric (lex -> ch )) ||
63+ // signed numbers will match with this condition
64+ (lex -> ch == '-' && is_numeric (__peek_next_char (lex )))) {
65+ __next_char (lex );
8066 }
8167
8268 int length = lex -> position - position ;
@@ -87,32 +73,25 @@ char* read_sequence(struct Lexer *lex) {
8773 }
8874
8975 memcpy (result , lex -> input + position , length + 1 );
76+
77+ result [length ] = '\0' ;
9078
91- // The parser will throw an error if token->literal contains both letters and digits
92- if (is_letter (result [length ]) == false && is_numeric (result [length ]) == false) {
93- // result == `[x-byte]\0\0`
94- result [length ] = '\0' ;
95- // Remove the last null terminator
96- result = realloc (result , length );
97-
98- if (result == NULL ) {
99- return NULL ;
100- }
101- }
79+ // if (!is_valid_char_in_sequence(result[length])) {
80+ // result[length] = '\0';
81+ // }
10282
10383 return result ;
10484}
10585
106-
10786/**
10887 * Skips over whitespace characters in the lexer input stream.
10988 *
11089 * @param lex A pointer to a lexer objec
11190 *
11291 * @return void
11392 */
114- void jump_whitespace (struct Lexer * lex ) {
93+ void __jump_whitespace (struct Lexer * lex ) {
11594 while (lex -> ch == ' ' || lex -> ch == '\t' || lex -> ch == '\n' || lex -> ch == '\r' ) {
116- next_char (lex );
95+ __next_char (lex );
11796 }
11897}
0 commit comments