Remove rules that don't need to be public from compiler.h

This commit is contained in:
Max Brunsfeld 2014-02-20 23:05:32 -08:00
parent a34da59889
commit 688fe993cf
9 changed files with 64 additions and 47 deletions

View file

@ -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(":") },

View file

@ -3,7 +3,6 @@
#include <vector>
#include <map>
#include <set>
#include <string>
#include <memory>
@ -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<tree_sitter::rules::CharacterRange> {
size_t operator()(const tree_sitter::rules::CharacterRange &range) const {
return (hash<char>()(range.min) ^ hash<char>()(range.max));
}
};
}
namespace tree_sitter {
namespace rules {
typedef std::shared_ptr<Rule> rule_ptr;
std::ostream& operator<<(std::ostream& stream, const rule_ptr &rule);
rule_ptr blank();
rule_ptr character(const std::set<CharacterRange> &matches);
rule_ptr character(const std::set<CharacterRange> &matches, bool);
rule_ptr choice(const std::vector<rule_ptr> &rules);
rule_ptr pattern(const std::string &value);
rule_ptr repeat(const rule_ptr content);
rule_ptr seq(const std::vector<rule_ptr> &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<const std::string, const rules::rule_ptr> &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);
}

View file

@ -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<CharacterRange> &ranges) {
return make_shared<CharacterSet>(ranges);
}
rule_ptr character(const set<CharacterRange> &ranges, bool sign) {
if (sign)
return character(ranges);
else
return CharacterSet(ranges).complement().copy();
}
}
}

View file

@ -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<CharacterRange> &matches);
rule_ptr character(const std::set<CharacterRange> &matches, bool);
}
}
#endif

View file

@ -1,4 +1,5 @@
#include "tree_sitter/compiler.h"
#include "character_range.h"
#include <string>
namespace tree_sitter {
using std::string;

View file

@ -0,0 +1,29 @@
#ifndef __tree_sitter_character_range_h__
#define __tree_sitter_character_range_h__
#include <map>
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<tree_sitter::rules::CharacterRange> {
size_t operator()(const tree_sitter::rules::CharacterRange &range) const {
return (hash<char>()(range.min) ^ hash<char>()(range.max));
}
};
}
#endif

View file

@ -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 <set>
#include <initializer_list>

View file

@ -20,17 +20,6 @@ namespace tree_sitter {
return make_shared<Blank>();
}
rule_ptr character(const set<CharacterRange> &ranges) {
return make_shared<CharacterSet>(ranges);
}
rule_ptr character(const set<CharacterRange> &ranges, bool sign) {
if (sign)
return character(ranges);
else
return CharacterSet(ranges).complement().copy();
}
rule_ptr choice(const vector<rule_ptr> &rules) {
return Choice::Build(rules);
}

View file

@ -105,6 +105,7 @@
1236A7CE18B3CC4800593ABB /* .travis.yml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = .travis.yml; sourceTree = "<group>"; };
1236A7D018B554C800593ABB /* prepared_grammar.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = prepared_grammar.cpp; sourceTree = "<group>"; };
1236A7D118B554C800593ABB /* prepared_grammar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = prepared_grammar.h; sourceTree = "<group>"; };
1236A7D418B72EB400593ABB /* character_range.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = character_range.h; sourceTree = "<group>"; };
1251209A1830145300C9B56A /* rule.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule.cpp; sourceTree = "<group>"; };
125120A3183083BD00C9B56A /* arithmetic.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; lineEnding = 0; path = arithmetic.hpp; sourceTree = "<group>"; 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 */,