Update rule hash implementation
This commit is contained in:
parent
3116b457fe
commit
257b8d7b68
18 changed files with 80 additions and 30 deletions
52
spec/fixtures/parsers/arithmetic.c
vendored
52
spec/fixtures/parsers/arithmetic.c
vendored
|
|
@ -63,7 +63,7 @@ static void ts_lex(TSParser *parser) {
|
|||
LEX_ERROR();
|
||||
case 10:
|
||||
if (isdigit(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(6);
|
||||
ADVANCE(8);
|
||||
LEX_ERROR();
|
||||
case 11:
|
||||
ACCEPT_TOKEN(ts_symbol_left_paren);
|
||||
|
|
@ -81,47 +81,43 @@ static void ts_lex(TSParser *parser) {
|
|||
if (isdigit(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(4);
|
||||
if (isalnum(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(16);
|
||||
ADVANCE(1);
|
||||
LEX_ERROR();
|
||||
case 16:
|
||||
if (isalnum(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(2);
|
||||
LEX_ERROR();
|
||||
case 17:
|
||||
ACCEPT_TOKEN(ts_symbol_expression);
|
||||
case 18:
|
||||
case 17:
|
||||
if (LOOKAHEAD_CHAR() == '*')
|
||||
ADVANCE(19);
|
||||
ADVANCE(18);
|
||||
LEX_ERROR();
|
||||
case 19:
|
||||
case 18:
|
||||
ACCEPT_TOKEN(ts_symbol_times);
|
||||
case 20:
|
||||
case 19:
|
||||
if (LOOKAHEAD_CHAR() == '(')
|
||||
ADVANCE(11);
|
||||
if (isdigit(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(4);
|
||||
if (isalnum(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(16);
|
||||
ADVANCE(1);
|
||||
LEX_ERROR();
|
||||
case 21:
|
||||
case 20:
|
||||
ACCEPT_TOKEN(ts_symbol_term);
|
||||
case 22:
|
||||
case 21:
|
||||
ACCEPT_TOKEN(ts_symbol_factor);
|
||||
case 23:
|
||||
case 22:
|
||||
if (LOOKAHEAD_CHAR() == '(')
|
||||
ADVANCE(11);
|
||||
if (isdigit(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(4);
|
||||
if (isalnum(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(16);
|
||||
ADVANCE(1);
|
||||
LEX_ERROR();
|
||||
case 23:
|
||||
if (LOOKAHEAD_CHAR() == ')')
|
||||
ADVANCE(24);
|
||||
LEX_ERROR();
|
||||
case 24:
|
||||
if (LOOKAHEAD_CHAR() == ')')
|
||||
ADVANCE(25);
|
||||
LEX_ERROR();
|
||||
case 25:
|
||||
ACCEPT_TOKEN(ts_symbol_right_paren);
|
||||
case 26:
|
||||
case 25:
|
||||
ACCEPT_TOKEN(ts_symbol_factor);
|
||||
default:
|
||||
LEX_ERROR();
|
||||
|
|
@ -183,13 +179,13 @@ TSTree ts_parse_arithmetic(const char *input) {
|
|||
PARSE_ERROR();
|
||||
}
|
||||
case 4:
|
||||
SET_LEX_STATE(17);
|
||||
SET_LEX_STATE(16);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
default:
|
||||
REDUCE(ts_symbol_expression, 3);
|
||||
}
|
||||
case 5:
|
||||
SET_LEX_STATE(18);
|
||||
SET_LEX_STATE(17);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_times:
|
||||
SHIFT(6);
|
||||
|
|
@ -197,7 +193,7 @@ TSTree ts_parse_arithmetic(const char *input) {
|
|||
PARSE_ERROR();
|
||||
}
|
||||
case 6:
|
||||
SET_LEX_STATE(20);
|
||||
SET_LEX_STATE(19);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_left_paren:
|
||||
SHIFT(9);
|
||||
|
|
@ -211,19 +207,19 @@ TSTree ts_parse_arithmetic(const char *input) {
|
|||
PARSE_ERROR();
|
||||
}
|
||||
case 7:
|
||||
SET_LEX_STATE(21);
|
||||
SET_LEX_STATE(20);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
default:
|
||||
REDUCE(ts_symbol_term, 3);
|
||||
}
|
||||
case 8:
|
||||
SET_LEX_STATE(22);
|
||||
SET_LEX_STATE(21);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
default:
|
||||
REDUCE(ts_symbol_factor, 1);
|
||||
}
|
||||
case 9:
|
||||
SET_LEX_STATE(23);
|
||||
SET_LEX_STATE(22);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_left_paren:
|
||||
SHIFT(9);
|
||||
|
|
@ -241,7 +237,7 @@ TSTree ts_parse_arithmetic(const char *input) {
|
|||
PARSE_ERROR();
|
||||
}
|
||||
case 10:
|
||||
SET_LEX_STATE(24);
|
||||
SET_LEX_STATE(23);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_right_paren:
|
||||
SHIFT(11);
|
||||
|
|
@ -249,7 +245,7 @@ TSTree ts_parse_arithmetic(const char *input) {
|
|||
PARSE_ERROR();
|
||||
}
|
||||
case 11:
|
||||
SET_LEX_STATE(26);
|
||||
SET_LEX_STATE(25);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
default:
|
||||
REDUCE(ts_symbol_factor, 3);
|
||||
|
|
|
|||
|
|
@ -9,6 +9,10 @@ namespace tree_sitter {
|
|||
return dynamic_cast<const Blank *>(&rule) != nullptr;
|
||||
}
|
||||
|
||||
size_t Blank::hash_code() const {
|
||||
return typeid(this).hash_code();
|
||||
}
|
||||
|
||||
std::string Blank::to_string() const {
|
||||
return "#<blank>";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,8 +8,10 @@ namespace tree_sitter {
|
|||
class Blank : public Rule {
|
||||
public:
|
||||
Blank();
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
std::string to_string() const;
|
||||
size_t hash_code() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "transition_map.h"
|
||||
|
||||
using std::string;
|
||||
using std::hash;
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
@ -14,6 +15,10 @@ namespace tree_sitter {
|
|||
return other && (other->value == value);
|
||||
}
|
||||
|
||||
size_t Character::hash_code() const {
|
||||
return typeid(this).hash_code() ^ hash<string>()(CharMatchToString(value));
|
||||
}
|
||||
|
||||
string Character::to_string() const {
|
||||
return string("#<char ") + CharMatchToString(value) + ">";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@ namespace tree_sitter {
|
|||
Character(char character);
|
||||
Character(CharClass character_class);
|
||||
Character(char min_character, char max_character);
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
size_t hash_code() const;
|
||||
std::string to_string() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,10 @@ namespace tree_sitter {
|
|||
return other && (*other->left == *left) && (*other->right == *right);
|
||||
}
|
||||
|
||||
size_t Choice::hash_code() const {
|
||||
return typeid(this).hash_code() ^ left->hash_code() ^ right->hash_code();
|
||||
}
|
||||
|
||||
string Choice::to_string() const {
|
||||
return string("#<choice ") + left->to_string() + " " + right->to_string() + ">";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ namespace tree_sitter {
|
|||
class Choice : public Rule {
|
||||
public:
|
||||
Choice(rule_ptr left, rule_ptr right);
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
size_t hash_code() const;
|
||||
std::string to_string() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "transition_map.h"
|
||||
|
||||
using std::string;
|
||||
using std::hash;
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
@ -107,7 +108,11 @@ namespace tree_sitter {
|
|||
auto pattern = dynamic_cast<const Pattern *>(&other);
|
||||
return pattern && (pattern->value == value);
|
||||
}
|
||||
|
||||
|
||||
size_t Pattern::hash_code() const {
|
||||
return typeid(this).hash_code() ^ hash<string>()(value);
|
||||
}
|
||||
|
||||
string Pattern::to_string() const {
|
||||
return string("#<pattern '") + value + "'>";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,9 +9,12 @@ namespace tree_sitter {
|
|||
const std::string value;
|
||||
public:
|
||||
Pattern(const std::string &string);
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
size_t hash_code() const;
|
||||
std::string to_string() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
|
||||
rule_ptr to_rule_tree() const;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,10 @@ namespace tree_sitter {
|
|||
return other && (*other->content == *content);
|
||||
}
|
||||
|
||||
size_t Repeat::hash_code() const {
|
||||
return typeid(this).hash_code() ^ content->hash_code();
|
||||
}
|
||||
|
||||
string Repeat::to_string() const {
|
||||
return string("#<repeat ") + content->to_string() + ">";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ namespace tree_sitter {
|
|||
class Repeat : public Rule {
|
||||
public:
|
||||
Repeat(rule_ptr content);
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
size_t hash_code() const;
|
||||
std::string to_string() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ namespace tree_sitter {
|
|||
class Rule {
|
||||
public:
|
||||
virtual bool operator==(const Rule& other) const = 0;
|
||||
virtual size_t hash_code() const = 0;
|
||||
virtual std::string to_string() const = 0;
|
||||
virtual void accept(Visitor &visitor) const = 0;
|
||||
};
|
||||
|
|
@ -24,7 +25,7 @@ namespace std {
|
|||
template<>
|
||||
struct hash<tree_sitter::rules::Rule> {
|
||||
size_t operator()(const tree_sitter::rules::Rule &rule) {
|
||||
return std::hash<std::string>()(rule.to_string());
|
||||
return rule.hash_code();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,10 @@ namespace tree_sitter {
|
|||
return other && (*other->left == *left) && (*other->right == *right);
|
||||
}
|
||||
|
||||
size_t Seq::hash_code() const {
|
||||
return typeid(this).hash_code() ^ left->hash_code() ^ right->hash_code();
|
||||
}
|
||||
|
||||
string Seq::to_string() const {
|
||||
return string("#<seq ") + left->to_string() + " " + right->to_string() + ">";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ namespace tree_sitter {
|
|||
class Seq : public Rule {
|
||||
public:
|
||||
Seq(rule_ptr left, rule_ptr right);
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
size_t hash_code() const;
|
||||
std::string to_string() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "transition_map.h"
|
||||
|
||||
using std::string;
|
||||
using std::hash;
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
@ -12,6 +13,10 @@ namespace tree_sitter {
|
|||
return (other != NULL) && (other->value == value);
|
||||
}
|
||||
|
||||
size_t String::hash_code() const {
|
||||
return typeid(this).hash_code() ^ hash<string>()(value);
|
||||
}
|
||||
|
||||
string String::to_string() const {
|
||||
return string("#<string '") + value + "'>";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ namespace tree_sitter {
|
|||
class String : public Rule {
|
||||
public:
|
||||
String(std::string value);
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
size_t hash_code() const;
|
||||
std::string to_string() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "transition_map.h"
|
||||
|
||||
using std::string;
|
||||
using std::hash;
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
@ -12,6 +13,10 @@ namespace tree_sitter {
|
|||
return other && (other->name == name);
|
||||
}
|
||||
|
||||
size_t Symbol::hash_code() const {
|
||||
return typeid(this).hash_code() ^ hash<string>()(name);
|
||||
}
|
||||
|
||||
string Symbol::to_string() const {
|
||||
return string("#<sym '") + name + "'>";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ namespace tree_sitter {
|
|||
class Symbol : public Rule {
|
||||
public:
|
||||
Symbol(const std::string &name);
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
size_t hash_code() const;
|
||||
std::string to_string() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue