Add string rules

This commit is contained in:
Max Brunsfeld 2013-11-07 18:30:00 -08:00
parent d830c7c255
commit ec8b7ccf20
6 changed files with 70 additions and 2 deletions

View file

@ -7,5 +7,6 @@
#include "rules/symbol.h"
#include "rules/choice.h"
#include "rules/seq.h"
#include "rules/string.h"
#endif

View file

@ -21,7 +21,7 @@ namespace tree_sitter {
}
string Char::to_string() const {
return std::to_string(value);
return std::string("'") + &value + "'";
}
}
}

31
src/rules/string.cpp Normal file
View file

@ -0,0 +1,31 @@
#include "string.h"
#include "transition_map.h"
#include "char.h"
#include "seq.h"
namespace tree_sitter {
namespace rules {
String::String(std::string value) : value(value) {};
TransitionMap<Rule> String::transitions() const {
rule_ptr result = rule_ptr(new Char(value[0]));
for (int i = 1; i < value.length(); i++) {
result = rule_ptr(new Seq(result, rule_ptr(new Char(value[i]))));
}
return result->transitions();
}
bool String::operator==(const Rule &rule) const {
const String *other = dynamic_cast<const String *>(&rule);
return (other != NULL) && (other->value == value);
}
String * String::copy() const {
return new String(value);
}
std::string String::to_string() const {
return value;
}
}
}

21
src/rules/string.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef __tree_sitter__string__
#define __tree_sitter__string__
#include "rule.h"
namespace tree_sitter {
namespace rules {
class String : public Rule {
public:
String(std::string value);
TransitionMap<Rule> transitions() const;
String * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
std::string value;
};
}
}
#endif