diff --git a/spec/compiler/prepare_grammar/expand_repeats_spec.cc b/spec/compiler/prepare_grammar/expand_repeats_spec.cc index f8924dd0..833b49f0 100644 --- a/spec/compiler/prepare_grammar/expand_repeats_spec.cc +++ b/spec/compiler/prepare_grammar/expand_repeats_spec.cc @@ -1,7 +1,6 @@ #include "compiler_spec_helper.h" #include "compiler/prepared_grammar.h" #include "compiler/prepare_grammar/expand_repeats.h" -#include "compiler/rules/symbol.h" START_TEST diff --git a/spec/compiler/prepare_grammar/extract_tokens_spec.cc b/spec/compiler/prepare_grammar/extract_tokens_spec.cc index a13e37b8..d39f8a41 100644 --- a/spec/compiler/prepare_grammar/extract_tokens_spec.cc +++ b/spec/compiler/prepare_grammar/extract_tokens_spec.cc @@ -1,7 +1,6 @@ #include "compiler_spec_helper.h" #include "compiler/prepared_grammar.h" #include "compiler/prepare_grammar/extract_tokens.h" -#include "compiler/rules/symbol.h" START_TEST diff --git a/spec/compiler/prepare_grammar/intern_symbols_spec.cc b/spec/compiler/prepare_grammar/intern_symbols_spec.cc index baee0563..544bd139 100644 --- a/spec/compiler/prepare_grammar/intern_symbols_spec.cc +++ b/spec/compiler/prepare_grammar/intern_symbols_spec.cc @@ -1,7 +1,7 @@ #include "compiler_spec_helper.h" #include "compiler/prepared_grammar.h" #include "compiler/prepare_grammar/intern_symbols.h" -#include "compiler/rules/symbol.h" +#include "compiler/rules/named_symbol.h" #include "compiler/rules/interned_symbol.h" START_TEST diff --git a/src/compiler/build_tables/conflict_manager.cc b/src/compiler/build_tables/conflict_manager.cc index 547a0813..235d84e4 100644 --- a/src/compiler/build_tables/conflict_manager.cc +++ b/src/compiler/build_tables/conflict_manager.cc @@ -7,7 +7,6 @@ namespace tree_sitter { namespace build_tables { - using rules::Symbol; using std::string; using std::to_string; using std::map; diff --git a/src/compiler/build_tables/first_set.h b/src/compiler/build_tables/first_set.h index 77a83476..30e6137c 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/symbol.h" +#include "compiler/rules/interned_symbol.h" namespace tree_sitter { class PreparedGrammar; diff --git a/src/compiler/build_tables/follow_sets.h b/src/compiler/build_tables/follow_sets.h index 2090eaea..9c2126cd 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/symbol.h" +#include "compiler/rules/interned_symbol.h" namespace tree_sitter { class PreparedGrammar; diff --git a/src/compiler/build_tables/lex_item.cc b/src/compiler/build_tables/lex_item.cc index 1920b04d..05b05d22 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/symbol.h" +#include "compiler/rules/interned_symbol.h" #include "compiler/rules/metadata.h" #include "compiler/rules/seq.h" #include "compiler/rules/visitor.h" diff --git a/src/compiler/build_tables/rule_can_be_blank.cc b/src/compiler/build_tables/rule_can_be_blank.cc index 521d93cd..c2242c35 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/symbol.h" +#include "compiler/rules/interned_symbol.h" #include "compiler/rules/visitor.h" #include "compiler/rules/seq.h" #include "compiler/rules/choice.h" diff --git a/src/compiler/prepare_grammar/expand_repeats.cc b/src/compiler/prepare_grammar/expand_repeats.cc index 86f00961..b43ba504 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/symbol.h" +#include "compiler/rules/interned_symbol.h" #include "compiler/rules/choice.h" #include "compiler/rules/blank.h" #include "compiler/rules/repeat.h" diff --git a/src/compiler/prepare_grammar/extract_tokens.cc b/src/compiler/prepare_grammar/extract_tokens.cc index 11fdb615..28fa7440 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/symbol.h" +#include "compiler/rules/interned_symbol.h" #include "compiler/rules/string.h" #include "compiler/rules/pattern.h" diff --git a/src/compiler/prepare_grammar/intern_symbols.cc b/src/compiler/prepare_grammar/intern_symbols.cc index 86bdbb70..91160a26 100644 --- a/src/compiler/prepare_grammar/intern_symbols.cc +++ b/src/compiler/prepare_grammar/intern_symbols.cc @@ -3,7 +3,7 @@ #include "tree_sitter/compiler.h" #include "compiler/prepared_grammar.h" #include "compiler/rules/visitor.h" -#include "compiler/rules/symbol.h" +#include "compiler/rules/named_symbol.h" #include "compiler/rules/interned_symbol.h" namespace tree_sitter { @@ -32,7 +32,7 @@ namespace tree_sitter { return -1; } - rule_ptr apply_to(const rules::Symbol *rule) { + rule_ptr apply_to(const rules::NamedSymbol *rule) { long index = index_of(rule->name); if (index == -1) missing_rule_name = rule->name; diff --git a/src/compiler/rules/interned_symbol.h b/src/compiler/rules/interned_symbol.h index 03e65c1e..3d41d410 100644 --- a/src/compiler/rules/interned_symbol.h +++ b/src/compiler/rules/interned_symbol.h @@ -1,7 +1,7 @@ #ifndef COMPILER_RULES_INTERNED_SYMBOL_H_ #define COMPILER_RULES_INTERNED_SYMBOL_H_ -#include "compiler/rules/symbol.h" +#include "compiler/rules/rule.h" namespace tree_sitter { namespace rules { diff --git a/src/compiler/rules/named_symbol.cc b/src/compiler/rules/named_symbol.cc new file mode 100644 index 00000000..a18afad6 --- /dev/null +++ b/src/compiler/rules/named_symbol.cc @@ -0,0 +1,33 @@ +#include "compiler/rules/named_symbol.h" +#include +#include "compiler/rules/visitor.h" + +namespace tree_sitter { + using std::string; + using std::hash; + + namespace rules { + NamedSymbol::NamedSymbol(const std::string &name) : name(name) {} + + bool NamedSymbol::operator==(const Rule &rule) const { + auto other = dynamic_cast(&rule); + return other && other->name == name; + } + + size_t NamedSymbol::hash_code() const { + return hash()(name); + } + + rule_ptr NamedSymbol::copy() const { + return std::make_shared(*this); + } + + string NamedSymbol::to_string() const { + return string("#"; + } + + void NamedSymbol::accept(Visitor *visitor) const { + visitor->visit(this); + } + } +} diff --git a/src/compiler/rules/symbol.h b/src/compiler/rules/named_symbol.h similarity index 55% rename from src/compiler/rules/symbol.h rename to src/compiler/rules/named_symbol.h index 19692dc4..eaa9caf5 100644 --- a/src/compiler/rules/symbol.h +++ b/src/compiler/rules/named_symbol.h @@ -7,31 +7,19 @@ namespace tree_sitter { namespace rules { - class Symbol : public Rule { + class NamedSymbol : public Rule { public: - explicit Symbol(const std::string &name); + explicit NamedSymbol(const std::string &name); bool operator==(const Rule& other) const; - bool operator==(const Symbol &other) const; - size_t hash_code() const; rule_ptr copy() const; std::string to_string() const; void accept(Visitor *visitor) const; - bool operator<(const Symbol &other) const; std::string name; }; } } -namespace std { - template<> - struct hash { - size_t operator()(const tree_sitter::rules::Symbol &rule) const { - return rule.hash_code(); - } - }; -} - #endif // COMPILER_RULES_SYMBOL_H_ diff --git a/src/compiler/rules/rules.cc b/src/compiler/rules/rules.cc index b04ea6f6..8db093c0 100644 --- a/src/compiler/rules/rules.cc +++ b/src/compiler/rules/rules.cc @@ -5,7 +5,7 @@ #include "tree_sitter/compiler.h" #include "compiler/rules/rule.h" #include "compiler/rules/blank.h" -#include "compiler/rules/symbol.h" +#include "compiler/rules/named_symbol.h" #include "compiler/rules/choice.h" #include "compiler/rules/seq.h" #include "compiler/rules/string.h" @@ -40,7 +40,7 @@ namespace tree_sitter { } rule_ptr sym(const string &name) { - return make_shared(name); + return make_shared(name); } rule_ptr pattern(const string &value) { diff --git a/src/compiler/rules/symbol.cc b/src/compiler/rules/symbol.cc deleted file mode 100644 index bb984157..00000000 --- a/src/compiler/rules/symbol.cc +++ /dev/null @@ -1,42 +0,0 @@ -#include "compiler/rules/symbol.h" -#include -#include -#include "compiler/rules/visitor.h" - -namespace tree_sitter { - using std::string; - using std::hash; - - namespace rules { - Symbol::Symbol(const std::string &name) : name(name) {} - - bool Symbol::operator==(const Rule &rule) const { - const Symbol *other = dynamic_cast(&rule); - return other && this->operator==(*other); - } - - bool Symbol::operator==(const Symbol &other) const { - return other.name == name; - } - - size_t Symbol::hash_code() const { - return hash()(name); - } - - rule_ptr Symbol::copy() const { - return std::make_shared(*this); - } - - string Symbol::to_string() const { - return string("#"; - } - - bool Symbol::operator<(const Symbol &other) const { - return name < other.name; - } - - void Symbol::accept(Visitor *visitor) const { - visitor->visit(this); - } - } -} diff --git a/src/compiler/rules/visitor.cc b/src/compiler/rules/visitor.cc index 04cfc5e0..e64b1b0a 100644 --- a/src/compiler/rules/visitor.cc +++ b/src/compiler/rules/visitor.cc @@ -1,7 +1,6 @@ #include "compiler/rules/visitor.h" #include "compiler/rules/rule.h" #include "compiler/rules/blank.h" -#include "compiler/rules/symbol.h" #include "compiler/rules/character_set.h" #include "compiler/rules/choice.h" #include "compiler/rules/seq.h" diff --git a/src/compiler/rules/visitor.h b/src/compiler/rules/visitor.h index a7538958..9f7d863d 100644 --- a/src/compiler/rules/visitor.h +++ b/src/compiler/rules/visitor.h @@ -6,7 +6,7 @@ namespace tree_sitter { namespace rules { class Blank; - class Symbol; + class NamedSymbol; class CharacterSet; class Choice; class Repeat; @@ -26,7 +26,7 @@ namespace tree_sitter { virtual void visit(const Repeat *rule) = 0; virtual void visit(const Seq *rule) = 0; virtual void visit(const String *rule) = 0; - virtual void visit(const Symbol *rule) = 0; + virtual void visit(const NamedSymbol *rule) = 0; virtual void visit(const ISymbol *rule) = 0; virtual ~Visitor(); }; @@ -50,7 +50,7 @@ namespace tree_sitter { virtual T apply_to(const Repeat *rule) { return default_apply((const Rule *)rule); } 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 Symbol *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); } void visit(const Blank *rule) { value_ = apply_to(rule); } @@ -61,7 +61,7 @@ namespace tree_sitter { void visit(const Repeat *rule) { value_ = apply_to(rule); } void visit(const Seq *rule) { value_ = apply_to(rule); } void visit(const String *rule) { value_ = apply_to(rule); } - void visit(const Symbol *rule) { value_ = apply_to(rule); } + void visit(const NamedSymbol *rule) { value_ = apply_to(rule); } void visit(const ISymbol *rule) { value_ = apply_to(rule); } private: