ISymbol -> Symbol
Interned symbols are now the main type of symbol in use
This commit is contained in:
parent
faf80aadac
commit
25eda9d889
45 changed files with 248 additions and 250 deletions
|
|
@ -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); }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 <string>
|
||||
#include <utility>
|
||||
#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_
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#include "compiler/rules/interned_symbol.h"
|
||||
#include "compiler/rules/symbol.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
#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<const ISymbol *>(&rule);
|
||||
bool Symbol::operator==(const Rule &rule) const {
|
||||
const Symbol *other = dynamic_cast<const Symbol *>(&rule);
|
||||
return other && this->operator==(*other);
|
||||
}
|
||||
|
||||
size_t ISymbol::hash_code() const {
|
||||
size_t Symbol::hash_code() const {
|
||||
return hash<int>()(index) ^ hash<int16_t>()(options);
|
||||
}
|
||||
|
||||
rule_ptr ISymbol::copy() const {
|
||||
return std::make_shared<ISymbol>(*this);
|
||||
rule_ptr Symbol::copy() const {
|
||||
return std::make_shared<Symbol>(*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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<tree_sitter::rules::ISymbol> {
|
||||
size_t operator()(const tree_sitter::rules::ISymbol &rule) const {
|
||||
struct hash<tree_sitter::rules::Symbol> {
|
||||
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_
|
||||
|
|
@ -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_;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue