tree-sitter/include/tree_sitter/parser.h

197 lines
5.3 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>
2017-07-13 17:17:22 -07:00
typedef uint16_t TSSymbol;
typedef uint16_t TSStateId;
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
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 {
struct {
2017-07-13 17:17:22 -07:00
TSStateId to_state;
bool extra : 1;
};
struct {
2014-07-20 20:27:33 -07:00
TSSymbol symbol;
int16_t dynamic_precedence;
2017-07-13 17:17:22 -07:00
uint8_t child_count;
uint8_t rename_sequence_id : 7;
bool fragile : 1;
2014-07-20 20:27:33 -07:00
};
2017-07-13 17:17:22 -07:00
};
TSParseActionType type : 4;
} 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 {
2017-07-13 17:17:22 -07:00
uint8_t 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 rename_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;
2017-07-13 17:17:22 -07:00
const uint16_t *parse_table;
2015-12-29 11:20:52 -08:00
const TSParseActionEntry *parse_actions;
2016-11-30 09:34:47 -08:00
const TSLexMode *lex_modes;
2017-07-13 17:17:22 -07:00
const TSSymbol *rename_sequences;
uint16_t max_rename_sequence_length;
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 *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
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
2017-07-13 17:17:22 -07:00
#define SHIFT(to_state_value) \
{ \
{ \
.type = TSParseActionTypeShift, \
.to_state = to_state_value, \
} \
2015-12-29 21:17:31 -08:00
}
2017-07-13 17:17:22 -07:00
#define RECOVER(to_state_value) \
{ \
{ \
.type = TSParseActionTypeRecover, \
.to_state = to_state_value \
} \
}
2017-07-13 17:17:22 -07:00
#define SHIFT_EXTRA() \
{ \
{ \
.type = TSParseActionTypeShift, \
.extra = true \
} \
2015-12-29 21:17:31 -08:00
}
2017-07-13 17:17:22 -07:00
#define REDUCE(symbol_val, child_count_val, ...) \
{ \
{ \
.type = TSParseActionTypeReduce, \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
} \
}
2015-12-29 21:17:31 -08:00
#define ACCEPT_INPUT() \
{ \
{ .type = TSParseActionTypeAccept } \
}
2017-07-13 17:17:22 -07:00
#define GET_LANGUAGE(...) \
static TSLanguage language = { \
.version = LANGUAGE_VERSION, \
.symbol_count = SYMBOL_COUNT, \
.rename_symbol_count = RENAME_SYMBOL_COUNT, \
2017-07-13 17:17:22 -07:00
.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, \
.rename_sequences = (const TSSymbol *)ts_rename_sequences, \
.max_rename_sequence_length = MAX_RENAME_SEQUENCE_LENGTH, \
.lex_fn = ts_lex, \
.external_token_count = EXTERNAL_TOKEN_COUNT, \
.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_