From ec8b7ccf209db943b5835eb3c1281c235dd79eb6 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 7 Nov 2013 18:30:00 -0800 Subject: [PATCH] Add string rules --- TreeSitter.xcodeproj/project.pbxproj | 6 ++++++ spec/rules_spec.cpp | 11 +++++++++- src/rules.h | 1 + src/rules/char.cpp | 2 +- src/rules/string.cpp | 31 ++++++++++++++++++++++++++++ src/rules/string.h | 21 +++++++++++++++++++ 6 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/rules/string.cpp create mode 100644 src/rules/string.h diff --git a/TreeSitter.xcodeproj/project.pbxproj b/TreeSitter.xcodeproj/project.pbxproj index 77785620..08c57912 100644 --- a/TreeSitter.xcodeproj/project.pbxproj +++ b/TreeSitter.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 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 */; }; + 12130617182C3D2900FCF928 /* string.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12130615182C3D2900FCF928 /* string.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 */; }; /* End PBXBuildFile section */ @@ -41,6 +42,8 @@ 12130610182C3A1100FCF928 /* blank.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = blank.h; sourceTree = ""; }; 12130612182C3A1700FCF928 /* seq.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = seq.cpp; sourceTree = ""; }; 12130613182C3A1700FCF928 /* seq.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = seq.h; sourceTree = ""; }; + 12130615182C3D2900FCF928 /* string.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = string.cpp; sourceTree = ""; }; + 12130616182C3D2900FCF928 /* string.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = string.h; sourceTree = ""; }; 12149265181E200B008E9BDA /* igloo-tests_CXX_prefix.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = "igloo-tests_CXX_prefix.hxx"; sourceTree = ""; }; 12149266181E200B008E9BDA /* igloo-tests_CXX_prefix.hxx.gch */ = {isa = PBXFileReference; lastKnownFileType = file; path = "igloo-tests_CXX_prefix.hxx.gch"; sourceTree = ""; }; 12149267181E200B008E9BDA /* snowhouse-tests_CXX_prefix.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = "snowhouse-tests_CXX_prefix.hxx"; sourceTree = ""; }; @@ -144,6 +147,8 @@ 12130613182C3A1700FCF928 /* seq.h */, 12130609182C389100FCF928 /* symbol.cpp */, 1213060A182C389100FCF928 /* symbol.h */, + 12130615182C3D2900FCF928 /* string.cpp */, + 12130616182C3D2900FCF928 /* string.h */, ); path = rules; sourceTree = ""; @@ -438,6 +443,7 @@ files = ( 12130614182C3A1700FCF928 /* seq.cpp in Sources */, 1214930F181E200B008E9BDA /* rules_spec.cpp in Sources */, + 12130617182C3D2900FCF928 /* string.cpp in Sources */, 12130611182C3A1100FCF928 /* blank.cpp in Sources */, 1213060E182C398300FCF928 /* choice.cpp in Sources */, 1214930E181E200B008E9BDA /* main.cpp in Sources */, diff --git a/spec/rules_spec.cpp b/spec/rules_spec.cpp index 1ac05e7d..6e90b056 100644 --- a/spec/rules_spec.cpp +++ b/spec/rules_spec.cpp @@ -67,5 +67,14 @@ Describe(Rules) { { new rules::Choice(symbol2, symbol3) } ))); } - }; + + It(handles_strings) { + AssertThat( + rules::String("bad").transitions(), + EqualsTransitionMap(TransitionMap( + { new rules::Char('b') }, + { new rules::Seq(rules::Char('a'), rules::Char('d')) } + ))); + } +}; }; diff --git a/src/rules.h b/src/rules.h index d4e676d0..1651eebd 100644 --- a/src/rules.h +++ b/src/rules.h @@ -7,5 +7,6 @@ #include "rules/symbol.h" #include "rules/choice.h" #include "rules/seq.h" +#include "rules/string.h" #endif diff --git a/src/rules/char.cpp b/src/rules/char.cpp index 03f91946..b8fc87b5 100644 --- a/src/rules/char.cpp +++ b/src/rules/char.cpp @@ -21,7 +21,7 @@ namespace tree_sitter { } string Char::to_string() const { - return std::to_string(value); + return std::string("'") + &value + "'"; } } } \ No newline at end of file diff --git a/src/rules/string.cpp b/src/rules/string.cpp new file mode 100644 index 00000000..a24d80c3 --- /dev/null +++ b/src/rules/string.cpp @@ -0,0 +1,31 @@ +#include "string.h" +#include "transition_map.h" +#include "char.h" +#include "seq.h" + +namespace tree_sitter { + namespace rules { + String::String(std::string value) : value(value) {}; + + TransitionMap String::transitions() const { + rule_ptr result = rule_ptr(new Char(value[0])); + for (int i = 1; i < value.length(); i++) { + result = rule_ptr(new Seq(result, rule_ptr(new Char(value[i])))); + } + return result->transitions(); + } + + bool String::operator==(const Rule &rule) const { + const String *other = dynamic_cast(&rule); + return (other != NULL) && (other->value == value); + } + + String * String::copy() const { + return new String(value); + } + + std::string String::to_string() const { + return value; + } + } +} \ No newline at end of file diff --git a/src/rules/string.h b/src/rules/string.h new file mode 100644 index 00000000..585f24f1 --- /dev/null +++ b/src/rules/string.h @@ -0,0 +1,21 @@ +#ifndef __tree_sitter__string__ +#define __tree_sitter__string__ + +#include "rule.h" + +namespace tree_sitter { + namespace rules { + class String : public Rule { + public: + String(std::string value); + TransitionMap transitions() const; + String * copy() const; + bool operator==(const Rule& other) const; + std::string to_string() const; + private: + std::string value; + }; + } +} + +#endif \ No newline at end of file