Make the compile function plain C and take a JSON grammar

This commit is contained in:
Max Brunsfeld 2016-01-10 20:04:41 -08:00
parent b69e19c525
commit d4632ab9a9
54 changed files with 325 additions and 234 deletions

View file

@ -1,66 +1,30 @@
#ifndef TREE_SITTER_COMPILER_H_
#define TREE_SITTER_COMPILER_H_
#include <memory>
#include <string>
#include <utility>
#include <vector>
#ifdef __cplusplus
extern "C" {
#endif
namespace tree_sitter {
typedef enum {
TSCompileErrorTypeNone,
TSCompileErrorTypeInvalidGrammar,
TSCompileErrorTypeInvalidRegex,
TSCompileErrorTypeUndefinedSymbol,
TSCompileErrorTypeInvalidUbiquitousToken,
TSCompileErrorTypeLexConflict,
TSCompileErrorTypeParseConflict,
} TSCompileErrorType;
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 repeat1(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 err(const rule_ptr &);
rule_ptr prec(int precedence, const rule_ptr &);
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 &);
rule_ptr token(const rule_ptr &rule);
struct Grammar {
std::vector<std::pair<std::string, rule_ptr>> rules;
std::vector<rule_ptr> extra_tokens;
std::vector<std::vector<std::string>> expected_conflicts;
};
enum GrammarErrorType {
GrammarErrorTypeRegex,
GrammarErrorTypeUndefinedSymbol,
GrammarErrorTypeInvalidUbiquitousToken,
GrammarErrorTypeLexConflict,
GrammarErrorTypeParseConflict,
};
class GrammarError {
public:
GrammarError(GrammarErrorType type, std::string message) : type(type), message(message) {}
bool operator==(const GrammarError &other) const {
return type == other.type && message == other.message;
}
GrammarErrorType type;
std::string message;
};
std::pair<std::string, const GrammarError *> compile(const Grammar &,
std::string);
struct CompileResult {
typedef struct {
const char *code;
const char *error_message;
};
TSCompileErrorType error_type;
} TSCompileResult;
extern "C" CompileResult compile(const char *input);
TSCompileResult ts_compile_grammar(const char *input);
} // namespace tree_sitter
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_COMPILER_H_

View file

@ -94,17 +94,17 @@ struct TSLanguage {
* Lexer Macros
*/
#define START_LEXER() \
lexer->start_fn(lexer, state); \
int32_t lookahead; \
next_state: \
#define START_LEXER() \
lexer->start_fn(lexer, state); \
int32_t lookahead; \
next_state: \
lookahead = lexer->lookahead;
#define START_TOKEN() lexer->start_token_fn(lexer);
#define GO_TO_STATE(state_value) \
{ \
state = state_value; \
state = state_value; \
goto next_state; \
}