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

Function transform

docopt_private.h:337–393  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

335 }
336
337 static inline std::vector<PatternList> transform(PatternList pattern)
338 {
339 std::vector<PatternList> result;
340
341 std::vector<PatternList> groups;
342 groups.emplace_back(std::move(pattern));
343
344 while(!groups.empty()) {
345 // pop off the first element
346 auto children = std::move(groups[0]);
347 groups.erase(groups.begin());
348
349 // find the first branch node in the list
350 auto child_iter = std::find_if(children.begin(), children.end(), [](std::shared_ptr<Pattern> const& p) {
351 return dynamic_cast<BranchPattern const*>(p.get());
352 });
353
354 // no branch nodes left : expansion is complete for this grouping
355 if (child_iter == children.end()) {
356 result.emplace_back(std::move(children));
357 continue;
358 }
359
360 // pop the child from the list
361 auto child = std::move(*child_iter);
362 children.erase(child_iter);
363
364 // expand the branch in the appropriate way
365 if (Either* either = dynamic_cast<Either*>(child.get())) {
366 // "[e] + children" for each child 'e' in Either
367 for(auto const& eitherChild : either->children()) {
368 PatternList group = { eitherChild };
369 group.insert(group.end(), children.begin(), children.end());
370
371 groups.emplace_back(std::move(group));
372 }
373 } else if (OneOrMore* oneOrMore = dynamic_cast<OneOrMore*>(child.get())) {
374 // child.children * 2 + children
375 auto const& subchildren = oneOrMore->children();
376 PatternList group = subchildren;
377 group.insert(group.end(), subchildren.begin(), subchildren.end());
378 group.insert(group.end(), children.begin(), children.end());
379
380 groups.emplace_back(std::move(group));
381 } else { // Required, Optional, OptionsShortcut
382 BranchPattern* branch = dynamic_cast<BranchPattern*>(child.get());
383
384 // child.children + children
385 PatternList group = branch->children();
386 group.insert(group.end(), children.begin(), children.end());
387
388 groups.emplace_back(std::move(group));
389 }
390 }
391
392 return result;
393 }
394

Callers 4

flat_filterFunction · 0.85
longOptionsFunction · 0.85
create_pattern_treeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected