From 25eda9d889ca9a3e2596d2b82bfc6601a3a47e3b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 28 Apr 2014 20:43:27 -0700 Subject: [PATCH] ISymbol -> Symbol Interned symbols are now the main type of symbol in use --- .../build_tables/conflict_manager_spec.cc | 8 ++-- spec/compiler/build_tables/first_set_spec.cc | 30 +++++++------- .../compiler/build_tables/follow_sets_spec.cc | 40 +++++++++---------- .../build_tables/item_set_closure_spec.cc | 6 +-- .../build_tables/item_set_transitions_spec.cc | 12 +++--- spec/compiler/build_tables/lex_item_spec.cc | 2 +- .../build_tables/rule_transitions_spec.cc | 30 +++++++------- spec/compiler/helpers/rule_helpers.cc | 10 ++--- .../prepare_grammar/intern_symbols_spec.cc | 2 +- src/compiler/build_tables/build_tables.cc | 12 +++--- src/compiler/build_tables/conflict_manager.cc | 4 +- src/compiler/build_tables/conflict_manager.h | 6 +-- src/compiler/build_tables/first_set.cc | 28 ++++++------- src/compiler/build_tables/first_set.h | 6 +-- src/compiler/build_tables/follow_sets.cc | 10 ++--- src/compiler/build_tables/follow_sets.h | 4 +- src/compiler/build_tables/item.cc | 2 +- src/compiler/build_tables/item.h | 6 +-- src/compiler/build_tables/item_set_closure.cc | 6 +-- .../build_tables/item_set_transitions.cc | 18 ++++----- .../build_tables/item_set_transitions.h | 4 +- src/compiler/build_tables/lex_item.cc | 4 +- src/compiler/build_tables/lex_item.h | 4 +- src/compiler/build_tables/merge_transitions.h | 6 +-- src/compiler/build_tables/parse_item.cc | 4 +- src/compiler/build_tables/parse_item.h | 12 +++--- .../build_tables/rule_can_be_blank.cc | 6 +-- src/compiler/build_tables/rule_transitions.cc | 12 +++--- src/compiler/build_tables/rule_transitions.h | 4 +- src/compiler/generate_code/c_code.cc | 8 ++-- src/compiler/lex_table.cc | 14 +++---- src/compiler/lex_table.h | 10 ++--- src/compiler/parse_table.cc | 20 +++++----- src/compiler/parse_table.h | 18 ++++----- .../prepare_grammar/expand_repeats.cc | 6 +-- .../prepare_grammar/extract_tokens.cc | 26 ++++++------ .../prepare_grammar/intern_symbols.cc | 5 +-- src/compiler/prepared_grammar.cc | 8 ++-- src/compiler/prepared_grammar.h | 6 +-- src/compiler/rules/built_in_symbols.cc | 6 +-- src/compiler/rules/built_in_symbols.h | 8 ++-- src/compiler/rules/named_symbol.h | 7 ++-- .../rules/{interned_symbol.cc => symbol.cc} | 30 +++++++------- .../rules/{interned_symbol.h => symbol.h} | 20 +++++----- src/compiler/rules/visitor.h | 8 ++-- 45 files changed, 248 insertions(+), 250 deletions(-) rename src/compiler/rules/{interned_symbol.cc => symbol.cc} (59%) rename src/compiler/rules/{interned_symbol.h => symbol.h} (59%) diff --git a/spec/compiler/build_tables/conflict_manager_spec.cc b/spec/compiler/build_tables/conflict_manager_spec.cc index 46e71530..1aa85c66 100644 --- a/spec/compiler/build_tables/conflict_manager_spec.cc +++ b/spec/compiler/build_tables/conflict_manager_spec.cc @@ -29,8 +29,8 @@ describe("resolving parse conflicts", []() { }); describe("lexical conflicts", [&]() { - ISymbol sym1(1, SymbolOptionToken); - ISymbol sym2(2, SymbolOptionToken); + Symbol sym1(1, SymbolOptionToken); + Symbol sym2(2, SymbolOptionToken); it("favors non-errors over lexical errors", [&]() { should_update = manager->resolve_lex_action(LexAction::Error(), LexAction::Advance(2)); @@ -50,8 +50,8 @@ describe("resolving parse conflicts", []() { }); describe("syntactic conflicts", [&]() { - ISymbol sym1(0); - ISymbol sym2(1); + Symbol sym1(0); + Symbol sym2(1); it("favors non-errors over parse errors", [&]() { should_update = manager->resolve_parse_action(sym1, ParseAction::Error(), ParseAction::Shift(2, { 0 })); diff --git a/spec/compiler/build_tables/first_set_spec.cc b/spec/compiler/build_tables/first_set_spec.cc index 753bf54b..5eb9f1bc 100644 --- a/spec/compiler/build_tables/first_set_spec.cc +++ b/spec/compiler/build_tables/first_set_spec.cc @@ -16,8 +16,8 @@ describe("computing FIRST sets", []() { it("ignores B when A cannot be blank", [&]() { auto rule = seq({ i_token(0), i_token(1) }); - AssertThat(first_set(rule, null_grammar), Equals(set({ - ISymbol(0, SymbolOptionToken), + AssertThat(first_set(rule, null_grammar), Equals(set({ + Symbol(0, SymbolOptionToken), }))); }); @@ -28,9 +28,9 @@ describe("computing FIRST sets", []() { blank() }), i_token(1) }); - AssertThat(first_set(rule, null_grammar), Equals(set({ - ISymbol(0, SymbolOptionToken), - ISymbol(1, SymbolOptionToken) + AssertThat(first_set(rule, null_grammar), Equals(set({ + Symbol(0, SymbolOptionToken), + Symbol(1, SymbolOptionToken) }))); }); @@ -48,9 +48,9 @@ describe("computing FIRST sets", []() { i_token(4) }) } }); - AssertThat(first_set(rule, grammar), Equals(set({ - ISymbol(0, SymbolOptionToken), - ISymbol(2, SymbolOptionToken), + AssertThat(first_set(rule, grammar), Equals(set({ + Symbol(0, SymbolOptionToken), + Symbol(2, SymbolOptionToken), }))); }); @@ -65,9 +65,9 @@ describe("computing FIRST sets", []() { blank() }) } }); - AssertThat(first_set(rule, grammar), Equals(set({ - ISymbol(0, SymbolOptionToken), - ISymbol(1, SymbolOptionToken), + AssertThat(first_set(rule, grammar), Equals(set({ + Symbol(0, SymbolOptionToken), + Symbol(1, SymbolOptionToken), }))); }); }); @@ -83,8 +83,8 @@ describe("computing FIRST sets", []() { auto rule = i_sym(0); - AssertThat(first_set(rule, grammar), Equals(set({ - ISymbol(11, SymbolOptionToken) + AssertThat(first_set(rule, grammar), Equals(set({ + Symbol(11, SymbolOptionToken) }))); }); }); @@ -92,8 +92,8 @@ describe("computing FIRST sets", []() { it("ignores metadata rules", [&]() { auto rule = make_shared(i_token(3), map()); - AssertThat(first_set(rule, null_grammar), Equals(set({ - ISymbol(3, SymbolOptionToken), + AssertThat(first_set(rule, null_grammar), Equals(set({ + Symbol(3, SymbolOptionToken), }))); }); }); diff --git a/spec/compiler/build_tables/follow_sets_spec.cc b/spec/compiler/build_tables/follow_sets_spec.cc index 3d34a078..02379cff 100644 --- a/spec/compiler/build_tables/follow_sets_spec.cc +++ b/spec/compiler/build_tables/follow_sets_spec.cc @@ -16,42 +16,42 @@ describe("computing FOLLOW sets", []() { }, {}); it("includes all of the starting non-terminals for the item, and their following terminals", [&]() { - ParseItem item(ISymbol(2), choice({ + ParseItem item(Symbol(2), choice({ seq({ i_sym(0), choice({ i_token(0), i_token(1) }) }), seq({ i_sym(1), i_token(2) }), - }), 0, ISymbol(10, SymbolOptionToken)); + }), 0, Symbol(10, SymbolOptionToken)); - AssertThat(follow_sets(item, grammar), Equals(map>({ - { ISymbol(0), set({ - ISymbol(0, SymbolOptionToken), - ISymbol(1, SymbolOptionToken) }) }, - { ISymbol(1), set({ - ISymbol(2, SymbolOptionToken) }) }, + AssertThat(follow_sets(item, grammar), Equals(map>({ + { Symbol(0), set({ + Symbol(0, SymbolOptionToken), + Symbol(1, SymbolOptionToken) }) }, + { Symbol(1), set({ + Symbol(2, SymbolOptionToken) }) }, }))); }); it("does not include terminals at the beginning of the item", [&]() { - ParseItem item(ISymbol(2), choice({ + ParseItem item(Symbol(2), choice({ seq({ i_sym(0), choice({ i_token(0), i_token(1) }) }), seq({ i_token(2), i_token(3) }), - }), 0, ISymbol(10, SymbolOptionToken)); + }), 0, Symbol(10, SymbolOptionToken)); - AssertThat(follow_sets(item, grammar), Equals(map>({ - { ISymbol(0), set({ - ISymbol(0, SymbolOptionToken), - ISymbol(1, SymbolOptionToken) }) }, + AssertThat(follow_sets(item, grammar), Equals(map>({ + { Symbol(0), set({ + Symbol(0, SymbolOptionToken), + Symbol(1, SymbolOptionToken) }) }, }))); }); it("includes the item's lookahead terminal if the rule after the non-terminal might be blank", [&]() { - ParseItem item(ISymbol(2), choice({ + ParseItem item(Symbol(2), choice({ seq({ i_sym(0), choice({ i_token(0), blank() }) }), - }), 0, ISymbol(10, SymbolOptionToken)); + }), 0, Symbol(10, SymbolOptionToken)); - AssertThat(follow_sets(item, grammar), Equals(map>({ - { ISymbol(0), set({ - ISymbol(0, SymbolOptionToken), - ISymbol(10, SymbolOptionToken) }) }, + AssertThat(follow_sets(item, grammar), Equals(map>({ + { Symbol(0), set({ + Symbol(0, SymbolOptionToken), + Symbol(10, SymbolOptionToken) }) }, }))); }); }); diff --git a/spec/compiler/build_tables/item_set_closure_spec.cc b/spec/compiler/build_tables/item_set_closure_spec.cc index c49480e1..e8ef8734 100644 --- a/spec/compiler/build_tables/item_set_closure_spec.cc +++ b/spec/compiler/build_tables/item_set_closure_spec.cc @@ -20,12 +20,12 @@ describe("computing closures of item sets", []() { it("adds items at the beginnings of referenced rules", [&]() { ParseItemSet item_set = item_set_closure( - ParseItem(ISymbol(0), grammar.rule(ISymbol(0)), 0, ISymbol(10, SymbolOptionToken)), + ParseItem(Symbol(0), grammar.rule(Symbol(0)), 0, Symbol(10, SymbolOptionToken)), grammar); AssertThat(item_set, Equals(ParseItemSet({ - ParseItem(ISymbol(1), grammar.rule(ISymbol(1)), 0, ISymbol(11, SymbolOptionToken)), - ParseItem(ISymbol(0), grammar.rule(ISymbol(0)), 0, ISymbol(10, SymbolOptionToken)), + ParseItem(Symbol(1), grammar.rule(Symbol(1)), 0, Symbol(11, SymbolOptionToken)), + ParseItem(Symbol(0), grammar.rule(Symbol(0)), 0, Symbol(10, SymbolOptionToken)), }))); }); }); diff --git a/spec/compiler/build_tables/item_set_transitions_spec.cc b/spec/compiler/build_tables/item_set_transitions_spec.cc index 3313a54e..dcaf6b0c 100644 --- a/spec/compiler/build_tables/item_set_transitions_spec.cc +++ b/spec/compiler/build_tables/item_set_transitions_spec.cc @@ -13,17 +13,17 @@ describe("item set transitions", []() { describe("when two items in the set have transitions on the same character", [&]() { it("merges the transitions by computing the union of the two item sets", [&]() { LexItemSet set1({ - LexItem(ISymbol(1), pattern("[a-f]")), - LexItem(ISymbol(2), pattern("[e-x]")) }); + LexItem(Symbol(1), pattern("[a-f]")), + LexItem(Symbol(2), pattern("[e-x]")) }); AssertThat(char_transitions(set1, grammar), Equals(map({ { CharacterSet({ {'a', 'd'} }), LexItemSet({ - LexItem(ISymbol(1), blank()) }) }, + LexItem(Symbol(1), blank()) }) }, { CharacterSet({ {'e', 'f'} }), LexItemSet({ - LexItem(ISymbol(1), blank()), - LexItem(ISymbol(2), blank()) }) }, + LexItem(Symbol(1), blank()), + LexItem(Symbol(2), blank()) }) }, { CharacterSet({ {'g', 'x'} }), LexItemSet({ - LexItem(ISymbol(2), blank()) }) }, + LexItem(Symbol(2), blank()) }) }, }))); }); }); diff --git a/spec/compiler/build_tables/lex_item_spec.cc b/spec/compiler/build_tables/lex_item_spec.cc index af846e65..1e467a48 100644 --- a/spec/compiler/build_tables/lex_item_spec.cc +++ b/spec/compiler/build_tables/lex_item_spec.cc @@ -9,7 +9,7 @@ START_TEST describe("lex items", []() { describe("determining if an item is the start of a token", [&]() { - ISymbol sym(1); + Symbol sym(1); rule_ptr token_start = make_shared(str("a"), map({ { START_TOKEN, 1 } })); diff --git a/spec/compiler/build_tables/rule_transitions_spec.cc b/spec/compiler/build_tables/rule_transitions_spec.cc index c7ca7448..2bd6fba8 100644 --- a/spec/compiler/build_tables/rule_transitions_spec.cc +++ b/spec/compiler/build_tables/rule_transitions_spec.cc @@ -27,25 +27,25 @@ describe("rule transitions", []() { it("handles symbols", [&]() { AssertThat( sym_transitions(i_sym(1)), - Equals(rule_map({ - { ISymbol(1), blank() } + Equals(rule_map({ + { Symbol(1), blank() } }))); }); it("handles choices", [&]() { AssertThat( sym_transitions(choice({ i_sym(1), i_sym(2) })), - Equals(rule_map({ - { ISymbol(1), blank() }, - { ISymbol(2), blank() } + Equals(rule_map({ + { Symbol(1), blank() }, + { Symbol(2), blank() } }))); }); it("handles sequences", [&]() { AssertThat( sym_transitions(seq({ i_sym(1), i_sym(2) })), - Equals(rule_map({ - { ISymbol(1), i_sym(2) } + Equals(rule_map({ + { Symbol(1), i_sym(2) } }))); }); @@ -57,8 +57,8 @@ describe("rule transitions", []() { i_sym(3), i_sym(4) })), - Equals(rule_map({ - { ISymbol(1), seq({ i_sym(2), i_sym(3), i_sym(4) }) } + Equals(rule_map({ + { Symbol(1), seq({ i_sym(2), i_sym(3), i_sym(4) }) } }))); }); @@ -73,8 +73,8 @@ describe("rule transitions", []() { i_sym(1), i_sym(2) }) - })), Equals(rule_map({ - { ISymbol(1), choice({ seq({ i_sym(1), i_sym(2) }), i_sym(2), }) } + })), Equals(rule_map({ + { Symbol(1), choice({ seq({ i_sym(1), i_sym(2) }), i_sym(2), }) } }))); }); @@ -84,8 +84,8 @@ describe("rule transitions", []() { choice({ seq({ i_sym(1), i_sym(2) }), seq({ i_sym(1), i_sym(3) }) })), - Equals(rule_map({ - { ISymbol(1), choice({ i_sym(2), i_sym(3) }) } + Equals(rule_map({ + { Symbol(1), choice({ i_sym(2), i_sym(3) }) } }))); }); @@ -192,8 +192,8 @@ describe("rule transitions", []() { rule_ptr rule = make_shared(seq({ i_sym(1), i_sym(2) }), metadata_value); AssertThat( sym_transitions(rule), - Equals(rule_map({ - { ISymbol(1), make_shared(i_sym(2), metadata_value)}, + Equals(rule_map({ + { Symbol(1), make_shared(i_sym(2), metadata_value)}, }))); }); diff --git a/spec/compiler/helpers/rule_helpers.cc b/spec/compiler/helpers/rule_helpers.cc index 4ff03be1..2655c9bb 100644 --- a/spec/compiler/helpers/rule_helpers.cc +++ b/spec/compiler/helpers/rule_helpers.cc @@ -1,6 +1,6 @@ #include "rule_helpers.h" #include -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { using std::make_shared; @@ -19,19 +19,19 @@ namespace tree_sitter { } rule_ptr i_sym(size_t index) { - return make_shared(index); + return make_shared(index); } rule_ptr i_aux_sym(size_t index) { - return make_shared(index, SymbolOptionAuxiliary); + return make_shared(index, SymbolOptionAuxiliary); } rule_ptr i_token(size_t index) { - return make_shared(index, SymbolOptionToken); + return make_shared(index, SymbolOptionToken); } rule_ptr i_aux_token(size_t index) { - return make_shared(index, SymbolOption(SymbolOptionAuxiliary|SymbolOptionToken)); + return make_shared(index, SymbolOption(SymbolOptionAuxiliary|SymbolOptionToken)); } } } diff --git a/spec/compiler/prepare_grammar/intern_symbols_spec.cc b/spec/compiler/prepare_grammar/intern_symbols_spec.cc index 544bd139..9361f3b3 100644 --- a/spec/compiler/prepare_grammar/intern_symbols_spec.cc +++ b/spec/compiler/prepare_grammar/intern_symbols_spec.cc @@ -2,7 +2,7 @@ #include "compiler/prepared_grammar.h" #include "compiler/prepare_grammar/intern_symbols.h" #include "compiler/rules/named_symbol.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" START_TEST diff --git a/src/compiler/build_tables/build_tables.cc b/src/compiler/build_tables/build_tables.cc index d2d3541e..0d3ae78d 100644 --- a/src/compiler/build_tables/build_tables.cc +++ b/src/compiler/build_tables/build_tables.cc @@ -10,7 +10,7 @@ #include "compiler/rules/repeat.h" #include "compiler/rules/blank.h" #include "compiler/rules/seq.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" #include "compiler/build_tables/conflict_manager.h" #include "compiler/build_tables/item.h" #include "compiler/build_tables/item_set_closure.h" @@ -25,7 +25,7 @@ namespace tree_sitter { using std::set; using std::unordered_map; using std::make_shared; - using rules::ISymbol; + using rules::Symbol; using rules::CharacterSet; namespace build_tables { @@ -46,7 +46,7 @@ namespace tree_sitter { void add_shift_actions(const ParseItemSet &item_set, ParseStateId state_id) { for (auto &transition : sym_transitions(item_set, grammar)) { - const ISymbol &symbol = transition.first; + const Symbol &symbol = transition.first; const ParseItemSet &item_set = transition.second; set precedence_values = precedence_values_for_item_set(item_set); @@ -162,11 +162,11 @@ namespace tree_sitter { void add_error_lex_state() { LexItemSet error_item_set; for (size_t i = 0; i < lex_grammar.rules.size(); i++) { - LexItem item(ISymbol(i, rules::SymbolOptionToken), after_separators(lex_grammar.rules[i].second)); + LexItem item(Symbol(i, rules::SymbolOptionToken), after_separators(lex_grammar.rules[i].second)); error_item_set.insert(item); } for (size_t i = 0; i < lex_grammar.aux_rules.size(); i++) { - LexItem item(ISymbol(i, rules::SymbolOption(rules::SymbolOptionToken|rules::SymbolOptionAuxiliary)), after_separators(lex_grammar.aux_rules[i].second)); + LexItem item(Symbol(i, rules::SymbolOption(rules::SymbolOptionToken|rules::SymbolOptionAuxiliary)), after_separators(lex_grammar.aux_rules[i].second)); error_item_set.insert(item); } error_item_set.insert(LexItem(rules::END_OF_INPUT(), after_separators(CharacterSet({ 0 }).copy()))); @@ -183,7 +183,7 @@ namespace tree_sitter { {} void build() { - auto start_symbol = make_shared(0); + auto start_symbol = make_shared(0); ParseItem item(rules::START(), start_symbol, {}, rules::END_OF_INPUT()); ParseItemSet item_set = item_set_closure(item, grammar); add_parse_state(item_set); diff --git a/src/compiler/build_tables/conflict_manager.cc b/src/compiler/build_tables/conflict_manager.cc index 235d84e4..0838a0cf 100644 --- a/src/compiler/build_tables/conflict_manager.cc +++ b/src/compiler/build_tables/conflict_manager.cc @@ -18,7 +18,7 @@ namespace tree_sitter { parse_grammar(parse_grammar), lex_grammar(lex_grammar) {} - bool ConflictManager::resolve_parse_action(const rules::ISymbol &symbol, + bool ConflictManager::resolve_parse_action(const rules::Symbol &symbol, const ParseAction &old_action, const ParseAction &new_action) { if (new_action.type < old_action.type) @@ -119,7 +119,7 @@ namespace tree_sitter { } } - void ConflictManager::record_conflict(const rules::ISymbol &symbol, + void ConflictManager::record_conflict(const rules::Symbol &symbol, const ParseAction &left, const ParseAction &right) { string name = symbol.is_token() ? diff --git a/src/compiler/build_tables/conflict_manager.h b/src/compiler/build_tables/conflict_manager.h index 7f8b0da8..d87b6609 100644 --- a/src/compiler/build_tables/conflict_manager.h +++ b/src/compiler/build_tables/conflict_manager.h @@ -7,7 +7,7 @@ #include #include "tree_sitter/compiler.h" #include "compiler/parse_table.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" #include "compiler/prepared_grammar.h" namespace tree_sitter { @@ -23,11 +23,11 @@ namespace tree_sitter { bool resolve_lex_action(const LexAction &old_action, const LexAction &new_action); - bool resolve_parse_action(const rules::ISymbol &symbol, + bool resolve_parse_action(const rules::Symbol &symbol, const ParseAction &old_action, const ParseAction &new_action); - void record_conflict(const rules::ISymbol &symbol, const ParseAction &left, const ParseAction &right); + void record_conflict(const rules::Symbol &symbol, const ParseAction &left, const ParseAction &right); const std::vector conflicts() const; }; } diff --git a/src/compiler/build_tables/first_set.cc b/src/compiler/build_tables/first_set.cc index 416fd952..9036ee9a 100644 --- a/src/compiler/build_tables/first_set.cc +++ b/src/compiler/build_tables/first_set.cc @@ -6,36 +6,36 @@ #include "compiler/rules/visitor.h" #include "compiler/rules/seq.h" #include "compiler/rules/choice.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { using std::set; - using rules::ISymbol; + using rules::Symbol; namespace build_tables { - class FirstSet : public rules::RuleFn> { + class FirstSet : public rules::RuleFn> { const PreparedGrammar *grammar; - set visited_symbols; + set visited_symbols; public: explicit FirstSet(const PreparedGrammar *grammar) : grammar(grammar) {} - set apply_to(const ISymbol *rule) { + set apply_to(const Symbol *rule) { auto insertion_result = visited_symbols.insert(*rule); if (insertion_result.second) { return (rule->is_token()) ? - set({ *rule }) : + set({ *rule }) : apply(grammar->rule(*rule)); } else { - return set(); + return set(); } } - set apply_to(const rules::Metadata *rule) { + set apply_to(const rules::Metadata *rule) { return apply(rule->rule); } - set apply_to(const rules::Choice *rule) { - set result; + set apply_to(const rules::Choice *rule) { + set result; for (const auto &el : rule->elements) { auto &&next_syms = apply(el); result.insert(next_syms.begin(), next_syms.end()); @@ -43,7 +43,7 @@ namespace tree_sitter { return result; } - set apply_to(const rules::Seq *rule) { + set apply_to(const rules::Seq *rule) { auto &&result = apply(rule->left); if (rule_can_be_blank(rule->left, *grammar)) { auto &&right_symbols = apply(rule->right); @@ -53,12 +53,12 @@ namespace tree_sitter { } }; - set first_set(const rules::rule_ptr &rule, const PreparedGrammar &grammar) { + set first_set(const rules::rule_ptr &rule, const PreparedGrammar &grammar) { return FirstSet(&grammar).apply(rule); } - set first_set(const ParseItemSet &item_set, const PreparedGrammar &grammar) { - set result; + set first_set(const ParseItemSet &item_set, const PreparedGrammar &grammar) { + set result; for (auto &item : item_set) { auto &&rule_set = first_set(item.rule, grammar); result.insert(rule_set.begin(), rule_set.end()); diff --git a/src/compiler/build_tables/first_set.h b/src/compiler/build_tables/first_set.h index 30e6137c..b816e8d8 100644 --- a/src/compiler/build_tables/first_set.h +++ b/src/compiler/build_tables/first_set.h @@ -3,7 +3,7 @@ #include #include "compiler/build_tables/parse_item.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { class PreparedGrammar; @@ -15,14 +15,14 @@ namespace tree_sitter { * the beginning of a string derivable from a given rule, * in a given gramamr. */ - std::set + std::set first_set(const rules::rule_ptr &rule, const PreparedGrammar &grammar); /* * Returns the set of terminal symbols that can appear at * the beginning of any item in the given set. */ - std::set + std::set first_set(const ParseItemSet &item_set, const PreparedGrammar &grammar); } } diff --git a/src/compiler/build_tables/follow_sets.cc b/src/compiler/build_tables/follow_sets.cc index a9439e1f..1384ab17 100644 --- a/src/compiler/build_tables/follow_sets.cc +++ b/src/compiler/build_tables/follow_sets.cc @@ -7,18 +7,18 @@ namespace tree_sitter { using std::set; using std::map; - using rules::ISymbol; + using rules::Symbol; using rules::rule_ptr; namespace build_tables { - map> follow_sets(const ParseItem &item, + map> follow_sets(const ParseItem &item, const PreparedGrammar &grammar) { - map> result; + map> result; for (auto &pair : sym_transitions(item.rule)) { - ISymbol symbol = pair.first; + Symbol symbol = pair.first; rule_ptr next_rule = pair.second; if (!symbol.is_token() && !symbol.is_built_in()) { - set following_terminals = first_set(next_rule, grammar); + set following_terminals = first_set(next_rule, grammar); if (rule_can_be_blank(next_rule, grammar)) following_terminals.insert(item.lookahead_sym); result.insert({ symbol, following_terminals }); diff --git a/src/compiler/build_tables/follow_sets.h b/src/compiler/build_tables/follow_sets.h index 9c2126cd..772b16f8 100644 --- a/src/compiler/build_tables/follow_sets.h +++ b/src/compiler/build_tables/follow_sets.h @@ -4,7 +4,7 @@ #include #include #include "compiler/build_tables/parse_item.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { class PreparedGrammar; @@ -17,7 +17,7 @@ namespace tree_sitter { * item. The values are the sets of terminals which can appear immediately * after the corresponding non-terminals. */ - std::map> + std::map> follow_sets(const ParseItem &item, const PreparedGrammar &grammar); } diff --git a/src/compiler/build_tables/item.cc b/src/compiler/build_tables/item.cc index 27a56be0..a421290b 100644 --- a/src/compiler/build_tables/item.cc +++ b/src/compiler/build_tables/item.cc @@ -4,7 +4,7 @@ namespace tree_sitter { namespace build_tables { - Item::Item(const rules::ISymbol &lhs, const rules::rule_ptr rule) : + Item::Item(const rules::Symbol &lhs, const rules::rule_ptr rule) : lhs(lhs), rule(rule) {} diff --git a/src/compiler/build_tables/item.h b/src/compiler/build_tables/item.h index 3b8f4873..b533b7e2 100644 --- a/src/compiler/build_tables/item.h +++ b/src/compiler/build_tables/item.h @@ -2,16 +2,16 @@ #define COMPILER_BUILD_TABLES_ITEM_H_ #include -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { namespace build_tables { class Item { public: - Item(const rules::ISymbol &lhs, rules::rule_ptr rule); + Item(const rules::Symbol &lhs, rules::rule_ptr rule); bool is_done() const; - rules::ISymbol lhs; + rules::Symbol lhs; rules::rule_ptr rule; }; } diff --git a/src/compiler/build_tables/item_set_closure.cc b/src/compiler/build_tables/item_set_closure.cc index 496e1ff5..a0124e98 100644 --- a/src/compiler/build_tables/item_set_closure.cc +++ b/src/compiler/build_tables/item_set_closure.cc @@ -8,7 +8,7 @@ namespace tree_sitter { using std::set; - using rules::ISymbol; + using rules::Symbol; using std::vector; namespace build_tables { @@ -22,8 +22,8 @@ namespace tree_sitter { auto insertion_result = result.insert(item); if (insertion_result.second) { for (const auto &pair : follow_sets(item, grammar)) { - const ISymbol &non_terminal = pair.first; - const set &terminals = pair.second; + const Symbol &non_terminal = pair.first; + const set &terminals = pair.second; for (const auto &terminal : terminals) { ParseItem next_item(non_terminal, grammar.rule(non_terminal), 0, terminal); items_to_add.push_back(next_item); diff --git a/src/compiler/build_tables/item_set_transitions.cc b/src/compiler/build_tables/item_set_transitions.cc index 2e595a1d..0f124010 100644 --- a/src/compiler/build_tables/item_set_transitions.cc +++ b/src/compiler/build_tables/item_set_transitions.cc @@ -4,14 +4,14 @@ #include "compiler/build_tables/item_set_closure.h" #include "compiler/build_tables/rule_transitions.h" #include "compiler/build_tables/merge_transitions.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { using std::map; using std::vector; using std::unordered_set; using rules::CharacterSet; - using rules::ISymbol; + using rules::Symbol; namespace build_tables { template @@ -20,13 +20,13 @@ namespace tree_sitter { return left; } - const ISymbol placeholder_lookahead = ISymbol(-100); - const ISymbol placeholder_lhs = ISymbol(-101); + const Symbol placeholder_lookahead = Symbol(-100); + const Symbol placeholder_lhs = Symbol(-101); - static map sym_transitions_for_rule(SymTransitions *self, const rules::rule_ptr &rule, const PreparedGrammar &grammar) { + static map sym_transitions_for_rule(SymTransitions *self, const rules::rule_ptr &rule, const PreparedGrammar &grammar) { auto pair = self->transitions_cache.find(rule); if (pair != self->transitions_cache.end()) return pair->second; - map result; + map result; for (auto &transition : sym_transitions(rule)) { ParseItem new_item(placeholder_lhs, transition.second, 1, placeholder_lookahead); result.insert({ @@ -38,7 +38,7 @@ namespace tree_sitter { return result; } - static map sym_transitions_for_item(SymTransitions *self, const ParseItem &item, const PreparedGrammar &grammar) { + static map sym_transitions_for_item(SymTransitions *self, const ParseItem &item, const PreparedGrammar &grammar) { auto result = sym_transitions_for_rule(self, item.rule, grammar); for (auto &pair : result) { vector new_items; @@ -70,9 +70,9 @@ namespace tree_sitter { return result; } - map + map SymTransitions::operator()(const ParseItemSet &item_set, const PreparedGrammar &grammar) { - map result; + map result; for (const ParseItem &item : item_set) merge_sym_transitions(result, sym_transitions_for_item(this, item, grammar), diff --git a/src/compiler/build_tables/item_set_transitions.h b/src/compiler/build_tables/item_set_transitions.h index d758a8ab..39354178 100644 --- a/src/compiler/build_tables/item_set_transitions.h +++ b/src/compiler/build_tables/item_set_transitions.h @@ -15,9 +15,9 @@ namespace tree_sitter { namespace build_tables { class SymTransitions { public: - std::map + std::map operator()(const ParseItemSet &item_set, const PreparedGrammar &grammar); - std::map> transitions_cache; + std::map> transitions_cache; }; std::map diff --git a/src/compiler/build_tables/lex_item.cc b/src/compiler/build_tables/lex_item.cc index 05b05d22..ed7c2953 100644 --- a/src/compiler/build_tables/lex_item.cc +++ b/src/compiler/build_tables/lex_item.cc @@ -1,6 +1,6 @@ #include "compiler/build_tables/lex_item.h" #include "compiler/build_tables/rule_can_be_blank.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" #include "compiler/rules/metadata.h" #include "compiler/rules/seq.h" #include "compiler/rules/visitor.h" @@ -11,7 +11,7 @@ namespace tree_sitter { using std::vector; namespace build_tables { - LexItem::LexItem(const rules::ISymbol &lhs, const rules::rule_ptr rule) : + LexItem::LexItem(const rules::Symbol &lhs, const rules::rule_ptr rule) : Item(lhs, rule) {} bool LexItem::operator==(const LexItem &other) const { diff --git a/src/compiler/build_tables/lex_item.h b/src/compiler/build_tables/lex_item.h index 7761d199..7ee51993 100644 --- a/src/compiler/build_tables/lex_item.h +++ b/src/compiler/build_tables/lex_item.h @@ -9,7 +9,7 @@ namespace tree_sitter { namespace build_tables { class LexItem : public Item { public: - LexItem(const rules::ISymbol &lhs, rules::rule_ptr rule); + LexItem(const rules::Symbol &lhs, rules::rule_ptr rule); bool operator==(const LexItem &other) const; bool is_token_start() const; }; @@ -25,7 +25,7 @@ namespace std { struct hash { size_t operator()(const tree_sitter::build_tables::Item &item) const { return - hash()(item.lhs) ^ + hash()(item.lhs) ^ hash()(item.rule); } }; diff --git a/src/compiler/build_tables/merge_transitions.h b/src/compiler/build_tables/merge_transitions.h index d0319c5b..b06eecc6 100644 --- a/src/compiler/build_tables/merge_transitions.h +++ b/src/compiler/build_tables/merge_transitions.h @@ -3,7 +3,7 @@ #include #include "compiler/rules/character_set.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { namespace build_tables { @@ -15,8 +15,8 @@ namespace tree_sitter { * using the given function. */ template - void merge_sym_transitions(std::map &left, - const std::map &right, + void merge_sym_transitions(std::map &left, + const std::map &right, std::function merge_fn) { for (auto &pair : right) { auto rule = pair.first; diff --git a/src/compiler/build_tables/parse_item.cc b/src/compiler/build_tables/parse_item.cc index 235766db..2b1814b3 100644 --- a/src/compiler/build_tables/parse_item.cc +++ b/src/compiler/build_tables/parse_item.cc @@ -8,10 +8,10 @@ namespace tree_sitter { using std::ostream; namespace build_tables { - ParseItem::ParseItem(const rules::ISymbol &lhs, + ParseItem::ParseItem(const rules::Symbol &lhs, const rules::rule_ptr rule, size_t consumed_symbol_count, - const rules::ISymbol &lookahead_sym) : + const rules::Symbol &lookahead_sym) : Item(lhs, rule), consumed_symbol_count(consumed_symbol_count), lookahead_sym(lookahead_sym) {} diff --git a/src/compiler/build_tables/parse_item.h b/src/compiler/build_tables/parse_item.h index 8b990b9b..1ab0fd7a 100644 --- a/src/compiler/build_tables/parse_item.h +++ b/src/compiler/build_tables/parse_item.h @@ -3,7 +3,7 @@ #include #include -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" #include "compiler/build_tables/item.h" #include "compiler/rules/metadata.h" @@ -11,15 +11,15 @@ namespace tree_sitter { namespace build_tables { class ParseItem : public Item { public: - ParseItem(const rules::ISymbol &lhs, + ParseItem(const rules::Symbol &lhs, rules::rule_ptr rule, const size_t consumed_symbol_count, - const rules::ISymbol &lookahead_sym); + const rules::Symbol &lookahead_sym); bool operator==(const ParseItem &other) const; int precedence() const; size_t consumed_symbol_count; - rules::ISymbol lookahead_sym; + rules::Symbol lookahead_sym; }; std::ostream& operator<<(std::ostream &stream, const ParseItem &item); @@ -33,10 +33,10 @@ namespace std { struct hash { size_t operator()(const tree_sitter::build_tables::ParseItem &item) const { return - hash()(item.lhs) ^ + hash()(item.lhs) ^ hash()(item.rule) ^ hash()(item.consumed_symbol_count) ^ - hash()(item.lookahead_sym); + hash()(item.lookahead_sym); } }; diff --git a/src/compiler/build_tables/rule_can_be_blank.cc b/src/compiler/build_tables/rule_can_be_blank.cc index c2242c35..090ec99b 100644 --- a/src/compiler/build_tables/rule_can_be_blank.cc +++ b/src/compiler/build_tables/rule_can_be_blank.cc @@ -2,7 +2,7 @@ #include #include "tree_sitter/compiler.h" #include "compiler/prepared_grammar.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" #include "compiler/rules/visitor.h" #include "compiler/rules/seq.h" #include "compiler/rules/choice.h" @@ -40,14 +40,14 @@ namespace tree_sitter { class CanBeBlankRecursive : public CanBeBlank { const PreparedGrammar *grammar; - set visited_symbols; + set visited_symbols; using CanBeBlank::visit; public: using CanBeBlank::apply_to; explicit CanBeBlankRecursive(const PreparedGrammar *grammar) : grammar(grammar) {} - bool apply_to(const rules::ISymbol *rule) { + bool apply_to(const rules::Symbol *rule) { if (visited_symbols.find(*rule) == visited_symbols.end()) { visited_symbols.insert(*rule); return !rule->is_token() && apply(grammar->rule(*rule)); diff --git a/src/compiler/build_tables/rule_transitions.cc b/src/compiler/build_tables/rule_transitions.cc index 8c1a7478..06bbe6cc 100644 --- a/src/compiler/build_tables/rule_transitions.cc +++ b/src/compiler/build_tables/rule_transitions.cc @@ -7,7 +7,7 @@ #include "compiler/rules/string.h" #include "compiler/rules/repeat.h" #include "compiler/rules/metadata.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" #include "compiler/rules/pattern.h" #include "compiler/rules/character_set.h" #include "compiler/rules/visitor.h" @@ -16,7 +16,7 @@ namespace tree_sitter { using std::map; using std::make_shared; using rules::rule_ptr; - using rules::ISymbol; + using rules::Symbol; using rules::CharacterSet; namespace build_tables { @@ -31,7 +31,7 @@ namespace tree_sitter { } template<> - void merge_transitions(map &left, const map &right) { + void merge_transitions(map &left, const map &right) { merge_sym_transitions(left, right, [](rule_ptr left, rule_ptr right) { return rules::Choice::Build({ left, right }); }); @@ -57,7 +57,7 @@ namespace tree_sitter { return apply_to_atom(rule); } - map apply_to(const ISymbol *rule) { + map apply_to(const Symbol *rule) { return apply_to_atom(rule); } @@ -114,8 +114,8 @@ namespace tree_sitter { return RuleTransitions().apply(rule); } - map sym_transitions(const rule_ptr &rule) { - return RuleTransitions().apply(rule); + map sym_transitions(const rule_ptr &rule) { + return RuleTransitions().apply(rule); } } } diff --git a/src/compiler/build_tables/rule_transitions.h b/src/compiler/build_tables/rule_transitions.h index 0aefa2a5..29e43fdc 100644 --- a/src/compiler/build_tables/rule_transitions.h +++ b/src/compiler/build_tables/rule_transitions.h @@ -3,14 +3,14 @@ #include #include "compiler/rules/character_set.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { namespace build_tables { std::map char_transitions(const rules::rule_ptr &rule); - std::map + std::map sym_transitions(const rules::rule_ptr &rule); } } diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index 2ae1325c..e56b657a 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -84,11 +84,11 @@ namespace tree_sitter { private: - const PreparedGrammar & grammar_for_symbol(const rules::ISymbol &symbol) { + const PreparedGrammar & grammar_for_symbol(const rules::Symbol &symbol) { return symbol.is_token() ? lexical_grammar : syntax_grammar; } - string symbol_id(const rules::ISymbol &symbol) { + string symbol_id(const rules::Symbol &symbol) { if (symbol.is_built_in()) { return (symbol == rules::ERROR()) ? "ts_builtin_sym_error" : @@ -102,7 +102,7 @@ namespace tree_sitter { } } - string symbol_name(const rules::ISymbol &symbol) { + string symbol_name(const rules::Symbol &symbol) { if (symbol.is_built_in()) { return (symbol == rules::ERROR()) ? "error" : "end"; } else if (symbol.is_token() && symbol.is_auxiliary()) { @@ -211,7 +211,7 @@ namespace tree_sitter { } string symbol_names_list() { - set symbols(parse_table.symbols); + set symbols(parse_table.symbols); symbols.insert(rules::END_OF_INPUT()); symbols.insert(rules::ERROR()); diff --git a/src/compiler/lex_table.cc b/src/compiler/lex_table.cc index 0651e853..6b078091 100644 --- a/src/compiler/lex_table.cc +++ b/src/compiler/lex_table.cc @@ -1,33 +1,33 @@ #include "compiler/lex_table.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { using std::string; using std::to_string; using std::map; using std::set; - using rules::ISymbol; + using rules::Symbol; using rules::CharacterSet; LexAction::LexAction() : type(LexActionTypeError), - symbol(ISymbol(-1)), + symbol(Symbol(-1)), state_index(-1) {} - LexAction::LexAction(LexActionType type, size_t state_index, ISymbol symbol) : + LexAction::LexAction(LexActionType type, size_t state_index, Symbol symbol) : type(type), symbol(symbol), state_index(state_index) {} LexAction LexAction::Error() { - return LexAction(LexActionTypeError, -1, ISymbol(-1)); + return LexAction(LexActionTypeError, -1, Symbol(-1)); } LexAction LexAction::Advance(size_t state_index) { - return LexAction(LexActionTypeAdvance, state_index, ISymbol(-1)); + return LexAction(LexActionTypeAdvance, state_index, Symbol(-1)); } - LexAction LexAction::Accept(ISymbol symbol) { + LexAction LexAction::Accept(Symbol symbol) { return LexAction(LexActionTypeAccept, -1, symbol); } diff --git a/src/compiler/lex_table.h b/src/compiler/lex_table.h index f8c74d88..15808ead 100644 --- a/src/compiler/lex_table.h +++ b/src/compiler/lex_table.h @@ -5,7 +5,7 @@ #include #include #include -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" #include "compiler/rules/character_set.h" namespace tree_sitter { @@ -16,16 +16,16 @@ namespace tree_sitter { } LexActionType; class LexAction { - LexAction(LexActionType type, size_t state_index, rules::ISymbol symbol); + LexAction(LexActionType type, size_t state_index, rules::Symbol symbol); public: LexAction(); - static LexAction Accept(rules::ISymbol symbol); + static LexAction Accept(rules::Symbol symbol); static LexAction Error(); static LexAction Advance(size_t state_index); bool operator==(const LexAction &action) const; LexActionType type; - rules::ISymbol symbol; + rules::Symbol symbol; size_t state_index; }; @@ -37,7 +37,7 @@ namespace std { struct hash { size_t operator()(const tree_sitter::LexAction &action) const { return (hash()(action.type) ^ - hash()(action.symbol) ^ + hash()(action.symbol) ^ hash()(action.state_index)); } }; diff --git a/src/compiler/parse_table.cc b/src/compiler/parse_table.cc index d6281abf..e9a7ef19 100644 --- a/src/compiler/parse_table.cc +++ b/src/compiler/parse_table.cc @@ -7,11 +7,11 @@ namespace tree_sitter { using std::to_string; using std::set; using std::vector; - using rules::ISymbol; + using rules::Symbol; ParseAction::ParseAction(ParseActionType type, size_t state_index, - ISymbol symbol, + Symbol symbol, size_t consumed_symbol_count, set precedence_values) : type(type), @@ -22,23 +22,23 @@ namespace tree_sitter { ParseAction::ParseAction() : type(ParseActionTypeError), - symbol(ISymbol(-1)), + symbol(Symbol(-1)), state_index(-1), consumed_symbol_count(0) {} ParseAction ParseAction::Error() { - return ParseAction(ParseActionTypeError, -1, ISymbol(-1), 0, { 0 }); + return ParseAction(ParseActionTypeError, -1, Symbol(-1), 0, { 0 }); } ParseAction ParseAction::Accept() { - return ParseAction(ParseActionTypeAccept, -1, ISymbol(-1), 0, { 0 }); + return ParseAction(ParseActionTypeAccept, -1, Symbol(-1), 0, { 0 }); } ParseAction ParseAction::Shift(size_t state_index, set precedence_values) { - return ParseAction(ParseActionTypeShift, state_index, ISymbol(-1), 0, precedence_values); + return ParseAction(ParseActionTypeShift, state_index, Symbol(-1), 0, precedence_values); } - ParseAction ParseAction::Reduce(ISymbol symbol, size_t consumed_symbol_count, int precedence) { + ParseAction ParseAction::Reduce(Symbol symbol, size_t consumed_symbol_count, int precedence) { return ParseAction(ParseActionTypeReduce, -1, symbol, consumed_symbol_count, { precedence }); } @@ -66,8 +66,8 @@ namespace tree_sitter { ParseState::ParseState() : lex_state_id(-1) {} - set ParseState::expected_inputs() const { - set result; + set ParseState::expected_inputs() const { + set result; for (auto &pair : actions) result.insert(pair.first); return result; @@ -90,7 +90,7 @@ namespace tree_sitter { return states.size() - 1; } - void ParseTable::add_action(ParseStateId id, ISymbol symbol, ParseAction action) { + void ParseTable::add_action(ParseStateId id, Symbol symbol, ParseAction action) { symbols.insert(symbol); states[id].actions[symbol] = action; } diff --git a/src/compiler/parse_table.h b/src/compiler/parse_table.h index 0b93cf71..409594ec 100644 --- a/src/compiler/parse_table.h +++ b/src/compiler/parse_table.h @@ -6,7 +6,7 @@ #include #include #include "compiler/lex_table.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { typedef enum { @@ -19,7 +19,7 @@ namespace tree_sitter { class ParseAction { ParseAction(ParseActionType type, size_t state_index, - rules::ISymbol symbol, + rules::Symbol symbol, size_t consumed_symbol_count, std::set precedence_values); public: @@ -27,11 +27,11 @@ namespace tree_sitter { static ParseAction Accept(); static ParseAction Error(); static ParseAction Shift(size_t state_index, std::set precedence_values); - static ParseAction Reduce(rules::ISymbol symbol, size_t consumed_symbol_count, int precedence); + static ParseAction Reduce(rules::Symbol symbol, size_t consumed_symbol_count, int precedence); bool operator==(const ParseAction &action) const; ParseActionType type; - rules::ISymbol symbol; + rules::Symbol symbol; size_t state_index; size_t consumed_symbol_count; std::set precedence_values; @@ -46,7 +46,7 @@ namespace std { size_t operator()(const tree_sitter::ParseAction &action) const { return ( hash()(action.type) ^ - hash()(action.symbol) ^ + hash()(action.symbol) ^ hash()(action.state_index) ^ hash()(action.consumed_symbol_count)); } @@ -57,8 +57,8 @@ namespace tree_sitter { class ParseState { public: ParseState(); - std::map actions; - std::set expected_inputs() const; + std::map actions; + std::set expected_inputs() const; LexStateId lex_state_id; }; @@ -69,10 +69,10 @@ namespace tree_sitter { class ParseTable { public: uint64_t add_state(); - void add_action(ParseStateId state_id, rules::ISymbol symbol, ParseAction action); + void add_action(ParseStateId state_id, rules::Symbol symbol, ParseAction action); std::vector states; - std::set symbols; + std::set symbols; }; } diff --git a/src/compiler/prepare_grammar/expand_repeats.cc b/src/compiler/prepare_grammar/expand_repeats.cc index b43ba504..49980181 100644 --- a/src/compiler/prepare_grammar/expand_repeats.cc +++ b/src/compiler/prepare_grammar/expand_repeats.cc @@ -5,7 +5,7 @@ #include "compiler/prepared_grammar.h" #include "compiler/rules/visitor.h" #include "compiler/rules/seq.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" #include "compiler/rules/choice.h" #include "compiler/rules/blank.h" #include "compiler/rules/repeat.h" @@ -22,7 +22,7 @@ namespace tree_sitter { using rules::Repeat; using rules::Rule; using rules::Seq; - using rules::ISymbol; + using rules::Symbol; namespace prepare_grammar { class ExpandRepeats : public rules::IdentityRuleFn { @@ -32,7 +32,7 @@ namespace tree_sitter { rule_ptr inner_rule = apply(rule->content); size_t index = aux_rules.size(); string helper_rule_name = rule_name + string("_repeat") + to_string(index); - rule_ptr repeat_symbol = make_shared(offset + index, rules::SymbolOptionAuxiliary); + rule_ptr repeat_symbol = make_shared(offset + index, rules::SymbolOptionAuxiliary); aux_rules.push_back({ helper_rule_name, Choice::Build({ diff --git a/src/compiler/prepare_grammar/extract_tokens.cc b/src/compiler/prepare_grammar/extract_tokens.cc index 28fa7440..01103bcf 100644 --- a/src/compiler/prepare_grammar/extract_tokens.cc +++ b/src/compiler/prepare_grammar/extract_tokens.cc @@ -9,7 +9,7 @@ #include "compiler/rules/choice.h" #include "compiler/rules/repeat.h" #include "compiler/rules/blank.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" #include "compiler/rules/string.h" #include "compiler/rules/pattern.h" @@ -21,7 +21,7 @@ namespace tree_sitter { using std::vector; using std::make_shared; using rules::rule_ptr; - using rules::ISymbol; + using rules::Symbol; using std::dynamic_pointer_cast; namespace prepare_grammar { @@ -31,10 +31,10 @@ namespace tree_sitter { }; class SymbolInliner : public rules::IdentityRuleFn { - map replacements; + map replacements; using rules::IdentityRuleFn::apply_to; - int new_index_for_symbol(const ISymbol &symbol) { + int new_index_for_symbol(const Symbol &symbol) { int result = symbol.index; for (const auto &pair : replacements) if (pair.first.index < symbol.index && @@ -43,18 +43,18 @@ namespace tree_sitter { return result; } - rule_ptr apply_to(const ISymbol *rule) { + rule_ptr apply_to(const Symbol *rule) { auto replacement_pair = replacements.find(*rule); if (replacement_pair != replacements.end()) return replacement_pair->second.copy(); else if (rule->is_built_in()) return rule->copy(); else - return make_shared(new_index_for_symbol(*rule), rule->options); + return make_shared(new_index_for_symbol(*rule), rule->options); } public: - SymbolInliner(const map &replacements, size_t rule_count, size_t aux_rule_count) : + SymbolInliner(const map &replacements, size_t rule_count, size_t aux_rule_count) : replacements(replacements) {} }; @@ -73,7 +73,7 @@ namespace tree_sitter { auto result = rule->copy(); if (IsToken().apply(result)) { size_t index = add_token(result); - return make_shared(index, rules::SymbolOption(rules::SymbolOptionToken|rules::SymbolOptionAuxiliary)); + return make_shared(index, rules::SymbolOption(rules::SymbolOptionToken|rules::SymbolOptionAuxiliary)); } else { return result; } @@ -86,15 +86,15 @@ namespace tree_sitter { pair extract_tokens(const PreparedGrammar &input_grammar) { vector> rules, tokens, aux_rules, aux_tokens; TokenExtractor extractor; - map symbol_replacements; + map symbol_replacements; for (size_t i = 0; i < input_grammar.rules.size(); i++) { auto pair = input_grammar.rules[i]; if (IsToken().apply(pair.second)) { tokens.push_back(pair); symbol_replacements.insert({ - ISymbol(i), - ISymbol(tokens.size() - 1, rules::SymbolOption(rules::SymbolOptionToken)) + Symbol(i), + Symbol(tokens.size() - 1, rules::SymbolOption(rules::SymbolOptionToken)) }); } else { rules.push_back({ pair.first, extractor.apply(pair.second) }); @@ -106,8 +106,8 @@ namespace tree_sitter { if (IsToken().apply(pair.second)) { aux_tokens.push_back(pair); symbol_replacements.insert({ - ISymbol(i, rules::SymbolOptionAuxiliary), - ISymbol(aux_tokens.size() - 1, rules::SymbolOption(rules::SymbolOptionAuxiliary|rules::SymbolOptionToken)) + Symbol(i, rules::SymbolOptionAuxiliary), + Symbol(aux_tokens.size() - 1, rules::SymbolOption(rules::SymbolOptionAuxiliary|rules::SymbolOptionToken)) }); } else { aux_rules.push_back({ pair.first, extractor.apply(pair.second) }); diff --git a/src/compiler/prepare_grammar/intern_symbols.cc b/src/compiler/prepare_grammar/intern_symbols.cc index 91160a26..0a05ea0b 100644 --- a/src/compiler/prepare_grammar/intern_symbols.cc +++ b/src/compiler/prepare_grammar/intern_symbols.cc @@ -4,7 +4,7 @@ #include "compiler/prepared_grammar.h" #include "compiler/rules/visitor.h" #include "compiler/rules/named_symbol.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { using std::string; @@ -12,7 +12,6 @@ namespace tree_sitter { using std::vector; using std::pair; using std::make_shared; - using std::exception; GrammarError::GrammarError(string rule_name) : rule_name(rule_name) {} @@ -36,7 +35,7 @@ namespace tree_sitter { long index = index_of(rule->name); if (index == -1) missing_rule_name = rule->name; - return make_shared(index); + return make_shared(index); } public: diff --git a/src/compiler/prepared_grammar.cc b/src/compiler/prepared_grammar.cc index a953ee0b..aa34cfbd 100644 --- a/src/compiler/prepared_grammar.cc +++ b/src/compiler/prepared_grammar.cc @@ -2,14 +2,14 @@ #include #include #include -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { using std::string; using std::pair; using std::ostream; using rules::rule_ptr; - using rules::ISymbol; + using rules::Symbol; PreparedGrammar::PreparedGrammar(const std::vector> &rules, const std::vector> &aux_rules) : @@ -20,13 +20,13 @@ namespace tree_sitter { Grammar(grammar), aux_rules({}) {} - const rule_ptr & PreparedGrammar::rule(const ISymbol &symbol) const { + const rule_ptr & PreparedGrammar::rule(const Symbol &symbol) const { return symbol.is_auxiliary() ? aux_rules[symbol.index].second : rules[symbol.index].second; } - const string & PreparedGrammar::rule_name(const ISymbol &symbol) const { + const string & PreparedGrammar::rule_name(const Symbol &symbol) const { return symbol.is_auxiliary() ? aux_rules[symbol.index].first : rules[symbol.index].first; diff --git a/src/compiler/prepared_grammar.h b/src/compiler/prepared_grammar.h index 6da53f24..1e63e18c 100644 --- a/src/compiler/prepared_grammar.h +++ b/src/compiler/prepared_grammar.h @@ -5,7 +5,7 @@ #include #include #include "tree_sitter/compiler.h" -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { class PreparedGrammar : public Grammar { @@ -15,8 +15,8 @@ namespace tree_sitter { PreparedGrammar(const Grammar &grammar); bool operator==(const PreparedGrammar &other) const; - const std::string & rule_name(const rules::ISymbol &symbol) const; - const rules::rule_ptr & rule(const rules::ISymbol &symbol) 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; }; diff --git a/src/compiler/rules/built_in_symbols.cc b/src/compiler/rules/built_in_symbols.cc index 99efb0f7..96d41f18 100644 --- a/src/compiler/rules/built_in_symbols.cc +++ b/src/compiler/rules/built_in_symbols.cc @@ -2,8 +2,8 @@ namespace tree_sitter { namespace rules { - ISymbol END_OF_INPUT() { return ISymbol(-1, SymbolOptionToken); } - ISymbol ERROR() { return ISymbol(-2, SymbolOptionToken); } - ISymbol START() { return ISymbol(-3); } + Symbol END_OF_INPUT() { return Symbol(-1, SymbolOptionToken); } + Symbol ERROR() { return Symbol(-2, SymbolOptionToken); } + Symbol START() { return Symbol(-3); } } } diff --git a/src/compiler/rules/built_in_symbols.h b/src/compiler/rules/built_in_symbols.h index 34ceda50..bdbec4e5 100644 --- a/src/compiler/rules/built_in_symbols.h +++ b/src/compiler/rules/built_in_symbols.h @@ -1,13 +1,13 @@ #ifndef COMPILER_RULES_BUILT_IN_SYMBOLS_H_ #define COMPILER_RULES_BUILT_IN_SYMBOLS_H_ -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { namespace rules { - ISymbol ERROR(); - ISymbol START(); - ISymbol END_OF_INPUT(); + Symbol ERROR(); + Symbol START(); + Symbol END_OF_INPUT(); } } diff --git a/src/compiler/rules/named_symbol.h b/src/compiler/rules/named_symbol.h index eaa9caf5..ab6bc892 100644 --- a/src/compiler/rules/named_symbol.h +++ b/src/compiler/rules/named_symbol.h @@ -1,8 +1,7 @@ -#ifndef COMPILER_RULES_SYMBOL_H_ -#define COMPILER_RULES_SYMBOL_H_ +#ifndef COMPILER_RULES_NAMED_SYMBOL_H_ +#define COMPILER_RULES_NAMED_SYMBOL_H_ #include -#include #include "compiler/rules/rule.h" namespace tree_sitter { @@ -22,4 +21,4 @@ namespace tree_sitter { } } -#endif // COMPILER_RULES_SYMBOL_H_ +#endif // COMPILER_RULES_NAMED_SYMBOL_H_ diff --git a/src/compiler/rules/interned_symbol.cc b/src/compiler/rules/symbol.cc similarity index 59% rename from src/compiler/rules/interned_symbol.cc rename to src/compiler/rules/symbol.cc index 1cd2d0c7..8652d8f4 100644 --- a/src/compiler/rules/interned_symbol.cc +++ b/src/compiler/rules/symbol.cc @@ -1,4 +1,4 @@ -#include "compiler/rules/interned_symbol.h" +#include "compiler/rules/symbol.h" #include #include #include "compiler/rules/visitor.h" @@ -9,56 +9,56 @@ namespace tree_sitter { using std::hash; namespace rules { - ISymbol::ISymbol(int index) : + Symbol::Symbol(int index) : index(index), options(SymbolOption(0)) {} - ISymbol::ISymbol(int index, SymbolOption options) : + Symbol::Symbol(int index, SymbolOption options) : index(index), options(options) {} - bool ISymbol::operator==(const ISymbol &other) const { + bool Symbol::operator==(const Symbol &other) const { return (other.index == index) && (other.options == options); } - bool ISymbol::operator==(const Rule &rule) const { - const ISymbol *other = dynamic_cast(&rule); + bool Symbol::operator==(const Rule &rule) const { + const Symbol *other = dynamic_cast(&rule); return other && this->operator==(*other); } - size_t ISymbol::hash_code() const { + size_t Symbol::hash_code() const { return hash()(index) ^ hash()(options); } - rule_ptr ISymbol::copy() const { - return std::make_shared(*this); + rule_ptr Symbol::copy() const { + return std::make_shared(*this); } - string ISymbol::to_string() const { + string Symbol::to_string() const { string name = (options & SymbolOptionAuxiliary) ? "aux_" : ""; name += (options & SymbolOptionToken) ? "token" : "sym"; return "#<" + name + std::to_string(index) + ">"; } - bool ISymbol::operator<(const ISymbol &other) const { + bool Symbol::operator<(const Symbol &other) const { if (options < other.options) return true; if (options > other.options) return false; return (index < other.index); } - bool ISymbol::is_token() const { + bool Symbol::is_token() const { return options & SymbolOptionToken; } - bool ISymbol::is_built_in() const { + bool Symbol::is_built_in() const { return index < 0; } - bool ISymbol::is_auxiliary() const { + bool Symbol::is_auxiliary() const { return options & SymbolOptionAuxiliary; } - void ISymbol::accept(Visitor *visitor) const { + void Symbol::accept(Visitor *visitor) const { visitor->visit(this); } } diff --git a/src/compiler/rules/interned_symbol.h b/src/compiler/rules/symbol.h similarity index 59% rename from src/compiler/rules/interned_symbol.h rename to src/compiler/rules/symbol.h index 3d41d410..c73ae8be 100644 --- a/src/compiler/rules/interned_symbol.h +++ b/src/compiler/rules/symbol.h @@ -1,5 +1,5 @@ -#ifndef COMPILER_RULES_INTERNED_SYMBOL_H_ -#define COMPILER_RULES_INTERNED_SYMBOL_H_ +#ifndef COMPILER_RULES_SYMBOL_H_ +#define COMPILER_RULES_SYMBOL_H_ #include "compiler/rules/rule.h" @@ -10,20 +10,20 @@ namespace tree_sitter { SymbolOptionAuxiliary = 1 << 1, } SymbolOption; - class ISymbol : public Rule { + class Symbol : public Rule { public: - explicit ISymbol(int index); - ISymbol(int index, SymbolOption options); + explicit Symbol(int index); + Symbol(int index, SymbolOption options); - bool operator==(const ISymbol &other) const; + bool operator==(const Symbol &other) const; bool operator==(const Rule &other) const; size_t hash_code() const; rule_ptr copy() const; std::string to_string() const; void accept(Visitor *visitor) const; - bool operator<(const ISymbol &other) const; + bool operator<(const Symbol &other) const; bool is_token() const; bool is_built_in() const; bool is_auxiliary() const; @@ -36,11 +36,11 @@ namespace tree_sitter { namespace std { template<> - struct hash { - size_t operator()(const tree_sitter::rules::ISymbol &rule) const { + struct hash { + size_t operator()(const tree_sitter::rules::Symbol &rule) const { return rule.hash_code(); } }; } -#endif // COMPILER_RULES_INTERNED_SYMBOL_H_ +#endif // COMPILER_RULES_SYMBOL_H_ diff --git a/src/compiler/rules/visitor.h b/src/compiler/rules/visitor.h index 9f7d863d..1fd3e7ca 100644 --- a/src/compiler/rules/visitor.h +++ b/src/compiler/rules/visitor.h @@ -12,7 +12,7 @@ namespace tree_sitter { class Repeat; class Seq; class String; - class ISymbol; + class Symbol; class Pattern; class Metadata; @@ -27,7 +27,7 @@ namespace tree_sitter { virtual void visit(const Seq *rule) = 0; virtual void visit(const String *rule) = 0; virtual void visit(const NamedSymbol *rule) = 0; - virtual void visit(const ISymbol *rule) = 0; + virtual void visit(const Symbol *rule) = 0; virtual ~Visitor(); }; @@ -51,7 +51,7 @@ namespace tree_sitter { virtual T apply_to(const Seq *rule) { return default_apply((const Rule *)rule); } virtual T apply_to(const String *rule) { return default_apply((const Rule *)rule); } virtual T apply_to(const NamedSymbol *rule) { return default_apply((const Rule *)rule); } - virtual T apply_to(const ISymbol *rule) { return default_apply((const Rule *)rule); } + virtual T apply_to(const Symbol *rule) { return default_apply((const Rule *)rule); } void visit(const Blank *rule) { value_ = apply_to(rule); } void visit(const CharacterSet *rule) { value_ = apply_to(rule); } @@ -62,7 +62,7 @@ namespace tree_sitter { void visit(const Seq *rule) { value_ = apply_to(rule); } void visit(const String *rule) { value_ = apply_to(rule); } void visit(const NamedSymbol *rule) { value_ = apply_to(rule); } - void visit(const ISymbol *rule) { value_ = apply_to(rule); } + void visit(const Symbol *rule) { value_ = apply_to(rule); } private: T value_;