forked from docopt/docopt.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docopt_private.h
674 lines (555 loc) · 17.7 KB
/
docopt_private.h
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//
// docopt_private.h
// docopt
//
// Created by Jared Grubb on 2013-11-04.
// Copyright (c) 2013 Jared Grubb. All rights reserved.
//
#ifndef docopt_docopt_private_h
#define docopt_docopt_private_h
#include <vector>
#include <memory>
#include <unordered_set>
#include <assert.h>
// Workaround GCC 4.8 not having std::regex
#if DOCTOPT_USE_BOOST_REGEX
#include <boost/regex.hpp>
namespace std {
using boost::regex;
using boost::sregex_iterator;
using boost::smatch;
using boost::regex_search;
namespace regex_constants {
using boost::regex_constants::match_not_null;
}
}
#else
#include <regex>
#endif
#include "docopt_value.h"
namespace docopt {
class Pattern;
class LeafPattern;
using PatternList = std::vector<std::shared_ptr<Pattern>>;
// Utility to use Pattern types in std hash-containers
struct PatternHasher {
template <typename P>
size_t operator()(std::shared_ptr<P> const& pattern) const {
return pattern->hash();
}
template <typename P>
size_t operator()(P const* pattern) const {
return pattern->hash();
}
template <typename P>
size_t operator()(P const& pattern) const {
return pattern.hash();
}
};
// Utility to use 'hash' as the equality operator as well in std containers
struct PatternPointerEquality {
template <typename P1, typename P2>
bool operator()(std::shared_ptr<P1> const& p1, std::shared_ptr<P2> const& p2) const {
return p1->hash()==p2->hash();
}
template <typename P1, typename P2>
bool operator()(P1 const* p1, P2 const* p2) const {
return p1->hash()==p2->hash();
}
};
// A hash-set that uniques by hash value
using UniquePatternSet = std::unordered_set<std::shared_ptr<Pattern>, PatternHasher, PatternPointerEquality>;
class Pattern {
public:
// flatten out children, stopping descent when the given filter returns 'true'
virtual std::vector<Pattern*> flat(bool (*filter)(Pattern const*)) = 0;
// flatten out all children into a list of LeafPattern objects
virtual void collect_leaves(std::vector<LeafPattern*>&) = 0;
// flatten out all children into a list of LeafPattern objects
std::vector<LeafPattern*> leaves();
// Attempt to find something in 'left' that matches this pattern's spec, and if so, move it to 'collected'
virtual bool match(PatternList& left, std::vector<std::shared_ptr<LeafPattern>>& collected) const = 0;
virtual std::string const& name() const = 0;
virtual bool hasValue() const { return false; }
virtual size_t hash() const = 0;
virtual ~Pattern() = default;
};
class LeafPattern
: public Pattern {
public:
LeafPattern(std::string name, value v = {})
: fName(std::move(name)),
fValue(std::move(v))
{}
virtual std::vector<Pattern*> flat(bool (*filter)(Pattern const*)) override {
if (filter(this)) {
return { this };
}
return {};
}
virtual void collect_leaves(std::vector<LeafPattern*>& lst) override final {
lst.push_back(this);
}
virtual bool match(PatternList& left, std::vector<std::shared_ptr<LeafPattern>>& collected) const override;
virtual bool hasValue() const override { return static_cast<bool>(fValue); }
value const& getValue() const { return fValue; }
void setValue(value&& v) { fValue = std::move(v); }
virtual std::string const& name() const override { return fName; }
virtual size_t hash() const override {
size_t seed = typeid(*this).hash_code();
hash_combine(seed, fName);
hash_combine(seed, fValue);
return seed;
}
protected:
virtual std::pair<size_t, std::shared_ptr<LeafPattern>> single_match(PatternList const&) const = 0;
private:
std::string fName;
value fValue;
};
class BranchPattern
: public Pattern {
public:
BranchPattern(PatternList children = {})
: fChildren(std::move(children))
{}
Pattern& fix() {
UniquePatternSet patterns;
fix_identities(patterns);
fix_repeating_arguments();
return *this;
}
virtual std::string const& name() const override {
throw std::runtime_error("Logic error: name() shouldnt be called on a BranchPattern");
}
virtual value const& getValue() const {
throw std::runtime_error("Logic error: name() shouldnt be called on a BranchPattern");
}
virtual std::vector<Pattern*> flat(bool (*filter)(Pattern const*)) override {
if (filter(this)) {
return {this};
}
std::vector<Pattern*> ret;
for(auto& child : fChildren) {
auto sublist = child->flat(filter);
ret.insert(ret.end(), sublist.begin(), sublist.end());
}
return ret;
}
virtual void collect_leaves(std::vector<LeafPattern*>& lst) override final {
for(auto& child : fChildren) {
child->collect_leaves(lst);
}
}
void setChildren(PatternList children) {
fChildren = std::move(children);
}
PatternList const& children() const { return fChildren; }
virtual void fix_identities(UniquePatternSet& patterns) {
for(auto& child : fChildren) {
// this will fix up all its children, if needed
if (auto bp = dynamic_cast<BranchPattern*>(child.get())) {
bp->fix_identities(patterns);
}
// then we try to add it to the list
auto inserted = patterns.insert(child);
if (!inserted.second) {
// already there? then reuse the existing shared_ptr for that thing
child = *inserted.first;
}
}
}
virtual size_t hash() const override {
size_t seed = typeid(*this).hash_code();
hash_combine(seed, fChildren.size());
for(auto const& child : fChildren) {
hash_combine(seed, child->hash());
}
return seed;
}
private:
void fix_repeating_arguments();
protected:
PatternList fChildren;
};
class Argument
: public LeafPattern {
public:
using LeafPattern::LeafPattern;
protected:
virtual std::pair<size_t, std::shared_ptr<LeafPattern>> single_match(PatternList const& left) const override;
};
class Command : public Argument {
public:
Command(std::string name, value v = value{false})
: Argument(std::move(name), std::move(v))
{}
protected:
virtual std::pair<size_t, std::shared_ptr<LeafPattern>> single_match(PatternList const& left) const override;
};
class Option final
: public LeafPattern
{
public:
static Option parse(std::string const& option_description);
Option(std::string shortOption,
std::string longOption,
int argcount = 0,
value v = value{false})
: LeafPattern(longOption.empty() ? shortOption : longOption,
std::move(v)),
fShortOption(std::move(shortOption)),
fLongOption(std::move(longOption)),
fArgcount(argcount)
{
// From Python:
// self.value = None if value is False and argcount else value
if (argcount && v.isBool() && !v.asBool()) {
setValue(value{});
}
}
Option(Option const&) = default;
Option(Option&&) = default;
Option& operator=(Option const&) = default;
Option& operator=(Option&&) = default;
using LeafPattern::setValue;
std::string const& longOption() const { return fLongOption; }
std::string const& shortOption() const { return fShortOption; }
int argCount() const { return fArgcount; }
virtual size_t hash() const override {
size_t seed = LeafPattern::hash();
hash_combine(seed, fShortOption);
hash_combine(seed, fLongOption);
hash_combine(seed, fArgcount);
return seed;
}
protected:
virtual std::pair<size_t, std::shared_ptr<LeafPattern>> single_match(PatternList const& left) const override;
private:
std::string fShortOption;
std::string fLongOption;
int fArgcount;
};
class Required : public BranchPattern {
public:
using BranchPattern::BranchPattern;
bool match(PatternList& left, std::vector<std::shared_ptr<LeafPattern>>& collected) const override;
};
class Optional : public BranchPattern {
public:
using BranchPattern::BranchPattern;
bool match(PatternList& left, std::vector<std::shared_ptr<LeafPattern>>& collected) const override {
for(auto const& pattern : fChildren) {
pattern->match(left, collected);
}
return true;
}
};
class OptionsShortcut : public Optional {
using Optional::Optional;
};
class OneOrMore : public BranchPattern {
public:
using BranchPattern::BranchPattern;
bool match(PatternList& left, std::vector<std::shared_ptr<LeafPattern>>& collected) const override;
};
class Either : public BranchPattern {
public:
using BranchPattern::BranchPattern;
bool match(PatternList& left, std::vector<std::shared_ptr<LeafPattern>>& collected) const override;
};
#pragma mark -
#pragma mark inline implementations
inline std::vector<LeafPattern*> Pattern::leaves()
{
std::vector<LeafPattern*> ret;
collect_leaves(ret);
return ret;
}
static inline std::vector<PatternList> transform(PatternList pattern)
{
std::vector<PatternList> result;
std::vector<PatternList> groups;
groups.emplace_back(std::move(pattern));
while(!groups.empty()) {
// pop off the first element
auto children = std::move(groups[0]);
groups.erase(groups.begin());
// find the first branch node in the list
auto child_iter = std::find_if(children.begin(), children.end(), [](std::shared_ptr<Pattern> const& p) {
return dynamic_cast<BranchPattern const*>(p.get());
});
// no branch nodes left : expansion is complete for this grouping
if (child_iter == children.end()) {
result.emplace_back(std::move(children));
continue;
}
// pop the child from the list
auto child = std::move(*child_iter);
children.erase(child_iter);
// expand the branch in the appropriate way
if (Either* either = dynamic_cast<Either*>(child.get())) {
// "[e] + children" for each child 'e' in Either
for(auto const& eitherChild : either->children()) {
PatternList group = { eitherChild };
group.insert(group.end(), children.begin(), children.end());
groups.emplace_back(std::move(group));
}
} else if (OneOrMore* oneOrMore = dynamic_cast<OneOrMore*>(child.get())) {
// child.children * 2 + children
auto const& subchildren = oneOrMore->children();
PatternList group = subchildren;
group.insert(group.end(), subchildren.begin(), subchildren.end());
group.insert(group.end(), children.begin(), children.end());
groups.emplace_back(std::move(group));
} else { // Required, Optional, OptionsShortcut
BranchPattern* branch = dynamic_cast<BranchPattern*>(child.get());
// child.children + children
PatternList group = branch->children();
group.insert(group.end(), children.begin(), children.end());
groups.emplace_back(std::move(group));
}
}
return result;
}
inline void BranchPattern::fix_repeating_arguments()
{
std::vector<PatternList> either = transform(children());
for(auto const& group : either) {
// use multiset to help identify duplicate entries
std::unordered_multiset<std::shared_ptr<Pattern>, PatternHasher> group_set {group.begin(), group.end()};
for(auto const& e : group_set) {
if (group_set.count(e) == 1)
continue;
LeafPattern* leaf = dynamic_cast<LeafPattern*>(e.get());
if (!leaf) continue;
bool ensureList = false;
bool ensureInt = false;
if (dynamic_cast<Command*>(leaf)) {
ensureInt = true;
} else if (dynamic_cast<Argument*>(leaf)) {
ensureList = true;
} else if (Option* o = dynamic_cast<Option*>(leaf)) {
if (o->argCount()) {
ensureList = true;
} else {
ensureInt = true;
}
}
if (ensureList) {
std::vector<std::string> newValue;
if (leaf->getValue().isString()) {
newValue = split(leaf->getValue().asString());
}
if (!leaf->getValue().isStringList()) {
leaf->setValue(value{newValue});
}
} else if (ensureInt) {
leaf->setValue(value{0});
}
}
}
}
inline bool LeafPattern::match(PatternList& left, std::vector<std::shared_ptr<LeafPattern>>& collected) const
{
auto match = single_match(left);
if (!match.second) {
return false;
}
left.erase(left.begin()+static_cast<std::ptrdiff_t>(match.first));
auto same_name = std::find_if(collected.begin(), collected.end(), [&](std::shared_ptr<LeafPattern> const& p) {
return p->name()==name();
});
if (getValue().isLong()) {
long val = 1;
if (same_name == collected.end()) {
collected.push_back(match.second);
match.second->setValue(value{val});
} else if ((**same_name).getValue().isLong()) {
val += (**same_name).getValue().asLong();
(**same_name).setValue(value{val});
} else {
(**same_name).setValue(value{val});
}
} else if (getValue().isStringList()) {
std::vector<std::string> val;
if (match.second->getValue().isString()) {
val.push_back(match.second->getValue().asString());
} else if (match.second->getValue().isStringList()) {
val = match.second->getValue().asStringList();
} else {
/// cant be!?
}
if (same_name == collected.end()) {
collected.push_back(match.second);
match.second->setValue(value{val});
} else if ((**same_name).getValue().isStringList()) {
std::vector<std::string> const& list = (**same_name).getValue().asStringList();
val.insert(val.begin(), list.begin(), list.end());
(**same_name).setValue(value{val});
} else {
(**same_name).setValue(value{val});
}
} else {
collected.push_back(match.second);
}
return true;
}
inline std::pair<size_t, std::shared_ptr<LeafPattern>> Argument::single_match(PatternList const& left) const
{
std::pair<size_t, std::shared_ptr<LeafPattern>> ret {};
for(size_t i = 0, size = left.size(); i < size; ++i)
{
auto arg = dynamic_cast<Argument const*>(left[i].get());
if (arg) {
ret.first = i;
ret.second = std::make_shared<Argument>(name(), arg->getValue());
break;
}
}
return ret;
}
inline std::pair<size_t, std::shared_ptr<LeafPattern>> Command::single_match(PatternList const& left) const
{
std::pair<size_t, std::shared_ptr<LeafPattern>> ret {};
for(size_t i = 0, size = left.size(); i < size; ++i)
{
auto arg = dynamic_cast<Argument const*>(left[i].get());
if (arg) {
if (name() == arg->getValue()) {
ret.first = i;
ret.second = std::make_shared<Command>(name(), value{true});
}
break;
}
}
return ret;
}
inline Option Option::parse(std::string const& option_description)
{
std::string shortOption, longOption;
int argcount = 0;
value val { false };
auto double_space = option_description.find(" ");
auto options_end = option_description.end();
if (double_space != std::string::npos) {
options_end = option_description.begin() + static_cast<std::ptrdiff_t>(double_space);
}
static const std::regex pattern {"(-{1,2})?(.*?)([,= ]|$)"};
for(std::sregex_iterator i {option_description.begin(), options_end, pattern, std::regex_constants::match_not_null},
e{};
i != e;
++i)
{
std::smatch const& match = *i;
if (match[1].matched) { // [1] is optional.
if (match[1].length()==1) {
shortOption = "-" + match[2].str();
} else {
longOption = "--" + match[2].str();
}
} else if (match[2].length() > 0) { // [2] always matches.
std::string m = match[2];
argcount = 1;
} else {
// delimeter
}
if (match[3].length() == 0) { // [3] always matches.
// Hit end of string. For some reason 'match_not_null' will let us match empty
// at the end, and then we'll spin in an infinite loop. So, if we hit an empty
// match, we know we must be at the end.
break;
}
}
if (argcount) {
std::smatch match;
if (std::regex_search(options_end, option_description.end(),
match,
std::regex{"\\[default: (.*)\\]", std::regex::icase}))
{
val = match[1].str();
}
}
return {std::move(shortOption),
std::move(longOption),
argcount,
std::move(val)};
}
inline std::pair<size_t, std::shared_ptr<LeafPattern>> Option::single_match(PatternList const& left) const
{
auto thematch = find_if(left.begin(), left.end(), [this](std::shared_ptr<Pattern> const& a) {
auto leaf = std::dynamic_pointer_cast<LeafPattern>(a);
return leaf && this->name() == leaf->name();
});
if (thematch == left.end()) {
return {};
}
return { std::distance(left.begin(), thematch), std::dynamic_pointer_cast<LeafPattern>(*thematch) };
}
inline bool Required::match(PatternList& left, std::vector<std::shared_ptr<LeafPattern>>& collected) const {
auto l = left;
auto c = collected;
for(auto const& pattern : fChildren) {
bool ret = pattern->match(l, c);
if (!ret) {
// leave (left, collected) untouched
return false;
}
}
left = std::move(l);
collected = std::move(c);
return true;
}
inline bool OneOrMore::match(PatternList& left, std::vector<std::shared_ptr<LeafPattern>>& collected) const
{
assert(fChildren.size() == 1);
auto l = left;
auto c = collected;
bool matched = true;
size_t times = 0;
decltype(l) l_;
bool firstLoop = true;
while (matched) {
// could it be that something didn't match but changed l or c?
matched = fChildren[0]->match(l, c);
if (matched)
++times;
if (firstLoop) {
firstLoop = false;
} else if (l == l_) {
break;
}
l_ = l;
}
if (times == 0) {
return false;
}
left = std::move(l);
collected = std::move(c);
return true;
}
inline bool Either::match(PatternList& left, std::vector<std::shared_ptr<LeafPattern>>& collected) const
{
using Outcome = std::pair<PatternList, std::vector<std::shared_ptr<LeafPattern>>>;
std::vector<Outcome> outcomes;
for(auto const& pattern : fChildren) {
// need a copy so we apply the same one for every iteration
auto l = left;
auto c = collected;
bool matched = pattern->match(l, c);
if (matched) {
outcomes.emplace_back(std::move(l), std::move(c));
}
}
auto min = std::min_element(outcomes.begin(), outcomes.end(), [](Outcome const& o1, Outcome const& o2) {
return o1.first.size() < o2.first.size();
});
if (min == outcomes.end()) {
// (left, collected) unchanged
return false;
}
std::tie(left, collected) = std::move(*min);
return true;
}
}
#endif