aboutsummaryrefslogtreecommitdiff
path: root/xsd/type-map/parser.cxx
blob: 186a329f3ec2b75d2171b17838c93b9ef53aa6be (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
// file      : xsd/type-map/parser.cxx
// copyright : Copyright (c) 2007-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <iostream>

#include <cutl/re.hxx>

#include <type-map/parser.hxx>

using std::endl;

namespace TypeMap
{
  typedef Lexer::Token Token;
  typedef cutl::re::wformat Format;

  Parser::Parser (Lexer& lex, String const& path)
      : lex_ (lex), path_ (path), e (std::wcerr)
  {
  }

  bool Parser::
  parse (Namespaces& ns)
  {
    try
    {
      Namespace* global = 0;

      for (Token t (lex_.next ()); t.type () != Token::eos; t = lex_.next ())
      {
        String l (t.lexeme ());

        if (l == L"namespace")
        {
          global = 0;

          if (!namespace_ (ns))
            return false;
        }
        else if (l == L"include")
        {
          if (global == 0)
          {
            ns.push_back (Namespace (Pattern ()));
            global = &(*ns.rbegin ());
          }

          if (!include (*global))
            return false;
        }
        else if (l == L"type" || t.type () == Token::token)
        {
          // Type mapping can have 'type' specifier omitted.
          //
          if (l == L"type")
            t = lex_.next ();

          if (global == 0)
          {
            ns.push_back (Namespace (Pattern ()));
            global = &(*ns.rbegin ());
          }

          if (!type (t, *global))
            return false;
        }
        else
        {
          e << path_ << ":" << t.line () << ": unexpected '" << l << "'"
            << endl;

          return false;
        }
      }
    }
    catch (Lexer::Failed const&)
    {
      return false;
    }

    return true;
  }

  bool Parser::
  namespace_ (Namespaces& ns)
  {
    // First get XML namespace.
    //
    Token t (lex_.next ());

    Pattern xsd_name;

    try
    {
      xsd_name = t.lexeme ();
    }
    catch (Format const& ex)
    {
      e << path_ << ":" << t.line () << ": invalid namespace pattern: "
        << ex.description ().c_str () << endl;
      return false;
    }

    if (t.type () != Token::token)
    {
      e << path_ << ":" << t.line () << ": expected XML namespace "
        << "instead of '" << xsd_name << "'" << endl;
      return false;
    }


    // See if we've got optional C++ mapping.
    //
    t = lex_.next ();

    bool has_cxx_name (false);
    String cxx_name;

    if (t.type () != Token::token)
    {
      if (t.lexeme () != L"{")
      {
        e << path_ << ":" << t.line () << ": expected C++ namespace or '{' "
          << "instead of '" << t.lexeme () << "'" << endl;
        return false;
      }
    }
    else
    {
      has_cxx_name = true;
      cxx_name = t.lexeme ();
    }

    // Swallow '{' if needed.
    //
    if (has_cxx_name)
    {
      t = lex_.next ();

      if (t.type () != Token::punct || t.lexeme () != L"{")
      {
        e << path_ << ":" << t.line () << ": expected '{' instead of '"
          << t.lexeme () << "'" << endl;
        return false;
      }
    }

    Namespace n (xsd_name, has_cxx_name, cxx_name);

    // Parse namespace body.
    //
    for (t = lex_.next ();; t = lex_.next ())
    {
      String l (t.lexeme ());

      if (l == L"include")
      {
        if (!include (n))
          return false;
      }
      else if (l == L"type" || t.type () == Token::token)
      {
        // Type mapping can have 'type' specifier omitted.
        //
        if (l == L"type")
          t = lex_.next ();

        if (!type (t, n))
          return false;
      }
      else if (t.type () == Token::punct && l == L"}")
      {
        break;
      }
      else
      {
        e << path_ << ":" << t.line () << ": unexpected '" << l << "'"
          << endl;
        return false;
      }
    }

    if (cxx_name || n.types_begin () != n.types_end () ||
        n.includes_begin () != n.includes_end ())
    {
      ns.push_back (n);
    }

    return true;
  }

  bool Parser::
  include (Namespace& n)
  {
    Token t (lex_.next ());

    String path (t.lexeme ());

    if (t.type () != Token::token)
    {
      e << path_ << ":" << t.line () << ": expected include path "
        << "instead of '" << path << "'" << endl;
      return false;
    }

    if (path && path[0] == L'<')
      n.includes_push_back (path);
    else
      n.includes_push_back (L'"' + path + L'"');

    t = lex_.next ();

    if (t.type () != Token::punct || t.lexeme () != L";")
    {
      e << path_ << ":" << t.line () << ": expected ';' after '"
        << path << "'" << endl;
      return false;
    }

    return true;
  }

  bool Parser::
  type (Token t, Namespace& n)
  {
    Pattern xsd_name;

    try
    {
      xsd_name = t.lexeme ();
    }
    catch (Format const& ex)
    {
      e << path_ << ":" << t.line () << ": invalid namespace pattern: "
        << ex.description ().c_str () << endl;
      return false;
    }

    if (t.type () != Token::token)
    {
      e << path_ << ":" << t.line () << ": expected XML Schema type name "
        << "instead of '" << xsd_name << "'" << endl;
      return false;
    }

    t = lex_.next ();
    String cxx_ret_name (t.lexeme ());

    if (t.type () != Token::token)
    {
      e << path_ << ":" << t.line () << ": expected C++ type name "
        << "instead of '" << cxx_ret_name << "'" << endl;
      return false;
    }

    t = lex_.next ();

    String cxx_arg_name;

    // See if we've got optional argument type.
    //
    if (t.type () == Token::token)
    {
      cxx_arg_name = t.lexeme ();
      t = lex_.next ();
    }

    if (t.type () != Token::punct || t.lexeme () != L";")
    {
      e << path_ << ":" << t.line () << ": expected ';' after '"
        << cxx_arg_name << "'" << endl;
      return false;
    }

    n.types_push_back (xsd_name, cxx_ret_name, cxx_arg_name);

    return true;
  }
}