Consolidate logic for making shared pointers to rules

This commit is contained in:
Max Brunsfeld 2013-12-19 23:05:54 -08:00
parent 9f78d72a7c
commit 6f444fcc79
22 changed files with 113 additions and 94 deletions

View file

@ -55,6 +55,7 @@
12FD40DF1860064C0041A84E /* tree.c in Sources */ = {isa = PBXBuildFile; fileRef = 12FD40DE1860064C0041A84E /* tree.c */; };
12FD40E2186245FE0041A84E /* transitions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12FD40E0186245FE0041A84E /* transitions.cpp */; };
12FD40E718639B910041A84E /* rule_visitor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12FD40E618639B910041A84E /* rule_visitor.cpp */; };
12FD40E918641FB70041A84E /* rules.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12FD40E818641FB70041A84E /* rules.cpp */; };
27A343CA69E17E0F9EBEDF1C /* pattern.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27A340F3EEB184C040521323 /* pattern.cpp */; };
/* End PBXBuildFile section */
@ -135,6 +136,7 @@
12FD40E1186245FE0041A84E /* transitions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = transitions.h; sourceTree = "<group>"; };
12FD40E41862B3530041A84E /* rule_visitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rule_visitor.h; sourceTree = "<group>"; };
12FD40E618639B910041A84E /* rule_visitor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule_visitor.cpp; sourceTree = "<group>"; };
12FD40E818641FB70041A84E /* rules.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rules.cpp; sourceTree = "<group>"; };
27A340F3EEB184C040521323 /* pattern.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = pattern.cpp; sourceTree = "<group>"; };
27A3438C4FA59A3882E8493B /* pattern.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pattern.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -160,11 +162,12 @@
12130602182C344400FCF928 /* rules */ = {
isa = PBXGroup;
children = (
12E71852181D081C0051A649 /* rules.h */,
1213060F182C3A1100FCF928 /* blank.cpp */,
12130610182C3A1100FCF928 /* blank.h */,
12130603182C348F00FCF928 /* char.cpp */,
12130604182C348F00FCF928 /* char.h */,
12F8BE8C183C79B2006CCF99 /* char_class.cpp */,
12F8BE8D183C79B2006CCF99 /* char_class.h */,
1213060C182C398300FCF928 /* choice.cpp */,
1213060D182C398300FCF928 /* choice.h */,
27A340F3EEB184C040521323 /* pattern.cpp */,
@ -173,18 +176,18 @@
12D136A3183678A2005F3369 /* repeat.h */,
1251209A1830145300C9B56A /* rule.cpp */,
12130607182C374800FCF928 /* rule.h */,
12FD40E618639B910041A84E /* rule_visitor.cpp */,
12FD40E41862B3530041A84E /* rule_visitor.h */,
12FD40E818641FB70041A84E /* rules.cpp */,
12E71852181D081C0051A649 /* rules.h */,
12130612182C3A1700FCF928 /* seq.cpp */,
12130613182C3A1700FCF928 /* seq.h */,
12130615182C3D2900FCF928 /* string.cpp */,
12130616182C3D2900FCF928 /* string.h */,
12130609182C389100FCF928 /* symbol.cpp */,
1213060A182C389100FCF928 /* symbol.h */,
12F8BE8C183C79B2006CCF99 /* char_class.cpp */,
12F8BE8D183C79B2006CCF99 /* char_class.h */,
12FD40E0186245FE0041A84E /* transitions.cpp */,
12FD40E1186245FE0041A84E /* transitions.h */,
12FD40E41862B3530041A84E /* rule_visitor.h */,
12FD40E618639B910041A84E /* rule_visitor.cpp */,
);
path = rules;
sourceTree = "<group>";
@ -431,6 +434,7 @@
12130611182C3A1100FCF928 /* blank.cpp in Sources */,
1213060E182C398300FCF928 /* choice.cpp in Sources */,
12F9A64E182DD5FD00FAF50C /* spec_helper.cpp in Sources */,
12FD40E918641FB70041A84E /* rules.cpp in Sources */,
12FD4061185E68470041A84E /* c_code.cpp in Sources */,
12FD40D8185FEEDF0041A84E /* rules_spec.cpp in Sources */,
125120A018307DEC00C9B56A /* parse_table.cpp in Sources */,

View file

@ -5,10 +5,6 @@ namespace tree_sitter {
namespace rules {
Blank::Blank() {}
rule_ptr blank() {
return std::make_shared<Blank>();
}
bool Blank::operator==(const Rule &rule) const {
return dynamic_cast<const Blank *>(&rule) != nullptr;
}

View file

@ -12,8 +12,6 @@ namespace tree_sitter {
std::string to_string() const;
void accept(RuleVisitor &visitor) const;
};
rule_ptr blank();
}
}

View file

@ -1,23 +1,19 @@
#include "rules.h"
#include "transition_map.h"
using namespace std;
using std::string;
namespace tree_sitter {
namespace rules {
Char::Char(char value) : value(value) {};
rule_ptr character(char value) {
return std::make_shared<Char>(value);
}
bool Char::operator==(const Rule &rule) const {
const Char *other = dynamic_cast<const Char *>(&rule);
return other && (other->value == value);
}
string Char::to_string() const {
return std::string("'") + value + "'";
return string("'") + value + "'";
}
void Char::accept(RuleVisitor &visitor) const {

View file

@ -14,8 +14,6 @@ namespace tree_sitter {
const char value;
};
rule_ptr character(char value);
}
}

View file

@ -1,16 +1,12 @@
#include "rules.h"
#include "transition_map.h"
using namespace std;
using std::string;
namespace tree_sitter {
namespace rules {
CharClass::CharClass(CharClassType value) : value(value) {};
rule_ptr char_class(CharClassType type) {
return std::make_shared<CharClass>(type);
}
bool CharClass::operator==(const Rule &rule) const {
const CharClass *other = dynamic_cast<const CharClass *>(&rule);
return other && (other->value == value);

View file

@ -19,8 +19,6 @@ namespace tree_sitter {
const CharClassType value;
};
rule_ptr char_class(CharClassType value);
}
}

View file

@ -1,24 +1,19 @@
#include "rules.h"
#include "transition_map.h"
using std::string;
namespace tree_sitter {
namespace rules {
Choice::Choice(rule_ptr left, rule_ptr right) : left(left), right(right) {};
rule_ptr choice(const std::initializer_list<rule_ptr> &rules) {
rule_ptr result;
for (auto rule : rules)
result = result.get() ? std::make_shared<Choice>(result, rule) : rule;
return result;
}
bool Choice::operator==(const Rule &rule) const {
const Choice *other = dynamic_cast<const Choice *>(&rule);
return other && (*other->left == *left) && (*other->right == *right);
}
std::string Choice::to_string() const {
return std::string("(choice ") + left->to_string() + " " + right->to_string() + ")";
string Choice::to_string() const {
return string("(choice ") + left->to_string() + " " + right->to_string() + ")";
}
void Choice::accept(RuleVisitor &visitor) const {

View file

@ -15,8 +15,6 @@ namespace tree_sitter {
const rule_ptr left;
const rule_ptr right;
};
rule_ptr choice(const std::initializer_list<rule_ptr> &rules);
}
}

View file

@ -1,7 +1,7 @@
#include "rules.h"
#include "transition_map.h"
using namespace std;
using std::string;
namespace tree_sitter {
namespace rules {
@ -93,26 +93,22 @@ namespace tree_sitter {
}
void error(const char *message) {
throw std::string("Invalid regex pattern '") + input + "': " + message;
throw string("Invalid regex pattern '") + input + "': " + message;
}
const std::string input;
const string input;
const size_t length;
int position;
};
Pattern::Pattern(const std::string &string) : value(string) {};
rule_ptr pattern(const std::string &value) {
return std::make_shared<Pattern>(value);
}
Pattern::Pattern(const string &string) : value(string) {};
bool Pattern::operator==(tree_sitter::rules::Rule const &other) const {
auto pattern = dynamic_cast<const Pattern *>(&other);
return pattern && (pattern->value == value);
}
std::string Pattern::to_string() const {
string Pattern::to_string() const {
return value;
}

View file

@ -14,8 +14,6 @@ namespace tree_sitter {
void accept(RuleVisitor &visitor) const;
rule_ptr to_rule_tree() const;
};
rule_ptr pattern(const std::string &value);
}
}

View file

@ -1,21 +1,19 @@
#include "rules.h"
#include "transition_map.h"
using std::string;
namespace tree_sitter {
namespace rules {
Repeat::Repeat(const rule_ptr content) : content(content) {}
rule_ptr repeat(const rule_ptr content) {
return std::make_shared<Repeat>(content);
}
bool Repeat::operator==(const Rule &rule) const {
const Repeat *other = dynamic_cast<const Repeat *>(&rule);
return other && (*other->content == *content);
}
std::string Repeat::to_string() const {
return std::string("(repeat ") + content->to_string() + ")";
string Repeat::to_string() const {
return string("(repeat ") + content->to_string() + ")";
}
void Repeat::accept(RuleVisitor &visitor) const {

View file

@ -14,8 +14,6 @@ namespace tree_sitter {
const rule_ptr content;
};
rule_ptr repeat(const rule_ptr content);
}
}

View file

@ -1,18 +1,18 @@
#include "rule.h"
#include <set>
using std::ostream;
using std::string;
namespace tree_sitter {
namespace rules {
std::ostream& operator<<(std::ostream& stream, const Rule &rule)
{
stream << rule.to_string();
return stream;
ostream& operator<<(ostream& stream, const Rule &rule) {
return stream << rule.to_string();
}
std::ostream& operator<<(std::ostream& stream, const rule_ptr &rule)
{
ostream& operator<<(ostream& stream, const rule_ptr &rule) {
if (rule.get() == nullptr)
stream << std::string("<NULL rule>");
stream << string("<NULL rule>");
else
stream << rule->to_string();
return stream;

View file

@ -0,0 +1,53 @@
#include "rules.h"
using std::make_shared;
using std::string;
using std::initializer_list;
namespace tree_sitter {
namespace rules {
rule_ptr blank() {
return make_shared<Blank>();
}
rule_ptr character(char value) {
return make_shared<Char>(value);
}
rule_ptr char_class(CharClassType type) {
return make_shared<CharClass>(type);
}
rule_ptr choice(const initializer_list<rule_ptr> &rules) {
rule_ptr result;
for (auto rule : rules)
result = result.get() ? make_shared<Choice>(result, rule) : rule;
return result;
}
rule_ptr pattern(const string &value) {
return make_shared<Pattern>(value);
}
rule_ptr repeat(const rule_ptr content) {
return std::make_shared<Repeat>(content);
}
rule_ptr seq(const initializer_list<rule_ptr> &rules) {
rule_ptr result = blank();
for (auto rule : rules)
result = (typeid(*result) != typeid(Blank)) ?
make_shared<Seq>(result, rule) :
rule;
return result;
}
rule_ptr str(const string &value) {
return make_shared<String>(value);
}
rule_ptr sym(const string &name) {
return make_shared<Symbol>(name);
}
}
}

View file

@ -13,4 +13,18 @@
#include "repeat.h"
#include "rule_visitor.h"
namespace tree_sitter {
namespace rules {
rule_ptr blank();
rule_ptr character(char value);
rule_ptr char_class(CharClassType value);
rule_ptr choice(const std::initializer_list<rule_ptr> &rules);
rule_ptr pattern(const std::string &value);
rule_ptr repeat(const rule_ptr content);
rule_ptr seq(const std::initializer_list<rule_ptr> &rules);
rule_ptr str(const std::string &value);
rule_ptr sym(const std::string &name);
}
}
#endif

View file

@ -1,26 +1,19 @@
#include "rules.h"
#include "transition_map.h"
using std::string;
namespace tree_sitter {
namespace rules {
Seq::Seq(rule_ptr left, rule_ptr right) : left(left), right(right) {};
rule_ptr seq(const std::initializer_list<rule_ptr> &rules) {
rule_ptr result;
for (auto rule : rules)
result = (result.get() && typeid(*result) != typeid(Blank)) ?
std::make_shared<Seq>(result, rule) :
rule;
return result;
}
bool Seq::operator==(const Rule &rule) const {
const Seq *other = dynamic_cast<const Seq *>(&rule);
return other && (*other->left == *left) && (*other->right == *right);
}
std::string Seq::to_string() const {
return std::string("(seq ") + left->to_string() + " " + right->to_string() + ")";
string Seq::to_string() const {
return string("(seq ") + left->to_string() + " " + right->to_string() + ")";
}
void Seq::accept(RuleVisitor &visitor) const {

View file

@ -15,8 +15,6 @@ namespace tree_sitter {
const rule_ptr left;
const rule_ptr right;
};
rule_ptr seq(const std::initializer_list<rule_ptr> &rules);
}
}

View file

@ -1,21 +1,19 @@
#include "rules.h"
#include "transition_map.h"
using std::string;
namespace tree_sitter {
namespace rules {
String::String(std::string value) : value(value) {};
String::String(string value) : value(value) {};
rule_ptr str(const std::string &value) {
return std::make_shared<String>(value);
}
bool String::operator==(const Rule &rule) const {
const String *other = dynamic_cast<const String *>(&rule);
return (other != NULL) && (other->value == value);
}
std::string String::to_string() const {
return std::string("(string '") + value + "')";
string String::to_string() const {
return string("(string '") + value + "')";
}
void String::accept(RuleVisitor &visitor) const {

View file

@ -14,8 +14,6 @@ namespace tree_sitter {
const std::string value;
};
rule_ptr str(const std::string &value);
}
}

View file

@ -1,21 +1,19 @@
#include "rules.h"
#include "transition_map.h"
using std::string;
namespace tree_sitter {
namespace rules {
Symbol::Symbol(const std::string &name) : name(name) {};
rule_ptr sym(const std::string &name) {
return std::make_shared<Symbol>(name);
}
bool Symbol::operator==(const Rule &rule) const {
const Symbol *other = dynamic_cast<const Symbol *>(&rule);
return other && (other->name == name);
}
std::string Symbol::to_string() const {
return std::string("(sym '") + name + "')";
string Symbol::to_string() const {
return string("(sym '") + name + "')";
}
void Symbol::accept(RuleVisitor &visitor) const {

View file

@ -13,9 +13,7 @@ namespace tree_sitter {
void accept(RuleVisitor &visitor) const;
const std::string name;
};
rule_ptr sym(const std::string &name);
};
}
}