Cleanup
This commit is contained in:
parent
29c81167c0
commit
04d18b56ed
10 changed files with 39 additions and 42 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
#include "parse_table.h"
|
#include "parse_table.h"
|
||||||
#include "prepare_grammar.h"
|
#include "prepare_grammar.h"
|
||||||
#include "c_code.h"
|
#include "c_code.h"
|
||||||
|
#include "../fixtures/grammars/arithmetic.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
START_TEST
|
START_TEST
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#include "spec_helper.h"
|
#include "spec_helper.h"
|
||||||
|
#include "item.h"
|
||||||
|
#include "../../fixtures/grammars/arithmetic.h"
|
||||||
|
|
||||||
using namespace tree_sitter::lr;
|
using namespace tree_sitter::lr;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "spec_helper.h"
|
#include "spec_helper.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include "table_builder.h"
|
||||||
|
|
||||||
using namespace tree_sitter::lr;
|
using namespace tree_sitter::lr;
|
||||||
using namespace tree_sitter::rules;
|
using namespace tree_sitter::rules;
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,9 @@
|
||||||
|
|
||||||
#include "bandit/bandit.h"
|
#include "bandit/bandit.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "transition_map.h"
|
#include <unordered_set>
|
||||||
#include "rules.h"
|
#include <unordered_map>
|
||||||
#include "item.h"
|
|
||||||
#include "item_set.h"
|
|
||||||
#include "grammar.h"
|
#include "grammar.h"
|
||||||
#include "parse_table.h"
|
|
||||||
#include "table_builder.h"
|
|
||||||
#include "../fixtures/grammars/arithmetic.h"
|
|
||||||
|
|
||||||
using namespace tree_sitter;
|
using namespace tree_sitter;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
@ -21,7 +16,7 @@ using namespace bandit;
|
||||||
|
|
||||||
namespace std {
|
namespace std {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline std::ostream& operator<<(std::ostream &stream, const unordered_set<T> &set) {
|
inline ostream& operator<<(ostream &stream, const unordered_set<T> &set) {
|
||||||
stream << string("#<set: ");
|
stream << string("#<set: ");
|
||||||
bool started = false;
|
bool started = false;
|
||||||
for (auto item : set) {
|
for (auto item : set) {
|
||||||
|
|
@ -33,7 +28,7 @@ namespace std {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TKey, typename TValue>
|
template<typename TKey, typename TValue>
|
||||||
inline std::ostream& operator<<(std::ostream &stream, const unordered_map<TKey, TValue> &map) {
|
inline ostream& operator<<(ostream &stream, const unordered_map<TKey, TValue> &map) {
|
||||||
stream << string("#<map: ");
|
stream << string("#<map: ");
|
||||||
bool started = false;
|
bool started = false;
|
||||||
for (auto pair : map) {
|
for (auto pair : map) {
|
||||||
|
|
@ -64,8 +59,8 @@ namespace snowhouse {
|
||||||
template<typename ExpectedType>
|
template<typename ExpectedType>
|
||||||
struct Stringizer<EqualsPointerConstraint<ExpectedType>>
|
struct Stringizer<EqualsPointerConstraint<ExpectedType>>
|
||||||
{
|
{
|
||||||
static std::string ToString(const EqualsPointerConstraint<ExpectedType>& constraint) {
|
static string ToString(const EqualsPointerConstraint<ExpectedType>& constraint) {
|
||||||
std::ostringstream builder;
|
ostringstream builder;
|
||||||
builder << "pointer to " << snowhouse::Stringize(constraint.expected);
|
builder << "pointer to " << snowhouse::Stringize(constraint.expected);
|
||||||
return builder.str();
|
return builder.str();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,23 @@
|
||||||
#include "grammar.h"
|
#include "grammar.h"
|
||||||
|
|
||||||
using namespace std;
|
using std::unordered_map;
|
||||||
|
using std::vector;
|
||||||
|
using std::string;
|
||||||
|
using std::pair;
|
||||||
|
using std::initializer_list;
|
||||||
|
using std::ostream;
|
||||||
|
|
||||||
namespace tree_sitter {
|
namespace tree_sitter {
|
||||||
Grammar::Grammar(const std::initializer_list<std::pair<const std::string, const rules::rule_ptr>> &rules) :
|
Grammar::Grammar(const initializer_list<pair<const string, const rules::rule_ptr>> &rules) :
|
||||||
rules(rules),
|
rules(rules),
|
||||||
start_rule_name(rules.begin()->first) {}
|
start_rule_name(rules.begin()->first) {}
|
||||||
|
|
||||||
Grammar::Grammar(std::string start_rule_name, const std::unordered_map<std::string, const rules::rule_ptr> &rules) :
|
Grammar::Grammar(std::string start_rule_name, const unordered_map<string, const rules::rule_ptr> &rules) :
|
||||||
rules(rules),
|
rules(rules),
|
||||||
start_rule_name(start_rule_name) {}
|
start_rule_name(start_rule_name) {}
|
||||||
|
|
||||||
|
|
||||||
const rules::rule_ptr Grammar::rule(const std::string &name) const {
|
const rules::rule_ptr Grammar::rule(const string &name) const {
|
||||||
auto iter = rules.find(name);
|
auto iter = rules.find(name);
|
||||||
return (iter == rules.end()) ?
|
return (iter == rules.end()) ?
|
||||||
rules::rule_ptr(nullptr) :
|
rules::rule_ptr(nullptr) :
|
||||||
|
|
@ -42,7 +47,7 @@ namespace tree_sitter {
|
||||||
return rules.find(symbol.name) != rules.end();
|
return rules.find(symbol.name) != rules.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream &stream, const Grammar &grammar) {
|
ostream& operator<<(ostream &stream, const Grammar &grammar) {
|
||||||
stream << string("#<grammar: ");
|
stream << string("#<grammar: ");
|
||||||
bool started = false;
|
bool started = false;
|
||||||
for (auto pair : grammar.rules) {
|
for (auto pair : grammar.rules) {
|
||||||
|
|
|
||||||
|
|
@ -2,29 +2,31 @@
|
||||||
#include "grammar.h"
|
#include "grammar.h"
|
||||||
#include "transitions.h"
|
#include "transitions.h"
|
||||||
|
|
||||||
#include <iostream>
|
using std::string;
|
||||||
|
using std::vector;
|
||||||
using namespace std;
|
using std::dynamic_pointer_cast;
|
||||||
|
using std::make_shared;
|
||||||
|
using std::ostream;
|
||||||
|
|
||||||
namespace tree_sitter {
|
namespace tree_sitter {
|
||||||
namespace lr {
|
namespace lr {
|
||||||
Item::Item(const std::string &rule_name, const rules::rule_ptr rule, int consumed_sym_count) :
|
Item::Item(const string &rule_name, const rules::rule_ptr rule, int consumed_sym_count) :
|
||||||
rule_name(rule_name),
|
rule_name(rule_name),
|
||||||
rule(rule),
|
rule(rule),
|
||||||
consumed_sym_count(consumed_sym_count) {};
|
consumed_sym_count(consumed_sym_count) {};
|
||||||
|
|
||||||
Item Item::at_beginning_of_rule(const std::string &rule_name, const Grammar &grammar) {
|
Item Item::at_beginning_of_rule(const string &rule_name, const Grammar &grammar) {
|
||||||
return Item(rule_name, grammar.rule(rule_name), 0);
|
return Item(rule_name, grammar.rule(rule_name), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Item Item::at_beginning_of_token(const std::string &rule_name, const Grammar &grammar) {
|
Item Item::at_beginning_of_token(const string &rule_name, const Grammar &grammar) {
|
||||||
return Item(rule_name, grammar.rule(rule_name), -1);
|
return Item(rule_name, grammar.rule(rule_name), -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
transition_map<rules::Rule, Item> Item::transitions() const {
|
transition_map<rules::Rule, Item> Item::transitions() const {
|
||||||
return lr::transitions(rule).map<Item>([&](rules::rule_ptr to_rule) -> item_ptr {
|
return lr::transitions(rule).map<Item>([&](rules::rule_ptr to_rule) -> item_ptr {
|
||||||
int next_sym_count = (consumed_sym_count == -1) ? -1 : (consumed_sym_count + 1);
|
int next_sym_count = (consumed_sym_count == -1) ? -1 : (consumed_sym_count + 1);
|
||||||
return std::make_shared<Item>(rule_name, to_rule, next_sym_count);
|
return make_shared<Item>(rule_name, to_rule, next_sym_count);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -51,7 +53,7 @@ namespace tree_sitter {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(ostream &stream, const Item &item) {
|
ostream& operator<<(ostream &stream, const Item &item) {
|
||||||
return stream <<
|
return stream <<
|
||||||
string("#<item '") <<
|
string("#<item '") <<
|
||||||
item.rule_name <<
|
item.rule_name <<
|
||||||
|
|
|
||||||
|
|
@ -45,9 +45,6 @@ namespace tree_sitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// State
|
|
||||||
LexState::LexState() : actions(unordered_map<CharMatch, unordered_set<LexAction>>()) {}
|
|
||||||
|
|
||||||
// Table
|
// Table
|
||||||
size_t LexTable::add_state() {
|
size_t LexTable::add_state() {
|
||||||
states.push_back(LexState());
|
states.push_back(LexState());
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,6 @@ namespace tree_sitter {
|
||||||
|
|
||||||
class LexState {
|
class LexState {
|
||||||
public:
|
public:
|
||||||
LexState();
|
|
||||||
std::unordered_map<CharMatch, std::unordered_set<LexAction>> actions;
|
std::unordered_map<CharMatch, std::unordered_set<LexAction>> actions;
|
||||||
std::unordered_set<LexAction> default_actions;
|
std::unordered_set<LexAction> default_actions;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#include "parse_table.h"
|
#include "parse_table.h"
|
||||||
|
|
||||||
using namespace std;
|
using std::string;
|
||||||
|
using std::ostream;
|
||||||
|
using std::to_string;
|
||||||
|
|
||||||
namespace tree_sitter {
|
namespace tree_sitter {
|
||||||
namespace lr {
|
namespace lr {
|
||||||
|
|
@ -48,11 +50,7 @@ namespace tree_sitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// State
|
// State
|
||||||
ParseState::ParseState() :
|
ParseState::ParseState() : lex_state_index(-1) {}
|
||||||
actions(unordered_map<string, unordered_set<ParseAction>>()),
|
|
||||||
default_actions(unordered_set<ParseAction>()),
|
|
||||||
lex_state_index(-1)
|
|
||||||
{}
|
|
||||||
|
|
||||||
// Table
|
// Table
|
||||||
size_t ParseTable::add_state() {
|
size_t ParseTable::add_state() {
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,10 @@
|
||||||
#include "table_builder.h"
|
#include "table_builder.h"
|
||||||
#include <unordered_map>
|
|
||||||
#include "item_set.h"
|
#include "item_set.h"
|
||||||
#include "rules.h"
|
#include "rules.h"
|
||||||
#include "item_set.h"
|
|
||||||
#include "grammar.h"
|
#include "grammar.h"
|
||||||
|
|
||||||
#include <iostream>
|
using std::pair;
|
||||||
|
using std::vector;
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
namespace tree_sitter {
|
namespace tree_sitter {
|
||||||
namespace lr {
|
namespace lr {
|
||||||
|
|
@ -109,15 +106,15 @@ namespace tree_sitter {
|
||||||
grammar(grammar),
|
grammar(grammar),
|
||||||
lex_grammar(lex_grammar) {};
|
lex_grammar(lex_grammar) {};
|
||||||
|
|
||||||
std::pair<ParseTable, LexTable> build() {
|
pair<ParseTable, LexTable> build() {
|
||||||
auto item = Item(ParseTable::START, rules::sym(grammar.start_rule_name), 0);
|
auto item = Item(ParseTable::START, rules::sym(grammar.start_rule_name), 0);
|
||||||
auto item_set = ItemSet(item, grammar);
|
auto item_set = ItemSet(item, grammar);
|
||||||
add_parse_state(item_set);
|
add_parse_state(item_set);
|
||||||
return std::pair<ParseTable, LexTable>(parse_table, lex_table);
|
return pair<ParseTable, LexTable>(parse_table, lex_table);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::pair<ParseTable, LexTable> build_tables(const Grammar &grammar, const Grammar &lex_grammar) {
|
pair<ParseTable, LexTable> build_tables(const Grammar &grammar, const Grammar &lex_grammar) {
|
||||||
return TableBuilder(grammar, lex_grammar).build();
|
return TableBuilder(grammar, lex_grammar).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue