Make Grammar a simple struct
This commit is contained in:
parent
4b04afac5e
commit
36870bfced
16 changed files with 53 additions and 100 deletions
|
|
@ -1,44 +0,0 @@
|
|||
#include "tree_sitter/compiler.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
|
||||
using std::ostream;
|
||||
using std::pair;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
Grammar::Grammar(const vector<pair<string, rule_ptr>> &rules)
|
||||
: rules_(rules), extra_tokens_({}) {}
|
||||
|
||||
const vector<pair<string, rule_ptr>> &Grammar::rules() const {
|
||||
return rules_;
|
||||
}
|
||||
|
||||
const vector<rule_ptr> &Grammar::extra_tokens() const {
|
||||
return extra_tokens_;
|
||||
}
|
||||
|
||||
const vector<vector<string>> &Grammar::expected_conflicts() const {
|
||||
return expected_conflicts_;
|
||||
}
|
||||
|
||||
Grammar &Grammar::extra_tokens(const vector<rule_ptr> &extra_tokens) {
|
||||
extra_tokens_ = extra_tokens;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Grammar &Grammar::expected_conflicts(
|
||||
const vector<vector<string>> &expected_conflicts) {
|
||||
expected_conflicts_ = expected_conflicts;
|
||||
return *this;
|
||||
}
|
||||
|
||||
GrammarError::GrammarError(GrammarErrorType type, string message)
|
||||
: type(type), message(message) {}
|
||||
|
||||
bool GrammarError::operator==(const GrammarError &other) const {
|
||||
return type == other.type && message == other.message;
|
||||
}
|
||||
|
||||
} // namespace tree_sitter
|
||||
|
|
@ -31,8 +31,8 @@ class InternSymbols : public rules::IdentityRuleFn {
|
|||
|
||||
public:
|
||||
std::shared_ptr<rules::Symbol> symbol_for_rule_name(string rule_name) {
|
||||
for (size_t i = 0; i < grammar.rules().size(); i++)
|
||||
if (grammar.rules()[i].first == rule_name)
|
||||
for (size_t i = 0; i < grammar.rules.size(); i++)
|
||||
if (grammar.rules[i].first == rule_name)
|
||||
return make_shared<rules::Symbol>(i);
|
||||
return nullptr;
|
||||
}
|
||||
|
|
@ -51,7 +51,7 @@ pair<InternedGrammar, const GrammarError *> intern_symbols(const Grammar &gramma
|
|||
InternedGrammar result;
|
||||
InternSymbols interner(grammar);
|
||||
|
||||
for (auto &pair : grammar.rules()) {
|
||||
for (auto &pair : grammar.rules) {
|
||||
auto new_rule = interner.apply(pair.second);
|
||||
if (!interner.missing_rule_name.empty())
|
||||
return { result, missing_rule_error(interner.missing_rule_name) };
|
||||
|
|
@ -61,14 +61,14 @@ pair<InternedGrammar, const GrammarError *> intern_symbols(const Grammar &gramma
|
|||
new_rule));
|
||||
}
|
||||
|
||||
for (auto &rule : grammar.extra_tokens()) {
|
||||
for (auto &rule : grammar.extra_tokens) {
|
||||
auto new_rule = interner.apply(rule);
|
||||
if (!interner.missing_rule_name.empty())
|
||||
return { result, missing_rule_error(interner.missing_rule_name) };
|
||||
result.extra_tokens.push_back(new_rule);
|
||||
}
|
||||
|
||||
for (auto &names : grammar.expected_conflicts()) {
|
||||
for (auto &names : grammar.expected_conflicts) {
|
||||
set<rules::Symbol> entry;
|
||||
for (auto &name : names) {
|
||||
auto symbol = interner.symbol_for_rule_name(name);
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace tree_sitter {
|
||||
|
||||
class Grammar;
|
||||
struct Grammar;
|
||||
class GrammarError;
|
||||
|
||||
namespace prepare_grammar {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue