Add character rule class

This commit is contained in:
Max Brunsfeld 2013-11-07 08:22:56 -08:00
parent 061d8a8efc
commit 849f2ee195
3 changed files with 78 additions and 30 deletions

View file

@ -6,9 +6,11 @@ using namespace std;
namespace tree_sitter {
namespace rules {
// Constructors
Blank::Blank() {}
Symbol::Symbol(int id) : id(id) {};
Char::Char(char value) : value(value) {};
Seq::Seq(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {};
Seq::Seq(const Rule *left, const Rule *right) : left(left), right(right) {};
Seq::Seq(shared_ptr<const Rule> left, shared_ptr<const Rule> right) : left(left), right(right) {};
@ -24,6 +26,10 @@ namespace tree_sitter {
TransitionMap<Rule> Symbol::transitions() const {
return TransitionMap<Rule>({ copy() }, { new Blank() });
}
TransitionMap<Rule> Char::transitions() const {
return TransitionMap<Rule>({ copy() }, { new Blank() });
}
TransitionMap<Rule> Choice::transitions() const {
auto result = left->transitions();
@ -35,11 +41,10 @@ namespace tree_sitter {
TransitionMap<Rule> Seq::transitions() const {
return left->transitions().map([&](rule_ptr left_rule) -> rule_ptr {
if (typeid(*left_rule) == typeid(Blank)) {
if (typeid(*left_rule) == typeid(Blank))
return right;
} else {
else
return rule_ptr(new Seq(left_rule, right));
}
});
}
@ -53,6 +58,11 @@ namespace tree_sitter {
return (other != NULL) && (other->id == id);
}
bool Char::operator==(const Rule &rule) const {
const Char *other = dynamic_cast<const Char *>(&rule);
return (other != NULL) && (other->value == value);
}
bool Choice::operator==(const Rule &rule) const {
const Choice *other = dynamic_cast<const Choice *>(&rule);
return (other != NULL) && (*other->left == *left) && (*other->right == *right);
@ -71,7 +81,11 @@ namespace tree_sitter {
Symbol * Symbol::copy() const {
return new Symbol(id);
}
Char * Char::copy() const {
return new Char(value);
}
Choice * Choice::copy() const {
return new Choice(left, right);
}
@ -86,7 +100,11 @@ namespace tree_sitter {
}
string Symbol::to_string() const {
return string(std::to_string(id));
return std::to_string(id);
}
string Char::to_string() const {
return std::to_string(value);
}
string Choice::to_string() const {

View file

@ -36,6 +36,17 @@ namespace tree_sitter {
private:
int id;
};
class Char : public Rule {
public:
Char(char value);
TransitionMap<Rule> transitions() const;
Char * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
char value;
};
class Choice : public Rule {
public: