Tidy up remaining files in build_tables namespace
This commit is contained in:
parent
9fd2821389
commit
71cc7a2dc2
14 changed files with 64 additions and 73 deletions
|
|
@ -21,7 +21,7 @@ class FirstSymbols : public rules::RuleFn<set<Symbol>> {
|
|||
public:
|
||||
explicit FirstSymbols(const SyntaxGrammar *grammar) : grammar(grammar) {}
|
||||
|
||||
protected:
|
||||
private:
|
||||
set<Symbol> apply_to(const Symbol *rule) {
|
||||
auto insertion_result = visited_symbols.insert(*rule);
|
||||
if (!insertion_result.second)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#include "compiler/build_tables/get_metadata.h"
|
||||
#include "compiler/build_tables/item.h"
|
||||
#include "compiler/build_tables/rule_can_be_blank.h"
|
||||
#include "compiler/rules/metadata.h"
|
||||
#include "compiler/build_tables/get_metadata.h"
|
||||
#include "tree_sitter/compiler.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
#ifndef COMPILER_BUILD_TABLES_ITEM_H_
|
||||
#define COMPILER_BUILD_TABLES_ITEM_H_
|
||||
|
||||
#include <unordered_set>
|
||||
#include "compiler/rules/symbol.h"
|
||||
#include "tree_sitter/compiler.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
|
|
|
|||
|
|
@ -22,10 +22,8 @@ const ParseItemSet item_set_closure(const ParseItem &starting_item,
|
|||
const set<Symbol> &starting_lookahead_symbols,
|
||||
const SyntaxGrammar &grammar) {
|
||||
ParseItemSet result;
|
||||
|
||||
vector<pair<ParseItem, set<Symbol>>> items_to_process = {
|
||||
{ starting_item, starting_lookahead_symbols }
|
||||
};
|
||||
vector<pair<ParseItem, set<Symbol>>> items_to_process;
|
||||
items_to_process.push_back({ starting_item, starting_lookahead_symbols });
|
||||
|
||||
while (!items_to_process.empty()) {
|
||||
ParseItem item = items_to_process.back().first;
|
||||
|
|
|
|||
|
|
@ -2,17 +2,20 @@
|
|||
#define COMPILER_BUILD_TABLES_ITEM_SET_CLOSURE_H_
|
||||
|
||||
#include <set>
|
||||
#include "compiler/rules/symbol.h"
|
||||
#include "compiler/build_tables/parse_item.h"
|
||||
#include "compiler/rules/symbol.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
|
||||
class SyntaxGrammar;
|
||||
|
||||
namespace build_tables {
|
||||
const ParseItemSet item_set_closure(
|
||||
const ParseItem &item, const std::set<rules::Symbol> &lookahead_symbols,
|
||||
const SyntaxGrammar &grammar);
|
||||
}
|
||||
}
|
||||
|
||||
const ParseItemSet item_set_closure(const ParseItem &,
|
||||
const std::set<rules::Symbol> &,
|
||||
const SyntaxGrammar &);
|
||||
|
||||
} // namespace build_tables
|
||||
} // namespace tree_sitter
|
||||
|
||||
#endif // COMPILER_BUILD_TABLES_ITEM_SET_CLOSURE_H_
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
#include "compiler/build_tables/item_set_transitions.h"
|
||||
#include <set>
|
||||
#include "compiler/build_tables/item_set_closure.h"
|
||||
#include "compiler/build_tables/rule_transitions.h"
|
||||
#include "compiler/build_tables/lex_item.h"
|
||||
#include "compiler/build_tables/merge_transitions.h"
|
||||
#include "compiler/rules/symbol.h"
|
||||
#include "compiler/build_tables/parse_item.h"
|
||||
#include "compiler/build_tables/rule_transitions.h"
|
||||
#include "compiler/prepared_grammar.h"
|
||||
#include "compiler/rules/symbol.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
|
|
|
|||
|
|
@ -1,20 +1,10 @@
|
|||
#include "compiler/build_tables/lex_conflict_manager.h"
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include "compiler/util/string_helpers.h"
|
||||
#include "compiler/prepared_grammar.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
|
||||
using std::string;
|
||||
using std::to_string;
|
||||
using std::map;
|
||||
using std::set;
|
||||
using std::vector;
|
||||
|
||||
LexConflictManager::LexConflictManager(const LexicalGrammar &grammar)
|
||||
: grammar(grammar) {}
|
||||
|
||||
|
|
@ -26,26 +16,30 @@ bool LexConflictManager::resolve_lex_action(const LexAction &old_action,
|
|||
switch (old_action.type) {
|
||||
case LexActionTypeError:
|
||||
return true;
|
||||
|
||||
case LexActionTypeAccept: {
|
||||
int old_precedence = *old_action.precedence_values.begin();
|
||||
|
||||
switch (new_action.type) {
|
||||
case LexActionTypeAccept: {
|
||||
int new_precedence = *new_action.precedence_values.begin();
|
||||
if (new_precedence > old_precedence) {
|
||||
if (new_precedence > old_precedence)
|
||||
return true;
|
||||
} else if (new_precedence < old_precedence) {
|
||||
else if (new_precedence < old_precedence)
|
||||
return false;
|
||||
} else {
|
||||
else
|
||||
return new_action.symbol.index < old_action.symbol.index;
|
||||
}
|
||||
}
|
||||
case LexActionTypeAdvance: { return true; }
|
||||
|
||||
case LexActionTypeAdvance:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ namespace build_tables {
|
|||
|
||||
using std::string;
|
||||
using std::ostream;
|
||||
using std::vector;
|
||||
|
||||
LexItem::LexItem(const rules::Symbol &lhs, const rules::rule_ptr rule)
|
||||
: Item(lhs, rule) {}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
#include "compiler/build_tables/parse_conflict_manager.h"
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include "compiler/util/string_helpers.h"
|
||||
#include "compiler/rules/built_in_symbols.h"
|
||||
#include "compiler/prepared_grammar.h"
|
||||
|
|
@ -12,8 +10,6 @@ namespace build_tables {
|
|||
|
||||
using std::string;
|
||||
using std::to_string;
|
||||
using std::map;
|
||||
using std::set;
|
||||
using std::vector;
|
||||
|
||||
ParseConflictManager::ParseConflictManager(const SyntaxGrammar &parse_grammar,
|
||||
|
|
@ -29,6 +25,7 @@ bool ParseConflictManager::resolve_parse_action(const rules::Symbol &symbol,
|
|||
switch (old_action.type) {
|
||||
case ParseActionTypeError:
|
||||
return true;
|
||||
|
||||
case ParseActionTypeShift: {
|
||||
int min_precedence = *old_action.precedence_values.begin();
|
||||
int max_precedence = *old_action.precedence_values.rbegin();
|
||||
|
|
@ -50,6 +47,7 @@ bool ParseConflictManager::resolve_parse_action(const rules::Symbol &symbol,
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
case ParseActionTypeReduce:
|
||||
switch (new_action.type) {
|
||||
case ParseActionTypeReduce: {
|
||||
|
|
@ -67,6 +65,7 @@ bool ParseConflictManager::resolve_parse_action(const rules::Symbol &symbol,
|
|||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,13 @@
|
|||
#ifndef COMPILER_BUILD_TABLES_PARSE_CONFLICT_MANAGER_H_
|
||||
#define COMPILER_BUILD_TABLES_PARSE_CONFLICT_MANAGER_H_
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include "tree_sitter/compiler.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "compiler/parse_table.h"
|
||||
#include "compiler/rules/symbol.h"
|
||||
#include "compiler/prepared_grammar.h"
|
||||
#include "compiler/rules/symbol.h"
|
||||
#include "tree_sitter/compiler.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
|
|
@ -19,17 +18,16 @@ class ParseConflictManager {
|
|||
std::set<Conflict> conflicts_;
|
||||
|
||||
public:
|
||||
ParseConflictManager(const SyntaxGrammar &parse_grammar,
|
||||
const LexicalGrammar &lex_grammar);
|
||||
ParseConflictManager(const SyntaxGrammar &, const LexicalGrammar &);
|
||||
bool resolve_parse_action(const rules::Symbol &symbol,
|
||||
const ParseAction &old_action,
|
||||
const ParseAction &new_action);
|
||||
const std::vector<Conflict> conflicts() const;
|
||||
|
||||
private:
|
||||
std::string symbol_name(const rules::Symbol &symbol);
|
||||
void record_conflict(const rules::Symbol &symbol, const ParseAction &left,
|
||||
const ParseAction &right);
|
||||
std::string symbol_name(const rules::Symbol &);
|
||||
void record_conflict(const rules::Symbol &, const ParseAction &,
|
||||
const ParseAction &);
|
||||
};
|
||||
|
||||
} // namespace build_tables
|
||||
|
|
|
|||
|
|
@ -1,14 +1,10 @@
|
|||
#include "compiler/build_tables/parse_item.h"
|
||||
#include <set>
|
||||
#include "tree_sitter/compiler.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
|
||||
using std::pair;
|
||||
using std::set;
|
||||
using std::string;
|
||||
using std::to_string;
|
||||
using std::ostream;
|
||||
|
||||
ParseItem::ParseItem(const rules::Symbol &lhs, const rules::rule_ptr rule,
|
||||
|
|
|
|||
|
|
@ -2,10 +2,9 @@
|
|||
#define COMPILER_BUILD_TABLES_PARSE_ITEM_H_
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include "compiler/rules/symbol.h"
|
||||
#include "compiler/build_tables/item.h"
|
||||
#include "compiler/rules/symbol.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
|
|
|
|||
|
|
@ -40,10 +40,12 @@ class CanBeBlankRecursive : public CanBeBlank {
|
|||
using CanBeBlank::visit;
|
||||
|
||||
public:
|
||||
using CanBeBlank::apply_to;
|
||||
explicit CanBeBlankRecursive(const SyntaxGrammar *grammar)
|
||||
: grammar(grammar) {}
|
||||
|
||||
private:
|
||||
using CanBeBlank::apply_to;
|
||||
|
||||
bool apply_to(const rules::Symbol *rule) {
|
||||
if (visited_symbols.find(*rule) == visited_symbols.end()) {
|
||||
visited_symbols.insert(*rule);
|
||||
|
|
|
|||
|
|
@ -4,11 +4,9 @@
|
|||
#include "compiler/rules/blank.h"
|
||||
#include "compiler/rules/choice.h"
|
||||
#include "compiler/rules/seq.h"
|
||||
#include "compiler/rules/string.h"
|
||||
#include "compiler/rules/repeat.h"
|
||||
#include "compiler/rules/metadata.h"
|
||||
#include "compiler/rules/symbol.h"
|
||||
#include "compiler/rules/pattern.h"
|
||||
#include "compiler/rules/character_set.h"
|
||||
#include "compiler/rules/visitor.h"
|
||||
|
||||
|
|
@ -17,48 +15,52 @@ namespace build_tables {
|
|||
|
||||
using std::map;
|
||||
using std::make_shared;
|
||||
using rules::rule_ptr;
|
||||
using rules::Symbol;
|
||||
using rules::CharacterSet;
|
||||
using rules::Choice;
|
||||
using rules::Symbol;
|
||||
using rules::rule_ptr;
|
||||
|
||||
template <typename T>
|
||||
void merge_transitions(map<T, rule_ptr> *left, const map<T, rule_ptr> &right);
|
||||
void merge_transitions(map<T, rule_ptr> *, const map<T, rule_ptr> &);
|
||||
|
||||
struct MergeAsChoice {
|
||||
void operator()(rule_ptr *left, const rule_ptr *right) {
|
||||
*left = Choice::Build({ *left, *right });
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
void merge_transitions(map<CharacterSet, rule_ptr> *left,
|
||||
const map<CharacterSet, rule_ptr> &right) {
|
||||
for (auto &pair : right)
|
||||
merge_char_transition<rule_ptr>(left, pair,
|
||||
[](rule_ptr *left, const rule_ptr *right) {
|
||||
*left = rules::Choice::Build({ *left, *right });
|
||||
});
|
||||
merge_char_transition<rule_ptr>(left, pair, MergeAsChoice());
|
||||
}
|
||||
|
||||
template <>
|
||||
void merge_transitions(map<Symbol, rule_ptr> *left,
|
||||
const map<Symbol, rule_ptr> &right) {
|
||||
for (auto &pair : right)
|
||||
merge_sym_transition<rule_ptr>(left, pair,
|
||||
[](rule_ptr *left, const rule_ptr *right) {
|
||||
*left = rules::Choice::Build({ *left, *right });
|
||||
});
|
||||
merge_sym_transition<rule_ptr>(left, pair, MergeAsChoice());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class RuleTransitions : public rules::RuleFn<map<T, rule_ptr>> {
|
||||
map<T, rule_ptr> apply_to_atom(const rules::Rule *rule) {
|
||||
auto atom = dynamic_cast<const T *>(rule);
|
||||
if (atom)
|
||||
return map<T, rule_ptr>({ { *atom, make_shared<rules::Blank>() } });
|
||||
private:
|
||||
map<T, rule_ptr> apply_to_primitive(const rules::Rule *rule) {
|
||||
auto primitive = dynamic_cast<const T *>(rule);
|
||||
if (primitive)
|
||||
return map<T, rule_ptr>({ { *primitive, make_shared<rules::Blank>() } });
|
||||
else
|
||||
return map<T, rule_ptr>();
|
||||
}
|
||||
|
||||
map<T, rule_ptr> apply_to(const CharacterSet *rule) {
|
||||
return apply_to_atom(rule);
|
||||
return apply_to_primitive(rule);
|
||||
}
|
||||
|
||||
map<T, rule_ptr> apply_to(const Symbol *rule) { return apply_to_atom(rule); }
|
||||
map<T, rule_ptr> apply_to(const Symbol *rule) {
|
||||
return apply_to_primitive(rule);
|
||||
}
|
||||
|
||||
map<T, rule_ptr> apply_to(const rules::Choice *rule) {
|
||||
map<T, rule_ptr> result;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue