-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.c
175 lines (164 loc) · 4.23 KB
/
tokenizer.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
#include "tokenizer.h"
char default_get_char(void *data)
{
int v;
if (data) {
v = getc((FILE*) data);
} else {
v = getchar();
}
if (v == EOF) {
return 0;
}
return (char)v;
}
struct memory_get_char_data_s {
char *memory;
int len;
int pos;
};
char memory_get_char(void *in_data)
{
struct memory_get_char_data_s *data = in_data;
if (data->pos >= data->len) {
return '\0';
}
return data->memory[data->pos ++];
}
static void tokenizer_init(tokenizer_ctx_t *ctx, char (*get_char)(void*), void *data);
void tokenizer_init_stdio(tokenizer_ctx_t *ctx, FILE *fd) {
tokenizer_init(ctx, default_get_char, (void*)fd);
}
#if 0
void tokenizer_init_memory(tokenizer_ctx_t *ctx, char *memory, size_t len) {
tokenizer_init(
}
#endif
static void tokenizer_init(tokenizer_ctx_t *ctx, char (*get_char)(void*), void *data)
{
memset(ctx, 0, sizeof(*ctx));
ctx->get_char = get_char ? get_char : default_get_char;
ctx->get_char_data = data;
ctx->token_buf_size = 32;
ctx->token_buf = calloc(1, ctx->token_buf_size);
/* initialize look-ahead with whitespace */
ctx->next_char = 0;
}
#define is_whitespace(x) ((x) == ' ' || (x) == '\n' || (x) == ';')
int match_special(char *buf, int pos, int next)
{
static char *tokens[] = {
"(", ")", ",", "'", "`", "\"", " ", "\n", ",@", NULL
};
int ret = 0;
if (pos == 2) {
for (int i = 0; tokens[i]; ++i) {
if (!strncmp(tokens[i], buf, 2)) {
/* full match */
ret = 1;
break;
}
}
} else if (pos == 1) {
for (int i = 0; tokens[i]; ++i) {
if (strlen(tokens[i]) == 1 && buf[0] == *tokens[i]) {
/* semi match */
ret = 1;
} else if (strlen(tokens[i]) == 2 && buf[0] == *tokens[i] && ret) {
if (tokens[i][1] == next) {
/* will completed in next call */
ret = 0;
break;
}
}
}
}
if (pos && !ret) {
for (int i = 0; tokens[i]; ++i) {
if (next == *tokens[i]) {
ret = 1;
break;
}
}
}
return ret;
}
char *tokenizer_get_token(tokenizer_ctx_t *ctx)
{
for (;;) {
if (ctx->next_char != 0) {
if (ctx->token_buf_pos >= ctx->token_buf_size) {
ctx->token_buf_size *= 2;
ctx->token_buf = realloc(ctx->token_buf, ctx->token_buf_size);
}
ctx->token_buf[ctx->token_buf_pos ++] = ctx->next_char;
}
if (0 == (ctx->next_char = ctx->get_char(ctx->get_char_data))) {
if (ctx->token_buf_pos > 1) {
ctx->token_buf[ctx->token_buf_pos] = '\0';
ctx->token_buf_pos = 0;
break;
} else {
return NULL;
}
} else if (!ctx->str && ctx->token_buf_pos == 1 && *ctx->token_buf == '"') {
ctx->str = 1;
if (ctx->next_char == '\\') {
ctx->esc = 1;
ctx->next_char = 0;
}
} else if (ctx->str) {
/* handle esc */
if (ctx->esc == 1) {
ctx->esc = 0;
switch (ctx->next_char) {
case 'n':
ctx->next_char = '\n';
break;
case 'r':
ctx->next_char = '\r';
break;
case '"':
/* special */
ctx->esc = 2;
break;
default:
break;
}
} else {
if (ctx->token_buf[ctx->token_buf_pos-1] == '"') {
if (ctx->esc == 2) {
ctx->esc = 0;
} else {
ctx->str = 0;
ctx->token_buf[ctx->token_buf_pos] = '\0';
ctx->token_buf_pos = 0;
}
break;
} else if (ctx->next_char == '\\') {
ctx->esc = 1;
ctx->next_char = 0;
}
}
} else if (ctx->token_buf_pos == 1 && (ctx->com || is_whitespace(*ctx->token_buf))) {
ctx->token_buf_pos = 0;
if (*ctx->token_buf == ';') {
ctx->com = 1;
} else if (*ctx->token_buf == '\n') {
ctx->com = 0;
}
} else if (is_whitespace(ctx->next_char)) {
if (ctx->token_buf_pos) {
ctx->token_buf[ctx->token_buf_pos] = '\0';
ctx->token_buf_pos = 0;
break;
}
continue;
} else if (match_special(ctx->token_buf, ctx->token_buf_pos, ctx->next_char)) {
ctx->token_buf[ctx->token_buf_pos] = '\0';
ctx->token_buf_pos = 0;
break;
}
}
return ctx->token_buf;
}