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