aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/build/script/parser.hxx
blob: 362c8344d2f7b1de5967fd35154b69a587e7ae91 (plain)
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
// file      : libbuild2/build/script/parser.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef LIBBUILD2_BUILD_SCRIPT_PARSER_HXX
#define LIBBUILD2_BUILD_SCRIPT_PARSER_HXX

#include <libbuild2/types.hxx>
#include <libbuild2/forward.hxx>
#include <libbuild2/utility.hxx>

#include <libbuild2/diagnostics.hxx>

#include <libbuild2/script/parser.hxx>

#include <libbuild2/build/script/token.hxx>
#include <libbuild2/build/script/script.hxx>

namespace build2
{
  namespace build
  {
    namespace script
    {
      class runner;

      class parser: public build2::script::parser
      {
        // Pre-parse. Issue diagnostics and throw failed in case of an error.
        //
      public:
        parser (context& c): build2::script::parser (c, false /* relex */) {}

        // Note that the returned script object references the passed path
        // name.
        //
        // Note also that we use the scope to lookup variable values while
        // trying to deduce the low verbosity diagnostics name (see code
        // around pre_parse_suspended for details). But that means we may
        // derive such a name based on the wrong value. This can happen if the
        // expanded variable value is reset after the recipe has been
        // pre-parsed or if such a value is set on the target (which is where
        // we start when looking up variables during the real parse). The
        // current thinking is that a remote possibility of this happening is
        // acceptable in this situation -- the worst that can happen is that
        // we will end up with mismatching diagnostics.
        //
        script
        pre_parse (const scope&,
                   const target_type&,
                   const small_vector<action, 1>&,
                   istream&, const path_name&, uint64_t line,
                   optional<string> diag_name, const location& diag_loc);

        // Recursive descent parser.
        //
        // Usually (but not always) parse functions receive the token/type
        // from which it should start consuming and in return the token/type
        // should contain the first token that has not been consumed.
        //
        // Functions that are called parse_*() rather than pre_parse_*() are
        // used for both stages.
        //
      protected:
        token
        pre_parse_script ();

        void
        pre_parse_line (token&, token_type&, bool if_line = false);

        void
        pre_parse_if_else (token&, token_type&);

        command_expr
        parse_command_line (token&, token_type&);

        // Execute. Issue diagnostics and throw failed in case of an error.
        //
      public:

        // By default call the runner's enter() and leave() functions that
        // initialize/clean up the environment before/after the script
        // execution.
        //
        // Note: having both root and base scopes for testing (where we pass
        // global scope for both).
        //
        void
        execute_body (const scope& root, const scope& base,
                      environment&, const script&, runner&,
                      bool enter = true, bool leave = true);


        // Execute the first or the second (dyndep) half of the depdb
        // preamble.
        //
        // Note that it's the caller's responsibility to make sure that the
        // runner's enter() function is called before the first preamble/body
        // command execution and leave() -- after the last command.
        //
        void
        execute_depdb_preamble (action a, const scope& base, const file& t,
                                environment& e, const script& s, runner& r,
                                depdb& dd)
        {
          auto b (s.depdb_preamble.begin ());
          exec_depdb_preamble (
            a, base, t,
            e, s, r,
            b,
            (s.depdb_dyndep
             ? b + *s.depdb_dyndep
             : s.depdb_preamble.end ()),
            dd);
        }

        void
        execute_depdb_preamble_dyndep (
          action a, const scope& base, file& t,
          environment& e, const script& s, runner& r,
          depdb& dd, bool& update, timestamp mt, bool& deferred_failure)
        {
          exec_depdb_preamble (
            a, base, t,
            e, s, r,
            s.depdb_preamble.begin () + *s.depdb_dyndep,
            s.depdb_preamble.end (),
            dd, &update, mt, &deferred_failure);
        }

        // This version doesn't actually execute the depdb-dyndep builtin (but
        // may execute some variable assignments) instead returning all the
        // information (extracted from options) necessary to implement the
        // depdb-dyndep --byproduct logic (which fits better into the rule
        // implementation).
        //
        enum class dyndep_format {make};

        struct dyndep_byproduct
        {
          location_value     location;
          dyndep_format      format;
          optional<dir_path> cwd;
          path               file;
          string             what;
          const target_type* default_type;
          bool               drop_cycles;
        };

        dyndep_byproduct
        execute_depdb_preamble_dyndep_byproduct (
          action a, const scope& base, const file& t,
          environment& e, const script& s, runner& r,
          depdb& dd, bool& update, timestamp mt)
        {
          // This is getting a bit ugly (we also don't really need to pass
          // depdb here). One day we will find a better way...
          //
          bool deferred_failure; // Dymmy.

          dyndep_byproduct v;
          exec_depdb_preamble (
            a, base, t,
            e, s, r,
            s.depdb_preamble.begin () + *s.depdb_dyndep,
            s.depdb_preamble.end (),
            dd, &update, mt, &deferred_failure, &v);
          return v;
        }

        // Parse a special builtin line into names, performing the variable
        // and pattern expansions. If omit_builtin is true, then omit the
        // builtin name from the result.
        //
        names
        execute_special (const scope& root, const scope& base,
                         environment&,
                         const line&,
                         bool omit_builtin = true);

      protected:
        // Setup the parser for subsequent exec_*() function calls.
        //
        void
        pre_exec (const scope& root, const scope& base,
                  environment&, const script*, runner*);

        using lines_iterator = lines::const_iterator;

        void
        exec_lines (lines_iterator, lines_iterator,
                    const function<exec_cmd_function>&);

        void
        exec_lines (const lines& l, const function<exec_cmd_function>& c)
        {
          exec_lines (l.begin (), l.end (), c);
        }

        names
        exec_special (token&, build2::script::token_type&, bool skip_first);

        void
        exec_depdb_preamble (action, const scope& base, const file&,
                             environment&, const script&, runner&,
                             lines_iterator begin, lines_iterator end,
                             depdb&,
                             bool* update = nullptr,
                             optional<timestamp> mt = nullopt,
                             bool* deferred_failure = nullptr,
                             dyndep_byproduct* = nullptr);

        void
        exec_depdb_dyndep (token&, build2::script::token_type&,
                           size_t line_index, const location&,
                           action, const scope& base, file&,
                           depdb&,
                           bool& update,
                           timestamp,
                           bool& deferred_failure,
                           dyndep_byproduct*);

        // Helpers.
        //
      public:
        static bool
        special_variable (const string&) noexcept;

        // Customization hooks.
        //
      protected:
        virtual lookup
        lookup_variable (name&&, string&&, const location&) override;

        virtual void
        lookup_function (string&&, const location&) override;

        // During execution translate the process path and executable targets
        // leaving the rest for the base parser to handle.
        //
        // During pre-parsing try to deduce the low-verbosity script
        // diagnostics name as a program/builtin name or obtain the custom
        // low-verbosity diagnostics specified with the diag builtin. Also
        // handle the depdb builtin calls.
        //
        // Note that the diag and depdb builtins can only appear at the
        // beginning of the command line.
        //
        virtual optional<process_path>
        parse_program (token&, build2::script::token_type&,
                       bool first, bool env,
                       names&, parse_names_result&) override;

      protected:
        script* script_;
        const small_vector<action, 1>* actions_; // Non-NULL during pre-parse.

        // True if this script is for file-based targets and performing update
        // is one of the actions, respectively. Only set for the pre-parse
        // mode.
        //
        bool file_based_;
        bool perform_update_;

        // Current low-verbosity script diagnostics and its weight.
        //
        // During pre-parsing each command leading names are translated into a
        // potential low-verbosity script diagnostics name, unless the
        // diagnostics is set manually (script name via the constructor or
        // custom diagnostics via the diag builtin). The potential script
        // name has a weight associated with it, so script names with greater
        // weights override names with lesser weights. The possible weights
        // are:
        //
        // 0     - builtins that do not add to the script semantics (exit,
        //         true, etc) and are never picked up as a script name
        //
        // [1 2] - other builtins
        //
        // 3     - process path or executable target
        //
        // 4     - manually set names
        //
        // If two potential script names with the same weights are encountered
        // then this ambiguity is reported unless a higher-weighted name is
        // encountered later.
        //
        // If the diag builtin is encountered, then its whole line is saved
        // (including the leading 'diag' word) for later execution and the
        // diagnostics weight is set to 4.
        //
        // Any attempt to manually set the custom diagnostics twice (the diag
        // builtin after the script name or after another diag builtin) is
        // reported as ambiguity.
        //
        // At the end of pre-parsing either diag_name_ or diag_line_ (but not
        // both) are present.
        //
        optional<pair<string, location>> diag_name_;
        optional<pair<string, location>> diag_name2_; // Ambiguous script name.
        optional<pair<line, location>>   diag_line_;
        uint8_t                          diag_weight_ = 0;

        // Custom dependency change tracking.
        //
        // The depdb builtin can be used to change the default dependency
        // change tracking:
        //
        // depdb clear           - Cancel the default variables, targets, and
        //                         prerequisites change tracking. Can only be
        //                         the first depdb builtin call.
        //
        // depdb hash <args>     - Track the argument list change as a hash.
        //
        // depdb string <arg>    - Track the argument (single) change as string.
        //
        // depdb env <var-names> - Track the environment variables change as a
        //                         hash.
        //
        // depdb dyndep ...      - Extract dynamic dependency information. Can
        //                         only be the last depdb builtin call in the
        //                         preamble. Note that such dependencies don't
        //                         end up in $<. We also don't cause clean of
        //                         such dependencies (since there may be no .d
        //                         file) -- they should also be listed as
        //                         static prerequisites of some other target
        //                         (e.g., lib{} for headers) or a custom clean
        //                         recipe should be provided.
        //
        //
        optional<location> depdb_clear_;    // depdb-clear location.
        optional<pair<location, size_t>>
                           depdb_dyndep_;   // depdb-dyndep location/position.
        bool               depdb_dyndep_byproduct_ = false; // --byproduct
        lines              depdb_preamble_; // Note: excluding depdb-clear.

        // If present, the first impure function called in the body of the
        // script that performs update of a file-based target.
        //
        // Note that during the line pre-parsing we cannot tell if this is a
        // body or depdb preamble line. Thus, if we encounter an impure
        // function call we just save its name/location and postpone the
        // potential failure till the end of the script pre-parsing, if it
        // turns out to be a body line.
        //
        optional<pair<string, location>> impure_func_;

        // True during pre-parsing when the pre-parse mode is temporarily
        // suspended to perform expansion.
        //
        bool pre_parse_suspended_ = false;

        // The alternative location where the next line should be saved.
        //
        // Before the script line gets parsed, it is set to a temporary value
        // that will by default be appended to the script. However,
        // parse_program() can point it to a different location where the line
        // should be saved instead (e.g., diag_line_, etc) or set it to NULL
        // if the line is handled in an ad-hoc way and should be dropped
        // (e.g., depdb_clear_, etc).
        //
        line* save_line_;

        // The if-else nesting level (and in the future for other flow
        // control constructs).
        //
        // Maintained during pre-parsing and is incremented when the cmd_if or
        // cmd_ifn lines are encountered, which in particular means that it is
        // already incremented by the time the if-condition expression is
        // pre-parsed. Decremented when the cmd_end line is encountered.
        //
        size_t level_ = 0;

        // Execute state.
        //
        runner* runner_;
        environment* environment_;
      };
    }
  }
}

#endif // LIBBUILD2_BUILD_SCRIPT_PARSER_HXX