2014-03-09 22:05:17 -07:00
|
|
|
#ifndef TREE_SITTER_COMPILER_H_
|
|
|
|
|
#define TREE_SITTER_COMPILER_H_
|
2014-02-16 22:13:08 -08:00
|
|
|
|
2014-07-22 07:52:12 -07:00
|
|
|
#include <memory>
|
2014-07-01 20:47:35 -07:00
|
|
|
#include <set>
|
2014-02-16 22:13:08 -08:00
|
|
|
#include <string>
|
2014-07-22 07:52:12 -07:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2014-02-16 22:13:08 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-07-22 07:52:12 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
namespace rules {
|
2014-07-22 07:52:12 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
class Rule;
|
|
|
|
|
typedef std::shared_ptr<Rule> rule_ptr;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
rule_ptr blank();
|
|
|
|
|
rule_ptr choice(const std::vector<rule_ptr> &rules);
|
|
|
|
|
rule_ptr repeat(const rule_ptr &content);
|
|
|
|
|
rule_ptr seq(const std::vector<rule_ptr> &rules);
|
|
|
|
|
rule_ptr sym(const std::string &name);
|
|
|
|
|
rule_ptr pattern(const std::string &value);
|
|
|
|
|
rule_ptr str(const std::string &value);
|
|
|
|
|
rule_ptr keyword(const std::string &value);
|
|
|
|
|
rule_ptr keypattern(const std::string &value);
|
|
|
|
|
rule_ptr err(const rule_ptr &rule);
|
|
|
|
|
rule_ptr prec(int precedence, rule_ptr rule);
|
|
|
|
|
rule_ptr token(rule_ptr rule);
|
2014-07-22 07:52:12 -07:00
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &stream, const rules::rule_ptr &rule);
|
|
|
|
|
|
|
|
|
|
} // namespace rules
|
2014-02-16 22:13:08 -08:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
class Grammar {
|
|
|
|
|
const std::vector<std::pair<std::string, rules::rule_ptr> > rules_;
|
|
|
|
|
std::set<std::string> ubiquitous_tokens_;
|
|
|
|
|
std::set<char> separators_;
|
2014-06-09 21:05:25 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
public:
|
|
|
|
|
Grammar(const std::vector<std::pair<std::string, rules::rule_ptr> > &rules);
|
|
|
|
|
bool operator==(const Grammar &other) const;
|
|
|
|
|
std::string start_rule_name() const;
|
|
|
|
|
const rules::rule_ptr rule(const std::string &name) const;
|
|
|
|
|
const std::vector<std::pair<std::string, rules::rule_ptr> > &rules() const;
|
|
|
|
|
const std::set<std::string> &ubiquitous_tokens() const;
|
|
|
|
|
Grammar &ubiquitous_tokens(const std::set<std::string> &ubiquitous_tokens);
|
|
|
|
|
const std::set<char> &separators() const;
|
|
|
|
|
Grammar &separators(const std::set<char> &separators);
|
|
|
|
|
};
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
struct Conflict {
|
2014-07-22 07:52:12 -07:00
|
|
|
explicit Conflict(std::string description);
|
2014-07-20 21:43:27 -07:00
|
|
|
std::string description;
|
|
|
|
|
bool operator==(const Conflict &other) const;
|
|
|
|
|
bool operator<(const Conflict &other) const;
|
|
|
|
|
};
|
2014-04-08 08:19:55 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
enum GrammarErrorType {
|
|
|
|
|
GrammarErrorTypeRegex,
|
|
|
|
|
GrammarErrorTypeUndefinedSymbol
|
|
|
|
|
};
|
2014-05-01 23:28:40 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
class GrammarError {
|
|
|
|
|
public:
|
|
|
|
|
GrammarError(GrammarErrorType type, std::string message);
|
|
|
|
|
bool operator==(const GrammarError &other) const;
|
|
|
|
|
GrammarErrorType type;
|
|
|
|
|
std::string message;
|
|
|
|
|
};
|
2014-05-01 23:28:40 -07:00
|
|
|
|
2014-07-22 07:52:12 -07:00
|
|
|
std::tuple<std::string, std::vector<Conflict>, const GrammarError *> compile(
|
|
|
|
|
const Grammar &grammar, std::string name);
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
std::ostream &operator<<(std::ostream &stream, const Grammar &grammar);
|
|
|
|
|
std::ostream &operator<<(std::ostream &stream, const Conflict &conflict);
|
|
|
|
|
std::ostream &operator<<(std::ostream &stream, const GrammarError *error);
|
2014-02-20 23:05:32 -08:00
|
|
|
|
2014-07-22 07:52:12 -07:00
|
|
|
} // namespace tree_sitter
|
2014-02-16 22:13:08 -08:00
|
|
|
|
2014-03-09 22:05:17 -07:00
|
|
|
#endif // TREE_SITTER_COMPILER_H_
|