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

#include <bdep/new-parsers.hxx>

#include <bdep/new-options.hxx> // bdep::cli namespace, cmd_new_*_options

namespace bdep
{
  namespace cli
  {
    using type = cmd_new_type;
    using lang = cmd_new_lang;
    using vcs  = cmd_new_vcs;

    // Parse comma-separated list of of options starting from the first comma
    // at pos, merging them with options parsed previously.
    //
    template <typename O>
    static void
    parse_options (const char* o, const string v, size_t pos, O& r)
    {
      // Use vector_scanner to parse the comma-separated list of
      // parameter-specific options. Make sure that option values are only
      // specified if required (no value for flags, etc).
      //
      vector<string> os;
      const options& ods (O::description ());

      for (size_t i (pos), j; i != string::npos; )
      {
        j = i + 1;
        i = v.find (',', j);

        string po (v, j, i != string::npos ? i - j : i);

        // Split the parameter-specific option into the name and value.
        //
        optional<string> pv;
        {
          size_t i (po.find ('='));

          if (i != string::npos)
          {
            pv = string (po, i + 1);
            po.resize (i);
          }
        }

        // Verify that the option is known and its value is only specified if
        // required.
        //
        {
          auto i (ods.find (po));

          if (i == ods.end ())
            throw invalid_value (o, po);

          bool flag (!pv);

          if (flag != i->flag ())
            throw invalid_value (o,
                                 po,
                                 string (flag ? "missing" : "unexpected") +
                                 " value for '" + po + "'");
        }

        os.push_back (move (po));

        if (pv)
          os.push_back (move (*pv));
      }

      vector_scanner s (os);
      r.parse (s);
    }

    void parser<type>::
    parse (type& r, bool& xs, scanner& s)
    {
      const char* o (s.next ());

      if (!s.more ())
        throw missing_value (o);

      string v (s.next ());
      size_t i (v.find (','));
      string l (v, 0, i);

      if (l == "exe")
      {
        r.type = type::exe;
        parse_options<cmd_new_exe_options> (o, v, i, r.exe_opt);
      }
      else if (l == "lib")
      {
        r.type = type::lib;
        parse_options<cmd_new_lib_options> (o, v, i, r.lib_opt);
      }
      else if (l == "bare")
      {
        r.type = type::bare;
        parse_options<cmd_new_bare_options> (o, v, i, r.bare_opt);
      }
      else if (l == "empty")
      {
        r.type = type::empty;
        parse_options<cmd_new_empty_options> (o, v, i, r.empty_opt);
      }
      else
        throw invalid_value (o, l);

      xs = true;
    }

    void parser<lang>::
    parse (lang& r, bool& xs, scanner& s)
    {
      const char* o (s.next ());

      if (!s.more ())
        throw missing_value (o);

      string v (s.next ());
      size_t i (v.find (','));
      string l (v, 0, i);

      if (l == "c")
      {
        r.lang = lang::c;
        parse_options<cmd_new_c_options> (o, v, i, r.c_opt);
      }
      else if (l == "c++")
      {
        r.lang = lang::cxx;
        parse_options<cmd_new_cxx_options> (o, v, i, r.cxx_opt);
      }
      else
        throw invalid_value (o, l);

      xs = true;
    }

    void parser<vcs>::
    parse (vcs& r, bool& xs, scanner& s)
    {
      const char* o (s.next ());

      if (!s.more ())
        throw missing_value (o);

      string v (s.next ());
      size_t i (v.find (','));
      string l (v, 0, i);

      if (l == "git")
      {
        r.vcs = vcs::git;
        parse_options<cmd_new_git_options> (o, v, i, r.git_opt);
      }
      else if (l == "none")
      {
        r.vcs = vcs::none;
        parse_options<cmd_new_none_options> (o, v, i, r.none_opt);
      }
      else
        throw invalid_value (o, l);

      xs = true;
    }
  }
}