From e105f5cebc78dac4d660bf4abef0cfccdeb99429 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 10 Jun 2014 10:30:00 -0700 Subject: [PATCH] Remove inheritance link btwn PreparedGrammar and Grammar --- .../build_tables/build_parse_table_spec.cc | 7 +-- .../prepare_grammar/extract_tokens_spec.cc | 6 +- .../prepare_grammar/intern_symbols_spec.cc | 2 +- .../build_tables/build_parse_table.cc | 2 +- src/compiler/generate_code/c_code.cc | 2 +- .../prepare_grammar/expand_repeats.cc | 5 +- src/compiler/prepare_grammar/expand_tokens.cc | 6 +- .../prepare_grammar/extract_tokens.cc | 11 ++-- .../prepare_grammar/intern_symbols.cc | 2 +- src/compiler/prepared_grammar.cc | 62 +++++++++++++------ src/compiler/prepared_grammar.h | 18 +++--- 11 files changed, 70 insertions(+), 53 deletions(-) diff --git a/spec/compiler/build_tables/build_parse_table_spec.cc b/spec/compiler/build_tables/build_parse_table_spec.cc index 91f8a0d8..295fa55a 100644 --- a/spec/compiler/build_tables/build_parse_table_spec.cc +++ b/spec/compiler/build_tables/build_parse_table_spec.cc @@ -10,14 +10,11 @@ using namespace build_tables; START_TEST describe("building parse tables", []() { - PreparedGrammar parse_grammar({ + auto parse_grammar = PreparedGrammar({ { "rule0", choice({ i_sym(1), i_sym(2) }) }, { "rule1", i_token(0) }, { "rule2", i_token(1) }, - }, {}, PreparedGrammarOptions({ - // ubiquitous_tokens - { Symbol(2, SymbolOptionToken) } - })); + }, {}).ubiquitous_tokens({ Symbol(2, SymbolOptionToken) }); PreparedGrammar lex_grammar({ { "token0", pattern("[a-c]") }, diff --git a/spec/compiler/prepare_grammar/extract_tokens_spec.cc b/spec/compiler/prepare_grammar/extract_tokens_spec.cc index 732c4362..7d6c9342 100644 --- a/spec/compiler/prepare_grammar/extract_tokens_spec.cc +++ b/spec/compiler/prepare_grammar/extract_tokens_spec.cc @@ -132,11 +132,9 @@ describe("extracting tokens from a grammar", []() { { "rule_A", str("ab") }, { "rule_B", i_sym(0) }, { "rule_C", i_sym(1) }, - }, {}, PreparedGrammarOptions({ - { Symbol(0) } - }))); + }, {}).ubiquitous_tokens({ Symbol(0) })); - AssertThat(result.first.options.ubiquitous_tokens, Equals(vector({ + AssertThat(result.first.ubiquitous_tokens(), Equals(vector({ { Symbol(0, SymbolOptionToken) } }))); }); diff --git a/spec/compiler/prepare_grammar/intern_symbols_spec.cc b/spec/compiler/prepare_grammar/intern_symbols_spec.cc index ca2711d3..dd7ccf66 100644 --- a/spec/compiler/prepare_grammar/intern_symbols_spec.cc +++ b/spec/compiler/prepare_grammar/intern_symbols_spec.cc @@ -49,7 +49,7 @@ describe("interning symbols in a grammar", []() { auto result = intern_symbols(grammar); AssertThat(result.second, Equals((GrammarError *)nullptr)); - AssertThat(result.first.options.ubiquitous_tokens, Equals(vector({ + AssertThat(result.first.ubiquitous_tokens(), Equals(vector({ Symbol(2) }))); }); diff --git a/src/compiler/build_tables/build_parse_table.cc b/src/compiler/build_tables/build_parse_table.cc index b4247caf..fdca019e 100644 --- a/src/compiler/build_tables/build_parse_table.cc +++ b/src/compiler/build_tables/build_parse_table.cc @@ -60,7 +60,7 @@ namespace tree_sitter { } void add_ubiquitous_token_actions(const ParseItemSet &item_set, ParseStateId state_id) { - for (const Symbol &symbol : grammar.options.ubiquitous_tokens) { + for (const Symbol &symbol : grammar.ubiquitous_tokens()) { auto &actions = parse_table.states[state_id].actions; if (actions.find(symbol) == actions.end()) parse_table.add_action(state_id, symbol, ParseAction::Shift(state_id, { 0 })); diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index 358d1a0f..8560a407 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -266,7 +266,7 @@ namespace tree_sitter { string ubiquitous_symbols_list() { string result = "UBIQUITOUS_SYMBOLS = {\n"; - for (auto &symbol : syntax_grammar.options.ubiquitous_tokens) + for (auto &symbol : syntax_grammar.ubiquitous_tokens()) result += indent("[" + symbol_id(symbol) + "] = 1,") + "\n"; return result + "};"; } diff --git a/src/compiler/prepare_grammar/expand_repeats.cc b/src/compiler/prepare_grammar/expand_repeats.cc index d812b2bb..d950d00e 100644 --- a/src/compiler/prepare_grammar/expand_repeats.cc +++ b/src/compiler/prepare_grammar/expand_repeats.cc @@ -51,7 +51,7 @@ namespace tree_sitter { }; PreparedGrammar expand_repeats(const PreparedGrammar &grammar) { - vector> rules, aux_rules(grammar.aux_rules); + vector> rules, aux_rules(grammar.aux_rules()); for (auto &pair : grammar.rules()) { ExpandRepeats expander(pair.first, aux_rules.size()); @@ -59,7 +59,8 @@ namespace tree_sitter { aux_rules.insert(aux_rules.end(), expander.aux_rules.begin(), expander.aux_rules.end()); } - return PreparedGrammar(rules, aux_rules, grammar.options); + return PreparedGrammar(rules, aux_rules). + ubiquitous_tokens(grammar.ubiquitous_tokens()); } } } diff --git a/src/compiler/prepare_grammar/expand_tokens.cc b/src/compiler/prepare_grammar/expand_tokens.cc index 9bb10c20..d28d1d9d 100644 --- a/src/compiler/prepare_grammar/expand_tokens.cc +++ b/src/compiler/prepare_grammar/expand_tokens.cc @@ -55,14 +55,16 @@ namespace tree_sitter { rules.push_back({ pair.first, rule }); } - for (auto &pair : grammar.aux_rules) { + for (auto &pair : grammar.aux_rules()) { auto rule = expander.apply(pair.second); if (expander.error) return { PreparedGrammar(), expander.error }; aux_rules.push_back({ pair.first, rule }); } - return { PreparedGrammar(rules, aux_rules, grammar.options), nullptr }; + return { + PreparedGrammar(rules, aux_rules).ubiquitous_tokens(grammar.ubiquitous_tokens()), + nullptr }; } } } diff --git a/src/compiler/prepare_grammar/extract_tokens.cc b/src/compiler/prepare_grammar/extract_tokens.cc index edd15486..2d07e175 100644 --- a/src/compiler/prepare_grammar/extract_tokens.cc +++ b/src/compiler/prepare_grammar/extract_tokens.cc @@ -113,8 +113,8 @@ namespace tree_sitter { } } - for (size_t i = 0; i < input_grammar.aux_rules.size(); i++) { - auto pair = input_grammar.aux_rules[i]; + for (size_t i = 0; i < input_grammar.aux_rules().size(); i++) { + auto pair = input_grammar.aux_rules()[i]; if (IsToken().apply(pair.second)) { aux_tokens.push_back(pair); symbol_replacements.insert({ @@ -133,14 +133,11 @@ namespace tree_sitter { pair.second = inliner.apply(pair.second); for (auto &pair : aux_rules) pair.second = inliner.apply(pair.second); - for (auto &symbol : input_grammar.options.ubiquitous_tokens) + for (auto &symbol : input_grammar.ubiquitous_tokens()) ubiquitous_tokens.push_back(inliner.replace_symbol(symbol)); - PreparedGrammarOptions parse_options(input_grammar.options); - parse_options.ubiquitous_tokens = ubiquitous_tokens; - return { - PreparedGrammar(rules, aux_rules, parse_options), + PreparedGrammar(rules, aux_rules).ubiquitous_tokens(ubiquitous_tokens), PreparedGrammar(tokens, aux_tokens) }; } diff --git a/src/compiler/prepare_grammar/intern_symbols.cc b/src/compiler/prepare_grammar/intern_symbols.cc index 3259e470..be48056b 100644 --- a/src/compiler/prepare_grammar/intern_symbols.cc +++ b/src/compiler/prepare_grammar/intern_symbols.cc @@ -65,7 +65,7 @@ namespace tree_sitter { } return { - PreparedGrammar(rules, {}, PreparedGrammarOptions({ ubiquitous_tokens })), + PreparedGrammar(rules, {}).ubiquitous_tokens(ubiquitous_tokens), nullptr }; } diff --git a/src/compiler/prepared_grammar.cc b/src/compiler/prepared_grammar.cc index a321b289..0fa4b599 100644 --- a/src/compiler/prepared_grammar.cc +++ b/src/compiler/prepared_grammar.cc @@ -8,48 +8,72 @@ namespace tree_sitter { using std::string; using std::pair; using std::ostream; + using std::vector; using rules::rule_ptr; using rules::Symbol; - PreparedGrammar::PreparedGrammar() : Grammar({}), aux_rules({}), options({}) {} + PreparedGrammar::PreparedGrammar() : + rules_({}), + aux_rules_({}), + ubiquitous_tokens_({}) {} PreparedGrammar::PreparedGrammar(const std::vector> &rules, const std::vector> &aux_rules) : - Grammar(rules), - aux_rules(aux_rules), - options({}) {} - - PreparedGrammar::PreparedGrammar(const std::vector> &rules, - const std::vector> &aux_rules, - PreparedGrammarOptions options) : - Grammar(rules), - aux_rules(aux_rules), - options(options) {} + rules_(rules), + aux_rules_(aux_rules), + ubiquitous_tokens_({}) {} const rule_ptr & PreparedGrammar::rule(const Symbol &symbol) const { return symbol.is_auxiliary() ? - aux_rules[symbol.index].second : + aux_rules_[symbol.index].second : rules_[symbol.index].second; } const string & PreparedGrammar::rule_name(const Symbol &symbol) const { return symbol.is_auxiliary() ? - aux_rules[symbol.index].first : + aux_rules_[symbol.index].first : rules_[symbol.index].first; } bool PreparedGrammar::operator==(const PreparedGrammar &other) const { - if (!Grammar::operator==(other)) return false; - if (other.aux_rules.size() != aux_rules.size()) return false; - for (size_t i = 0; i < aux_rules.size(); i++) { - auto &pair = aux_rules[i]; - auto &other_pair = other.aux_rules[i]; + if (other.rules_.size() != rules_.size()) return false; + + for (size_t i = 0; i < rules_.size(); i++) { + auto &pair = rules_[i]; + auto &other_pair = other.rules_[i]; if (other_pair.first != pair.first) return false; if (!other_pair.second->operator==(*pair.second)) return false; } + + if (other.aux_rules_.size() != aux_rules_.size()) return false; + for (size_t i = 0; i < aux_rules_ + .size(); i++) { + auto &pair = aux_rules_[i]; + auto &other_pair = other.aux_rules_[i]; + if (other_pair.first != pair.first) return false; + if (!other_pair.second->operator==(*pair.second)) return false; + } + return true; } + + const vector> & PreparedGrammar::rules() const { + return rules_; + } + const vector> & PreparedGrammar::aux_rules() const { + return aux_rules_; + } + + const vector & PreparedGrammar::ubiquitous_tokens() const { + return ubiquitous_tokens_; + } + + const PreparedGrammar & PreparedGrammar::ubiquitous_tokens(const vector &ubiquitous_tokens) { + ubiquitous_tokens_ = ubiquitous_tokens; + return *this; + } + ostream& operator<<(ostream &stream, const PreparedGrammar &grammar) { stream << string("# "); diff --git a/src/compiler/prepared_grammar.h b/src/compiler/prepared_grammar.h index 7378f38b..78f2baf0 100644 --- a/src/compiler/prepared_grammar.h +++ b/src/compiler/prepared_grammar.h @@ -8,25 +8,23 @@ #include "compiler/rules/symbol.h" namespace tree_sitter { - struct PreparedGrammarOptions { - std::vector ubiquitous_tokens; - }; + class PreparedGrammar { + const std::vector> rules_; + const std::vector> aux_rules_; + std::vector ubiquitous_tokens_; - class PreparedGrammar : public Grammar { public: PreparedGrammar(); PreparedGrammar(const std::vector> &rules, const std::vector> &aux_rules); - PreparedGrammar(const std::vector> &rules, - const std::vector> &aux_rules, - PreparedGrammarOptions options); bool operator==(const PreparedGrammar &other) const; const std::string & rule_name(const rules::Symbol &symbol) const; const rules::rule_ptr & rule(const rules::Symbol &symbol) const; - - const std::vector> aux_rules; - const PreparedGrammarOptions options; + const std::vector & ubiquitous_tokens() const; + const PreparedGrammar & ubiquitous_tokens(const std::vector &ubiquitous_tokens); + const std::vector> & rules() const; + const std::vector> & aux_rules() const; }; std::ostream& operator<<(std::ostream &stream, const PreparedGrammar &grammar);