-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.h
173 lines (170 loc) · 5.01 KB
/
lexer.h
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
static char *lexer_h_headers[] = {
"stddef.h"
};
static char *lexer_c_headers[] = {
"stdio.h",
"stdlib.h",
"string.h",
"unistd.h",
"fcntl.h"
};
static char lexer_h[] =
"typedef struct {\n"
" int fd;\n"
" int cur_buf;\n"
" char buf[8192];\n"
" char *symtab;\n"
" size_t max_bytes, num_bytes;\n"
" size_t buf_off, read_len;\n"
" size_t cur_line, cur_chr;\n"
"} lexer_t;\n"
"\n"
"typedef enum { LEX_ERROR = -1, LEX_SUCCESS = 0, LEX_EOF = 1 } lexer_res_t;\n"
"\n"
"lexer_t* lexer_create(const char *filename);\n"
"lexer_res_t lexer_next_tok(lexer_t *lex, lexeme_t *m);\n"
"void lexer_free(lexer_t *lex);\n";
static char lexer_c[] =
"lexer_t* lexer_create(const char *filename) {\n"
" lexer_t *lex;\n"
" int fd = -1;\n"
"\n"
" lex = malloc(sizeof(lexer_t));\n"
" if(!lex) {\n"
" perror(\"malloc\");\n"
" goto exit;\n"
" }\n"
" lex->symtab = NULL;\n"
"\n"
" fd = open(filename, O_RDONLY);\n"
" if(fd < 0) {\n"
" perror(\"open\");\n"
" goto exit;\n"
" }\n"
" lex->fd = fd;\n"
"\n"
" lex->cur_buf = 1;\n"
" lex->buf_off = 0;\n"
" lex->read_len = 0;\n"
" lex->cur_line = 1;\n"
" lex->cur_chr = 1;\n"
"\n"
" lex->max_bytes = 32;\n"
" lex->num_bytes = 0;\n"
" lex->symtab = malloc(lex->max_bytes);\n"
" if(!lex->symtab) {\n"
" perror(\"malloc\");\n"
" goto exit;\n"
" }\n"
"\n"
" return lex;\n"
"exit:\n"
" if(lex) {\n"
" if(lex->symtab) free(lex->symtab);\n"
" free(lex);\n"
" }\n"
" if(fd >= 0) close(fd);\n"
" return NULL;\n"
"}\n"
"\n"
"lexer_res_t lexer_next_tok(lexer_t *lex, lexeme_t *m) {\n"
" int cur_state = 0, next_state = -1;\n"
" int (*target)(lexeme_t*) = NULL;\n"
" int (*next_target)(lexeme_t*) = NULL;\n"
" size_t lexeme_len = 0;\n"
" size_t targ_off, targ_line, targ_chr;\n"
" int targ_buf;\n"
" char *buf = lex->buf + (lex->cur_buf*sizeof(lex->buf)/2);\n"
" unsigned char c = 0;\n"
" ssize_t read_len = 0;\n"
"\n"
" for(;;) {\n"
" for(size_t i = lex->buf_off; i < lex->read_len; i++) {\n"
" c = buf[i];\n"
"\n"
" if(next_target) {\n"
" target = next_target;\n"
" targ_buf = lex->cur_buf;\n"
" targ_off = i;\n"
" targ_line = lex->cur_line;\n"
" targ_chr = lex->cur_chr;\n"
" next_target = NULL;\n"
" }\n"
"\n"
" next_state = states[cur_state*255 + c];\n"
"\n"
" if(lex->num_bytes == lex->max_bytes) {\n"
" lex->max_bytes <<= 1;\n"
" char *tmp = realloc(lex->symtab, lex->max_bytes);\n"
" if(!tmp) {\n"
" perror(\"realloc\");\n"
" return LEX_ERROR;\n"
" }\n"
" lex->symtab = tmp;\n"
" }\n"
" lex->symtab[lex->num_bytes++] = next_state == 0 ? 0 : c;\n"
"\n"
" if(next_state == 0)\n"
" break;\n"
" next_target = targets[next_state];\n"
"\n"
" cur_state = next_state;\n"
" if(c == '\\n') {\n"
" lex->cur_line++;\n"
" lex->cur_chr = 0;\n"
" }\n"
" lex->cur_chr++;\n"
" lex->buf_off++;\n"
" lexeme_len++;\n"
" }\n"
"\n"
" if(lex->buf_off == lex->read_len) {\n"
" lex->cur_buf = !lex->cur_buf;\n"
" buf = lex->buf + (lex->cur_buf*sizeof(lex->buf)/2);\n"
" read_len = read(lex->fd, buf, sizeof(lex->buf)/2);\n"
" if(read_len < 0) {\n"
" perror(\"read\");\n"
" return LEX_ERROR;\n"
" }\n"
" lex->read_len = read_len;\n"
" lex->buf_off=0;\n"
" }\n"
"\n"
" if(next_state == 0 || lex->read_len == 0) {\n"
" if(target) {\n"
" lex->num_bytes = 0;\n"
" m->str = lex->symtab;\n"
" m->str_len = lexeme_len-1;\n"
" int class = target(m);\n"
" if(class < 0) {\n"
" return LEX_ERROR;\n"
" } else if(class == 0) {\n"
" cur_state = 0;\n"
" if(lex->read_len == 0)\n"
" return LEX_EOF;\n"
" } else {\n"
" m->class = class;\n"
" lex->cur_buf = targ_buf;\n"
" lex->buf_off = targ_off;\n"
" lex->cur_line = targ_line;\n"
" lex->cur_chr = targ_chr;\n"
" return LEX_SUCCESS;\n"
" }\n"
" target = NULL;\n"
" } else if(next_state == 0) {\n"
" fprintf(stderr, \"%lu:%lu unexpected %c\\n\", lex->cur_line, lex->cur_chr, c);\n"
" return LEX_ERROR;\n"
" } else {\n"
" return LEX_EOF;\n"
" }\n"
" }\n"
" }\n"
"\n"
" return LEX_ERROR;\n"
"}\n"
"\n"
"void lexer_free(lexer_t *lex) {\n"
" close(lex->fd);\n"
" free(lex->symtab);\n"
" free(lex);\n"
"}\n";