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

#include <build2/cc/parser.hxx>

#include <build2/cc/lexer.hxx>

using namespace std;
using namespace butl;

namespace build2
{
  namespace cc
  {
    using type = token_type;

    translation_unit parser::
    parse (ifdstream& is, const path& name)
    {
      lexer l (is, name);
      l_ = &l;

      translation_unit u {{"", false, {}}};
      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.
      //
      size_t bb (0);     // {}-balance.
      bool   ex (false); // True if inside top-level export{} block.

      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.

            if (ex && bb == 0)
              ex = false; // Closed top-level export{}.

            continue;
          }
        case type::identifier:
          {
            // Constructs we need to recognize (the last one is only not to
            // confuse it with others).
            //
            // [export]  import <module-name> [<attributes>] ;
            // [export]  module <module-name> [<attributes>] ;
            //  export { import <module-name> [<attributes>] ; }
            //  extern   module ...
            //
            const string& id (t.value);

            if (bb == 0)
            {
              if      (id == "import")
              {
                parse_import (t, false);
              }
              else if (id == "module")
              {
                parse_module (t, false);
              }
              else if (id == "export")
              {
                switch (l_->next (t))
                {
                case type::lcbrace:    ++bb; ex = true; break;
                case 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).
                    break;
                  }
                default: n = false; break;
                }
              }
              else if (id == "extern")
              {
                // Skip to make sure not recognized as module.
                //
                n = l_->next (t) == type::identifier && t.value == "module";
              }
            }
            else if (ex && bb == 1)
            {
              if (id == "import")
              {
                parse_import (t, true);
              }
            }
            continue;
          }
        default: continue;
        }

        break;
      }

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

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

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

      l_->next (t); // Start of 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;

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

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

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

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

      l_->next (t); // Start of 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_->mod.name.empty ())
        fail (t) << "multiple module declarations";

      u_->mod.name = move (n);
      u_->mod.iface = ex;
    }

    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;
    }
  }
}