Make separate Item classes for parsing and lexing
This commit is contained in:
parent
d015d57a53
commit
289992344e
10 changed files with 132 additions and 117 deletions
|
|
@ -7,35 +7,9 @@ using std::ostream;
|
|||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
const int NO_SYMBOLS = -1;
|
||||
|
||||
Item::Item(const string &rule_name, const rules::rule_ptr rule, int consumed_sym_count) :
|
||||
Item::Item(const string &rule_name, const rules::rule_ptr rule) :
|
||||
rule_name(rule_name),
|
||||
rule(rule),
|
||||
consumed_sym_count(consumed_sym_count) {};
|
||||
|
||||
Item Item::at_beginning_of_rule(const string &rule_name, const Grammar &grammar) {
|
||||
return Item(rule_name, grammar.rule(rule_name), 0);
|
||||
}
|
||||
|
||||
Item Item::at_beginning_of_token(const string &rule_name, const Grammar &grammar) {
|
||||
return Item(rule_name, grammar.rule(rule_name), NO_SYMBOLS);
|
||||
}
|
||||
|
||||
int Item::next_sym_count() const {
|
||||
return (consumed_sym_count == NO_SYMBOLS) ? NO_SYMBOLS : (consumed_sym_count + 1);
|
||||
}
|
||||
|
||||
bool Item::operator==(const Item &other) const {
|
||||
bool rule_names_eq = other.rule_name == rule_name;
|
||||
bool rules_eq = (*other.rule == *rule);
|
||||
bool consumed_sym_counts_eq = (other.consumed_sym_count == consumed_sym_count);
|
||||
return rule_names_eq && rules_eq && consumed_sym_counts_eq;
|
||||
}
|
||||
|
||||
bool Item::operator<(const Item &other) const {
|
||||
return rule_name < other.rule_name;
|
||||
}
|
||||
rule(rule) {};
|
||||
|
||||
bool Item::is_done() const {
|
||||
for (auto pair : rule_transitions(rule))
|
||||
|
|
@ -46,11 +20,35 @@ namespace tree_sitter {
|
|||
|
||||
ostream& operator<<(ostream &stream, const Item &item) {
|
||||
return stream <<
|
||||
string("#<item '") <<
|
||||
item.rule_name <<
|
||||
string("' ") <<
|
||||
*item.rule <<
|
||||
string(">");
|
||||
string("#<item '") <<
|
||||
item.rule_name <<
|
||||
string("' ") <<
|
||||
*item.rule <<
|
||||
string(">");
|
||||
}
|
||||
|
||||
bool Item::operator<(const Item &other) const {
|
||||
return rule_name < other.rule_name;
|
||||
}
|
||||
|
||||
LexItem::LexItem(const std::string &rule_name, const rules::rule_ptr rule) : Item(rule_name, rule) {}
|
||||
|
||||
bool LexItem::operator==(const LexItem &other) const {
|
||||
bool rule_names_eq = other.rule_name == rule_name;
|
||||
bool rules_eq = (*other.rule == *rule);
|
||||
return rule_names_eq && rules_eq;
|
||||
}
|
||||
|
||||
ParseItem::ParseItem(const std::string &rule_name, const rules::rule_ptr rule, int consumed_sym_count) :
|
||||
Item(rule_name, rule),
|
||||
consumed_sym_count(consumed_sym_count),
|
||||
lookahead_sym_name("") {}
|
||||
|
||||
bool ParseItem::operator==(const ParseItem &other) const {
|
||||
bool rule_names_eq = other.rule_name == rule_name;
|
||||
bool rules_eq = (*other.rule == *rule);
|
||||
bool consumed_sym_counts_eq = (other.consumed_sym_count == consumed_sym_count);
|
||||
return rule_names_eq && rules_eq && consumed_sym_counts_eq;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue