tree-sitter/include/tree_sitter/compiler.h
Max Brunsfeld 9a198562e0 Treat parse conflicts as errors in grammar compilation
For now, only reduce/reduce conflicts w/ no tie-breaking precedence
are treated as errors. The rest are dropped, because shift/reduce
conflicts are currently very common because we don't have a way
of specifying associativity along w/ precedence.
2015-03-15 20:31:41 -07:00

71 lines
2 KiB
C++

#ifndef TREE_SITTER_COMPILER_H_
#define TREE_SITTER_COMPILER_H_
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
namespace tree_sitter {
namespace rules {
class Rule;
typedef std::shared_ptr<Rule> rule_ptr;
rule_ptr blank();
rule_ptr choice(const std::vector<rule_ptr> &);
rule_ptr repeat(const rule_ptr &);
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 keyword(const std::string &);
rule_ptr keypattern(const std::string &);
rule_ptr err(const rule_ptr &);
rule_ptr prec(int precedence, const rule_ptr &);
rule_ptr token(const rule_ptr &rule);
std::ostream &operator<<(std::ostream &stream, const rules::rule_ptr &rule);
} // namespace rules
class Grammar {
const std::vector<std::pair<std::string, rules::rule_ptr>> rules_;
std::set<rules::rule_ptr> ubiquitous_tokens_;
public:
explicit Grammar(const std::vector<std::pair<std::string, rules::rule_ptr>> &);
bool operator==(const Grammar &) const;
std::string start_rule_name() const;
const rules::rule_ptr rule(const std::string &) const;
const std::vector<std::pair<std::string, rules::rule_ptr>> &rules() const;
const std::set<rules::rule_ptr> &ubiquitous_tokens() const;
Grammar &ubiquitous_tokens(const std::set<rules::rule_ptr> &);
};
enum GrammarErrorType {
GrammarErrorTypeRegex,
GrammarErrorTypeUndefinedSymbol,
GrammarErrorTypeInvalidUbiquitousToken,
GrammarErrorTypeLexConflict,
GrammarErrorTypeParseConflict,
};
class GrammarError {
public:
GrammarError(GrammarErrorType type, std::string message);
bool operator==(const GrammarError &other) const;
GrammarErrorType type;
std::string message;
};
std::pair<std::string, const GrammarError *> compile(const Grammar &, std::string);
std::ostream &operator<<(std::ostream &stream, const Grammar &grammar);
std::ostream &operator<<(std::ostream &stream, const GrammarError *error);
} // namespace tree_sitter
#endif // TREE_SITTER_COMPILER_H_