This commit is contained in:
Max Brunsfeld 2014-01-04 15:30:05 -08:00
parent 29c81167c0
commit 04d18b56ed
10 changed files with 39 additions and 42 deletions

View file

@ -3,6 +3,7 @@
#include "parse_table.h"
#include "prepare_grammar.h"
#include "c_code.h"
#include "../fixtures/grammars/arithmetic.h"
#include <fstream>
START_TEST

View file

@ -1,4 +1,6 @@
#include "spec_helper.h"
#include "item.h"
#include "../../fixtures/grammars/arithmetic.h"
using namespace tree_sitter::lr;

View file

@ -1,5 +1,6 @@
#include "spec_helper.h"
#include <functional>
#include "table_builder.h"
using namespace tree_sitter::lr;
using namespace tree_sitter::rules;

View file

@ -3,14 +3,9 @@
#include "bandit/bandit.h"
#include <iostream>
#include "transition_map.h"
#include "rules.h"
#include "item.h"
#include "item_set.h"
#include <unordered_set>
#include <unordered_map>
#include "grammar.h"
#include "parse_table.h"
#include "table_builder.h"
#include "../fixtures/grammars/arithmetic.h"
using namespace tree_sitter;
using namespace std;
@ -21,7 +16,7 @@ using namespace bandit;
namespace std {
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: ");
bool started = false;
for (auto item : set) {
@ -33,7 +28,7 @@ namespace std {
}
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: ");
bool started = false;
for (auto pair : map) {
@ -64,8 +59,8 @@ namespace snowhouse {
template<typename ExpectedType>
struct Stringizer<EqualsPointerConstraint<ExpectedType>>
{
static std::string ToString(const EqualsPointerConstraint<ExpectedType>& constraint) {
std::ostringstream builder;
static string ToString(const EqualsPointerConstraint<ExpectedType>& constraint) {
ostringstream builder;
builder << "pointer to " << snowhouse::Stringize(constraint.expected);
return builder.str();
}

View file

@ -1,18 +1,23 @@
#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 {
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),
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),
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);
return (iter == rules.end()) ?
rules::rule_ptr(nullptr) :
@ -42,7 +47,7 @@ namespace tree_sitter {
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: ");
bool started = false;
for (auto pair : grammar.rules) {

View file

@ -2,29 +2,31 @@
#include "grammar.h"
#include "transitions.h"
#include <iostream>
using namespace std;
using std::string;
using std::vector;
using std::dynamic_pointer_cast;
using std::make_shared;
using std::ostream;
namespace tree_sitter {
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(rule),
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);
}
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);
}
transition_map<rules::Rule, Item> Item::transitions() const {
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);
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;
}
std::ostream& operator<<(ostream &stream, const Item &item) {
ostream& operator<<(ostream &stream, const Item &item) {
return stream <<
string("#<item '") <<
item.rule_name <<

View file

@ -45,9 +45,6 @@ namespace tree_sitter {
}
}
// State
LexState::LexState() : actions(unordered_map<CharMatch, unordered_set<LexAction>>()) {}
// Table
size_t LexTable::add_state() {
states.push_back(LexState());

View file

@ -48,7 +48,6 @@ namespace tree_sitter {
class LexState {
public:
LexState();
std::unordered_map<CharMatch, std::unordered_set<LexAction>> actions;
std::unordered_set<LexAction> default_actions;
};

View file

@ -1,6 +1,8 @@
#include "parse_table.h"
using namespace std;
using std::string;
using std::ostream;
using std::to_string;
namespace tree_sitter {
namespace lr {
@ -48,11 +50,7 @@ namespace tree_sitter {
}
// State
ParseState::ParseState() :
actions(unordered_map<string, unordered_set<ParseAction>>()),
default_actions(unordered_set<ParseAction>()),
lex_state_index(-1)
{}
ParseState::ParseState() : lex_state_index(-1) {}
// Table
size_t ParseTable::add_state() {

View file

@ -1,13 +1,10 @@
#include "table_builder.h"
#include <unordered_map>
#include "item_set.h"
#include "rules.h"
#include "item_set.h"
#include "grammar.h"
#include <iostream>
using namespace std;
using std::pair;
using std::vector;
namespace tree_sitter {
namespace lr {
@ -109,15 +106,15 @@ namespace tree_sitter {
grammar(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_set = ItemSet(item, grammar);
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();
}
}