aboutsummaryrefslogtreecommitdiff
path: root/build/parser.cxx
blob: 289e5714e766394c2dbbac8178de238b525acedc (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
383
384
385
386
387
// file      : build/parser.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <build/parser>

#include <memory>   // unique_ptr
#include <iostream>

#include <build/token>
#include <build/lexer>

#include <build/scope>
#include <build/target>
#include <build/prerequisite>

using namespace std;

namespace build
{
  // Output the token type and value in a format suitable for diagnostics.
  //
  ostream&
  operator<< (ostream&, const token&);

  typedef token_type type;

  void parser::
  parse (istream& is, const path& p, scope& s)
  {
    lexer l (is, p.string (), diag_);
    lexer_ = &l;
    path_ = &p;
    scope_ = &s;

    token t (type::eos, 0, 0);
    type tt;
    next (t, tt);

    parse_clause (t, tt);

    if (tt != type::eos)
    {
      error (t) << "unexpected " << t << endl;
      throw parser_error ();
    }
  }

  void parser::
  parse_clause (token& t, token_type& tt)
  {
    while (tt != type::eos)
    {
      // We always start with one or more names.
      //
      if (tt != type::name && tt != type::lcbrace)
        break; // Something else. Let our caller handle that.

      names tns (parse_names (t, tt));

      if (tt == type::colon)
      {
        next (t, tt);

        // Dependency declaration.
        //
        if (tt == type::name || tt == type::lcbrace)
        {
          names pns (parse_names (t, tt));

          // Prepare the prerequisite list.
          //
          target::prerequisites_type ps;
          ps.reserve (pns.size ());

          for (auto& pn: pns)
          {
            // Resolve prerequisite type.
            //
            //@@ TODO: derive type from extension, factor to common function
            //
            const char* tt (pn.type.empty () ? "file" : pn.type.c_str ());

            auto i (target_types.find (tt));

            if (i == target_types.end ())
            {
              //@@ TODO name (or better yet, type) location

              error (t) << "unknown prerequisite type '" << tt << "'" << endl;
              throw parser_error ();
            }

            const target_type& ti (i->second);

            // We need to split the name into its directory part (if any)
            // and the name part. We cannot assume the name part is a
            // valid filesystem name so we will have to do the splitting
            // manually.
            //
            path d;
            string n;

            {
              path::size_type i (path::traits::rfind_separator (pn.name));

              if (i == string::npos)
                n = move (pn.name); // NOTE: steal!
              else
              {
                d = path (pn.name, i);
                n.assign (pn.name, i + 1, string::npos);
                d.normalize ();
              }
            }

            //cout << "prerequisite " << tt << " " << n << " " << d << endl;

            // Find or insert.
            //
            auto r (scope_->prerequisites.emplace (
                      ti, move (n), move (d), *scope_));

            ps.push_back (const_cast<prerequisite&> (*r.first));
          }

          for (auto& tn: tns)
          {
            path d;
            string n;

            // The same deal as in handling prerequisites above.
            //
            {
              path::size_type i (path::traits::rfind_separator (tn.name));

              if (i == string::npos)
              {
                d = scope_->path (); // Already normalized.
                n = move (tn.name); // NOTE: steal!
              }
              else
              {
                d = path (tn.name, i);
                n.assign (tn.name, i + 1, string::npos);

                if (d.relative ())
                  d = scope_->path () / d;

                d.normalize ();
              }
            }

            // Resolve target type.
            //
            //@@ TODO: derive type from extension
            //
            const char* tt (tn.type.empty () ? "file" : tn.type.c_str ());

            auto i (target_types.find (tt));

            if (i == target_types.end ())
            {
              //@@ TODO name (or better yet, type) location

              error (t) << "unknown target type '" << tt << "'" << endl;
              throw parser_error ();
            }

            const target_type& ti (i->second);

            //@@ TODO would be nice to first check if this target is
            //   already in the set before allocating a new instance.

            //cout << "target " << tt << " " << n << " " << d << endl;

            // Find or insert.
            //
            auto r (
              targets.emplace (
                unique_ptr<target> (ti.factory (move (n), move (d)))));

            target& t (**r.first);

            t.prerequisites = ps; //@@ TODO: move is last target.

            if (default_target == nullptr)
              default_target = &t;
          }

          if (tt == type::newline)
            next (t, tt);
          else if (tt != type::eos)
          {
            error (t) << "expected newline instead of " << t << endl;
            throw parser_error ();
          }

          continue;
        }

        if (tt == type::newline)
        {
          // See if we have a directory/target scope.
          //
          if (next (t, tt) == type::lcbrace)
          {
            // Should be on its own line.
            //
            if (next (t, tt) != type::newline)
            {
              error (t) << "expected newline after '{'" << endl;
              throw parser_error ();
            }

            // See if this is a directory or target scope. Different
            // things can appear inside depending on which one it is.
            //
            bool dir (false);
            for (const auto& n: tns)
            {
              if (n.type.empty () && n.name.back () == '/')
              {
                if (tns.size () != 1)
                {
                  // @@ TODO: point to name.
                  //
                  error (t) << "multiple names in directory scope" << endl;
                  throw parser_error ();
                }

                dir = true;
              }
            }

            next (t, tt);

            if (dir)
            {
              scope& prev (*scope_);
              path p (tns[0].name);

              if (p.relative ())
                p = prev.path () / p;

              p.normalize ();
              scope_ = &scopes[p];

              // A directory scope can contain anything that a top level can.
              //
              parse_clause (t, tt);

              scope_ = &prev;
            }
            else
            {
              // @@ TODO: target scope.
            }

            if (tt != type::rcbrace)
            {
              error (t) << "expected '}' instead of " << t << endl;
              throw parser_error ();
            }

            // Should be on its own line.
            //
            if (next (t, tt) == type::newline)
              next (t, tt);
            else if (tt != type::eos)
            {
              error (t) << "expected newline after '}'" << endl;
              throw parser_error ();
            }
          }

          continue;
        }

        if (tt == type::eos)
          continue;

        error (t) << "expected newline insetad of " << t << endl;
        throw parser_error ();
      }

      error (t) << "unexpected " << t << endl;
      throw parser_error ();
    }
  }

  void parser::
  parse_names (token& t, type& tt, names& ns, const string* tp)
  {
    for (bool first (true);; first = false)
    {
      // Untyped name group, e.g., '{foo bar}'.
      //
      if (tt == type::lcbrace)
      {
        next (t, tt);
        parse_names (t, tt, ns, tp);

        if (tt != type::rcbrace)
        {
          error (t) << "expected '}' instead of " << t << endl;
          throw parser_error ();
        }

        next (t, tt);
        continue;
      }

      // Name.
      //
      if (tt == type::name)
      {
        string name (t.name ()); //@@ move?

        // See if this is a type name, that is, it is followed by '{'.
        //
        if (next (t, tt) == type::lcbrace)
        {
          if (tp != nullptr)
          {
            error (t) << "nested type name '" << name << "'" << endl;
            throw parser_error ();
          }

          next (t, tt);
          parse_names (t, tt, ns, &name);

          if (tt != type::rcbrace)
          {
            error (t) << "expected '}' instead of " << t << endl;
            throw parser_error ();
          }

          next (t, tt);
          continue;
        }

        ns.emplace_back ((tp != nullptr ? *tp : string ()), move (name));
        continue;
      }

      if (!first)
        break;

      error (t) << "expected name instead of " << t << endl;
      throw parser_error ();
    }
  }

  token_type parser::
  next (token& t, token_type& tt)
  {
    t = lexer_->next ();
    tt = t.type ();
    return tt;
  }

  ostream& parser::
  error (const token& t)
  {
    return diag_ << path_->string () << ':' << t.line () << ':' <<
      t.column () << ": error: ";
  }

  // Output the token type and value in a format suitable for diagnostics.
  //
  ostream&
  operator<< (ostream& os, const token& t)
  {
    switch (t.type ())
    {
    case token_type::eos:     os << "<end-of-stream>"; break;
    case token_type::newline: os << "<newline>"; break;
    case token_type::colon:   os << "':'"; break;
    case token_type::lcbrace: os << "'{'"; break;
    case token_type::rcbrace: os << "'}'"; break;
    case token_type::name:    os << '\'' << t.name () << '\''; break;
    }

    return os;
  }
}