From 34d96909d1566fcc6ff9d27111add30192ef11e9 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 12 Jan 2015 23:01:52 -0800 Subject: [PATCH] Move {Syntax,Lexical}Grammar into separate files --- project.gyp | 3 +- .../action_takes_precedence_spec.cc | 2 +- .../build_tables/build_conflict_spec.cc | 3 +- .../build_tables/build_parse_table_spec.cc | 3 +- .../build_tables/first_symbols_spec.cc | 2 +- .../build_tables/item_set_closure_spec.cc | 2 +- .../build_tables/item_set_transitions_spec.cc | 2 +- spec/compiler/build_tables/lex_item_spec.cc | 1 - .../build_tables/rule_can_be_blank_spec.cc | 2 +- .../prepare_grammar/expand_repeats_spec.cc | 2 +- .../prepare_grammar/expand_tokens_spec.cc | 2 +- .../prepare_grammar/extract_tokens_spec.cc | 4 +- .../prepare_grammar/intern_symbols_spec.cc | 1 - .../build_tables/action_takes_precedence.h | 2 +- src/compiler/build_tables/build_conflict.cc | 3 +- src/compiler/build_tables/build_lex_table.cc | 2 +- .../build_tables/build_parse_table.cc | 3 +- src/compiler/build_tables/build_tables.cc | 3 +- src/compiler/build_tables/first_symbols.cc | 2 +- src/compiler/build_tables/item_set_closure.cc | 2 +- .../build_tables/item_set_transitions.cc | 2 +- .../build_tables/rule_can_be_blank.cc | 2 +- src/compiler/compile.cc | 3 +- src/compiler/generate_code/c_code.cc | 3 +- src/compiler/lexical_grammar.cc | 37 +++++++++++++ src/compiler/lexical_grammar.h | 33 ++++++++++++ .../prepare_grammar/expand_repeats.cc | 2 +- src/compiler/prepare_grammar/expand_tokens.cc | 2 +- .../prepare_grammar/extract_tokens.cc | 3 +- .../prepare_grammar/intern_symbols.cc | 1 - .../prepare_grammar/prepare_grammar.cc | 3 +- .../prepare_grammar/prepare_grammar.h | 3 +- src/compiler/prepared_grammar.cc | 53 ------------------- src/compiler/syntax_grammar.cc | 35 ++++++++++++ .../{prepared_grammar.h => syntax_grammar.h} | 41 ++++---------- 35 files changed, 153 insertions(+), 116 deletions(-) create mode 100644 src/compiler/lexical_grammar.cc create mode 100644 src/compiler/lexical_grammar.h delete mode 100644 src/compiler/prepared_grammar.cc create mode 100644 src/compiler/syntax_grammar.cc rename src/compiler/{prepared_grammar.h => syntax_grammar.h} (50%) diff --git a/project.gyp b/project.gyp index 4b9e721c..e672b2b1 100644 --- a/project.gyp +++ b/project.gyp @@ -29,6 +29,7 @@ 'src/compiler/generate_code/c_code.cc', 'src/compiler/grammar.cc', 'src/compiler/lex_table.cc', + 'src/compiler/lexical_grammar.cc', 'src/compiler/parse_table.cc', 'src/compiler/prepare_grammar/expand_repeats.cc', 'src/compiler/prepare_grammar/expand_tokens.cc', @@ -38,7 +39,7 @@ 'src/compiler/prepare_grammar/parse_regex.cc', 'src/compiler/prepare_grammar/prepare_grammar.cc', 'src/compiler/prepare_grammar/token_description.cc', - 'src/compiler/prepared_grammar.cc', + 'src/compiler/syntax_grammar.cc', 'src/compiler/rules/blank.cc', 'src/compiler/rules/built_in_symbols.cc', 'src/compiler/rules/character_range.cc', diff --git a/spec/compiler/build_tables/action_takes_precedence_spec.cc b/spec/compiler/build_tables/action_takes_precedence_spec.cc index 7bb03c49..09ffaac6 100644 --- a/spec/compiler/build_tables/action_takes_precedence_spec.cc +++ b/spec/compiler/build_tables/action_takes_precedence_spec.cc @@ -2,7 +2,7 @@ #include "compiler/rules/built_in_symbols.h" #include "compiler/parse_table.h" #include "compiler/build_tables/action_takes_precedence.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" using namespace rules; using namespace build_tables; diff --git a/spec/compiler/build_tables/build_conflict_spec.cc b/spec/compiler/build_tables/build_conflict_spec.cc index 50572180..1f30ac12 100644 --- a/spec/compiler/build_tables/build_conflict_spec.cc +++ b/spec/compiler/build_tables/build_conflict_spec.cc @@ -1,6 +1,7 @@ #include "compiler/compiler_spec_helper.h" #include "compiler/build_tables/build_conflict.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" +#include "compiler/lexical_grammar.h" using namespace rules; using namespace build_tables; diff --git a/spec/compiler/build_tables/build_parse_table_spec.cc b/spec/compiler/build_tables/build_parse_table_spec.cc index a9de0863..4a6874c8 100644 --- a/spec/compiler/build_tables/build_parse_table_spec.cc +++ b/spec/compiler/build_tables/build_parse_table_spec.cc @@ -1,7 +1,8 @@ #include "compiler/compiler_spec_helper.h" #include "compiler/build_tables/build_parse_table.h" #include "compiler/parse_table.h" -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/rules/built_in_symbols.h" using namespace rules; diff --git a/spec/compiler/build_tables/first_symbols_spec.cc b/spec/compiler/build_tables/first_symbols_spec.cc index 679a07cd..111e4910 100644 --- a/spec/compiler/build_tables/first_symbols_spec.cc +++ b/spec/compiler/build_tables/first_symbols_spec.cc @@ -1,5 +1,5 @@ #include "compiler/compiler_spec_helper.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/build_tables/first_symbols.h" #include "compiler/rules/metadata.h" diff --git a/spec/compiler/build_tables/item_set_closure_spec.cc b/spec/compiler/build_tables/item_set_closure_spec.cc index 5e869659..0b81b66a 100644 --- a/spec/compiler/build_tables/item_set_closure_spec.cc +++ b/spec/compiler/build_tables/item_set_closure_spec.cc @@ -1,5 +1,5 @@ #include "compiler/compiler_spec_helper.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/build_tables/item_set_closure.h" #include "compiler/build_tables/item_set_transitions.h" diff --git a/spec/compiler/build_tables/item_set_transitions_spec.cc b/spec/compiler/build_tables/item_set_transitions_spec.cc index 8b235e8b..977054e4 100644 --- a/spec/compiler/build_tables/item_set_transitions_spec.cc +++ b/spec/compiler/build_tables/item_set_transitions_spec.cc @@ -1,6 +1,6 @@ #include "compiler/compiler_spec_helper.h" #include "compiler/build_tables/item_set_transitions.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/helpers/rule_helpers.h" using namespace rules; diff --git a/spec/compiler/build_tables/lex_item_spec.cc b/spec/compiler/build_tables/lex_item_spec.cc index 82107285..2e832a88 100644 --- a/spec/compiler/build_tables/lex_item_spec.cc +++ b/spec/compiler/build_tables/lex_item_spec.cc @@ -1,7 +1,6 @@ #include "compiler/compiler_spec_helper.h" #include "compiler/build_tables/item_set_transitions.h" #include "compiler/rules/metadata.h" -#include "compiler/prepared_grammar.h" using namespace rules; using namespace build_tables; diff --git a/spec/compiler/build_tables/rule_can_be_blank_spec.cc b/spec/compiler/build_tables/rule_can_be_blank_spec.cc index 14c10f26..4c0d03fd 100644 --- a/spec/compiler/build_tables/rule_can_be_blank_spec.cc +++ b/spec/compiler/build_tables/rule_can_be_blank_spec.cc @@ -1,7 +1,7 @@ #include "compiler/compiler_spec_helper.h" #include "compiler/build_tables/rule_can_be_blank.h" #include "compiler/rules/metadata.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" using namespace rules; using build_tables::rule_can_be_blank; diff --git a/spec/compiler/prepare_grammar/expand_repeats_spec.cc b/spec/compiler/prepare_grammar/expand_repeats_spec.cc index e9ea305c..62f12b8b 100644 --- a/spec/compiler/prepare_grammar/expand_repeats_spec.cc +++ b/spec/compiler/prepare_grammar/expand_repeats_spec.cc @@ -1,5 +1,5 @@ #include "compiler/compiler_spec_helper.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/prepare_grammar/expand_repeats.h" #include "compiler/helpers/containers.h" diff --git a/spec/compiler/prepare_grammar/expand_tokens_spec.cc b/spec/compiler/prepare_grammar/expand_tokens_spec.cc index 5d2bc2f8..da3a7ce4 100644 --- a/spec/compiler/prepare_grammar/expand_tokens_spec.cc +++ b/spec/compiler/prepare_grammar/expand_tokens_spec.cc @@ -1,5 +1,5 @@ #include "compiler/compiler_spec_helper.h" -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" #include "compiler/helpers/containers.h" #include "compiler/prepare_grammar/expand_tokens.h" diff --git a/spec/compiler/prepare_grammar/extract_tokens_spec.cc b/spec/compiler/prepare_grammar/extract_tokens_spec.cc index 542535ee..40d614d4 100644 --- a/spec/compiler/prepare_grammar/extract_tokens_spec.cc +++ b/spec/compiler/prepare_grammar/extract_tokens_spec.cc @@ -1,7 +1,7 @@ #include "compiler/compiler_spec_helper.h" -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/prepare_grammar/extract_tokens.h" -#include "compiler/prepared_grammar.h" #include "compiler/helpers/containers.h" START_TEST diff --git a/spec/compiler/prepare_grammar/intern_symbols_spec.cc b/spec/compiler/prepare_grammar/intern_symbols_spec.cc index 1449d2b3..0c486182 100644 --- a/spec/compiler/prepare_grammar/intern_symbols_spec.cc +++ b/spec/compiler/prepare_grammar/intern_symbols_spec.cc @@ -1,5 +1,4 @@ #include "compiler/compiler_spec_helper.h" -#include "compiler/prepared_grammar.h" #include "compiler/prepare_grammar/intern_symbols.h" #include "compiler/rules/named_symbol.h" #include "compiler/rules/symbol.h" diff --git a/src/compiler/build_tables/action_takes_precedence.h b/src/compiler/build_tables/action_takes_precedence.h index ee674276..98a38e13 100644 --- a/src/compiler/build_tables/action_takes_precedence.h +++ b/src/compiler/build_tables/action_takes_precedence.h @@ -5,7 +5,7 @@ #include "tree_sitter/compiler.h" #include "compiler/parse_table.h" #include "compiler/rules/symbol.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" namespace tree_sitter { namespace build_tables { diff --git a/src/compiler/build_tables/build_conflict.cc b/src/compiler/build_tables/build_conflict.cc index f870c03c..ae239696 100644 --- a/src/compiler/build_tables/build_conflict.cc +++ b/src/compiler/build_tables/build_conflict.cc @@ -1,7 +1,8 @@ #include "compiler/build_tables/build_conflict.h" #include "compiler/rules/symbol.h" #include "compiler/rules/built_in_symbols.h" -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/build_tables/parse_item.h" #include diff --git a/src/compiler/build_tables/build_lex_table.cc b/src/compiler/build_tables/build_lex_table.cc index a8ce9e19..e41952a4 100644 --- a/src/compiler/build_tables/build_lex_table.cc +++ b/src/compiler/build_tables/build_lex_table.cc @@ -8,7 +8,7 @@ #include "compiler/build_tables/item_set_transitions.h" #include "compiler/build_tables/lex_item.h" #include "compiler/parse_table.h" -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" #include "compiler/rules/built_in_symbols.h" #include "compiler/rules/choice.h" #include "compiler/rules/metadata.h" diff --git a/src/compiler/build_tables/build_parse_table.cc b/src/compiler/build_tables/build_parse_table.cc index af68a2fa..7ba85a04 100644 --- a/src/compiler/build_tables/build_parse_table.cc +++ b/src/compiler/build_tables/build_parse_table.cc @@ -9,7 +9,8 @@ #include "compiler/build_tables/action_takes_precedence.h" #include "compiler/build_tables/build_conflict.h" #include "compiler/build_tables/parse_item.h" -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/rules/symbol.h" #include "compiler/rules/built_in_symbols.h" diff --git a/src/compiler/build_tables/build_tables.cc b/src/compiler/build_tables/build_tables.cc index f1903f08..c4fd7681 100644 --- a/src/compiler/build_tables/build_tables.cc +++ b/src/compiler/build_tables/build_tables.cc @@ -1,6 +1,7 @@ #include "compiler/build_tables/build_lex_table.h" #include "compiler/build_tables/build_parse_table.h" -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" +#include "compiler/syntax_grammar.h" namespace tree_sitter { namespace build_tables { diff --git a/src/compiler/build_tables/first_symbols.cc b/src/compiler/build_tables/first_symbols.cc index 8144610c..f90d7886 100644 --- a/src/compiler/build_tables/first_symbols.cc +++ b/src/compiler/build_tables/first_symbols.cc @@ -1,6 +1,6 @@ #include "compiler/build_tables/first_symbols.h" #include "compiler/build_tables/rule_can_be_blank.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/rules/choice.h" #include "compiler/rules/metadata.h" #include "compiler/rules/seq.h" diff --git a/src/compiler/build_tables/item_set_closure.cc b/src/compiler/build_tables/item_set_closure.cc index 6bd55a9f..b84104b3 100644 --- a/src/compiler/build_tables/item_set_closure.cc +++ b/src/compiler/build_tables/item_set_closure.cc @@ -7,7 +7,7 @@ #include "compiler/build_tables/rule_transitions.h" #include "compiler/build_tables/rule_can_be_blank.h" #include "compiler/build_tables/item.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" namespace tree_sitter { namespace build_tables { diff --git a/src/compiler/build_tables/item_set_transitions.cc b/src/compiler/build_tables/item_set_transitions.cc index a23a5e06..93dcba18 100644 --- a/src/compiler/build_tables/item_set_transitions.cc +++ b/src/compiler/build_tables/item_set_transitions.cc @@ -4,7 +4,7 @@ #include "compiler/build_tables/merge_transitions.h" #include "compiler/build_tables/parse_item.h" #include "compiler/build_tables/rule_transitions.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/rules/symbol.h" namespace tree_sitter { diff --git a/src/compiler/build_tables/rule_can_be_blank.cc b/src/compiler/build_tables/rule_can_be_blank.cc index c22c21d8..687bbeb6 100644 --- a/src/compiler/build_tables/rule_can_be_blank.cc +++ b/src/compiler/build_tables/rule_can_be_blank.cc @@ -1,7 +1,7 @@ #include "compiler/build_tables/rule_can_be_blank.h" #include #include "tree_sitter/compiler.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/rules/symbol.h" #include "compiler/rules/visitor.h" #include "compiler/rules/seq.h" diff --git a/src/compiler/compile.cc b/src/compiler/compile.cc index be3b612c..efcfb7fd 100644 --- a/src/compiler/compile.cc +++ b/src/compiler/compile.cc @@ -2,7 +2,8 @@ #include "compiler/prepare_grammar/prepare_grammar.h" #include "compiler/build_tables/build_tables.h" #include "compiler/generate_code/c_code.h" -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" +#include "compiler/lexical_grammar.h" namespace tree_sitter { diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index 09ba02ff..1bbb6839 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -7,7 +7,8 @@ #include "compiler/generate_code/c_code.h" #include "compiler/lex_table.h" #include "compiler/parse_table.h" -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/rules/built_in_symbols.h" #include "compiler/util/string_helpers.h" diff --git a/src/compiler/lexical_grammar.cc b/src/compiler/lexical_grammar.cc new file mode 100644 index 00000000..214088dd --- /dev/null +++ b/src/compiler/lexical_grammar.cc @@ -0,0 +1,37 @@ +#include "compiler/lexical_grammar.h" +#include +#include +#include +#include "compiler/rules/symbol.h" + +namespace tree_sitter { + +using std::string; +using std::pair; +using std::vector; +using std::set; + +LexicalGrammar::LexicalGrammar() {} + +LexicalGrammar::LexicalGrammar( + const vector> &rules, + const vector> &aux_rules) + : rules(rules), aux_rules(aux_rules) {} + +LexicalGrammar::LexicalGrammar( + const vector> &rules, + const vector> &aux_rules, + const vector &separators) + : rules(rules), aux_rules(aux_rules), separators(separators) {} + +const rules::rule_ptr &LexicalGrammar::rule(const rules::Symbol &symbol) const { + return symbol.is_auxiliary() ? aux_rules[symbol.index].second + : rules[symbol.index].second; +} + +const string &LexicalGrammar::rule_name(const rules::Symbol &symbol) const { + return symbol.is_auxiliary() ? aux_rules[symbol.index].first + : rules[symbol.index].first; +} + +} // namespace tree_sitter diff --git a/src/compiler/lexical_grammar.h b/src/compiler/lexical_grammar.h new file mode 100644 index 00000000..73efac22 --- /dev/null +++ b/src/compiler/lexical_grammar.h @@ -0,0 +1,33 @@ +#ifndef COMPILER_LEXICAL_GRAMMAR_H_ +#define COMPILER_LEXICAL_GRAMMAR_H_ + +#include +#include +#include +#include "tree_sitter/compiler.h" +#include "compiler/rules/symbol.h" + +namespace tree_sitter { + +class LexicalGrammar { + public: + LexicalGrammar(); + LexicalGrammar( + const std::vector> &rules, + const std::vector> &aux_rules); + LexicalGrammar( + const std::vector> &rules, + const std::vector> &aux_rules, + const std::vector &separators); + + const std::string &rule_name(const rules::Symbol &symbol) const; + const rules::rule_ptr &rule(const rules::Symbol &symbol) const; + + const std::vector> rules; + const std::vector> aux_rules; + std::vector separators; +}; + +} // namespace tree_sitter + +#endif // COMPILER_LEXICAL_GRAMMAR_H_ diff --git a/src/compiler/prepare_grammar/expand_repeats.cc b/src/compiler/prepare_grammar/expand_repeats.cc index 844a9ead..9e8f4349 100644 --- a/src/compiler/prepare_grammar/expand_repeats.cc +++ b/src/compiler/prepare_grammar/expand_repeats.cc @@ -2,7 +2,7 @@ #include #include #include -#include "compiler/prepared_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/rules/visitor.h" #include "compiler/rules/seq.h" #include "compiler/rules/symbol.h" diff --git a/src/compiler/prepare_grammar/expand_tokens.cc b/src/compiler/prepare_grammar/expand_tokens.cc index fa5cbc7d..f52de48b 100644 --- a/src/compiler/prepare_grammar/expand_tokens.cc +++ b/src/compiler/prepare_grammar/expand_tokens.cc @@ -2,7 +2,7 @@ #include #include #include -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" #include "compiler/rules/visitor.h" #include "compiler/rules/pattern.h" #include "compiler/rules/string.h" diff --git a/src/compiler/prepare_grammar/extract_tokens.cc b/src/compiler/prepare_grammar/extract_tokens.cc index 5b416a02..4dbf854b 100644 --- a/src/compiler/prepare_grammar/extract_tokens.cc +++ b/src/compiler/prepare_grammar/extract_tokens.cc @@ -4,7 +4,8 @@ #include #include #include "tree_sitter/compiler.h" -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" +#include "compiler/syntax_grammar.h" #include "compiler/rules/visitor.h" #include "compiler/rules/symbol.h" #include "compiler/rules/string.h" diff --git a/src/compiler/prepare_grammar/intern_symbols.cc b/src/compiler/prepare_grammar/intern_symbols.cc index 66dad499..c5364cf3 100644 --- a/src/compiler/prepare_grammar/intern_symbols.cc +++ b/src/compiler/prepare_grammar/intern_symbols.cc @@ -3,7 +3,6 @@ #include #include #include "tree_sitter/compiler.h" -#include "compiler/prepared_grammar.h" #include "compiler/rules/visitor.h" #include "compiler/rules/named_symbol.h" #include "compiler/rules/symbol.h" diff --git a/src/compiler/prepare_grammar/prepare_grammar.cc b/src/compiler/prepare_grammar/prepare_grammar.cc index 0c3af84a..d5fa73e9 100644 --- a/src/compiler/prepare_grammar/prepare_grammar.cc +++ b/src/compiler/prepare_grammar/prepare_grammar.cc @@ -3,7 +3,8 @@ #include "compiler/prepare_grammar/extract_tokens.h" #include "compiler/prepare_grammar/intern_symbols.h" #include "compiler/prepare_grammar/prepare_grammar.h" -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" +#include "compiler/syntax_grammar.h" namespace tree_sitter { namespace prepare_grammar { diff --git a/src/compiler/prepare_grammar/prepare_grammar.h b/src/compiler/prepare_grammar/prepare_grammar.h index 32f917bc..5800ea80 100644 --- a/src/compiler/prepare_grammar/prepare_grammar.h +++ b/src/compiler/prepare_grammar/prepare_grammar.h @@ -2,7 +2,8 @@ #define COMPILER_PREPARE_GRAMMAR_PREPARE_GRAMMAR_H_ #include -#include "compiler/prepared_grammar.h" +#include "compiler/lexical_grammar.h" +#include "compiler/syntax_grammar.h" namespace tree_sitter { diff --git a/src/compiler/prepared_grammar.cc b/src/compiler/prepared_grammar.cc deleted file mode 100644 index 12c6a946..00000000 --- a/src/compiler/prepared_grammar.cc +++ /dev/null @@ -1,53 +0,0 @@ -#include "compiler/prepared_grammar.h" -#include -#include -#include -#include "compiler/rules/symbol.h" - -namespace tree_sitter { - -using std::string; -using std::pair; -using std::vector; -using std::set; - -const rules::rule_ptr &PreparedGrammar::rule(const rules::Symbol &symbol) const { - return symbol.is_auxiliary() ? aux_rules[symbol.index].second - : rules[symbol.index].second; -} - -const string &PreparedGrammar::rule_name(const rules::Symbol &symbol) const { - return symbol.is_auxiliary() ? aux_rules[symbol.index].first - : rules[symbol.index].first; -} - -PreparedGrammar::PreparedGrammar() {} -SyntaxGrammar::SyntaxGrammar() {} -LexicalGrammar::LexicalGrammar() {} - -SyntaxGrammar::SyntaxGrammar(const vector> &rules, - const vector> &aux_rules) - : PreparedGrammar(rules, aux_rules) {} - -LexicalGrammar::LexicalGrammar( - const vector> &rules, - const vector> &aux_rules) - : PreparedGrammar(rules, aux_rules) {} - -PreparedGrammar::PreparedGrammar( - const vector> &rules, - const vector> &aux_rules) - : rules(rules), aux_rules(aux_rules) {} - -SyntaxGrammar::SyntaxGrammar(const vector> &rules, - const vector> &aux_rules, - const set &ubiquitous_tokens) - : PreparedGrammar(rules, aux_rules), ubiquitous_tokens(ubiquitous_tokens) {} - -LexicalGrammar::LexicalGrammar( - const vector> &rules, - const vector> &aux_rules, - const vector &separators) - : PreparedGrammar(rules, aux_rules), separators(separators) {} - -} // namespace tree_sitter diff --git a/src/compiler/syntax_grammar.cc b/src/compiler/syntax_grammar.cc new file mode 100644 index 00000000..88192065 --- /dev/null +++ b/src/compiler/syntax_grammar.cc @@ -0,0 +1,35 @@ +#include "compiler/syntax_grammar.h" +#include +#include +#include +#include "compiler/rules/symbol.h" + +namespace tree_sitter { + +using std::string; +using std::pair; +using std::vector; +using std::set; + +SyntaxGrammar::SyntaxGrammar() {} + +SyntaxGrammar::SyntaxGrammar(const vector> &rules, + const vector> &aux_rules) + : rules(rules), aux_rules(aux_rules) {} + +SyntaxGrammar::SyntaxGrammar(const vector> &rules, + const vector> &aux_rules, + const set &ubiquitous_tokens) + : rules(rules), aux_rules(aux_rules), ubiquitous_tokens(ubiquitous_tokens) {} + +const rules::rule_ptr &SyntaxGrammar::rule(const rules::Symbol &symbol) const { + return symbol.is_auxiliary() ? aux_rules[symbol.index].second + : rules[symbol.index].second; +} + +const string &SyntaxGrammar::rule_name(const rules::Symbol &symbol) const { + return symbol.is_auxiliary() ? aux_rules[symbol.index].first + : rules[symbol.index].first; +} + +} // namespace tree_sitter diff --git a/src/compiler/prepared_grammar.h b/src/compiler/syntax_grammar.h similarity index 50% rename from src/compiler/prepared_grammar.h rename to src/compiler/syntax_grammar.h index f448eec6..101ca78f 100644 --- a/src/compiler/prepared_grammar.h +++ b/src/compiler/syntax_grammar.h @@ -1,5 +1,5 @@ -#ifndef COMPILER_PREPARED_GRAMMAR_H_ -#define COMPILER_PREPARED_GRAMMAR_H_ +#ifndef COMPILER_SYNTAX_GRAMMAR_H_ +#define COMPILER_SYNTAX_GRAMMAR_H_ #include #include @@ -10,21 +10,7 @@ namespace tree_sitter { -class PreparedGrammar { - public: - PreparedGrammar(); - PreparedGrammar( - const std::vector> &rules, - const std::vector> &aux_rules); - - const std::vector> rules; - const std::vector> aux_rules; - - const std::string &rule_name(const rules::Symbol &symbol) const; - const rules::rule_ptr &rule(const rules::Symbol &symbol) const; -}; - -class SyntaxGrammar : public PreparedGrammar { +class SyntaxGrammar { public: SyntaxGrammar(); SyntaxGrammar( @@ -35,23 +21,14 @@ class SyntaxGrammar : public PreparedGrammar { const std::vector> &aux_rules, const std::set &ubiquitous_tokens); + const std::string &rule_name(const rules::Symbol &symbol) const; + const rules::rule_ptr &rule(const rules::Symbol &symbol) const; + + const std::vector> rules; + const std::vector> aux_rules; std::set ubiquitous_tokens; }; -class LexicalGrammar : public PreparedGrammar { - public: - LexicalGrammar(); - LexicalGrammar( - const std::vector> &rules, - const std::vector> &aux_rules); - LexicalGrammar( - const std::vector> &rules, - const std::vector> &aux_rules, - const std::vector &separators); - - std::vector separators; -}; - } // namespace tree_sitter -#endif // COMPILER_PREPARED_GRAMMAR_H_ +#endif // COMPILER_SYNTAX_GRAMMAR_H_