-
Notifications
You must be signed in to change notification settings - Fork 7
/
token.c
208 lines (193 loc) · 4.52 KB
/
token.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <token.h>
#include <text.h>
#include <macro.h>
#include <lib.h>
char *yytext = NULL;
int yyleng = 0;
static unsigned int inmacro = 0;
static char *macrotext;
static char *macropos;
void mark_start(void)
{
if (inmacro)
yytext = macropos;
else
yytext = text_getpos();
yyleng = 0;
}
void mark_end(void)
{
if (inmacro)
yyleng = macropos - yytext;
else
yyleng = text_getpos() - yytext;
}
int token_getchar(void)
{
/* output expanded macro char */
if (inmacro) {
if (macropos < macrotext) {
macropos = macrotext;
return *macropos;
}
if (*macropos)
return *macropos++;
inmacro = 0;
macropos = NULL;
mark_start(); /* remark */
}
return text_getchar();
}
static int back_token_inescape = 0;
/* handle `\realchar` */
int token_getrealchar(void)
{
int realchar, c;
/* remark start position */
mark_start();
/* get real char */
realchar = token_getchar();
/* handle special char */
switch (realchar) {
case 'n':
c = '\n';
break;
case 't':
c = '\t';
break;
case '\\':
c = '\\';
break;
defalut:
/* we ignore escape characher for other char */
c = realchar;
break;
}
/* for back_token() */
back_token_inescape = 1;
/*
* Reset real char!
* Should we restore its original char?
* We will restore it in back_token().
*/
*yytext = c;
return c;
}
/* expand {macro} */
void token_expand_macro(void)
{
static char macro[128];
int c, len;
if (inmacro)
text_errx("recursive macro expand");
len = 0;
while (len < 128) {
c = text_getchar();
/* macro end */
if (c == '}')
break;
if (!ismacrochar(c))
text_errx("invalid macro char");
macro[len] = c;
len++;
}
if (len >= 128)
text_errx("macro name is too long");
macro[len] = '\0';
macrotext = expand_macro(macro);
if (!macrotext)
text_errx("macro is not existed");
inmacro = 1;
macropos = macrotext;
}
/* map char to token */
static const int tokenmap[128] = {
/* 0 '\0' SOH STX ETX ENQ ACK '\a' '\b' */
L, L, L, L, L, L, L, L,
/* 8 '\b' '\t' '\n' '\v' '\f' '\r' SO SI */
L, SPACE, EOL, SPACE, L, L, L, L,
/* 16 DLE DC1 DC2 DC3 DC4 NAK SYN ETB */
L, L, L, L, L, L, L, L,
/* 24 CAN EM SUB ESC FS GS RS US */
L, L, L, L, L, L, L, L,
/* 32 ' ' ! " # $ % & ' */
SPACE, L, L, L, DOLLAR, L, L, L,
/* 40 ( ) * + - . ´ */
LP, RP, AST, ADD, L, DASH, DOT, L,
/* 48 0 1 2 3 4 5 6 7 */
L, L, L, L, L, L, L, L,
/* 56 8 9 : ; < = > ? */
L, L, L, L, L, L, L, QST,
/* 64 @ A B C D E F G */
L, L, L, L, L, L, L, L,
/* 72 H I J K L M N O */
L, L, L, L, L, L, L, L,
/* 80 P Q R S T U V W */
L, L, L, L, L, L, L, L,
/* 88 X Y Z [ \ ] ^ _ */
L, L, L, LSB, L, RSB, UPA, L,
/* 96 ` a b c d e f g */
L, L, L, L, L, L, L, L,
/* 104 h i j k l m n o */
L, L, L, L, L, L, L, L,
/* 112 p q r s t u v w */
L, L, L, L, L, L, L, L,
/* 120 x y z { | } ~ DEL */
L, L, L, LCP, OR, RCP, L, L,
};
token_t get_token(void)
{
static unsigned int inquota = 0; /* "in quotation" */
int c;
token_t type;
restart:
mark_start();
c = token_getchar();
/* handle "quato" */
if (c == '"') {
inquota = ~inquota;
goto restart;
}
/* handle {macro} */
if (c == '{' && !inquota) {
token_expand_macro();
goto restart;
}
type = L;
/*
* if we back token(escape char) last,
* we should take care here!
*/
if (back_token_inescape == 1) {
/* token_getrealchar -> (now)get_token */
back_token_inescape = 0;
} else if (back_token_inescape == 2) {
/* token_getrealchar -> back_token -> (now)get_token */
back_token_inescape = 1;
goto end;
}
if (!inquota) {
/* hansle escape character \ */
if (c == '\\')
c = token_getrealchar();
else
type = (c == EOF) ? _EOF : tokenmap[c];
}
end:
mark_end();
return type;
}
void back_token(void)
{
if (inmacro)
macropos--;
else
text_backchar();
/* backed token is escape char, e.g `\n` */
if (back_token_inescape == 1)
back_token_inescape = 2;
}