Add string rules
This commit is contained in:
parent
d830c7c255
commit
ec8b7ccf20
6 changed files with 70 additions and 2 deletions
|
|
@ -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 = "<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>"; };
|
||||
12130615182C3D2900FCF928 /* string.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = string.cpp; sourceTree = "<group>"; };
|
||||
12130616182C3D2900FCF928 /* string.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = string.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>"; };
|
||||
|
|
@ -144,6 +147,8 @@
|
|||
12130613182C3A1700FCF928 /* seq.h */,
|
||||
12130609182C389100FCF928 /* symbol.cpp */,
|
||||
1213060A182C389100FCF928 /* symbol.h */,
|
||||
12130615182C3D2900FCF928 /* string.cpp */,
|
||||
12130616182C3D2900FCF928 /* string.h */,
|
||||
);
|
||||
path = rules;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -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 */,
|
||||
|
|
|
|||
|
|
@ -67,5 +67,14 @@ Describe(Rules) {
|
|||
{ new rules::Choice(symbol2, symbol3) }
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
It(handles_strings) {
|
||||
AssertThat(
|
||||
rules::String("bad").transitions(),
|
||||
EqualsTransitionMap(TransitionMap<rules::Rule>(
|
||||
{ new rules::Char('b') },
|
||||
{ new rules::Seq(rules::Char('a'), rules::Char('d')) }
|
||||
)));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -7,5 +7,6 @@
|
|||
#include "rules/symbol.h"
|
||||
#include "rules/choice.h"
|
||||
#include "rules/seq.h"
|
||||
#include "rules/string.h"
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
string Char::to_string() const {
|
||||
return std::to_string(value);
|
||||
return std::string("'") + &value + "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
31
src/rules/string.cpp
Normal file
31
src/rules/string.cpp
Normal file
|
|
@ -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<Rule> 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<const String *>(&rule);
|
||||
return (other != NULL) && (other->value == value);
|
||||
}
|
||||
|
||||
String * String::copy() const {
|
||||
return new String(value);
|
||||
}
|
||||
|
||||
std::string String::to_string() const {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/rules/string.h
Normal file
21
src/rules/string.h
Normal file
|
|
@ -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<Rule> transitions() const;
|
||||
String * copy() const;
|
||||
bool operator==(const Rule& other) const;
|
||||
std::string to_string() const;
|
||||
private:
|
||||
std::string value;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue