-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.cc
More file actions
265 lines (224 loc) · 6.49 KB
/
Token.cc
File metadata and controls
265 lines (224 loc) · 6.49 KB
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
// vim:sw=4:ai:aw:ts=4:
/** @file
* Implementation of the Token class.
*/
//
//static char sccsid[] = "@(#)Token.cc 1.5 AKK - 20060526";
//static char sccsid[] = "@(#)Token.cc 2.1 AKK - 20121130";
//
#include "asserts.h"
#include "Token.h"
#include <cctype> // C: character typing
#include <cstring> // C: string functions
// The complexity level of tokens recognized
Token::Level Token::level = Token::BASH; // highest level
// Function to set the desired token level
void Token::setLevel(Level new_level)
{
require( (Token::BASIC <= new_level) && (new_level <= Token::BASH) );
level = new_level;
}
// ========================================================
// Om conflicten met namen in de rest van het programma te
// vermijden zijn heel veel dingen "static" gemaakt, m.a.w.
// ze zijn alleen zichtbaar in deze source file.
// ========================================================
static
inline // Is C a whitespace character ?
bool isBlank(char c) { return ((c == ' ') | (c == '\t')); }
static
// The list of all characters that terminate a "word"
// which depends on the set of tokens recognized.
const char *specials[4] =
{
"\n\r&;|<> \t", // 0: basic
"\n\r()&;|<>$ \t", // 1: advanced
"\n\r(){}&;|<>$` \t", // 2: sh-script
"\n\r(){}&;|<>$` \t", // 3: bash/ksh-script
};
static
// Does C occur in the list of special characters ?
bool isSpecial(Token::Level level, char c)
{
return (strchr(specials[level], c) != 0);
}
static
// The mnemonics for the token types
// (WARNING: must stay in sync with the token_t enum !)
const char *tNames[] =
{
// == basic ==
"END",
"EOL",
"SEQUENCE",
"PIPE",
"INPUT",
"OUTPUT",
"APPEND",
"BACKGROUND",
// == advanced level ==
"AND",
"OR",
"SUBS",
"ENDS",
"VAR",
"EXPR",
// == sh level ==
"HERE",
"BQUOTE",
"BREAK",
"GROUP",
"GRPEND",
// == ksh/bash level ==
"EVAL",
"WORD"
};
// The global scanner function.
// Note: The "if (!is.get(c)) ..." construct used below
// is needed to handle "unexpected EOF" situations.
Token *Token::nextToken(istream& is)
{
string s;
char c = -1;
// Worth the trouble doing anything ?
if (!is)
return new Token(Token::END);
// Get one character (if possible)
if (!is.get(c))
return new Token(Token::END);
// Skip any white space ...
while (isBlank(c)) {
if (!is.get(c))
return new Token(Token::END);
}
// What have we got now ?
switch(c) { // First look for special cases
case '\n': case '\r':
return new Token(Token::EOL);
case ';': // Can be either ; or ;;
if (Token::level >= Token::SH) { // iff sh-script: could be ;;
if (!is.get(c)) // lookahead
return new Token(Token::SEQUENCE);
if (c == ';')
return new Token(Token::BREAK);
is.putback(c); // undo lookahead
}
return new Token(Token::SEQUENCE);
case '&': // Can be either & or &&
if (Token::level > Token::BASIC) { // iff advanced: could be &&
if (!is.get(c)) // lookahead
return new Token(Token::BACKGROUND);
if (c == '&')
return new Token(Token::AND);
is.putback(c); // undo lookahead
}
return new Token(Token::BACKGROUND);
case '|': // Can be either | or ||
if (Token::level > Token::BASIC) { // if advanced: could be ||
if (!is.get(c)) // lookahead
return new Token(Token::PIPE);
if (c == '|')
return new Token(Token::OR);
is.putback(c); // undo lookahead
}
return new Token(Token::PIPE);
case '>': // Can be either > or >>
if (!is.get(c)) // lookahead
return new Token(Token::OUTPUT);
if (c == '>')
return new Token(Token::APPEND);
is.putback(c); // undo lookahead
return new Token(Token::OUTPUT);
case '<': // Can be either < or <<
if (Token::level >= Token::SH) { // if sh-scripts: could be <<
if (!is.get(c)) // lookahead
return new Token(Token::INPUT);
if (c == '<')
return new Token(Token::HERE);
is.putback(c); // undo lookahead
}
return new Token(Token::INPUT);
case '(': // subshell begin
if (Token::level == Token::BASIC) // must at least be advanced level
break; // treat like ordinary char
return new Token(Token::SUBS);
case ')': // subshell end
if (Token::level == Token::BASIC) // must at least be advanced level
break; // treat like ordinary char
return new Token(Token::ENDS);
case '$': // variables: $something
if (Token::level == Token::BASIC) // must at least be advanced level
break; // treat like ordinary char
if (!is.get(c)) // lookahead
return new Token(Token::WORD, ""); // just a $ ?
if (Token::level >= Token::BASH) { // bash/ksh ?
// The $( pair is not a variable
if (c == '(')
return new Token(Token::EVAL);
}
// The following non-letter pairs are common
// $* $@ $# $0 ... $9 $? $! $$
if (strchr("*@#0123456789?!$", c) != NULL) {
s += c;
return new Token(Token::VAR, s);
}
// Having handled all the special cases,
// we now expect a true word (e.g. $PATH)
// which MUST begin with a letter.
if (!isalpha(c)) {
is.putback(c); // Can not use this one
return new Token(Token::WORD, ""); // just a $ ?
}
// Oke, this looks promissing ...
// Collect an alfanumeric string (a _ also counts as a letter)
do {
s += c;
if(!is.get(c))
return new Token(Token::VAR, s);
} while (isalnum(c) || (c == '_'));
is.putback(c); // undo lookahead
return new Token(Token::VAR, s);
case '{': // sh-script ? group begin
if (!(Token::level >= Token::SH))
break; // treat like ordinary char
return new Token(Token::GROUP);
case '}': // sh-script ? group end
if (!(Token::level >= Token::SH))
break; // treat like ordinary char
return new Token(Token::GRPEND);
case '`': // sh-script? (back-quotes)
if (!(Token::level >= Token::SH))
break; // treat like ordinary char
return new Token(Token::BQUOTE);
} // end special character switch
// Assume it is just a word;
// Read any text upto something special.
Token::token_t type = Token::WORD;
do {
s += c;
if (!is.get(c))
return new Token(type, s);
if (Token::level > Token::BASIC) { // advanced ?
// An embedded (simple) wildcard character ?
// MAYBE: Handle [...] as well
if (strchr("*?", c) != NULL) // yes
type = Token::EXPR; // change type from WORD to EXPR
}
} while (!isSpecial(Token::level, c));
is.putback(c); // undo lookahead
return new Token(type, s);
}
// The output operator for tokens
ostream& operator<<(ostream& os, const Token& t)
{
os << "Token: type="<<t.type << " ("<<tNames[t.type]<<")";
switch (t.type) { // with text ?
case Token::VAR:
case Token::EXPR:
case Token::WORD:
os << " '"<<t.text<<"'";
default:
break;
}
return os;
}