Move public rule functions out of rule namespace
This way, there's only one public namespace: tree_sitter
This commit is contained in:
parent
e386c634aa
commit
bd77ab1ac9
66 changed files with 127 additions and 167 deletions
|
|
@ -1,5 +1,6 @@
|
|||
#include "compiler/rules/blank.h"
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include "compiler/rules/visitor.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
|
|
@ -7,6 +8,10 @@ namespace rules {
|
|||
|
||||
Blank::Blank() {}
|
||||
|
||||
rule_ptr Blank::build() {
|
||||
return std::make_shared<Blank>();
|
||||
}
|
||||
|
||||
bool Blank::operator==(const Rule &rule) const {
|
||||
return dynamic_cast<const Blank *>(&rule) != nullptr;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#define COMPILER_RULES_BLANK_H_
|
||||
|
||||
#include <string>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
@ -10,6 +10,7 @@ namespace rules {
|
|||
class Blank : public Rule {
|
||||
public:
|
||||
Blank();
|
||||
static rule_ptr build();
|
||||
|
||||
bool operator==(const Rule &other) const;
|
||||
size_t hash_code() const;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
#include "compiler/rules/character_range.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
#include <string>
|
||||
#include <map>
|
||||
#include "tree_sitter/compiler.h"
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
@ -16,8 +16,6 @@ enum MetadataKey {
|
|||
ASSOCIATIVITY,
|
||||
};
|
||||
|
||||
const Associativity AssociativityUnspecified = (Associativity)0;
|
||||
|
||||
class Metadata : public Rule {
|
||||
public:
|
||||
Metadata(rule_ptr rule, std::map<MetadataKey, int> value);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#define COMPILER_RULES_NAMED_SYMBOL_H_
|
||||
|
||||
#include <string>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ using std::hash;
|
|||
|
||||
Pattern::Pattern(const string &string) : value(string) {}
|
||||
|
||||
bool Pattern::operator==(tree_sitter::rules::Rule const &other) const {
|
||||
bool Pattern::operator==(tree_sitter::Rule const &other) const {
|
||||
auto pattern = dynamic_cast<const Pattern *>(&other);
|
||||
return pattern && (pattern->value == value);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#define COMPILER_RULES_PATTERN_H_
|
||||
|
||||
#include <string>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#define COMPILER_RULES_REPEAT_H_
|
||||
|
||||
#include <string>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
|
|||
|
|
@ -1,29 +0,0 @@
|
|||
#include "compiler/rules/rule.h"
|
||||
#include <set>
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
||||
using std::ostream;
|
||||
using std::string;
|
||||
|
||||
bool Rule::operator!=(const Rule &other) const {
|
||||
return !this->operator==(other);
|
||||
}
|
||||
|
||||
ostream &operator<<(ostream &stream, const Rule &rule) {
|
||||
return stream << rule.to_string();
|
||||
}
|
||||
|
||||
ostream &operator<<(ostream &stream, const rule_ptr &rule) {
|
||||
if (rule.get())
|
||||
stream << *rule;
|
||||
else
|
||||
stream << string("(null-rule)");
|
||||
return stream;
|
||||
}
|
||||
|
||||
Rule::~Rule() {}
|
||||
|
||||
} // namespace rules
|
||||
} // namespace tree_sitter
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
#ifndef COMPILER_RULES_RULE_H_
|
||||
#define COMPILER_RULES_RULE_H_
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
||||
class Visitor;
|
||||
class Rule;
|
||||
|
||||
typedef std::shared_ptr<Rule> rule_ptr;
|
||||
|
||||
class Rule {
|
||||
public:
|
||||
virtual bool operator==(const Rule &other) const = 0;
|
||||
bool operator!=(const Rule &other) const;
|
||||
virtual size_t hash_code() const = 0;
|
||||
virtual rule_ptr copy() const = 0;
|
||||
virtual std::string to_string() const = 0;
|
||||
virtual void accept(Visitor *visitor) const = 0;
|
||||
virtual ~Rule();
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &stream, const Rule &rule);
|
||||
std::ostream &operator<<(std::ostream &stream, const rule_ptr &rule);
|
||||
|
||||
} // namespace rules
|
||||
} // namespace tree_sitter
|
||||
|
||||
namespace std {
|
||||
|
||||
template <>
|
||||
struct hash<tree_sitter::rules::rule_ptr> {
|
||||
size_t operator()(const tree_sitter::rules::rule_ptr &rule) const {
|
||||
return typeid(*rule).hash_code() ^ rule->hash_code();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
#endif // COMPILER_RULES_RULE_H_
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
#include <set>
|
||||
#include <string>
|
||||
#include "tree_sitter/compiler.h"
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
#include "compiler/rules/blank.h"
|
||||
#include "compiler/rules/named_symbol.h"
|
||||
#include "compiler/rules/choice.h"
|
||||
|
|
@ -16,7 +16,6 @@
|
|||
#include "compiler/rules/built_in_symbols.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
||||
using std::make_shared;
|
||||
using std::string;
|
||||
|
|
@ -24,45 +23,45 @@ using std::set;
|
|||
using std::vector;
|
||||
using std::map;
|
||||
|
||||
static rule_ptr metadata(rule_ptr rule, map<MetadataKey, int> values) {
|
||||
return std::make_shared<Metadata>(rule, values);
|
||||
static rule_ptr metadata(rule_ptr rule, map<rules::MetadataKey, int> values) {
|
||||
return std::make_shared<rules::Metadata>(rule, values);
|
||||
}
|
||||
|
||||
rule_ptr blank() {
|
||||
return make_shared<Blank>();
|
||||
return rules::Blank::build();
|
||||
}
|
||||
|
||||
rule_ptr choice(const vector<rule_ptr> &rules) {
|
||||
return Choice::build(rules);
|
||||
return rules::Choice::build(rules);
|
||||
}
|
||||
|
||||
rule_ptr repeat(const rule_ptr &content) {
|
||||
return Repeat::build(content);
|
||||
return rules::Repeat::build(content);
|
||||
}
|
||||
|
||||
rule_ptr seq(const vector<rule_ptr> &rules) {
|
||||
return Seq::build(rules);
|
||||
return rules::Seq::build(rules);
|
||||
}
|
||||
|
||||
rule_ptr sym(const string &name) {
|
||||
return make_shared<NamedSymbol>(name);
|
||||
return make_shared<rules::NamedSymbol>(name);
|
||||
}
|
||||
|
||||
rule_ptr pattern(const string &value) {
|
||||
return make_shared<Pattern>(value);
|
||||
return make_shared<rules::Pattern>(value);
|
||||
}
|
||||
|
||||
rule_ptr str(const string &value) {
|
||||
return token(prec(1, make_shared<String>(value)));
|
||||
return token(prec(1, make_shared<rules::String>(value)));
|
||||
}
|
||||
|
||||
rule_ptr err(const rule_ptr &rule) {
|
||||
return choice({ rule, ERROR().copy() });
|
||||
return choice({ rule, rules::ERROR().copy() });
|
||||
}
|
||||
|
||||
rule_ptr prec(int precedence, const rule_ptr &rule, Associativity associativity) {
|
||||
return metadata(
|
||||
rule, { { PRECEDENCE, precedence }, { ASSOCIATIVITY, associativity } });
|
||||
return metadata(rule, { { rules::PRECEDENCE, precedence },
|
||||
{ rules::ASSOCIATIVITY, associativity } });
|
||||
}
|
||||
|
||||
rule_ptr prec(int precedence, const rule_ptr &rule) {
|
||||
|
|
@ -70,8 +69,7 @@ rule_ptr prec(int precedence, const rule_ptr &rule) {
|
|||
}
|
||||
|
||||
rule_ptr token(const rule_ptr &rule) {
|
||||
return metadata(rule, { { IS_TOKEN, 1 } });
|
||||
return metadata(rule, { { rules::IS_TOKEN, 1 } });
|
||||
}
|
||||
|
||||
} // namespace rules
|
||||
} // namespace tree_sitter
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#define COMPILER_RULES_STRING_H_
|
||||
|
||||
#include <string>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#define COMPILER_RULES_SYMBOL_H_
|
||||
|
||||
#include <string>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include "compiler/rules/visitor.h"
|
||||
#include <vector>
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
#include "compiler/rules/blank.h"
|
||||
#include "compiler/rules/character_set.h"
|
||||
#include "compiler/rules/choice.h"
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef COMPILER_RULES_VISITOR_H_
|
||||
#define COMPILER_RULES_VISITOR_H_
|
||||
|
||||
#include "compiler/rules/rule.h"
|
||||
#include "compiler/rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue