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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
// file : libbuild2/build/script/parser.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <libbuild2/build/script/parser.hxx>
#include <libbuild2/build/script/lexer.hxx>
#include <libbuild2/build/script/runner.hxx>
using namespace std;
namespace build2
{
namespace build
{
namespace script
{
using type = token_type;
//
// Pre-parse.
//
script parser::
pre_parse (istream& is, const path_name& pn, uint64_t line)
{
path_ = &pn;
pre_parse_ = true;
lexer l (is, *path_, line, lexer_mode::command_line);
set_lexer (&l);
script s;
script_ = &s;
runner_ = nullptr;
environment_ = nullptr;
s.start_loc = location (*path_, line, 1);
token t (pre_parse_script ());
assert (t.type == type::eos);
s.end_loc = get_location (t);
return s;
}
token parser::
pre_parse_script ()
{
// enter: next token is first token of the script
// leave: eos (returned)
token t;
type tt;
// Parse lines until we see eos.
//
for (;;)
{
// Start lexing each line.
//
tt = peek (lexer_mode::first_token);
// Determine the line type by peeking at the first token.
//
switch (tt)
{
case type::eos:
{
next (t, tt);
return t;
}
default:
{
pre_parse_line (t, tt);
assert (tt == type::newline);
break;
}
}
}
}
void parser::
pre_parse_line (token& t, type& tt, bool if_line)
{
// Determine the line type/start token.
//
line_type lt (
pre_parse_line_start (t, tt, lexer_mode::second_token));
line ln;
switch (lt)
{
case line_type::var:
{
// Check if we are trying to modify any of the special variables.
//
if (special_variable (t.value))
fail (t) << "attempt to set '" << t.value << "' special "
<< "variable";
// We don't pre-enter variables.
//
ln.var = nullptr;
next (t, tt); // Assignment kind.
mode (lexer_mode::variable_line);
parse_variable_line (t, tt);
if (tt != type::newline)
fail (t) << "expected newline instead of " << t;
break;
}
case line_type::cmd_elif:
case line_type::cmd_elifn:
case line_type::cmd_else:
case line_type::cmd_end:
{
if (!if_line)
{
fail (t) << lt << " without preceding 'if'";
}
}
// Fall through.
case line_type::cmd_if:
case line_type::cmd_ifn:
next (t, tt); // Skip to start of command.
// Fall through.
case line_type::cmd:
{
pair<command_expr, here_docs> p;
if (lt != line_type::cmd_else && lt != line_type::cmd_end)
p = parse_command_expr (t, tt, lexer::redirect_aliases);
if (tt != type::newline)
fail (t) << "expected newline instead of " << t;
parse_here_documents (t, tt, p);
break;
}
}
assert (tt == type::newline);
ln.type = lt;
ln.tokens = replay_data ();
script_->lines.push_back (move (ln));
if (lt == line_type::cmd_if || lt == line_type::cmd_ifn)
{
tt = peek (lexer_mode::first_token);
pre_parse_if_else (t, tt);
}
}
void parser::
pre_parse_if_else (token& t, type& tt)
{
// enter: peeked first token of next line (type in tt)
// leave: newline
// Parse lines until we see closing 'end'. Nested if-else blocks are
// handled recursively.
//
for (line_type bt (line_type::cmd_if); // Current block.
;
tt = peek (lexer_mode::first_token))
{
const location ll (get_location (peeked ()));
if (tt == type::eos)
fail (ll) << "expected closing 'end'";
// Parse one line. Note that this one line can still be multiple
// lines in case of if-else. In this case we want to view it as
// cmd_if, not cmd_end. Thus remember the start position of the
// next logical line.
//
size_t i (script_->lines.size ());
pre_parse_line (t, tt, true /* if_line */);
assert (tt == type::newline);
line_type lt (script_->lines[i].type);
// First take care of 'end'.
//
if (lt == line_type::cmd_end)
return;
// Check if-else block sequencing.
//
if (bt == line_type::cmd_else)
{
if (lt == line_type::cmd_else ||
lt == line_type::cmd_elif ||
lt == line_type::cmd_elifn)
fail (ll) << lt << " after " << bt;
}
// Update current if-else block.
//
switch (lt)
{
case line_type::cmd_elif:
case line_type::cmd_elifn: bt = line_type::cmd_elif; break;
case line_type::cmd_else: bt = line_type::cmd_else; break;
default: break;
}
}
}
command_expr parser::
parse_command_line (token& t, type& tt)
{
// enter: first token of the command line
// leave: <newline>
// Note: this one is only used during execution.
//
assert (!pre_parse_);
pair<command_expr, here_docs> p (
parse_command_expr (t, tt, lexer::redirect_aliases));
assert (tt == type::newline);
parse_here_documents (t, tt, p);
assert (tt == type::newline);
return move (p.first);
}
//
// Execute.
//
void parser::
execute (const scope& rs, const scope& bs,
environment& e, const script& s, runner& r)
{
path_ = nullptr; // Set by replays.
pre_parse_ = false;
set_lexer (nullptr);
// The script shouldn't be able to modify the scopes.
//
root_ = const_cast<scope*> (&rs);
scope_ = const_cast<scope*> (&bs);
pbase_ = scope_->src_path_;
script_ = const_cast<script*> (&s);
runner_ = &r;
environment_ = &e;
exec_script ();
}
void parser::
exec_script ()
{
const script& s (*script_);
runner_->enter (*environment_, s.start_loc);
// Note that we rely on "small function object" optimization for the
// exec_*() lambdas.
//
auto exec_set = [this] (const variable& var,
token& t, build2::script::token_type& tt,
const location&)
{
next (t, tt);
type kind (tt); // Assignment kind.
mode (lexer_mode::variable_line);
value rhs (parse_variable_line (t, tt));
assert (tt == type::newline);
// Assign.
//
value& lhs (kind == type::assign
? environment_->assign (var)
: environment_->append (var));
apply_value_attributes (&var, lhs, move (rhs), kind);
};
auto exec_cmd = [this] (token& t, build2::script::token_type& tt,
size_t li,
bool single,
const location& ll)
{
// We use the 0 index to signal that this is the only command.
//
if (single)
li = 0;
command_expr ce (
parse_command_line (t, static_cast<token_type&> (tt)));
runner_->run (*environment_, ce, li, ll);
};
auto exec_if = [this] (token& t, build2::script::token_type& tt,
size_t li,
const location& ll)
{
command_expr ce (
parse_command_line (t, static_cast<token_type&> (tt)));
// Assume if-else always involves multiple commands.
//
return runner_->run_if (*environment_, ce, li, ll);
};
size_t li (1);
exec_lines (s.lines.begin (), s.lines.end (),
exec_set, exec_cmd, exec_if,
li,
&environment_->var_pool);
runner_->leave (*environment_, s.end_loc);
}
// When add a special variable don't forget to update lexer::word().
//
bool parser::
special_variable (const string& n) noexcept
{
return n == ">" || n == "<" || n == "~";
}
lookup parser::
lookup_variable (name&& qual, string&& name, const location& loc)
{
// In the pre-parse mode collect the referenced variable names for the
// script semantics change tracking.
//
if (pre_parse_)
{
// Add the variable name skipping special variables and suppressing
// duplicates. While at it, check if the script temporary directory
// is referenced and set the flag, if that's the case.
//
if (special_variable (name))
{
if (name == "~")
script_->temp_dir = true;
}
else if (!name.empty ())
{
auto& vars (script_->vars);
if (find (vars.begin (), vars.end (), name) == vars.end ())
vars.push_back (move (name));
}
return lookup ();
}
if (!qual.empty ())
fail (loc) << "qualified variable name";
lookup r (environment_->lookup (name));
// Fail if non-script-local variable with an untracked name.
//
if (r.defined () && !r.belongs (*environment_))
{
const auto& vars (script_->vars);
if (find (vars.begin (), vars.end (), name) == vars.end ())
fail (loc) << "use of untracked variable '" << name << "'";
}
return r;
}
}
}
}
|