diff --git a/TreeSitter.xcodeproj/project.pbxproj b/TreeSitter.xcodeproj/project.pbxproj index 1cb42b25..dd9052f8 100644 --- a/TreeSitter.xcodeproj/project.pbxproj +++ b/TreeSitter.xcodeproj/project.pbxproj @@ -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 = ""; }; 12FD40E41862B3530041A84E /* rule_visitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rule_visitor.h; sourceTree = ""; }; 12FD40E618639B910041A84E /* rule_visitor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule_visitor.cpp; sourceTree = ""; }; + 12FD40E818641FB70041A84E /* rules.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rules.cpp; sourceTree = ""; }; 27A340F3EEB184C040521323 /* pattern.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = pattern.cpp; sourceTree = ""; }; 27A3438C4FA59A3882E8493B /* pattern.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pattern.h; sourceTree = ""; }; /* 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 = ""; @@ -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 */, diff --git a/src/compiler/rules/blank.cpp b/src/compiler/rules/blank.cpp index acf89c58..5d5e1583 100644 --- a/src/compiler/rules/blank.cpp +++ b/src/compiler/rules/blank.cpp @@ -5,10 +5,6 @@ namespace tree_sitter { namespace rules { Blank::Blank() {} - rule_ptr blank() { - return std::make_shared(); - } - bool Blank::operator==(const Rule &rule) const { return dynamic_cast(&rule) != nullptr; } diff --git a/src/compiler/rules/blank.h b/src/compiler/rules/blank.h index 2c313674..abcee2ac 100644 --- a/src/compiler/rules/blank.h +++ b/src/compiler/rules/blank.h @@ -12,8 +12,6 @@ namespace tree_sitter { std::string to_string() const; void accept(RuleVisitor &visitor) const; }; - - rule_ptr blank(); } } diff --git a/src/compiler/rules/char.cpp b/src/compiler/rules/char.cpp index 73776d95..a8e40933 100644 --- a/src/compiler/rules/char.cpp +++ b/src/compiler/rules/char.cpp @@ -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(value); - } - bool Char::operator==(const Rule &rule) const { const Char *other = dynamic_cast(&rule); return other && (other->value == value); } string Char::to_string() const { - return std::string("'") + value + "'"; + return string("'") + value + "'"; } void Char::accept(RuleVisitor &visitor) const { diff --git a/src/compiler/rules/char.h b/src/compiler/rules/char.h index 2d360745..702f03ac 100644 --- a/src/compiler/rules/char.h +++ b/src/compiler/rules/char.h @@ -14,8 +14,6 @@ namespace tree_sitter { const char value; }; - - rule_ptr character(char value); } } diff --git a/src/compiler/rules/char_class.cpp b/src/compiler/rules/char_class.cpp index 2bde4ac8..eb7614e2 100644 --- a/src/compiler/rules/char_class.cpp +++ b/src/compiler/rules/char_class.cpp @@ -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(type); - } - bool CharClass::operator==(const Rule &rule) const { const CharClass *other = dynamic_cast(&rule); return other && (other->value == value); diff --git a/src/compiler/rules/char_class.h b/src/compiler/rules/char_class.h index f2a30347..033652e9 100644 --- a/src/compiler/rules/char_class.h +++ b/src/compiler/rules/char_class.h @@ -19,8 +19,6 @@ namespace tree_sitter { const CharClassType value; }; - - rule_ptr char_class(CharClassType value); } } diff --git a/src/compiler/rules/choice.cpp b/src/compiler/rules/choice.cpp index 5dfc6f66..d2157d9f 100644 --- a/src/compiler/rules/choice.cpp +++ b/src/compiler/rules/choice.cpp @@ -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 &rules) { - rule_ptr result; - for (auto rule : rules) - result = result.get() ? std::make_shared(result, rule) : rule; - return result; - } - bool Choice::operator==(const Rule &rule) const { const Choice *other = dynamic_cast(&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 { diff --git a/src/compiler/rules/choice.h b/src/compiler/rules/choice.h index 48b7714f..405bd9b0 100644 --- a/src/compiler/rules/choice.h +++ b/src/compiler/rules/choice.h @@ -15,8 +15,6 @@ namespace tree_sitter { const rule_ptr left; const rule_ptr right; }; - - rule_ptr choice(const std::initializer_list &rules); } } diff --git a/src/compiler/rules/pattern.cpp b/src/compiler/rules/pattern.cpp index ece4890b..0d2b7e5f 100644 --- a/src/compiler/rules/pattern.cpp +++ b/src/compiler/rules/pattern.cpp @@ -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(value); - } + Pattern::Pattern(const string &string) : value(string) {}; bool Pattern::operator==(tree_sitter::rules::Rule const &other) const { auto pattern = dynamic_cast(&other); return pattern && (pattern->value == value); } - std::string Pattern::to_string() const { + string Pattern::to_string() const { return value; } diff --git a/src/compiler/rules/pattern.h b/src/compiler/rules/pattern.h index d0d30834..eb7680a8 100644 --- a/src/compiler/rules/pattern.h +++ b/src/compiler/rules/pattern.h @@ -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); } } diff --git a/src/compiler/rules/repeat.cpp b/src/compiler/rules/repeat.cpp index eaae7361..699c066a 100644 --- a/src/compiler/rules/repeat.cpp +++ b/src/compiler/rules/repeat.cpp @@ -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(content); - } - bool Repeat::operator==(const Rule &rule) const { const Repeat *other = dynamic_cast(&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 { diff --git a/src/compiler/rules/repeat.h b/src/compiler/rules/repeat.h index 53aacf48..dc7608dc 100644 --- a/src/compiler/rules/repeat.h +++ b/src/compiler/rules/repeat.h @@ -14,8 +14,6 @@ namespace tree_sitter { const rule_ptr content; }; - - rule_ptr repeat(const rule_ptr content); } } diff --git a/src/compiler/rules/rule.cpp b/src/compiler/rules/rule.cpp index eebdd36a..cb12a64c 100644 --- a/src/compiler/rules/rule.cpp +++ b/src/compiler/rules/rule.cpp @@ -1,18 +1,18 @@ #include "rule.h" #include +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(""); + stream << string(""); else stream << rule->to_string(); return stream; diff --git a/src/compiler/rules/rules.cpp b/src/compiler/rules/rules.cpp new file mode 100644 index 00000000..94c827c5 --- /dev/null +++ b/src/compiler/rules/rules.cpp @@ -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(); + } + + rule_ptr character(char value) { + return make_shared(value); + } + + rule_ptr char_class(CharClassType type) { + return make_shared(type); + } + + rule_ptr choice(const initializer_list &rules) { + rule_ptr result; + for (auto rule : rules) + result = result.get() ? make_shared(result, rule) : rule; + return result; + } + + rule_ptr pattern(const string &value) { + return make_shared(value); + } + + rule_ptr repeat(const rule_ptr content) { + return std::make_shared(content); + } + + rule_ptr seq(const initializer_list &rules) { + rule_ptr result = blank(); + for (auto rule : rules) + result = (typeid(*result) != typeid(Blank)) ? + make_shared(result, rule) : + rule; + return result; + } + + rule_ptr str(const string &value) { + return make_shared(value); + } + + rule_ptr sym(const string &name) { + return make_shared(name); + } + } +} diff --git a/src/compiler/rules/rules.h b/src/compiler/rules/rules.h index 148f8dee..e18bf4f0 100644 --- a/src/compiler/rules/rules.h +++ b/src/compiler/rules/rules.h @@ -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 &rules); + rule_ptr pattern(const std::string &value); + rule_ptr repeat(const rule_ptr content); + rule_ptr seq(const std::initializer_list &rules); + rule_ptr str(const std::string &value); + rule_ptr sym(const std::string &name); + } +} + #endif diff --git a/src/compiler/rules/seq.cpp b/src/compiler/rules/seq.cpp index 23fede7a..8ef9a67a 100644 --- a/src/compiler/rules/seq.cpp +++ b/src/compiler/rules/seq.cpp @@ -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 &rules) { - rule_ptr result; - for (auto rule : rules) - result = (result.get() && typeid(*result) != typeid(Blank)) ? - std::make_shared(result, rule) : - rule; - return result; - } - bool Seq::operator==(const Rule &rule) const { const Seq *other = dynamic_cast(&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 { diff --git a/src/compiler/rules/seq.h b/src/compiler/rules/seq.h index 3babe2fb..79b66682 100644 --- a/src/compiler/rules/seq.h +++ b/src/compiler/rules/seq.h @@ -15,8 +15,6 @@ namespace tree_sitter { const rule_ptr left; const rule_ptr right; }; - - rule_ptr seq(const std::initializer_list &rules); } } diff --git a/src/compiler/rules/string.cpp b/src/compiler/rules/string.cpp index e57a998a..ef5864d5 100644 --- a/src/compiler/rules/string.cpp +++ b/src/compiler/rules/string.cpp @@ -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(value); - } - bool String::operator==(const Rule &rule) const { const String *other = dynamic_cast(&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 { diff --git a/src/compiler/rules/string.h b/src/compiler/rules/string.h index 53e99ecb..ace109e1 100644 --- a/src/compiler/rules/string.h +++ b/src/compiler/rules/string.h @@ -14,8 +14,6 @@ namespace tree_sitter { const std::string value; }; - - rule_ptr str(const std::string &value); } } diff --git a/src/compiler/rules/symbol.cpp b/src/compiler/rules/symbol.cpp index f9e51f46..33f04dc6 100644 --- a/src/compiler/rules/symbol.cpp +++ b/src/compiler/rules/symbol.cpp @@ -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(name); - } - bool Symbol::operator==(const Rule &rule) const { const Symbol *other = dynamic_cast(&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 { diff --git a/src/compiler/rules/symbol.h b/src/compiler/rules/symbol.h index bda020a0..fe54d577 100644 --- a/src/compiler/rules/symbol.h +++ b/src/compiler/rules/symbol.h @@ -13,9 +13,7 @@ namespace tree_sitter { void accept(RuleVisitor &visitor) const; const std::string name; - }; - - rule_ptr sym(const std::string &name); + }; } }