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

#include <build/parser>

#include <iostream>

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

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)
  {
    lexer l (is, p.string (), diag_);
    lexer_ = &l;
    path_ = &p;

    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 ns (parse_names (t, tt));

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

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

          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 it is.
            //
            bool dir (false);
            for (const auto& n: ns)
            {
              if (n.back () == '/')
              {
                if (ns.size () != 1)
                {
                  // @@ TODO: point to name.
                  //
                  error (t) << "multiple names in directory scope" << endl;
                  throw parser_error ();
                }

                dir = true;
              }
            }

            next (t, tt);

            if (dir)
              // A directory scope can contain anything that a top level can.
              //
              parse_clause (t, tt);
            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)
  {
    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);

        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 ());

        // See if this is a type name, that is, it is followed by '{'.
        //
        if (next (t, tt) == type::lcbrace)
        {
          //cout << "type: " << name << endl;

          //@@ TODO:
          //
          //   - detect nested typed name groups, e.g., 'cxx{hxx{foo}}'.
          //
          next (t, tt);
          parse_names (t, tt, ns);

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

          next (t, tt);
          continue;
        }

        // This is a target, directory, or variable name.
        //cout << "name: " << name << endl;
        ns.push_back (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;
  }
}