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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
// file : libbuild2/script/lexer.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <libbuild2/script/lexer.hxx>
#include <cstring> // strchr()
using namespace std;
namespace build2
{
namespace script
{
using type = token_type;
void lexer::
mode (base_mode m, char ps, optional<const char*> esc, uintptr_t data)
{
bool a (false); // attributes
const char* s1 (nullptr);
const char* s2 (nullptr);
bool s (true); // space
bool n (true); // newline
bool q (true); // quotes
if (!esc)
{
assert (!state_.empty ());
esc = state_.top ().escapes;
}
switch (m)
{
case lexer_mode::command_expansion:
{
// Note that whitespaces are not word separators in this mode.
//
s1 = "|&<>";
s2 = " ";
s = false;
break;
}
case lexer_mode::here_line_single:
{
// This one is like a single-quoted string except it treats
// newlines as a separator. We also treat quotes as literals.
//
// Note that it might be tempting to enable line continuation
// escapes. However, we will then have to also enable escaping of
// the backslash, which makes it a lot less tempting.
//
s1 = "\n";
s2 = " ";
esc = ""; // Disable escape sequences.
s = false;
q = false;
break;
}
case lexer_mode::here_line_double:
{
// This one is like a double-quoted string except it treats
// newlines as a separator. We also treat quotes as literals.
//
s1 = "$(\n";
s2 = " ";
s = false;
q = false;
break;
}
default:
{
// Make sure pair separators are only enabled where we expect
// them.
//
// @@ Should we disable pair separators in the eval mode?
//
assert (ps == '\0' ||
m == lexer_mode::eval ||
m == lexer_mode::attribute_value);
base_lexer::mode (m, ps, esc);
return;
}
}
assert (ps == '\0');
state_.push (state {m, data, nullopt, a, ps, s, n, q, *esc, s1, s2});
}
token lexer::
next ()
{
token r;
switch (state_.top ().mode)
{
case lexer_mode::command_expansion:
case lexer_mode::here_line_single:
case lexer_mode::here_line_double:
r = next_line ();
break;
default:
r = base_lexer::next ();
break;
}
if (r.qtype != quote_type::unquoted)
++quoted_;
return r;
}
token lexer::
next_line ()
{
bool sep (skip_spaces ().first);
xchar c (get ());
uint64_t ln (c.line), cn (c.column);
const state& st (state_.top ());
lexer_mode m (st.mode);
auto make_token = [&sep, &m, ln, cn] (type t)
{
bool q (m == lexer_mode::here_line_double);
return token (t, string (), sep,
(q ? quote_type::double_ : quote_type::unquoted), q,
ln, cn,
token_printer);
};
if (eos (c))
return make_token (type::eos);
// NOTE: remember to update mode() if adding new special characters.
if (m != lexer_mode::command_expansion)
{
switch (c)
{
case '\n':
{
sep = true; // Treat newline as always separated.
return make_token (type::newline);
}
}
}
if (m != lexer_mode::here_line_single)
{
switch (c)
{
// Variable expansion, function call, and evaluation context.
//
case '$': return make_token (type::dollar);
case '(': return make_token (type::lparen);
}
}
// Command operators.
//
if (m == lexer_mode::command_expansion)
{
if (optional<token> t = next_cmd_op (c, sep, m))
return move (*t);
}
// Otherwise it is a word.
//
unget (c);
return word (st, sep);
}
optional<token> lexer::
next_cmd_op (const xchar& c, bool sep, lexer_mode m)
{
auto make_token = [&sep, &m, &c] (type t, string v = string ())
{
bool q (m == lexer_mode::here_line_double);
return token (t, move (v), sep,
(q ? quote_type::double_ : quote_type::unquoted), q,
c.line, c.column,
token_printer);
};
auto make_token_with_modifiers =
[&make_token, this] (type t,
const char* mods, // To recorgnize.
const char* stop = nullptr) // To stop after.
{
string v;
if (mods != nullptr)
{
for (xchar p (peek ());
(strchr (mods, p) != nullptr && // Modifier.
strchr (v.c_str (), p) == nullptr); // Not already seen.
p = peek ())
{
get ();
v += p;
if (stop != nullptr && strchr (stop, p) != nullptr)
break;
}
}
return make_token (t, move (v));
};
switch (c)
{
// |, ||
//
case '|':
{
if (peek () == '|')
{
get ();
return make_token (type::log_or);
}
else
return make_token (type::pipe);
}
// &, &&
//
case '&':
{
xchar p (peek ());
if (p == '&')
{
get ();
return make_token (type::log_and);
}
// These modifiers are mutually exclusive so stop after seeing
// either one.
//
return make_token_with_modifiers (type::clean, "!?", "!?");
}
// <
//
case '<':
{
type r (type::in_str);
xchar p (peek ());
if (p == '|' || p == '-' || p == '<')
{
get ();
switch (p)
{
case '|': return make_token (type::in_pass);
case '-': return make_token (type::in_null);
case '<':
{
r = type::in_doc;
p = peek ();
if (p == '<')
{
get ();
r = type::in_file;
}
break;
}
}
}
// Handle modifiers.
//
const char* mods (nullptr);
switch (r)
{
case type::in_str:
case type::in_doc: mods = ":/"; break;
}
return make_token_with_modifiers (r, mods);
}
// >
//
case '>':
{
type r (type::out_str);
xchar p (peek ());
if (p == '|' || p == '-' || p == '!' || p == '&' ||
p == '=' || p == '+' || p == '>')
{
get ();
switch (p)
{
case '|': return make_token (type::out_pass);
case '-': return make_token (type::out_null);
case '!': return make_token (type::out_trace);
case '&': return make_token (type::out_merge);
case '=': return make_token (type::out_file_ovr);
case '+': return make_token (type::out_file_app);
case '>':
{
r = type::out_doc;
p = peek ();
if (p == '>')
{
get ();
r = type::out_file_cmp;
}
break;
}
}
}
// Handle modifiers.
//
const char* mods (nullptr);
const char* stop (nullptr);
switch (r)
{
case type::out_str:
case type::out_doc: mods = ":/~"; stop = "~"; break;
}
return make_token_with_modifiers (r, mods, stop);
}
}
return nullopt;
}
}
}
|