diff --git a/examples/grammars/json.hpp b/examples/grammars/json.hpp index 9a5e6cf5..9bd1364d 100644 --- a/examples/grammars/json.hpp +++ b/examples/grammars/json.hpp @@ -36,12 +36,12 @@ namespace test_grammars { comma_sep(sym("value")), _sym("right_bracket"), }) }, { "string", seq({ - character({ '"' }), + str("\""), repeat(choice({ pattern("[^\"]"), str("\\\""), })), - character({ '"' }) }) }, + str("\"") }) }, { "number", pattern("\\d+") }, { "comma", str(",") }, { "colon", str(":") }, diff --git a/include/tree_sitter/compiler.h b/include/tree_sitter/compiler.h index dbd72e4e..8e1c5767 100644 --- a/include/tree_sitter/compiler.h +++ b/include/tree_sitter/compiler.h @@ -3,7 +3,6 @@ #include #include -#include #include #include @@ -11,47 +10,20 @@ namespace tree_sitter { namespace rules { class Rule; class Symbol; - - struct CharacterRange { - char min; - char max; - CharacterRange(char); - CharacterRange(char, char); - bool operator==(const CharacterRange &) const; - bool operator<(const CharacterRange &) const; - std::string to_string() const; - }; - } -} - -namespace std { - template<> - struct hash { - size_t operator()(const tree_sitter::rules::CharacterRange &range) const { - return (hash()(range.min) ^ hash()(range.max)); - } - }; -} - -namespace tree_sitter { - namespace rules { typedef std::shared_ptr rule_ptr; + std::ostream& operator<<(std::ostream& stream, const rule_ptr &rule); rule_ptr blank(); - rule_ptr character(const std::set &matches); - rule_ptr character(const std::set &matches, bool); rule_ptr choice(const std::vector &rules); - rule_ptr pattern(const std::string &value); rule_ptr repeat(const rule_ptr content); rule_ptr seq(const std::vector &rules); - rule_ptr str(const std::string &value); rule_ptr sym(const std::string &name); rule_ptr _sym(const std::string &name); + rule_ptr pattern(const std::string &value); + rule_ptr str(const std::string &value); } -} -namespace tree_sitter { class Grammar { public: Grammar(std::string start_rule_name, const std::map &rules); @@ -61,7 +33,7 @@ namespace tree_sitter { }; std::ostream& operator<<(std::ostream &stream, const Grammar &grammar); - + std::string compile(const Grammar &grammar, std::string name); } diff --git a/spec/spec_helper.cpp b/spec/spec_helper.cpp index dae44a92..0266266c 100644 --- a/spec/spec_helper.cpp +++ b/spec/spec_helper.cpp @@ -1,7 +1,23 @@ #include "spec_helper.h" +#include "rules/character_set.h" string src_dir() { const char * dir = getenv("TREESITTER_DIR"); if (!dir) dir = getenv("PWD"); return dir; +} + +namespace tree_sitter { + namespace rules { + rule_ptr character(const set &ranges) { + return make_shared(ranges); + } + + rule_ptr character(const set &ranges, bool sign) { + if (sign) + return character(ranges); + else + return CharacterSet(ranges).complement().copy(); + } + } } \ No newline at end of file diff --git a/spec/spec_helper.h b/spec/spec_helper.h index 8deb0e52..954be878 100644 --- a/spec/spec_helper.h +++ b/spec/spec_helper.h @@ -6,6 +6,7 @@ #include "helpers/equals_pointer.h" #include "tree_sitter/compiler.h" #include "tree_sitter/runtime.h" +#include "rules/character_range.h" using namespace tree_sitter; using namespace std; @@ -16,4 +17,11 @@ using namespace bandit; string src_dir(); +namespace tree_sitter { + namespace rules { + rule_ptr character(const std::set &matches); + rule_ptr character(const std::set &matches, bool); + } +} + #endif diff --git a/src/compiler/rules/character_range.cpp b/src/compiler/rules/character_range.cpp index 635583d6..b654ad90 100644 --- a/src/compiler/rules/character_range.cpp +++ b/src/compiler/rules/character_range.cpp @@ -1,4 +1,5 @@ -#include "tree_sitter/compiler.h" +#include "character_range.h" +#include namespace tree_sitter { using std::string; diff --git a/src/compiler/rules/character_range.h b/src/compiler/rules/character_range.h new file mode 100644 index 00000000..bb6fe2db --- /dev/null +++ b/src/compiler/rules/character_range.h @@ -0,0 +1,29 @@ +#ifndef __tree_sitter_character_range_h__ +#define __tree_sitter_character_range_h__ + +#include + +namespace tree_sitter { + namespace rules { + struct CharacterRange { + char min; + char max; + CharacterRange(char); + CharacterRange(char, char); + bool operator==(const CharacterRange &) const; + bool operator<(const CharacterRange &) const; + std::string to_string() const; + }; + } +} + +namespace std { + template<> + struct hash { + size_t operator()(const tree_sitter::rules::CharacterRange &range) const { + return (hash()(range.min) ^ hash()(range.max)); + } + }; +} + +#endif diff --git a/src/compiler/rules/character_set.h b/src/compiler/rules/character_set.h index 98734df8..975df757 100644 --- a/src/compiler/rules/character_set.h +++ b/src/compiler/rules/character_set.h @@ -1,8 +1,8 @@ #ifndef __tree_sitter__character_set__ #define __tree_sitter__character_set__ -#include "tree_sitter/compiler.h" #include "rule.h" +#include "./character_range.h" #include #include diff --git a/src/compiler/rules/rules.cpp b/src/compiler/rules/rules.cpp index db7352b6..68f78e7e 100644 --- a/src/compiler/rules/rules.cpp +++ b/src/compiler/rules/rules.cpp @@ -20,17 +20,6 @@ namespace tree_sitter { return make_shared(); } - rule_ptr character(const set &ranges) { - return make_shared(ranges); - } - - rule_ptr character(const set &ranges, bool sign) { - if (sign) - return character(ranges); - else - return CharacterSet(ranges).complement().copy(); - } - rule_ptr choice(const vector &rules) { return Choice::Build(rules); } diff --git a/tree_sitter.xcodeproj/project.pbxproj b/tree_sitter.xcodeproj/project.pbxproj index 6c50a75e..a1ba2c9f 100644 --- a/tree_sitter.xcodeproj/project.pbxproj +++ b/tree_sitter.xcodeproj/project.pbxproj @@ -105,6 +105,7 @@ 1236A7CE18B3CC4800593ABB /* .travis.yml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = .travis.yml; sourceTree = ""; }; 1236A7D018B554C800593ABB /* prepared_grammar.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = prepared_grammar.cpp; sourceTree = ""; }; 1236A7D118B554C800593ABB /* prepared_grammar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = prepared_grammar.h; sourceTree = ""; }; + 1236A7D418B72EB400593ABB /* character_range.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = character_range.h; sourceTree = ""; }; 1251209A1830145300C9B56A /* rule.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule.cpp; sourceTree = ""; }; 125120A3183083BD00C9B56A /* arithmetic.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; lineEnding = 0; path = arithmetic.hpp; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.cpp; }; 12661BF318A1505A00A259FB /* character_set_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = character_set_spec.cpp; path = spec/compiler/rules/character_set_spec.cpp; sourceTree = SOURCE_ROOT; }; @@ -198,6 +199,7 @@ 1213060F182C3A1100FCF928 /* blank.cpp */, 12130610182C3A1100FCF928 /* blank.h */, 1236A7C318B287DC00593ABB /* character_range.cpp */, + 1236A7D418B72EB400593ABB /* character_range.h */, 12130603182C348F00FCF928 /* character_set.cpp */, 12130604182C348F00FCF928 /* character_set.h */, 1213060C182C398300FCF928 /* choice.cpp */,