aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/cc/parser.cxx
blob: 690f41cd897e78269ac7bd4e87749253311f1f63 (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
// file      : libbuild2/cc/parser.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/cc/parser.hxx>

#include <libbuild2/cc/lexer.hxx>

using namespace std;
using namespace butl;

namespace build2
{
  namespace cc
  {
    using type = token_type;

    unit parser::
    parse (ifdstream& is, const path_name& in)
    {
      lexer l (is, in);
      l_ = &l;

      unit u;
      u_ = &u;

      // If the source has errors then we want the compiler to issues the
      // diagnostics. However, the errors could as likely be because we are
      // mis-parsing things. Initially, as a middle ground, we were going to
      // issue warnings. But the problem with this approach is that they are
      // easy to miss. So for now we fail. And it turns out we don't mis-
      // parse much.
      //
      size_t bb (0); // {}-balance.

      token t;
      for (bool n (true); (n ? l_->next (t) : t.type) != type::eos; )
      {
        // Break to stop, continue to continue, set n to false if the
        // next token already extracted.
        //
        n = true;

        switch (t.type)
        {
        case type::lcbrace:
          {
            ++bb;
            continue;
          }
        case type::rcbrace:
          {
            if (bb-- == 0)
              break; // Imbalance.

            continue;
          }
        case type::identifier:
          {
            // Constructs we need to recognize:
            //
            //           module                              ;
            // [export]  import <module-name> [<attributes>] ;
            // [export]  import <header-name> [<attributes>] ;
            // [export]  module <module-name> [<attributes>] ;
            //
            // Additionally, when include is translated to an import, it's
            // normally replaced with the special __import keyword since it
            // may appear in C context.
            //
            const string& id (t.value);

            if (bb == 0)
            {
              if      (id == "import" || id == "__import")
              {
                parse_import (t, false);
              }
              else if (id == "module")
              {
                parse_module (t, false);
              }
              else if (id == "export")
              {
                if (l_->next (t) == type::identifier)
                {
                  if      (id == "module") parse_module (t, true);
                  else if (id == "import") parse_import (t, true);
                  else n = false; // Something else (e.g., export namespace).
                }
                else
                  n = false;
              }
            }
            continue;
          }
        default: continue;
        }

        break;
      }

      if (bb != 0)
        /*warn*/ fail (t) << "{}-imbalance detected";

      if (module_marker_ && u.module_info.name.empty ())
        fail (*module_marker_) << "module declaration expected after "
                               << "leading module marker";

      checksum = l.checksum ();
      return u;
    }

    void parser::
    parse_import (token& t, bool ex)
    {
      // enter: import keyword
      // leave: semi

      string un;
      unit_type ut;
      switch (l_->next (t)) // Start of module/header name.
      {
      case type::less:
      case type::string:
        {
          un = parse_header_name (t);
          ut = unit_type::module_header;
          break;
        }
      case type::identifier:
        {
          un = parse_module_name (t);
          ut = unit_type::module_iface;
          break;
        }
      default:
        fail (t) << "module or header name expected instead of " << t << endf;
      }

      // Should be {}-balanced.
      //
      for (; t.type != type::eos && t.type != type::semi; l_->next (t)) ;

      if (t.type != type::semi)
        fail (t) << "';' expected instead of " << t;

      // For now we skip header units (see a comment on module type/info
      // string serialization in compile rule for details). Note that
      // currently parse_header_name() always returns empty name.
      //
      if (ut == unit_type::module_header)
        return;

      // Ignore duplicates. We don't expect a large numbers of (direct)
      // imports so vector/linear search is probably more efficient than a
      // set.
      //
      auto& is (u_->module_info.imports);

      auto i (find_if (is.begin (), is.end (),
                       [&un] (const module_import& i)
                       {
                         return i.name == un;
                       }));

      if (i == is.end ())
        is.push_back (module_import {ut, move (un), ex, 0});
      else
        i->exported = i->exported || ex;
    }

    void parser::
    parse_module (token& t, bool ex)
    {
      // enter: module keyword
      // leave: semi

      location l (get_location (t));

      l_->next (t);

      // Handle the leading 'module;' marker (p0713).
      //
      // Note that we don't bother diagnosing invalid/duplicate markers
      // leaving that to the compiler.
      //
      if (!ex && t.type == type::semi)
      {
        module_marker_ = location_value (move (l));
        return;
      }

      // Otherwise it should be the start of the module name.
      //
      string n (parse_module_name (t));

      // Should be {}-balanced.
      //
      for (; t.type != type::eos && t.type != type::semi; l_->next (t)) ;

      if (t.type != type::semi)
        fail (t) << "';' expected instead of " << t;

      if (!u_->module_info.name.empty ())
        fail (l) << "multiple module declarations";

      u_->type = ex ? unit_type::module_iface : unit_type::module_impl;
      u_->module_info.name = move (n);
    }

    string parser::
    parse_module_name (token& t)
    {
      // enter: first token of module name
      // leave: token after module name

      string n;

      // <identifier>[ . <identifier>]*
      //
      for (;; l_->next (t))
      {
        if (t.type != type::identifier)
          fail (t) << "module name expected instead of " << t;

        n += t.value;

        if (l_->next (t) != type::dot)
          break;

        n += '.';
      }

      return n;
    }

    string parser::
    parse_header_name (token& t)
    {
      // enter: first token of module name, either string or less
      // leave: token after module name

      string n;

      // NOTE: actual name is a TODO if/when we need it.
      //
      if (t.type == type::string)
        /*n = move (t.value)*/;
      else
      {
        while (l_->next (t) != type::greater)
        {
          if (t.type == type::eos)
            fail (t) << "closing '>' expected after header name" << endf;
        }
      }

      l_->next (t);
      return n;
    }
  }
}