Remove subclasses of Symbol for terminals and non-terminals
This commit is contained in:
parent
ed80d9cf52
commit
29c81167c0
23 changed files with 88 additions and 191 deletions
|
|
@ -1,28 +0,0 @@
|
|||
#include "rules.h"
|
||||
#include "transition_map.h"
|
||||
|
||||
using std::string;
|
||||
using std::hash;
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
NonTerminal::NonTerminal(const std::string &name) : Symbol(name) {};
|
||||
|
||||
bool NonTerminal::operator==(const Rule &rule) const {
|
||||
const NonTerminal *other = dynamic_cast<const NonTerminal *>(&rule);
|
||||
return other && (other->name == name);
|
||||
}
|
||||
|
||||
rule_ptr NonTerminal::copy() const {
|
||||
return std::make_shared<NonTerminal>(*this);
|
||||
}
|
||||
|
||||
string NonTerminal::to_string() const {
|
||||
return string("#<non-terminal '") + name + "'>";
|
||||
}
|
||||
|
||||
void NonTerminal::accept(Visitor &visitor) const {
|
||||
visitor.visit(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
#ifndef __tree_sitter__non_terminal__
|
||||
#define __tree_sitter__non_terminal__
|
||||
|
||||
#include "symbol.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
class NonTerminal : public Symbol {
|
||||
public:
|
||||
NonTerminal(const std::string &name);
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
rule_ptr copy() const;
|
||||
std::string to_string() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -47,11 +47,7 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
sym_ptr sym(const string &name) {
|
||||
return make_shared<NonTerminal>(name);
|
||||
}
|
||||
|
||||
rule_ptr token(const std::string &name) {
|
||||
return make_shared<Token>(name);
|
||||
return make_shared<Symbol>(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,14 +4,12 @@
|
|||
#include "rule.h"
|
||||
#include "blank.h"
|
||||
#include "symbol.h"
|
||||
#include "token.h"
|
||||
#include "choice.h"
|
||||
#include "seq.h"
|
||||
#include "string.h"
|
||||
#include "pattern.h"
|
||||
#include "character.h"
|
||||
#include "repeat.h"
|
||||
#include "non_terminal.h"
|
||||
#include "visitor.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
|
|
@ -26,7 +24,6 @@ namespace tree_sitter {
|
|||
rule_ptr seq(const std::initializer_list<rule_ptr> &rules);
|
||||
rule_ptr str(const std::string &value);
|
||||
sym_ptr sym(const std::string &name);
|
||||
rule_ptr token(const std::string &name);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,10 @@ namespace tree_sitter {
|
|||
return string("#<sym '") + name + "'>";
|
||||
}
|
||||
|
||||
bool Symbol::operator<(const Symbol &other) const {
|
||||
return name < other.name;
|
||||
}
|
||||
|
||||
void Symbol::accept(Visitor &visitor) const {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ namespace tree_sitter {
|
|||
rule_ptr copy() const;
|
||||
std::string to_string() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
bool operator<(const Symbol &other) const;
|
||||
|
||||
const std::string name;
|
||||
};
|
||||
|
|
@ -22,4 +23,10 @@ namespace tree_sitter {
|
|||
}
|
||||
}
|
||||
|
||||
namespace std {
|
||||
template<>
|
||||
struct hash<tree_sitter::rules::Symbol> : hash<tree_sitter::rules::Rule> {};
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
#include "rules.h"
|
||||
#include "transition_map.h"
|
||||
|
||||
using std::string;
|
||||
using std::hash;
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
Token::Token(const std::string &name) : Symbol(name) {};
|
||||
|
||||
bool Token::operator==(const Rule &rule) const {
|
||||
const Token *other = dynamic_cast<const Token *>(&rule);
|
||||
return other && (other->name == name);
|
||||
}
|
||||
|
||||
rule_ptr Token::copy() const {
|
||||
return std::make_shared<Token>(*this);
|
||||
}
|
||||
|
||||
string Token::to_string() const {
|
||||
return string("#<token '") + name + "'>";
|
||||
}
|
||||
|
||||
void Token::accept(Visitor &visitor) const {
|
||||
visitor.visit(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
#ifndef __tree_sitter__token__
|
||||
#define __tree_sitter__token__
|
||||
|
||||
#include "symbol.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
class Token : public Symbol {
|
||||
public:
|
||||
Token(const std::string &name);
|
||||
|
||||
bool operator==(const Rule& other) const;
|
||||
rule_ptr copy() const;
|
||||
std::string to_string() const;
|
||||
void accept(Visitor &visitor) const;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -11,6 +11,5 @@ namespace tree_sitter {
|
|||
void Visitor::visit(const Seq *rule) { default_visit(rule); }
|
||||
void Visitor::visit(const String *rule) { default_visit(rule); }
|
||||
void Visitor::visit(const Pattern *rule) { default_visit(rule); }
|
||||
void Visitor::visit(const Token *rule) { default_visit(rule); }
|
||||
}
|
||||
}
|
||||
|
|
@ -16,7 +16,6 @@ namespace tree_sitter {
|
|||
virtual void visit(const Seq *rule);
|
||||
virtual void visit(const String *rule);
|
||||
virtual void visit(const Pattern *rule);
|
||||
virtual void visit(const Token *rule);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue