-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.c
177 lines (153 loc) · 3.91 KB
/
lexer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "token.c"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "error.c"
Error *error_type;
typedef struct LEXER_STRUCT
{
char c;
unsigned int i;
char *contents;
} lexer_T;
lexer_T *init_lexer(char *contents);
void lexer_advance(lexer_T *lexer);
void lexer_skip_whitespace(lexer_T *lexer);
token_T *lexer_get_next_token(lexer_T *lexer);
token_T *lexer_collect_string(lexer_T *lexer);
token_T *lexer_advance_with_token(lexer_T *lexer, token_T *token);
token_T *lexer_collect_id(lexer_T *lexer);
char *lexer_get_current_char_as_string(lexer_T *lexer);
token_T *lexer_collect_argument(lexer_T *lexer);
lexer_T *init_lexer(char *contents)
{
lexer_T *lexer = calloc(1, sizeof(lexer_T));
lexer->contents = contents;
lexer->i = 0;
lexer->c = contents[lexer->i];
return lexer;
}
void lexer_advance(lexer_T *lexer)
{
if (error_type->isError)
{
return;
}
if (lexer->c != '\0' && lexer->i < strlen(lexer->contents))
{
lexer->i++;
lexer->c = lexer->contents[lexer->i];
}
}
void lexer_skip_whitespace(lexer_T *lexer)
{
while (lexer->c == ' ')
{
lexer_advance(lexer);
}
}
token_T *lexer_get_next_token(lexer_T *lexer)
{
if (error_type->isError)
{
return NULL;
}
while (lexer->c != '\0' && lexer->i < strlen(lexer->contents))
{
// printf("lol2,%d\n", lexer->i);
if (error_type->isError)
{
return NULL;
}
if (lexer->c == ' ' || lexer->c == 10)
{
lexer_skip_whitespace(lexer);
}
if (lexer->c == 'x')
{
if (lexer->contents[lexer->i - 1] == '(' &&
(lexer->contents[lexer->i + 1] == ',' ||
lexer->contents[lexer->i + 1] == ')'))
{
return lexer_advance_with_token(
lexer, init_token(TOKEN_ARGUMENTS,
lexer_get_current_char_as_string(lexer)));
}
else if (lexer->contents[lexer->i + 1] == ')' &&
(lexer->contents[lexer->i - 1] == ',' ||
lexer->contents[lexer->i - 1] == '('))
{
return lexer_advance_with_token(
lexer, init_token(TOKEN_ARGUMENTS,
lexer_get_current_char_as_string(lexer)));
}
}
if (isdigit(lexer->c))
{
return lexer_collect_argument(lexer);
}
if (isalnum(lexer->c))
{
return lexer_collect_id(lexer);
}
switch (lexer->c)
{
case '(':
return lexer_advance_with_token(
lexer,
init_token(TOKEN_LPAREN, lexer_get_current_char_as_string(lexer)));
break;
case ')':
return lexer_advance_with_token(
lexer,
init_token(TOKEN_RPAREN, lexer_get_current_char_as_string(lexer)));
break;
case ',':
return lexer_advance_with_token(
lexer,
init_token(TOKEN_COMMA, lexer_get_current_char_as_string(lexer)));
break;
default:
define_error(error_type, lexer->i, "Unexpected token");
}
}
return NULL;
}
token_T *lexer_advance_with_token(lexer_T *lexer, token_T *token)
{
lexer_advance(lexer);
return token;
}
char *lexer_get_current_char_as_string(lexer_T *lexer)
{
char *str = calloc(2, sizeof(char));
str[0] = lexer->c;
str[1] = '\0';
return str;
}
token_T *lexer_collect_id(lexer_T *lexer)
{
char *value = calloc(1, sizeof(char));
value[0] = '\0';
while (isalnum(lexer->c))
{
char *s = lexer_get_current_char_as_string(lexer);
value = realloc(value, (strlen(value) + strlen(s) + 1) * sizeof(char));
strcat(value, s);
lexer_advance(lexer);
}
return init_token(TOKEN_FUNC, value);
}
token_T *lexer_collect_argument(lexer_T *lexer)
{
char *value = calloc(1, sizeof(char));
value[0] = '\0';
while (isalnum(lexer->c) || lexer->c == '.')
{
char *s = lexer_get_current_char_as_string(lexer);
value = realloc(value, (strlen(value) + strlen(s) + 1) * sizeof(char));
strcat(value, s);
lexer_advance(lexer);
}
return init_token(TOKEN_ARGUMENTS, value);
}