Put rule classes in their own files

This commit is contained in:
Max Brunsfeld 2013-11-07 13:24:01 -08:00
parent 849f2ee195
commit d830c7c255
17 changed files with 326 additions and 215 deletions

View file

@ -7,9 +7,13 @@
objects = {
/* Begin PBXBuildFile section */
12130605182C348F00FCF928 /* char.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12130603182C348F00FCF928 /* char.cpp */; };
1213060B182C389100FCF928 /* symbol.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12130609182C389100FCF928 /* symbol.cpp */; };
1213060E182C398300FCF928 /* choice.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1213060C182C398300FCF928 /* choice.cpp */; };
12130611182C3A1100FCF928 /* blank.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1213060F182C3A1100FCF928 /* blank.cpp */; };
12130614182C3A1700FCF928 /* seq.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12130612182C3A1700FCF928 /* seq.cpp */; };
1214930E181E200B008E9BDA /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 121492E9181E200B008E9BDA /* main.cpp */; };
1214930F181E200B008E9BDA /* rules_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 121492EA181E200B008E9BDA /* rules_spec.cpp */; };
12E71853181D081C0051A649 /* rules.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12E71851181D081C0051A649 /* rules.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@ -26,6 +30,17 @@
/* Begin PBXFileReference section */
121306011829FAED00FCF928 /* spec_helper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = spec_helper.h; sourceTree = SOURCE_ROOT; };
12130603182C348F00FCF928 /* char.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = char.cpp; sourceTree = "<group>"; };
12130604182C348F00FCF928 /* char.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = char.h; sourceTree = "<group>"; };
12130607182C374800FCF928 /* rule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rule.h; sourceTree = "<group>"; };
12130609182C389100FCF928 /* symbol.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = symbol.cpp; sourceTree = "<group>"; };
1213060A182C389100FCF928 /* symbol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = symbol.h; sourceTree = "<group>"; };
1213060C182C398300FCF928 /* choice.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = choice.cpp; sourceTree = "<group>"; };
1213060D182C398300FCF928 /* choice.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = choice.h; sourceTree = "<group>"; };
1213060F182C3A1100FCF928 /* blank.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = blank.cpp; sourceTree = "<group>"; };
12130610182C3A1100FCF928 /* blank.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = blank.h; sourceTree = "<group>"; };
12130612182C3A1700FCF928 /* seq.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = seq.cpp; sourceTree = "<group>"; };
12130613182C3A1700FCF928 /* seq.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = seq.h; sourceTree = "<group>"; };
12149265181E200B008E9BDA /* igloo-tests_CXX_prefix.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = "igloo-tests_CXX_prefix.hxx"; sourceTree = "<group>"; };
12149266181E200B008E9BDA /* igloo-tests_CXX_prefix.hxx.gch */ = {isa = PBXFileReference; lastKnownFileType = file; path = "igloo-tests_CXX_prefix.hxx.gch"; sourceTree = "<group>"; };
12149267181E200B008E9BDA /* snowhouse-tests_CXX_prefix.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = "snowhouse-tests_CXX_prefix.hxx"; sourceTree = "<group>"; };
@ -101,7 +116,6 @@
121492EA181E200B008E9BDA /* rules_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = rules_spec.cpp; path = spec/rules_spec.cpp; sourceTree = SOURCE_ROOT; };
12C344421822F27700B07BE3 /* transition_map.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = transition_map.h; sourceTree = "<group>"; };
12E71794181D02A80051A649 /* specs */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = specs; sourceTree = BUILT_PRODUCTS_DIR; };
12E71851181D081C0051A649 /* rules.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rules.cpp; sourceTree = "<group>"; };
12E71852181D081C0051A649 /* rules.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rules.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -116,6 +130,24 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
12130602182C344400FCF928 /* rules */ = {
isa = PBXGroup;
children = (
1213060F182C3A1100FCF928 /* blank.cpp */,
12130610182C3A1100FCF928 /* blank.h */,
12130603182C348F00FCF928 /* char.cpp */,
12130604182C348F00FCF928 /* char.h */,
1213060C182C398300FCF928 /* choice.cpp */,
1213060D182C398300FCF928 /* choice.h */,
12130607182C374800FCF928 /* rule.h */,
12130612182C3A1700FCF928 /* seq.cpp */,
12130613182C3A1700FCF928 /* seq.h */,
12130609182C389100FCF928 /* symbol.cpp */,
1213060A182C389100FCF928 /* symbol.h */,
);
path = rules;
sourceTree = "<group>";
};
1214925C181E200B008E9BDA /* externals */ = {
isa = PBXGroup;
children = (
@ -327,7 +359,7 @@
12E71701181D01890051A649 /* src */ = {
isa = PBXGroup;
children = (
12E71851181D081C0051A649 /* rules.cpp */,
12130602182C344400FCF928 /* rules */,
12E71852181D081C0051A649 /* rules.h */,
12C344421822F27700B07BE3 /* transition_map.h */,
);
@ -404,9 +436,13 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
12E71853181D081C0051A649 /* rules.cpp in Sources */,
12130614182C3A1700FCF928 /* seq.cpp in Sources */,
1214930F181E200B008E9BDA /* rules_spec.cpp in Sources */,
12130611182C3A1100FCF928 /* blank.cpp in Sources */,
1213060E182C398300FCF928 /* choice.cpp in Sources */,
1214930E181E200B008E9BDA /* main.cpp in Sources */,
12130605182C348F00FCF928 /* char.cpp in Sources */,
1213060B182C389100FCF928 /* symbol.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View file

@ -1,18 +1,6 @@
#include "spec_helper.h"
#include "rules.h"
#include "transition_map.h"
#include <igloo/igloo_alt.h>
using namespace std;
using namespace igloo;
using namespace tree_sitter;
EqualsContainerConstraint<
TransitionMap<rules::Rule>,
bool (*)(const TransitionMap<rules::Rule>::pair_type &, const TransitionMap<rules::Rule>::pair_type &)
>
EqualsTransitionMap(const TransitionMap<rules::Rule> &expected) {
return EqualsContainer(expected, TransitionMap<rules::Rule>::elements_equal);
}
Describe(Rules) {
Describe(transitions) {

View file

@ -5,6 +5,10 @@
#include "transition_map.h"
#include "rules.h"
using namespace igloo;
using namespace tree_sitter;
// Assertion helpers for transition maps
namespace snowhouse {
template<>
std::string Stringize(const tree_sitter::TransitionMap<tree_sitter::rules::Rule> &map) {
@ -19,4 +23,10 @@ namespace snowhouse {
}
}
typedef TransitionMap<rules::Rule> tmap;
typedef bool (* tmap_comparator)(const tmap::pair_type &, const tmap::pair_type &);
EqualsContainerConstraint<tmap, tmap_comparator> EqualsTransitionMap(const tmap &expected) {
return EqualsContainer(expected, tmap::elements_equal);
}
#endif

View file

@ -1,118 +0,0 @@
#include "rules.h"
#include "spec_helper.h"
#include "transition_map.h"
using namespace std;
namespace tree_sitter {
namespace rules {
// Constructors
Blank::Blank() {}
Symbol::Symbol(int id) : id(id) {};
Char::Char(char value) : value(value) {};
Seq::Seq(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {};
Seq::Seq(const Rule *left, const Rule *right) : left(left), right(right) {};
Seq::Seq(shared_ptr<const Rule> left, shared_ptr<const Rule> right) : left(left), right(right) {};
Choice::Choice(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {};
Choice::Choice(const Rule *left, const Rule *right) : left(left), right(right) {};
Choice::Choice(shared_ptr<const Rule> left, shared_ptr<const Rule> right) : left(left), right(right) {};
// Transitions
TransitionMap<Rule> Blank::transitions() const {
return TransitionMap<Rule>();
}
TransitionMap<Rule> Symbol::transitions() const {
return TransitionMap<Rule>({ copy() }, { new Blank() });
}
TransitionMap<Rule> Char::transitions() const {
return TransitionMap<Rule>({ copy() }, { new Blank() });
}
TransitionMap<Rule> Choice::transitions() const {
auto result = left->transitions();
result.merge(right->transitions(), [&](rule_ptr left, rule_ptr right) -> rule_ptr {
return rule_ptr(new Choice(left, right));
});
return result;
}
TransitionMap<Rule> Seq::transitions() const {
return left->transitions().map([&](rule_ptr left_rule) -> rule_ptr {
if (typeid(*left_rule) == typeid(Blank))
return right;
else
return rule_ptr(new Seq(left_rule, right));
});
}
// Equality
bool Blank::operator==(const Rule &rule) const {
return dynamic_cast<const Blank *>(&rule) != NULL;
}
bool Symbol::operator==(const Rule &rule) const {
const Symbol *other = dynamic_cast<const Symbol *>(&rule);
return (other != NULL) && (other->id == id);
}
bool Char::operator==(const Rule &rule) const {
const Char *other = dynamic_cast<const Char *>(&rule);
return (other != NULL) && (other->value == value);
}
bool Choice::operator==(const Rule &rule) const {
const Choice *other = dynamic_cast<const Choice *>(&rule);
return (other != NULL) && (*other->left == *left) && (*other->right == *right);
}
bool Seq::operator==(const Rule &rule) const {
const Seq *other = dynamic_cast<const Seq *>(&rule);
return (other != NULL) && (*other->left == *left) && (*other->right == *right);
}
// Copying
Blank * Blank::copy() const {
return new Blank();
}
Symbol * Symbol::copy() const {
return new Symbol(id);
}
Char * Char::copy() const {
return new Char(value);
}
Choice * Choice::copy() const {
return new Choice(left, right);
}
Seq * Seq::copy() const {
return new Seq(left, right);
}
// Description
string Blank::to_string() const {
return "blank";
}
string Symbol::to_string() const {
return std::to_string(id);
}
string Char::to_string() const {
return std::to_string(value);
}
string Choice::to_string() const {
return string("(choice ") + left->to_string() + " " + right->to_string() + ")";
}
string Seq::to_string() const {
return string("(seq ") + left->to_string() + " " + right->to_string() + ")";
}
}
}

View file

@ -1,81 +1,11 @@
#ifndef __TreeSitter__Rule__
#define __TreeSitter__Rule__
#ifndef __TreeSitter__rules__
#define __TreeSitter__rules__
#include <string>
namespace tree_sitter {
template<class value> class TransitionMap;
namespace rules {
class Rule {
public:
virtual TransitionMap<Rule> transitions() const = 0;
virtual Rule * copy() const = 0;
virtual bool operator==(const Rule& other) const = 0;
virtual std::string to_string() const = 0;
};
typedef std::shared_ptr<const Rule> rule_ptr;
class Blank : public Rule {
public:
Blank();
TransitionMap<Rule> transitions() const;
Blank * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
};
class Symbol : public Rule {
public:
Symbol(int id);
TransitionMap<Rule> transitions() const;
Symbol * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
int id;
};
class Char : public Rule {
public:
Char(char value);
TransitionMap<Rule> transitions() const;
Char * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
char value;
};
class Choice : public Rule {
public:
Choice(const Rule &left, const Rule &right);
Choice(const Rule *left, const Rule *right);
Choice(std::shared_ptr<const Rule> left, std::shared_ptr<const Rule> right);
TransitionMap<Rule> transitions() const;
Choice * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
rule_ptr left;
rule_ptr right;
};
class Seq : public Rule {
public:
Seq(const Rule &left, const Rule &right);
Seq(const Rule *left, const Rule *right);
Seq(std::shared_ptr<const Rule> left, std::shared_ptr<const Rule> right);
TransitionMap<Rule> transitions() const;
Seq * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
rule_ptr left;
rule_ptr right;
};
}
}
#include "rules/rule.h"
#include "rules/blank.h"
#include "rules/char.h"
#include "rules/symbol.h"
#include "rules/choice.h"
#include "rules/seq.h"
#endif

24
src/rules/blank.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "blank.h"
#include "transition_map.h"
namespace tree_sitter {
namespace rules {
Blank::Blank() {}
TransitionMap<Rule> Blank::transitions() const {
return TransitionMap<Rule>();
}
bool Blank::operator==(const Rule &rule) const {
return dynamic_cast<const Blank *>(&rule) != NULL;
}
Blank * Blank::copy() const {
return new Blank();
}
std::string Blank::to_string() const {
return "blank";
}
}
}

19
src/rules/blank.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef __tree_sitter__blank__
#define __tree_sitter__blank__
#include "rule.h"
namespace tree_sitter {
namespace rules {
class Blank : public Rule {
public:
Blank();
TransitionMap<Rule> transitions() const;
Blank * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
};
}
}
#endif

27
src/rules/char.cpp Normal file
View file

@ -0,0 +1,27 @@
#include "char.h"
#include "transition_map.h"
using namespace std;
namespace tree_sitter {
namespace rules {
Char::Char(char value) : value(value) {};
TransitionMap<Rule> Char::transitions() const {
return TransitionMap<Rule>({ copy() }, { new Blank() });
}
bool Char::operator==(const Rule &rule) const {
const Char *other = dynamic_cast<const Char *>(&rule);
return (other != NULL) && (other->value == value);
}
Char * Char::copy() const {
return new Char(value);
}
string Char::to_string() const {
return std::to_string(value);
}
}
}

21
src/rules/char.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef __tree_sitter__char__
#define __tree_sitter__char__
#include "rule.h"
namespace tree_sitter {
namespace rules {
class Char : public Rule {
public:
Char(char value);
TransitionMap<Rule> transitions() const;
Char * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
char value;
};
}
}
#endif

30
src/rules/choice.cpp Normal file
View file

@ -0,0 +1,30 @@
#include "choice.h"
#include "transition_map.h"
namespace tree_sitter {
namespace rules {
Choice::Choice(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {};
Choice::Choice(rule_ptr left, rule_ptr right) : left(left), right(right) {};
TransitionMap<Rule> Choice::transitions() const {
auto result = left->transitions();
result.merge(right->transitions(), [&](rule_ptr left, rule_ptr right) -> rule_ptr {
return rule_ptr(new Choice(left, right));
});
return result;
}
bool Choice::operator==(const Rule &rule) const {
const Choice *other = dynamic_cast<const Choice *>(&rule);
return (other != NULL) && (*other->left == *left) && (*other->right == *right);
}
Choice * Choice::copy() const {
return new Choice(left, right);
}
std::string Choice::to_string() const {
return std::string("(choice ") + left->to_string() + " " + right->to_string() + ")";
}
}
}

23
src/rules/choice.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef __tree_sitter__choice__
#define __tree_sitter__choice__
#include "rule.h"
namespace tree_sitter {
namespace rules {
class Choice : public Rule {
public:
Choice(const Rule &left, const Rule &right);
Choice(std::shared_ptr<const Rule> left, std::shared_ptr<const Rule> right);
TransitionMap<Rule> transitions() const;
Choice * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
rule_ptr left;
rule_ptr right;
};
}
}
#endif

22
src/rules/rule.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef __TreeSitter__rule__
#define __TreeSitter__rule__
#include <string>
namespace tree_sitter {
template<class value> class TransitionMap;
namespace rules {
class Rule {
public:
virtual TransitionMap<Rule> transitions() const = 0;
virtual Rule * copy() const = 0;
virtual bool operator==(const Rule& other) const = 0;
virtual std::string to_string() const = 0;
};
typedef std::shared_ptr<const Rule> rule_ptr;
}
}
#endif

32
src/rules/seq.cpp Normal file
View file

@ -0,0 +1,32 @@
#include "seq.h"
#include "blank.h"
#include "transition_map.h"
namespace tree_sitter {
namespace rules {
Seq::Seq(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {};
Seq::Seq(rule_ptr left, rule_ptr right) : left(left), right(right) {};
TransitionMap<Rule> Seq::transitions() const {
return left->transitions().map([&](rule_ptr left_rule) -> rule_ptr {
if (typeid(*left_rule) == typeid(Blank))
return right;
else
return rule_ptr(new Seq(left_rule, right));
});
}
bool Seq::operator==(const Rule &rule) const {
const Seq *other = dynamic_cast<const Seq *>(&rule);
return (other != NULL) && (*other->left == *left) && (*other->right == *right);
}
Seq * Seq::copy() const {
return new Seq(left, right);
}
std::string Seq::to_string() const {
return std::string("(seq ") + left->to_string() + " " + right->to_string() + ")";
}
}
}

23
src/rules/seq.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef __tree_sitter__seq__
#define __tree_sitter__seq__
#include "rule.h"
namespace tree_sitter {
namespace rules {
class Seq : public Rule {
public:
Seq(const Rule &left, const Rule &right);
Seq(std::shared_ptr<const Rule> left, std::shared_ptr<const Rule> right);
TransitionMap<Rule> transitions() const;
Seq * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
rule_ptr left;
rule_ptr right;
};
}
}
#endif

25
src/rules/symbol.cpp Normal file
View file

@ -0,0 +1,25 @@
#include "symbol.h"
#include "transition_map.h"
namespace tree_sitter {
namespace rules {
Symbol::Symbol(int id) : id(id) {};
TransitionMap<Rule> Symbol::transitions() const {
return TransitionMap<Rule>({ copy() }, { new Blank() });
}
bool Symbol::operator==(const Rule &rule) const {
const Symbol *other = dynamic_cast<const Symbol *>(&rule);
return (other != NULL) && (other->id == id);
}
Symbol * Symbol::copy() const {
return new Symbol(id);
}
std::string Symbol::to_string() const {
return std::to_string(id);
}
}
}

21
src/rules/symbol.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef __tree_sitter__sym__
#define __tree_sitter__sym__
#include "rule.h"
namespace tree_sitter {
namespace rules {
class Symbol : public Rule {
public:
Symbol(int id);
TransitionMap<Rule> transitions() const;
Symbol * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
int id;
};
}
}
#endif

View file

@ -6,8 +6,6 @@
#include "rules.h"
namespace tree_sitter {
class rules::Rule;
template<typename MappedType>
class TransitionMap {
public: