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