cpp

Coverage Report

Created: 2022-11-10 11:34

/home/andy/git/oilshell/oil/cpp/leaky_frontend_flag_spec.cc
Line
Count
Source (jump to first uncovered line)
1
// frontend_flag_spec.cc
2
3
#include "cpp/leaky_frontend_flag_spec.h"
4
5
#include "_gen/frontend/arg_types.h"
6
#include "mycpp/builtins.h"
7
// for definition of args::Reader, etc.
8
#include "prebuilt/frontend/args.mycpp.h"
9
10
namespace flag_spec {
11
12
using arg_types::kFlagSpecs;
13
using arg_types::kFlagSpecsAndMore;
14
using runtime_asdl::flag_type_e;
15
using runtime_asdl::value__Bool;
16
using runtime_asdl::value__Undef;
17
using runtime_asdl::value_t;
18
19
0
void _CreateStrList(const char** in, List<Str*>* out) {
20
0
  int i = 0;
21
0
  while (true) {
22
0
    const char* s = in[i];
23
0
    if (!s) {
24
0
      break;
25
0
    }
26
    // log("a0 %s", s);
27
0
    out->append(StrFromC(s));
28
0
    ++i;
29
0
  }
30
0
}
31
32
void _CreateDefaults(DefaultPair_c* in,
33
0
                     Dict<Str*, runtime_asdl::value_t*>* out) {
34
0
  int i = 0;
35
0
  while (true) {
36
0
    DefaultPair_c* pair = &(in[i]);
37
0
    if (!pair->name) {
38
0
      break;
39
0
    }
40
0
    value_t* val;
41
0
    switch (pair->typ) {
42
0
    case flag_type_e::Bool:
43
0
      val = Alloc<value__Bool>(pair->val.b);
44
0
      break;
45
0
    case flag_type_e::Int:
46
0
      val = Alloc<value__Int>(pair->val.i);
47
0
      break;
48
0
    case flag_type_e::Float:
49
0
      val = Alloc<value__Float>(pair->val.f);
50
0
      break;
51
0
    case flag_type_e::Str: {
52
0
      const char* s = pair->val.s;
53
0
      if (s == nullptr) {
54
0
        val = Alloc<value__Undef>();
55
0
      } else {
56
0
        val = Alloc<value__Str>(StrFromC(s));
57
0
      }
58
0
    } break;
59
0
    default:
60
0
      assert(0);  // NOTE(Jesse): Pretty sure this is InvalidCodePath()
61
0
    }
62
0
    out->set(StrFromC(pair->name), val);
63
0
    ++i;
64
0
  }
65
0
}
66
67
#ifndef CPP_UNIT_TEST
68
0
void _CreateActions(Action_c* in, Dict<Str*, args::_Action*>* out) {
69
0
  int i = 0;
70
0
  while (true) {
71
0
    Action_c* p = &(in[i]);
72
0
    if (!p->key) {
73
0
      break;
74
0
    }
75
    // log("a1 %s", p->name);
76
0
    args::_Action* action = nullptr;
77
0
    switch (p->type) {
78
0
    case ActionType_c::SetToString: {
79
0
      List<Str*>* valid = nullptr;
80
0
      if (p->strs) {
81
0
        valid = NewList<Str*>();
82
0
        _CreateStrList(p->strs, valid);
83
0
      }
84
0
      auto a = Alloc<args::SetToString>(StrFromC(p->name), false, valid);
85
0
      action = a;
86
0
    } break;
87
0
    case ActionType_c::SetToString_q:
88
0
      action = Alloc<args::SetToString>(StrFromC(p->name), true, nullptr);
89
0
      break;
90
0
    case ActionType_c::SetToInt:
91
0
      action = Alloc<args::SetToInt>(StrFromC(p->name));
92
0
      break;
93
0
    case ActionType_c::SetToFloat:
94
0
      action = Alloc<args::SetToFloat>(StrFromC(p->name));
95
0
      break;
96
0
    case ActionType_c::SetToTrue:
97
0
      action = Alloc<args::SetToTrue>(StrFromC(p->name));
98
0
      break;
99
0
    case ActionType_c::SetAttachedBool:
100
0
      action = Alloc<args::SetAttachedBool>(StrFromC(p->name));
101
0
      break;
102
0
    case ActionType_c::SetOption:
103
0
      action = Alloc<args::SetOption>(StrFromC(p->name));
104
0
      break;
105
0
    case ActionType_c::SetNamedOption: {
106
0
      auto a = Alloc<args::SetNamedOption>(false);
107
0
      if (p->strs) {
108
0
        _CreateStrList(p->strs, a->names);
109
0
      }
110
0
      action = a;
111
0
    } break;
112
0
    case ActionType_c::SetNamedOption_shopt: {
113
0
      auto a = Alloc<args::SetNamedOption>(true);
114
0
      if (p->strs) {
115
0
        _CreateStrList(p->strs, a->names);
116
0
      }
117
0
      action = a;
118
0
    } break;
119
0
    case ActionType_c::SetAction:
120
0
      action = Alloc<args::SetAction>(StrFromC(p->name));
121
0
      break;
122
0
    case ActionType_c::SetNamedAction: {
123
0
      auto a = Alloc<args::SetNamedAction>();
124
0
      if (p->strs) {
125
0
        _CreateStrList(p->strs, a->names);
126
0
      }
127
0
      action = a;
128
0
    } break;
129
0
    }
130
131
0
    if (action) {
132
0
      out->set(StrFromC(p->key), action);
133
0
    }
134
0
    ++i;
135
0
  }
136
0
}
137
#endif
138
139
// "Inflate" the static C data into a heap-allocated ASDL data structure.
140
//
141
// TODO: Make a GLOBAL CACHE?  It could be shared between subinterpreters even?
142
0
flag_spec::_FlagSpec* CreateSpec(FlagSpec_c* in) {
143
0
  auto out = Alloc<flag_spec::_FlagSpec>();
144
0
  out->arity0 = NewList<Str*>();
145
0
  out->arity1 = NewDict<Str*, args::_Action*>();
146
0
  out->actions_long = NewDict<Str*, args::_Action*>();
147
0
  out->plus_flags = NewList<Str*>();
148
0
  out->defaults = NewDict<Str*, runtime_asdl::value_t*>();
149
150
0
  if (in->arity0) {
151
0
    _CreateStrList(in->arity0, out->arity0);
152
0
  }
153
0
#ifndef CPP_UNIT_TEST
154
0
  if (in->arity1) {
155
0
    _CreateActions(in->arity1, out->arity1);
156
0
  }
157
0
  if (in->actions_long) {
158
0
    _CreateActions(in->actions_long, out->actions_long);
159
0
  }
160
0
#endif
161
0
  if (in->plus_flags) {
162
0
    _CreateStrList(in->plus_flags, out->plus_flags);
163
0
  }
164
0
  if (in->defaults) {
165
0
    _CreateDefaults(in->defaults, out->defaults);
166
0
  }
167
0
  return out;
168
0
}
169
170
0
flag_spec::_FlagSpecAndMore* CreateSpec2(FlagSpecAndMore_c* in) {
171
0
  auto out = Alloc<flag_spec::_FlagSpecAndMore>();
172
0
  out->actions_short = NewDict<Str*, args::_Action*>();
173
0
  out->actions_long = NewDict<Str*, args::_Action*>();
174
0
  out->plus_flags = NewList<Str*>();
175
0
  out->defaults = NewDict<Str*, runtime_asdl::value_t*>();
176
177
0
#ifndef CPP_UNIT_TEST
178
0
  if (in->actions_short) {
179
0
    _CreateActions(in->actions_short, out->actions_short);
180
0
  }
181
182
0
  if (in->actions_long) {
183
0
    _CreateActions(in->actions_long, out->actions_long);
184
0
  }
185
0
#endif
186
0
  if (in->plus_flags) {
187
0
    _CreateStrList(in->plus_flags, out->plus_flags);
188
0
  }
189
0
  if (in->defaults) {
190
0
    _CreateDefaults(in->defaults, out->defaults);
191
0
  }
192
0
  return out;
193
0
}
194
195
0
flag_spec::_FlagSpec* LookupFlagSpec(Str* spec_name) {
196
0
  int i = 0;
197
0
  while (true) {
198
0
    const char* name = kFlagSpecs[i].name;
199
0
    if (name == nullptr) {
200
0
      break;
201
0
    }
202
0
    if (str_equals0(name, spec_name)) {
203
      // log("%s found", spec_name->data_);
204
0
      return CreateSpec(&kFlagSpecs[i]);
205
0
    }
206
207
0
    i++;
208
0
  }
209
  // log("%s not found", spec_name->data_);
210
0
  return nullptr;
211
0
}
212
213
0
flag_spec::_FlagSpecAndMore* LookupFlagSpec2(Str* spec_name) {
214
0
  int i = 0;
215
0
  while (true) {
216
0
    const char* name = kFlagSpecsAndMore[i].name;
217
0
    if (name == nullptr) {
218
0
      break;
219
0
    }
220
0
    if (str_equals0(name, spec_name)) {
221
      // log("%s found", spec_name->data_);
222
0
      return CreateSpec2(&kFlagSpecsAndMore[i]);
223
0
    }
224
225
0
    i++;
226
0
  }
227
  // log("%s not found", spec_name->data_);
228
0
  return nullptr;
229
0
}
230
231
0
args::_Attributes* Parse(Str* spec_name, args::Reader* arg_r) {
232
0
  flag_spec::_FlagSpec* spec = LookupFlagSpec(spec_name);
233
0
  assert(spec);  // should always be found
234
235
#ifdef CPP_UNIT_TEST
236
  // hack because we don't want to depend on a translation of args.py
237
  return nullptr;
238
#else
239
0
  return args::Parse(spec, arg_r);
240
0
#endif
241
0
}
242
243
Tuple2<args::_Attributes*, args::Reader*> ParseCmdVal(
244
0
    Str* spec_name, runtime_asdl::cmd_value__Argv* cmd_val) {
245
#ifdef CPP_UNIT_TEST
246
  return Tuple2<args::_Attributes*, args::Reader*>(nullptr, nullptr);
247
#else
248
0
  auto arg_r = Alloc<args::Reader>(cmd_val->argv, cmd_val->arg_spids);
249
0
  arg_r->Next();  // move past the builtin name
250
251
0
  flag_spec::_FlagSpec* spec = LookupFlagSpec(spec_name);
252
0
  assert(spec);  // should always be found
253
0
  return Tuple2<args::_Attributes*, args::Reader*>(args::Parse(spec, arg_r),
254
0
                                                   arg_r);
255
0
#endif
256
0
}
257
258
// With optional arg
259
Tuple2<args::_Attributes*, args::Reader*> ParseCmdVal(
260
    Str* spec_name, runtime_asdl::cmd_value__Argv* cmd_val,
261
0
    bool accept_typed_args) {
262
  // TODO: disallow typed args!
263
0
  return ParseCmdVal(spec_name, cmd_val);
264
0
}
265
266
Tuple2<args::_Attributes*, args::Reader*> ParseLikeEcho(
267
0
    Str* spec_name, runtime_asdl::cmd_value__Argv* cmd_val) {
268
#ifdef CPP_UNIT_TEST
269
  return Tuple2<args::_Attributes*, args::Reader*>(nullptr, nullptr);
270
#else
271
0
  auto arg_r = Alloc<args::Reader>(cmd_val->argv, cmd_val->arg_spids);
272
0
  arg_r->Next();  // move past the builtin name
273
274
0
  flag_spec::_FlagSpec* spec = LookupFlagSpec(spec_name);
275
0
  assert(spec);  // should always be found
276
0
  return Tuple2<args::_Attributes*, args::Reader*>(
277
0
      args::ParseLikeEcho(spec, arg_r), arg_r);
278
0
#endif
279
0
}
280
281
0
args::_Attributes* ParseMore(Str* spec_name, args::Reader* arg_r) {
282
#ifdef CPP_UNIT_TEST
283
  return nullptr;
284
#else
285
  // TODO: Fill this in from constant data!
286
0
  flag_spec::_FlagSpecAndMore* spec = LookupFlagSpec2(spec_name);
287
0
  assert(spec);
288
0
  return args::ParseMore(spec, arg_r);
289
0
#endif
290
0
}
291
292
}  // namespace flag_spec