MCPcopy Create free account
hub / github.com/docopt/docopt.cpp / Tokens

Class Tokens

docopt.cpp:62–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60#endif
61
62class Tokens {
63public:
64 Tokens(std::vector<std::string> tokens, bool isParsingArgv = true)
65 : fTokens(std::move(tokens)),
66 fIsParsingArgv(isParsingArgv)
67 {}
68
69 explicit operator bool() const {
70 return fIndex < fTokens.size();
71 }
72
73 static Tokens from_pattern(std::string const& source) {
74 static const std::regex re_separators {
75 "(?:\\s*)" // any spaces (non-matching subgroup)
76 "("
77 "[\\[\\]\\(\\)\\|]" // one character of brackets or parens or pipe character
78 "|"
79 "\\.\\.\\." // elipsis
80 ")" };
81
82 static const std::regex re_strings {
83 "(?:\\s*)" // any spaces (non-matching subgroup)
84 "("
85 "\\S*<.*?>" // strings, but make sure to keep "< >" strings together
86 "|"
87 "[^<>\\s]+" // string without <>
88 ")" };
89
90 // We do two stages of regex matching. The '[]()' and '...' are strong delimeters
91 // and need to be split out anywhere they occur (even at the end of a token). We
92 // first split on those, and then parse the stuff between them to find the string
93 // tokens. This is a little harder than the python version, since they have regex.split
94 // and we dont have anything like that.
95
96 std::vector<std::string> tokens;
97 std::for_each(std::sregex_iterator{ source.begin(), source.end(), re_separators },
98 std::sregex_iterator{},
99 [&](std::smatch const& match)
100 {
101 // handle anything before the separator (this is the "stuff" between the delimeters)
102 if (match.prefix().matched) {
103 std::for_each(std::sregex_iterator{match.prefix().first, match.prefix().second, re_strings},
104 std::sregex_iterator{},
105 [&](std::smatch const& m)
106 {
107 tokens.push_back(m[1].str());
108 });
109 }
110
111 // handle the delimter token itself
112 if (match[1].matched) {
113 tokens.push_back(match[1].str());
114 }
115 });
116
117 return Tokens(tokens, false);
118 }
119

Callers 2

from_patternMethod · 0.85
docopt_parseMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected