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-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
|
|
|
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();
|
2015-03-15 20:31:41 -07:00
|
|
|
rule_ptr choice(const std::vector<rule_ptr> &);
|
|
|
|
|
rule_ptr repeat(const rule_ptr &);
|
2015-10-12 15:33:00 -07:00
|
|
|
rule_ptr repeat1(const rule_ptr &);
|
2015-03-15 20:31:41 -07:00
|
|
|
rule_ptr seq(const std::vector<rule_ptr> &);
|
|
|
|
|
rule_ptr sym(const std::string &);
|
|
|
|
|
rule_ptr pattern(const std::string &);
|
|
|
|
|
rule_ptr str(const std::string &);
|
|
|
|
|
rule_ptr err(const rule_ptr &);
|
2015-03-23 21:02:40 -07:00
|
|
|
rule_ptr prec(int precedence, const rule_ptr &);
|
2015-10-13 11:23:02 -07:00
|
|
|
rule_ptr prec_left(const rule_ptr &);
|
|
|
|
|
rule_ptr prec_left(int precedence, const rule_ptr &);
|
|
|
|
|
rule_ptr prec_right(const rule_ptr &);
|
|
|
|
|
rule_ptr prec_right(int precedence, const rule_ptr &);
|
2015-03-15 20:31:41 -07:00
|
|
|
rule_ptr token(const rule_ptr &rule);
|
2014-07-22 07:52:12 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
class Grammar {
|
2015-09-03 17:49:20 -07:00
|
|
|
const std::vector<std::pair<std::string, rule_ptr>> rules_;
|
2015-12-17 15:50:48 -08:00
|
|
|
std::vector<rule_ptr> extra_tokens_;
|
2015-09-08 23:43:45 -07:00
|
|
|
std::vector<std::vector<std::string>> expected_conflicts_;
|
2014-06-09 21:05:25 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
public:
|
2015-09-03 17:49:20 -07:00
|
|
|
explicit Grammar(const std::vector<std::pair<std::string, rule_ptr>> &);
|
2015-12-17 15:50:48 -08:00
|
|
|
Grammar &extra_tokens(const std::vector<rule_ptr> &);
|
2015-09-08 23:43:45 -07:00
|
|
|
Grammar &expected_conflicts(const std::vector<std::vector<std::string>> &);
|
2015-09-03 17:49:20 -07:00
|
|
|
const std::vector<std::pair<std::string, rule_ptr>> &rules() const;
|
2015-12-17 15:50:48 -08:00
|
|
|
const std::vector<rule_ptr> &extra_tokens() const;
|
2015-09-08 23:43:45 -07:00
|
|
|
const std::vector<std::vector<std::string>> &expected_conflicts() const;
|
2014-07-20 21:43:27 -07:00
|
|
|
};
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
enum GrammarErrorType {
|
|
|
|
|
GrammarErrorTypeRegex,
|
2014-09-07 22:16:45 -07:00
|
|
|
GrammarErrorTypeUndefinedSymbol,
|
2015-03-15 20:31:41 -07:00
|
|
|
GrammarErrorTypeInvalidUbiquitousToken,
|
|
|
|
|
GrammarErrorTypeLexConflict,
|
|
|
|
|
GrammarErrorTypeParseConflict,
|
2014-07-20 21:43:27 -07:00
|
|
|
};
|
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
|
|
|
|
2015-07-27 18:29:48 -07:00
|
|
|
std::pair<std::string, const GrammarError *> compile(const Grammar &,
|
|
|
|
|
std::string);
|
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_
|