Remove ItemSet class; just use a set
This commit is contained in:
parent
29f73afbc5
commit
d015d57a53
11 changed files with 93 additions and 162 deletions
|
|
@ -33,6 +33,10 @@ namespace tree_sitter {
|
|||
return rule_names_eq && rules_eq && consumed_sym_counts_eq;
|
||||
}
|
||||
|
||||
bool Item::operator<(const Item &other) const {
|
||||
return rule_name < other.rule_name;
|
||||
}
|
||||
|
||||
bool Item::is_done() const {
|
||||
for (auto pair : rule_transitions(rule))
|
||||
if (*pair.first == rules::Blank())
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <string>
|
||||
#include "rule.h"
|
||||
#include <set>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace tree_sitter {
|
||||
class Grammar;
|
||||
|
|
@ -18,6 +19,7 @@ namespace tree_sitter {
|
|||
static Item at_beginning_of_token(const std::string &rule_name, const Grammar &grammar);
|
||||
|
||||
bool operator==(const Item &other) const;
|
||||
bool operator<(const Item &other) const;
|
||||
bool is_done() const;
|
||||
int next_sym_count() const;
|
||||
|
||||
|
|
@ -26,6 +28,8 @@ namespace tree_sitter {
|
|||
const int consumed_sym_count;
|
||||
};
|
||||
|
||||
typedef std::set<Item> ItemSet;
|
||||
|
||||
std::ostream& operator<<(std::ostream &stream, const Item &item);
|
||||
}
|
||||
}
|
||||
|
|
@ -33,13 +37,24 @@ namespace tree_sitter {
|
|||
namespace std {
|
||||
template<>
|
||||
struct hash<tree_sitter::build_tables::Item> {
|
||||
size_t operator()(const tree_sitter::build_tables::Item &item) {
|
||||
size_t operator()(const tree_sitter::build_tables::Item &item) const {
|
||||
return
|
||||
hash<std::string>()(item.rule_name) ^
|
||||
hash<tree_sitter::rules::Rule>()(*item.rule) ^
|
||||
hash<int>()(item.consumed_sym_count);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct hash<const tree_sitter::build_tables::ItemSet> {
|
||||
size_t operator()(const tree_sitter::build_tables::ItemSet &item_set) const {
|
||||
size_t result = hash<size_t>()(item_set.size());
|
||||
for (auto item : item_set)
|
||||
result ^= hash<tree_sitter::build_tables::Item>()(item);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
#include "item_set.h"
|
||||
|
||||
using std::vector;
|
||||
using std::ostream;
|
||||
using std::string;
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
ItemSet::ItemSet(const vector<Item> &items) : contents(items) {}
|
||||
|
||||
bool ItemSet::operator==(const tree_sitter::build_tables::ItemSet &other) const {
|
||||
return contents == other.contents;
|
||||
}
|
||||
|
||||
ItemSet::const_iterator ItemSet::begin() const {
|
||||
return contents.begin();
|
||||
}
|
||||
|
||||
ItemSet::const_iterator ItemSet::end() const {
|
||||
return contents.end();
|
||||
}
|
||||
|
||||
size_t ItemSet::size() const {
|
||||
return contents.size();
|
||||
}
|
||||
|
||||
ostream& operator<<(ostream &stream, const ItemSet &item_set) {
|
||||
stream << string("#<item_set ");
|
||||
for (Item item : item_set)
|
||||
stream << item << string(" ");
|
||||
return stream << string(">");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
#ifndef __TreeSitter__item_set__
|
||||
#define __TreeSitter__item_set__
|
||||
|
||||
#include "item.h"
|
||||
#include "rule.h"
|
||||
#include <vector>
|
||||
#include <set>
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
class ItemSet;
|
||||
|
||||
class ItemSet {
|
||||
const std::vector<Item> contents;
|
||||
public:
|
||||
ItemSet(const std::vector<Item> &items);
|
||||
bool operator==(const ItemSet &other) const;
|
||||
|
||||
typedef Item value_type;
|
||||
typedef std::vector<Item>::const_iterator const_iterator;
|
||||
const_iterator begin() const;
|
||||
const_iterator end() const;
|
||||
size_t size() const;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream &stream, const ItemSet &item_set);
|
||||
}
|
||||
}
|
||||
|
||||
namespace std {
|
||||
template<>
|
||||
struct hash<const tree_sitter::build_tables::ItemSet> {
|
||||
size_t operator()(const tree_sitter::build_tables::ItemSet &item_set) const {
|
||||
size_t result = hash<size_t>()(item_set.size());
|
||||
for (auto item : item_set)
|
||||
result ^= hash<tree_sitter::build_tables::Item>()(item);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -8,25 +8,25 @@ using std::vector;
|
|||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
static bool vector_contains(vector<Item> items, build_tables::Item item) {
|
||||
static bool contains(ItemSet items, Item item) {
|
||||
return (std::find(items.begin(), items.end(), item) != items.end());
|
||||
}
|
||||
|
||||
static void add_item(vector<Item> &vector, const Item &item, const Grammar &grammar) {
|
||||
if (!vector_contains(vector, item)) {
|
||||
vector.push_back(item);
|
||||
static void add_item(ItemSet &item_set, const Item &item, const Grammar &grammar) {
|
||||
if (!contains(item_set, item)) {
|
||||
item_set.insert(item);
|
||||
for (rules::Symbol rule : next_non_terminals(item, grammar)) {
|
||||
Item next_item = Item::at_beginning_of_rule(rule.name, grammar);
|
||||
add_item(vector, next_item, grammar);
|
||||
add_item(item_set, next_item, grammar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ItemSet item_set_closure(const ItemSet &item_set, const Grammar &grammar) {
|
||||
vector<Item> items;
|
||||
const ItemSet item_set_closure(const ItemSet &item_set, const Grammar &grammar) {
|
||||
ItemSet result;
|
||||
for (Item item : item_set)
|
||||
add_item(items, item, grammar);
|
||||
return ItemSet(items);
|
||||
add_item(result, item, grammar);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
#ifndef __tree_sitter__close_item_set__
|
||||
#define __tree_sitter__close_item_set__
|
||||
|
||||
#include "item_set.h"
|
||||
#include "item.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
class Grammar;
|
||||
|
||||
namespace build_tables {
|
||||
ItemSet item_set_closure(const ItemSet &item_set, const Grammar &grammar);
|
||||
const ItemSet item_set_closure(const ItemSet &item_set, const Grammar &grammar);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
#include "character.h"
|
||||
#include "symbol.h"
|
||||
#include "transition_map.h"
|
||||
#include "item_set.h"
|
||||
#include "item.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef __tree_sitter__first_terminal__
|
||||
#define __tree_sitter__first_terminal__
|
||||
|
||||
#include "item_set.h"
|
||||
#include "item.h"
|
||||
#include "symbol.h"
|
||||
#include <set>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#include "./perform.h"
|
||||
#include "item_set.h"
|
||||
#include "item.h"
|
||||
#include "item_set_closure.h"
|
||||
#include "next_symbols.h"
|
||||
#include "item_set_transitions.h"
|
||||
|
|
@ -83,10 +83,10 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
ItemSet lex_item_set_for_parse_item_set(const ItemSet &parse_item_set) {
|
||||
vector<Item> items;
|
||||
ItemSet result;
|
||||
for (rules::Symbol symbol : next_terminals(parse_item_set, grammar))
|
||||
items.push_back(Item::at_beginning_of_token(symbol.name, lex_grammar));
|
||||
return ItemSet(items);
|
||||
result.insert(Item::at_beginning_of_token(symbol.name, lex_grammar));
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t add_parse_state(const ItemSet &item_set) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue