Symbol -> NamedSymbol
This commit is contained in:
parent
66c033e411
commit
faf80aadac
18 changed files with 51 additions and 76 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
namespace tree_sitter {
|
||||
namespace build_tables {
|
||||
using rules::Symbol;
|
||||
using std::string;
|
||||
using std::to_string;
|
||||
using std::map;
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <set>
|
||||
#include "compiler/build_tables/parse_item.h"
|
||||
#include "compiler/rules/symbol.h"
|
||||
#include "compiler/rules/interned_symbol.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
class PreparedGrammar;
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
#include <set>
|
||||
#include <map>
|
||||
#include "compiler/build_tables/parse_item.h"
|
||||
#include "compiler/rules/symbol.h"
|
||||
#include "compiler/rules/interned_symbol.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
class PreparedGrammar;
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#include <set>
|
||||
#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"
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
33
src/compiler/rules/named_symbol.cc
Normal file
33
src/compiler/rules/named_symbol.cc
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include "compiler/rules/named_symbol.h"
|
||||
#include <string>
|
||||
#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<const NamedSymbol *>(&rule);
|
||||
return other && other->name == name;
|
||||
}
|
||||
|
||||
size_t NamedSymbol::hash_code() const {
|
||||
return hash<string>()(name);
|
||||
}
|
||||
|
||||
rule_ptr NamedSymbol::copy() const {
|
||||
return std::make_shared<NamedSymbol>(*this);
|
||||
}
|
||||
|
||||
string NamedSymbol::to_string() const {
|
||||
return string("#<sym '") + name + "'>";
|
||||
}
|
||||
|
||||
void NamedSymbol::accept(Visitor *visitor) const {
|
||||
visitor->visit(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<tree_sitter::rules::Symbol> {
|
||||
size_t operator()(const tree_sitter::rules::Symbol &rule) const {
|
||||
return rule.hash_code();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // COMPILER_RULES_SYMBOL_H_
|
||||
|
|
@ -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<Symbol>(name);
|
||||
return make_shared<NamedSymbol>(name);
|
||||
}
|
||||
|
||||
rule_ptr pattern(const string &value) {
|
||||
|
|
|
|||
|
|
@ -1,42 +0,0 @@
|
|||
#include "compiler/rules/symbol.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
#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<const Symbol *>(&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<string>()(name);
|
||||
}
|
||||
|
||||
rule_ptr Symbol::copy() const {
|
||||
return std::make_shared<Symbol>(*this);
|
||||
}
|
||||
|
||||
string Symbol::to_string() const {
|
||||
return string("#<sym '") + name + "'>";
|
||||
}
|
||||
|
||||
bool Symbol::operator<(const Symbol &other) const {
|
||||
return name < other.name;
|
||||
}
|
||||
|
||||
void Symbol::accept(Visitor *visitor) const {
|
||||
visitor->visit(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue