-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokens.c
258 lines (231 loc) · 6.46 KB
/
tokens.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
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
//
// Created by chenzhang on 08/01/2019.
//
#include "Python.h"
#include "grammar.h"
#include "tokenizer.h"
#include "errcode.h"
#include "main.h"
#include "vec.h"
const char* END_MARKER = "__END_MARKER_FOR_PARSER__";
extern grammar _PyParser_Grammar;
char* token2chars[] = {
"", // "ENDMARKER",
"<NAME>", // "NAME",
"<num>", // "NUMBER",
"<str>", // "STRING",
"<ENTER>", // "NEWLINE", // --- Use <ENTER>
"<IND>", // "INDENT", // --- Use <IND>
"<UNIND>", // "DEDENT", // --- Use <UNIND>
"(", // "LPAR",
")", // "RPAR",
"[", // "LSQB",
"]", // "RSQB",
":", // "COLON",
",", // "COMMA",
";", // "SEMI",
"+", // "PLUS",
"-", // "MINUS",
"*", // "STAR",
"/", // "SLASH",
"|", // "VBAR",
"&", // "AMPER",
"<", // "LESS",
">", // "GREATER",
"=", // "EQUAL",
".", // "DOT",
"%", // "PERCENT",
"`", // "BACKQUOTE",
"{", // "LBRACE",
"}", // "RBRACE",
"==", // "EQEQUAL",
"!=", // "NOTEQUAL",
"<=", // "LESSEQUAL",
">=", // "GREATEREQUAL",
"~", // "TILDE",
"^", // "CIRCUMFLEX",
"<<", // "LEFTSHIFT",
">>", // "RIGHTSHIFT",
"**", // "DOUBLESTAR",
"+=", // "PLUSEQUAL",
"-=", // "MINEQUAL",
"*=", // "STAREQUAL",
"/=", // "SLASHEQUAL",
"%=", // "PERCENTEQUAL",
"&=", // "AMPEREQUAL",
"|=", // "VBAREQUAL",
"^=", // "CIRCUMFLEXEQUAL",
"<<=", // "LEFTSHIFTEQUAL",
">>=", // "RIGHTSHIFTEQUAL",
"**=", // "DOUBLESTAREQUAL",
"//", // "DOUBLESLASH",
"//=", // "DOUBLESLASHEQUAL",
"@", // "AT",
/* This table must match the #defines in token.h! */
"<OP>", // "OP",
"<ERROR>", // "<ERRORTOKEN>",
"<N_TOKENS>", // "<N_TOKENS>"
};
int split_type_and_str(char* s, char** output) {
*output = malloc(strlen(s));
int type;
sscanf(s, "%d %s", &type, *output);
free(s);
return type;
}
int get_type_from_token(char* token) {
for (int i = 2; i <= 50; ++i) {
if (strcmp(token, token2chars[i]) == 0)
return i;
}
return 1;
}
static vec_str_t* add_token(struct tok_state *tok) {
vec_str_t* vec_str = (vec_str_t*)malloc(sizeof(vec_str_t));
vec_init(vec_str);
int started = 0;
for (;;) {
char *a, *b;
int type;
size_t len;
char *str;
type = PyTokenizer_Get(tok, &a, &b);
if (type == ERRORTOKEN) {
fprintf(stderr, "ERRORTOKEN\n");
// --- 添加 err_token
vec_push(vec_str ,strdup("52 <ERROR>"));
break;
}
if (type == ENDMARKER && started) {
type = NEWLINE; /* Add an extra newline */
started = 0;
/* Add the right number of dedent tokens,
except if a certain flag is given --
codeop.py uses this. */
if (tok->indent) {
tok->pendin = -tok->indent;
tok->indent = 0;
}
} else
started = 1;
if (type == ENDMARKER) {
break;
}
if (type == 1) {
len = b - a; /* XXX this may compute NULL - NULL */
str = (char *) PyObject_MALLOC(len + 1);
if (str == NULL) {
fprintf(stderr, "no mem for next token\n");
break;
}
if (len > 0)
strncpy(str, a, len);
str[len] = '\0';
}
else {
str = token2chars[type];
}
// --- 判定 ---
if (strcmp(str, END_MARKER) == 0) {
if (type == 1)
PyObject_Free(str);
break;
}
char *v = malloc(strlen(str) + 10);
sprintf(v, "%d %s", type, str);
if (type == 1)
PyObject_Free(str);
vec_push(vec_str, v);
}
PyTokenizer_Free(tok);
return vec_str;
}
vec_str_t* _tokenize(const char* code, int add_endmarker) {
size_t len = strlen(code);
char *str;
if (add_endmarker) {
str = (char *) PyObject_Malloc(len + strlen(END_MARKER) + 2);
strcpy(str, code);
char last = code[len - 1];
// 判定最后一个字符是否是空格/换行,如果不是则需要加一个空格
if (last != '\n' && last != ' ' && last != '\t') {
strcat(str, " ");
}
// 增加结束符
strcat(str, END_MARKER);
} else {
str = (char *) PyObject_Malloc(len + 1);
strcpy(str, code);
}
struct tok_state *tok;
if ((tok = PyTokenizer_FromString(str, 1)) == NULL) {
fprintf(stderr, "ERROR in PyTokenizer_FromString\n");
PyObject_Free(str);
return NULL;
}
// parse...
return add_token(tok);
}
char* tokenize(const char* code, int add_endmarker) {
vec_str_t* vec_str = _tokenize(code, add_endmarker);
// Calculate total size
size_t totalLen = 0;
for (int i = 0; i < vec_str->length; i++) {
char* s = vec_str->data[i];
totalLen += 2*strlen(s) + 1;
}
char* ans = malloc(totalLen);
ans[0]='\0';
int len = 0;
for (int i = 0; i < vec_str->length; i++) {
char *s = vec_str->data[i], *str;
split_type_and_str(s, &str);
size_t slen = strlen(str);
strcpy(ans+len, str);
len += slen;
ans[len++] = ' ';
ans[len] = '\0';
free(str);
}
vec_deinit(vec_str);
free(vec_str);
// printf("Allocated address: %p\n", (void*)ans);
return ans;
}
static int _isidentifier(int type, const char* token) {
if (type != NAME) return 0;
grammar* g = &_PyParser_Grammar;
int n = g->g_ll.ll_nlabels;
const char *s = token;
label *l = g->g_ll.ll_label;
int i;
// Check if it's a keyword
for (i = n; i > 0; i--, l++) {
if (l->lb_type != NAME || l->lb_str == NULL ||
l->lb_str[0] != s[0] ||
strcmp(l->lb_str, s) != 0)
continue;
return 0;
}
return 1;
}
int isidentifier(const char* token) {
vec_str_t* vec_str = _tokenize(token, 1);
int is = 1;
for (int i = 0; i < vec_str->length; ++i) {
char *s = vec_str->data[i], *str;
int type = split_type_and_str(s, &str);
if (i > 0 || !_isidentifier(type, str))
is = 0;
free(str);
}
vec_deinit(vec_str);
free(vec_str);
return is;
}
char* _get_token_info(int index) {
label* l = _PyParser_Grammar.g_ll.ll_label+index;
int type = l->lb_type;
char* str = l->lb_str;
return type >= NT_OFFSET ? NULL : str ? str : token2chars[type];
}