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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
#include "lexer.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "expansion.h"
#include "utils.h"
#define NEWLINEESCAPE (in[lexer->pos] == '\\' && in[lexer->pos + 1] == '\n')
#define SEPARATORS " \n;&|<>()"
#define ISSEPARATOR(I) strchr(SEPARATORS, (int)I) != NULL
struct lexer *lexer_new(struct string *input)
{
struct lexer *new = malloc(sizeof(struct lexer));
new->input = input;
new->pos = 0;
new->processed = 0;
new->current_tok.value = NULL;
new->current_tok.type = TOKEN_EOF;
return new;
}
void lexer_free(struct lexer *lexer)
{
free(lexer);
}
static void yeet_comment(struct lexer *l)
{
while (l->input->data[l->pos] && l->input->data[l->pos] != '\n')
l->pos++;
}
static int create_ionumber(struct lexer *l)
{
struct string *str = string_create(NULL);
int i;
for (i = l->pos; isdigit(l->input->data[i]); i++)
string_pushc(str, l->input->data[i]);
if (l->input->data[i] == '<' || l->input->data[i] == '>')
{
l->current_tok.type = TOKEN_IONUMBER;
l->current_tok.value = str;
return 1;
}
else
{
string_free(str);
return 0;
}
}
static int look_for_next(char *in, int i, char c)
{
int escaped = 0;
while (in[i] && (in[i] != c || (escaped && in[i] == c)))
{
if (in[i] == '\\')
escaped ^= 1;
else
escaped = 0;
i++;
}
return i;
}
static struct string *substitution(char *in, int i)
{
char split = in[i];
in[i] = '\0';
struct string *new_input = string_create(in);
if (split == '$')
{
i++;
}
i++;
int error = 0;
struct string *substitute =
expand_substitution(in, &i, &error, (split == '`') ? '`' : ')');
if (error)
{
string_free(new_input);
return NULL;
}
string_catenate(new_input, substitute);
struct string *end_input = string_create(in + i + 1);
string_catenate(new_input, end_input);
return new_input;
}
static char extract_word(struct lexer *l, int *end)
{
char *in = l->input->data;
int i = l->pos;
// True if we are currently inside quotes
int escaped = 0;
while (in[i])
{
if (!escaped && ((in[i] == '$' && in[i + 1] == '(') || in[i] == '`'))
{
struct string *new_input = substitution(in, i);
if (new_input == NULL)
{
l->current_tok.type = TOKEN_ERROR;
return '\0';
}
string_free(l->input);
l->input = new_input;
in = l->input->data;
}
// Checking that we are NOT in a quote and have a separator
if (!escaped && ISSEPARATOR(in[i]))
break;
if (!escaped && in[i] == '\'')
{
i++;
while (in[i] && in[i] != '\'')
i++;
if (!in[i])
{
l->current_tok.type = TOKEN_ERROR;
return '\0';
}
}
else if (!escaped && in[i] == '"')
{
i = look_for_next(in, i + 1, in[i]);
if (!in[i])
{
l->current_tok.type = TOKEN_ERROR;
return '\0';
}
}
else if (in[i] == '\\')
{
escaped ^= 1;
}
else
escaped = 0;
i++;
}
char tmp = in[i];
in[i] = '\0';
*end = i;
return tmp;
}
static enum token_type word_or_ass(struct token t)
{
char *word = t.value->data;
if (isdigit(word[0]))
{
return TOKEN_WORD;
}
for (int i = 0; word[i]; i++)
{
if (word[i] == '=')
{
return TOKEN_ASS_WORD;
}
if (word[i] != '_' && !isalnum(word[i]))
{
return TOKEN_WORD;
}
}
return TOKEN_WORD;
}
static void lex_word(struct lexer *l)
{
int i = 0;
char tmp = extract_word(l, &i);
if (l->current_tok.type == TOKEN_ERROR)
return;
char *in = l->input->data;
l->current_tok.type = TOKEN_ERROR;
// Identifies reserved words
for (int n = 0; reserved_words[n].word != NULL; n++)
{
if (STRINGS_ARE_EQUAL(in + l->pos, reserved_words[n].word))
l->current_tok.type = reserved_words[n].type;
}
// If we couldn't identify a reserved word
if (l->current_tok.type == TOKEN_ERROR)
{
struct string *pp = string_create(in + l->pos);
if (pp == NULL)
{
l->current_tok.type = TOKEN_ERROR;
in[i] = tmp;
return;
}
l->current_tok.value = pp;
// Set the token type
l->current_tok.type = word_or_ass(l->current_tok);
}
in[i] = tmp;
}
static struct token set_ttype(struct lexer *lexer, enum token_type type)
{
lexer->current_tok.type = type;
return lexer->current_tok;
}
static struct token lex_and_or(struct lexer *l)
{
if (l->input->data[l->pos] == '|' && l->input->data[l->pos + 1] == '|')
l->current_tok.type = TOKEN_OR;
else if (l->input->data[l->pos] == '|' && l->input->data[l->pos + 1] != '|')
l->current_tok.type = TOKEN_PIPE;
else if (l->input->data[l->pos] == '&' && l->input->data[l->pos + 1] == '&')
l->current_tok.type = TOKEN_AND;
else
l->current_tok.type = TOKEN_ERROR;
return l->current_tok;
}
static struct token lex_redirect(struct lexer *l)
{
l->current_tok.type = TOKEN_REDIR;
struct string *val = (l->current_tok.value = string_create(NULL));
string_pushc(val, l->input->data[l->pos]);
if ((l->input->data[l->pos + 1] == '>' || l->input->data[l->pos + 1] == '&')
|| (l->input->data[l->pos] == '>' && l->input->data[l->pos + 1] == '|'))
string_pushc(val, l->input->data[l->pos + 1]);
return l->current_tok;
}
static void lex_special(struct lexer *l)
{
// If the first char is a digit and we recognized a number followed by a
// redir
if (isdigit(l->input->data[l->pos]) && create_ionumber(l))
// return immediately
return;
lex_word(l);
}
struct token lexer_next_token(struct lexer *lexer)
{
if (lexer->pos >= lexer->input->length)
{
lexer->current_tok.type = TOKEN_EOF;
lexer->current_tok.value = NULL;
return lexer->current_tok;
}
char *in = lexer->input->data;
for (; in[lexer->pos] && (in[lexer->pos] == ' ' || NEWLINEESCAPE);
lexer->pos++)
{
if (in[lexer->pos] == '\\' && in[lexer->pos + 1] == '\n')
lexer->pos++;
continue;
}
switch (in[lexer->pos])
{
case ';':
return set_ttype(lexer, TOKEN_SEMICOLON);
case '\n':
return set_ttype(lexer, TOKEN_NEWLINE);
case '\0':
return set_ttype(lexer, TOKEN_EOF);
case '(':
return set_ttype(lexer, TOKEN_PAR_LEFT);
case ')':
return set_ttype(lexer, TOKEN_PAR_RIGHT);
case '{':
return set_ttype(lexer, TOKEN_CURLY_LEFT);
case '}':
return set_ttype(lexer, TOKEN_CURLY_RIGHT);
case '|':
/* FALLTHROUGH */
case '&':
return lex_and_or(lexer);
case '<':
/* FALLTHROUGH */
case '>':
return lex_redirect(lexer);
case '#':
yeet_comment(lexer);
return lexer_next_token(lexer);
default:
lex_special(lexer);
return lexer->current_tok;
}
}
static void move_pos(struct lexer *lexer)
{
enum token_type t = lexer->current_tok.type;
if (t == TOKEN_EOF)
{
return;
}
if (t == TOKEN_IF || t == TOKEN_FI || t == TOKEN_IN || t == TOKEN_DO
|| t == TOKEN_OR || t == TOKEN_AND)
lexer->pos += 2;
else if (t == TOKEN_FOR)
lexer->pos += 3;
else if (t == TOKEN_ELSE || t == TOKEN_ELIF || t == TOKEN_THEN
|| t == TOKEN_DONE)
lexer->pos += 4;
else if (t == TOKEN_WHILE || t == TOKEN_UNTIL)
lexer->pos += 5;
else if (t == TOKEN_WORD || t == TOKEN_IONUMBER || t == TOKEN_REDIR
|| t == TOKEN_ASS_WORD)
lexer->pos += lexer->current_tok.value->length;
else
lexer->pos++;
}
struct token lexer_peek(struct lexer *lexer)
{
if (lexer->processed)
return lexer->current_tok;
lexer->processed = 1;
struct token res = lexer_next_token(lexer);
move_pos(lexer);
return res;
}
struct token lexer_pop(struct lexer *lexer)
{
struct token res;
if (lexer->processed)
{
res = lexer->current_tok;
lexer->processed = 0;
return res;
}
res = lexer_next_token(lexer);
move_pos(lexer);
lexer->processed = 0;
return res;
}
|