-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeScan.c
299 lines (260 loc) · 7.49 KB
/
SeScan.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
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
/*---------------------------------------------------------------------------+
| Free form input scanner
+---------------------------------------------------------------------------*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "SeParse.h"
#include "y.tab.h"
/*---------------------------------------------------------------------------+
| parse buffer - stores the string to be parsed (logical line)
+---------------------------------------------------------------------------*/
static unsigned parseBufLen;
static unsigned parseBufNextCh;
static char* parseBuf;
void scSetInputBuffer(line)
char *line ;
{
assert(line != 0);
parseBufNextCh = 0;
parseBufLen = strlen(line);
parseBuf = line;
}
int scGetChar() {
if (parseBufNextCh < parseBufLen) {
#ifdef TEST
printf("Reading char %c\n", parseBuf[parseBufNextCh]);
#endif
return parseBuf[parseBufNextCh++];
}
else
return EOF;
}
void scUngetChar(c)
int c;
{
if (parseBufNextCh > 0)
parseBuf[--parseBufNextCh] = c;
}
int scLook() {
if (parseBuf != 0)
return parseBuf[parseBufNextCh];
else
return EOF;
}
/*---------------------------------------------------------------------------+
| strBuf - stores word to be returned
+---------------------------------------------------------------------------*/
#define GROW_SIZE 100
static char *strBuf = 0;
static unsigned strBufSize = 0;
static unsigned strBufNextCh = 0;
static void _growBuf(size)
unsigned size;
{
if (strBuf == 0)
strBuf = (char *)malloc(strBufSize = size + GROW_SIZE);
else if (strBufSize < (strBufNextCh + size + 1)) {
strBufSize += (size + 1) + GROW_SIZE;
strBuf = (char *)realloc(strBuf, strBufSize);
}
}
static void _startWord() {
strBufNextCh = 0;
}
static void _addCh(ch)
int ch;
{
_growBuf(1);
strBuf[strBufNextCh++] = ch;
}
static char* _getWd() {
if (strBuf != 0) strBuf[strBufNextCh] = 0;
strBufNextCh = 0;
return strBuf;
}
/*---------------------------------------------------------------------------+
| scanner state
+---------------------------------------------------------------------------*/
enum SC_STATE {
SC_IN_WORD,
SC_IN_STR,
SC_OUTSIDE,
};
static ScanState = SC_OUTSIDE;
static ScanDelim = 0; /* Current string delimiter */
void NEW_STATE(st)
int st;
{
#ifdef TEST
static char* state_names[] = { "InWord", "InStr", "Out", "NONE" };
printf("**Entering state %s\n", state_names[st]);
#endif
ScanState = st;
}
#define C_ESC BACK_CHAR
#define C_SPACE ' '
#define C_SEP ';'
#define C_STR '"'
int cType(c)
int c;
{
if (isspace(c)) return C_SPACE;
switch (c) {
case CTRL_CHAR: case BACK_CHAR: return C_ESC;
case ';' : case ',' : case '(' : case ')' : return C_SEP;
case '"' : case '\'' : return C_STR;
default : return c;
}
}
int doEsc();
/*---------------------------------------------------------------------------+
| scNextWord breaks lines into sequences of words
|
| Args: recognize_seps - 1 == recognize and return "();,"
| 0 == ignore those and put them into words
| wd - pointer to last recognized word, = 0 if at end of line
|
| Returns: input token type (usually a representative char)
+---------------------------------------------------------------------------*/
int scNextWord(recognize_seps, wd)
int recognize_seps;
char **wd;
{
int c;
while ((c = scGetChar()) != EOF) {
switch (ScanState) {
case SC_OUTSIDE:
switch (cType(c)) {
case C_ESC: NEW_STATE(SC_IN_WORD); _startWord(); _addCh(doEsc(c)); break;
case C_SPACE: break;
case C_SEP: if (recognize_seps) return c;
else {
NEW_STATE(SC_IN_WORD); _startWord(); _addCh(c);
} break;
case C_STR: NEW_STATE(SC_IN_STR); _startWord(); ScanDelim = c; break;
default: NEW_STATE(SC_IN_WORD); _startWord(); _addCh(c); break;
}
break;
case SC_IN_STR:
switch (cType(c)) {
case C_STR:
if (ScanDelim == c) { NEW_STATE(SC_OUTSIDE); *wd = _getWd();
return WORD;
}
else _addCh(c); break;
case C_ESC: _addCh(doEsc(c)); break;
default: _addCh(c); break;
}
break;
case SC_IN_WORD:
switch (cType(c)) {
case C_ESC: _addCh(doEsc(c)); break;
case C_STR: NEW_STATE(SC_IN_STR); ScanDelim = c; break;
case C_SEP:
if (recognize_seps) {
ScanState = SC_OUTSIDE; *wd = _getWd(); scUngetChar(c);
return WORD;
}
else _addCh(c); break;
case C_SPACE: NEW_STATE(SC_OUTSIDE); *wd = _getWd(); return WORD;break;
default: _addCh(c); break;
}
break;
default: perror("Unknown state"); abort();
break;
}
}
switch(ScanState) {
case SC_IN_WORD: *wd = _getWd(); NEW_STATE(SC_OUTSIDE); return WORD;
case SC_IN_STR: *wd = _getWd(); NEW_STATE(SC_OUTSIDE); return WORD;
case SC_OUTSIDE: return 0;
default: perror("Unknown state"); abort();
}
}
/*---------------------------------------------------------------------------+
| getOct - read an octal number with up to 3 digits
+---------------------------------------------------------------------------*/
int getOct()
{
char intstr[4]; int next = 0;
int ch;
while ((ch = scGetChar()) != EOF && next <= 3)
if (isdigit(ch))
intstr[next++] = ch;
else if (ch != EOF) {
scUngetChar(ch);
break;
}
intstr[next]=0;
return strtol(intstr, NULL, 8);
}
/*---------------------------------------------------------------------------+
| Mapping table for the \[a-z] sequences: we let the C compiler initialize
| them for us... Of course we can change the value of a particular char if
| we feel like it.
+---------------------------------------------------------------------------*/
static char special[26] =
{ '\a', '\b', 'c', 'd', 'e', '\f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', '\n', 'o', 'p', 'q', '\r', 's', '\t', 'u', '\v', 'w', 'x',
'y', 'z'
};
/*---------------------------------------------------------------------------+
| doEsc - process esc (\) and control (^) sequences
+---------------------------------------------------------------------------*/
int doEsc(escChar)
int escChar;
{
int c = scGetChar();
switch (escChar) {
case BACK_CHAR:
if (c == EOF) return escChar;
else if (isdigit(c)) { scUngetChar(c); return getOct(); }
else if (islower(c)) return special[c - 'a'];
else return c;
break;
case CTRL_CHAR:
if (islower(c)) return c - 'a' + 1;
else if (isupper(c)) return c - 'A' + 1;
else { scUngetChar(c); return escChar; }
break;
default:
perror("Unknown escape sequence"); abort();
}
}
/*---------------------------------------------------------------------------+
| yylex - interface with yacc parser generator
+---------------------------------------------------------------------------*/
int yylex()
{
int state = scNextWord(1, &yylval.sval);
return state;
}
/*---------------------------------------------------------------------------+
| GetWord - return successive words in input line, 0 when finished
+---------------------------------------------------------------------------*/
char*
GetNextWord()
{
char *word;
if (scNextWord(0, &word) == 0) return "";
return word;
}
char*
GetFirstWord(line)
char *line;
{
scSetInputBuffer(line);
return GetNextWord();
}
#ifdef TEST
main()
{
scSetInputBuf("Just to see if we'\\'re \\n\\033 able to distinguish' words and strings
\"Also 'quotes' inside strings\" and 'strs \"inside quotes\"'
Not to forget ^S and ^q control ^ chars");
while (lGetWord() != 0);
}
#endif