tree-sitter/include/tree_sitter/parser.h

200 lines
6.5 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
2014-10-03 16:06:08 -07:00
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef unsigned short TSSymbol;
typedef unsigned short TSStateId;
typedef uint8_t TSExternalTokenState[16];
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
typedef struct {
bool visible : 1;
bool named : 1;
bool extra : 1;
bool structural : 1;
} TSSymbolMetadata;
typedef struct {
void (*advance)(void *, bool);
void (*mark_end)(void *);
2014-09-13 00:15:24 -07:00
int32_t lookahead;
TSSymbol result_symbol;
} TSLexer;
typedef enum {
2014-07-20 20:27:33 -07:00
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef struct {
2014-07-20 20:27:33 -07:00
union {
TSStateId to_state;
struct {
short dynamic_precedence;
2014-07-20 20:27:33 -07:00
TSSymbol symbol;
unsigned short child_count;
};
} params;
TSParseActionType type : 4;
bool extra : 1;
bool fragile : 1;
} TSParseAction;
2016-11-30 09:34:47 -08:00
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
2016-11-30 09:34:47 -08:00
} TSLexMode;
2015-12-29 11:20:52 -08:00
typedef union {
TSParseAction action;
struct {
unsigned short count;
bool reusable : 1;
bool depends_on_lookahead : 1;
};
2015-12-29 11:20:52 -08:00
} TSParseActionEntry;
typedef struct TSLanguage {
uint32_t version;
uint32_t symbol_count;
uint32_t token_count;
uint32_t external_token_count;
2014-07-20 20:27:33 -07:00
const char **symbol_names;
const TSSymbolMetadata *symbol_metadata;
2015-12-29 11:20:52 -08:00
const unsigned short *parse_table;
const TSParseActionEntry *parse_actions;
2016-11-30 09:34:47 -08:00
const TSLexMode *lex_modes;
2016-09-03 22:46:14 -07:00
bool (*lex_fn)(TSLexer *, TSStateId);
2016-11-30 09:34:47 -08:00
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)();
void (*destroy)(void *);
void (*reset)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
bool (*serialize)(void *, TSExternalTokenState);
void (*deserialize)(void *, const TSExternalTokenState);
2016-11-30 09:34:47 -08:00
} external_scanner;
} TSLanguage;
/*
* Lexer Macros
*/
2014-06-09 21:14:38 -07:00
#define START_LEXER() \
bool result = false; \
int32_t lookahead; \
next_state: \
lookahead = lexer->lookahead;
2014-03-28 13:51:32 -07:00
#define ADVANCE(state_value) \
{ \
lexer->advance(lexer, false); \
state = state_value; \
goto next_state; \
}
#define SKIP(state_value) \
{ \
lexer->advance(lexer, true); \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(to_state_value) \
{ \
{ \
.type = TSParseActionTypeShift, .params = {.to_state = to_state_value } \
} \
2015-12-29 21:17:31 -08:00
}
#define RECOVER(to_state_value) \
{ \
{ \
.type = TSParseActionTypeRecover, .params = {.to_state = to_state_value } \
} \
}
2015-12-29 21:17:31 -08:00
#define SHIFT_EXTRA() \
{ \
{ .type = TSParseActionTypeShift, .extra = true } \
}
#define REDUCE(symbol_val, child_count_val, dynamic_precedence_val) \
{ \
{ \
.type = TSParseActionTypeReduce, \
.params = { \
.symbol = symbol_val, \
.child_count = child_count_val, \
.dynamic_precedence = dynamic_precedence_val, \
} \
} \
}
#define REDUCE_FRAGILE(symbol_val, child_count_val, dynamic_precedence_val) \
{ \
{ \
.type = TSParseActionTypeReduce, \
.fragile = true, \
.params = { \
.symbol = symbol_val, \
.child_count = child_count_val, \
.dynamic_precedence = dynamic_precedence_val, \
} \
} \
}
2015-12-29 21:17:31 -08:00
#define ACCEPT_INPUT() \
{ \
{ .type = TSParseActionTypeAccept } \
}
2016-11-30 09:34:47 -08:00
#define GET_LANGUAGE(...) \
static TSLanguage language = { \
.version = LANGUAGE_VERSION, \
2016-11-30 09:34:47 -08:00
.symbol_count = SYMBOL_COUNT, \
.token_count = TOKEN_COUNT, \
.symbol_metadata = ts_symbol_metadata, \
.parse_table = (const unsigned short *)ts_parse_table, \
.parse_actions = ts_parse_actions, \
.lex_modes = ts_lex_modes, \
.symbol_names = ts_symbol_names, \
.lex_fn = ts_lex, \
.external_token_count = EXTERNAL_TOKEN_COUNT, \
2016-11-30 09:34:47 -08:00
.external_scanner = {__VA_ARGS__} \
}; \
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_