tree-sitter/include/tree_sitter/parser.h

159 lines
5.1 KiB
C
Raw Normal View History

2014-03-09 22:05:17 -07:00
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
2014-02-15 17:00:33 -08:00
#ifdef __cplusplus
extern "C" {
#endif
2014-03-08 15:04:23 -08:00
#include <stdio.h>
2014-09-13 00:25:12 -07:00
#include <stdint.h>
#include "tree_sitter/runtime.h"
2014-07-17 23:29:11 -07:00
typedef struct TSTree TSTree;
#define ts_lex_state_error 0
typedef struct TSLexer {
2014-07-20 20:27:33 -07:00
TSInput input;
int debug;
2014-07-20 20:27:33 -07:00
const char *chunk;
size_t chunk_start;
size_t chunk_size;
TSLength current_position;
TSLength token_end_position;
TSLength token_start_position;
2014-09-13 00:15:24 -07:00
size_t lookahead_size;
int32_t lookahead;
2014-07-31 13:11:39 -07:00
TSTree *(*accept_fn)(struct TSLexer *, TSSymbol, int);
int (*advance_fn)(struct TSLexer *);
} TSLexer;
2014-09-13 00:15:24 -07:00
static inline int32_t ts_lexer_lookahead_char(const TSLexer *lexer) {
return lexer->lookahead;
}
static inline void ts_lexer_start_token(TSLexer *lexer) {
lexer->token_start_position = lexer->current_position;
}
static inline int ts_lexer_advance(TSLexer *lexer) {
return lexer->advance_fn(lexer);
}
2014-07-31 13:11:39 -07:00
static inline TSTree *ts_lexer_accept(TSLexer *lexer, TSSymbol symbol,
int is_hidden) {
return lexer->accept_fn(lexer, symbol, is_hidden);
}
typedef unsigned short TSStateId;
typedef enum {
2014-07-20 20:27:33 -07:00
TSParseActionTypeError,
TSParseActionTypeShift,
TSParseActionTypeShiftExtra,
TSParseActionTypeReduce,
TSParseActionTypeReduceExtra,
TSParseActionTypeAccept,
} TSParseActionType;
typedef struct {
2014-07-20 20:27:33 -07:00
TSParseActionType type;
union {
TSStateId to_state;
struct {
TSSymbol symbol;
unsigned short child_count;
};
} data;
} TSParseAction;
struct TSLanguage {
2014-07-20 20:27:33 -07:00
size_t symbol_count;
const char **symbol_names;
const int *hidden_symbol_flags;
const TSParseAction *parse_table;
const TSStateId *lex_states;
TSTree *(*lex_fn)(TSLexer *, TSStateId);
};
#define DEBUG_LEX(...) \
if (lexer->debug) { \
fprintf(stderr, "LEX "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
}
2014-06-09 21:14:38 -07:00
#define START_LEXER() \
DEBUG_LEX("START %d", lex_state); \
int32_t lookahead; \
next_state: \
lookahead = ts_lexer_lookahead_char(lexer); \
DEBUG_LEX( \
(0 < lookahead && lookahead <= 255 ? "CHAR '%c'" : "CHAR %d"), \
lookahead);
2014-04-04 13:10:55 -07:00
#define START_TOKEN() ts_lexer_start_token(lexer);
2014-09-03 18:53:38 -07:00
#define ADVANCE(state_index) \
{ \
DEBUG_LEX("ADVANCE %d", state_index); \
ts_lexer_advance(lexer); \
2014-09-03 18:53:38 -07:00
lex_state = state_index; \
goto next_state; \
2014-07-20 20:27:33 -07:00
}
2014-03-28 13:51:32 -07:00
2014-07-31 13:11:39 -07:00
#define ACCEPT_TOKEN(symbol) \
{ \
DEBUG_LEX("TOKEN %s", ts_symbol_names[symbol]); \
return ts_lexer_accept(lexer, symbol, ts_hidden_symbol_flags[symbol]); \
2014-07-20 20:27:33 -07:00
}
2014-03-28 13:51:32 -07:00
#define LEX_ERROR() \
{ \
DEBUG_LEX("ERROR"); \
return ts_lexer_accept(lexer, ts_builtin_sym_error, 0); \
2014-07-20 20:27:33 -07:00
}
2014-03-28 13:51:32 -07:00
#define SHIFT(to_state_value) \
{ \
.type = TSParseActionTypeShift, .data = { .to_state = to_state_value } \
}
#define SHIFT_EXTRA() \
2014-07-20 20:27:33 -07:00
{ .type = TSParseActionTypeShiftExtra }
#define REDUCE_EXTRA(symbol_val) \
{ \
.type = TSParseActionTypeReduceExtra, .data = { .symbol = symbol_val } \
}
#define REDUCE(symbol_val, child_count_val) \
{ \
.type = TSParseActionTypeReduce, \
.data = { .symbol = symbol_val, .child_count = child_count_val } \
}
#define ACCEPT_INPUT() \
2014-07-20 20:27:33 -07:00
{ .type = TSParseActionTypeAccept }
2014-05-09 15:37:30 -07:00
#define EXPORT_LANGUAGE(language_name) \
static TSLanguage language = { .symbol_count = SYMBOL_COUNT, \
.hidden_symbol_flags = ts_hidden_symbol_flags, \
.parse_table = \
(const TSParseAction *)ts_parse_actions, \
.lex_states = ts_lex_states, \
.symbol_names = ts_symbol_names, \
.lex_fn = ts_lex, }; \
\
2014-07-31 13:11:39 -07:00
const TSLanguage *language_name() { return &language; }
2014-05-09 15:37:30 -07:00
#ifdef __cplusplus
}
#endif
2014-02-15 17:00:33 -08:00
2014-03-09 22:05:17 -07:00
#endif // TREE_SITTER_PARSER_H_