tree-sitter/src/compiler/rules/seq.cpp

37 lines
1.2 KiB
C++
Raw Normal View History

2013-11-07 13:24:01 -08:00
#include "seq.h"
#include "blank.h"
#include "transition_map.h"
namespace tree_sitter {
2013-11-07 13:24:01 -08:00
namespace rules {
Seq::Seq(rule_ptr left, rule_ptr right) : left(left), right(right) {};
rule_ptr seq(const std::initializer_list<rule_ptr> &rules) {
2013-11-20 19:00:20 -08:00
rule_ptr result;
for (auto rule : rules)
result = (result.get() && typeid(*result) != typeid(Blank)) ?
std::make_shared<Seq>(result, rule) :
rule;
return result;
}
2013-11-07 13:24:01 -08:00
TransitionMap<Rule> Seq::transitions() const {
return left->transitions().map<Rule>([&](rule_ptr left_rule) -> rule_ptr {
2013-11-07 13:24:01 -08:00
if (typeid(*left_rule) == typeid(Blank))
return right;
else
return seq({ left_rule, right });
2013-11-07 13:24:01 -08:00
});
}
bool Seq::operator==(const Rule &rule) const {
const Seq *other = dynamic_cast<const Seq *>(&rule);
return other && (*other->left == *left) && (*other->right == *right);
2013-11-07 13:24:01 -08:00
}
std::string Seq::to_string() const {
return std::string("(seq ") + left->to_string() + " " + right->to_string() + ")";
}
}
}