tree-sitter/include/tree_sitter/parser.h

217 lines
6.6 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-09-13 00:25:12 -07:00
#include <stdint.h>
2014-10-03 16:06:08 -07:00
#include <stdbool.h>
#include "tree_sitter/runtime.h"
#define ts_lex_state_error 0
#define ts_parse_state_error ((TSStateId)-1)
#define TS_DEBUG_BUFFER_SIZE 512
typedef struct TSTree TSTree;
typedef unsigned short TSStateId;
typedef struct {
size_t bytes;
size_t chars;
size_t rows;
size_t columns;
} TSLength;
typedef struct {
bool visible : 1;
bool named : 1;
bool extra : 1;
bool structural : 1;
} TSSymbolMetadata;
typedef struct TSLexer {
void (*start_fn)(struct TSLexer *, TSStateId);
bool (*advance_fn)(struct TSLexer *, TSStateId, bool);
TSTree *(*accept_fn)(struct TSLexer *, TSSymbol, TSSymbolMetadata,
const char *, bool fragile);
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;
TSStateId starting_state;
2014-09-13 00:15:24 -07:00
TSInput input;
TSDebugger debugger;
char debug_buffer[TS_DEBUG_BUFFER_SIZE];
} TSLexer;
typedef enum {
2015-12-29 11:20:52 -08:00
TSParseActionTypeError,
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 {
TSSymbol symbol;
unsigned short child_count;
};
} data;
TSParseActionType type : 3;
bool extra : 1;
bool fragile : 1;
bool can_hide_split : 1;
} TSParseAction;
2015-12-29 11:20:52 -08:00
typedef union {
TSParseAction action;
unsigned int count;
} TSParseActionEntry;
struct TSLanguage {
2014-07-20 20:27:33 -07:00
size_t symbol_count;
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;
2014-07-20 20:27:33 -07:00
const TSStateId *lex_states;
const TSParseAction *recovery_actions;
TSTree *(*lex_fn)(TSLexer *, TSStateId, bool);
};
/*
* Lexer Macros
*/
2014-06-09 21:14:38 -07:00
#define START_LEXER() \
lexer->start_fn(lexer, state); \
int32_t lookahead; \
next_state: \
lookahead = lexer->lookahead;
2014-03-28 13:51:32 -07:00
#define GO_TO_STATE(state_value) \
2015-07-27 18:29:48 -07:00
{ \
state = state_value; \
2015-07-27 18:29:48 -07:00
goto next_state; \
2014-07-20 20:27:33 -07:00
}
2014-03-28 13:51:32 -07:00
#define ADVANCE(state_value) \
{ \
lexer->advance_fn(lexer, state_value, true); \
GO_TO_STATE(state_value); \
}
#define SKIP(state_value) \
{ \
lexer->advance_fn(lexer, state_value, false); \
GO_TO_STATE(state_value); \
}
#define ACCEPT_FRAGILE_TOKEN(symbol) \
return lexer->accept_fn(lexer, symbol, ts_symbol_metadata[symbol], \
ts_symbol_names[symbol], true);
#define ACCEPT_TOKEN(symbol) \
return lexer->accept_fn(lexer, symbol, ts_symbol_metadata[symbol], \
ts_symbol_names[symbol], false);
2016-03-02 09:55:25 -08:00
#define LEX_ERROR() \
if (error_mode) { \
if (state == ts_lex_state_error) \
lexer->advance_fn(lexer, state, true); \
2016-03-02 09:55:25 -08:00
GO_TO_STATE(ts_lex_state_error) \
} else { \
return lexer->accept_fn(lexer, ts_builtin_sym_error, (TSSymbolMetadata){}, \
"ERROR", false); \
}
/*
* Parse Table Macros
*/
enum {
FRAGILE = 1,
CAN_HIDE_SPLIT = 2,
};
2015-12-29 21:17:31 -08:00
#define ERROR() \
{ \
{ .type = TSParseActionTypeError } \
}
#define SHIFT(to_state_value, flags) \
{ \
{ \
.type = TSParseActionTypeShift, \
.can_hide_split = (flags & CAN_HIDE_SPLIT) != 0, \
.data = {.to_state = to_state_value } \
} \
}
#define RECOVER(to_state_value) \
{ \
.type = TSParseActionTypeRecover, .data = {.to_state = to_state_value } \
}
2015-12-29 21:17:31 -08:00
#define SHIFT_EXTRA() \
{ \
{ .type = TSParseActionTypeShift, .extra = true } \
}
#define REDUCE_EXTRA(symbol_val) \
{ \
{ \
.type = TSParseActionTypeReduce, .extra = true, \
.data = {.symbol = symbol_val, .child_count = 1 } \
} \
}
#define REDUCE(symbol_val, child_count_val, flags) \
{ \
{ \
.type = TSParseActionTypeReduce, .fragile = (flags & FRAGILE) != 0, \
.can_hide_split = (flags & CAN_HIDE_SPLIT) != 0, \
.data = {.symbol = symbol_val, .child_count = child_count_val } \
} \
}
#define ACCEPT_INPUT() \
{ \
{ .type = TSParseActionTypeAccept } \
}
#define EXPORT_LANGUAGE(language_name) \
static TSLanguage language = { \
.symbol_count = SYMBOL_COUNT, \
.symbol_metadata = ts_symbol_metadata, \
.parse_table = (const unsigned short *)ts_parse_table, \
.parse_actions = ts_parse_actions, \
.recovery_actions = ts_recovery_actions, \
.lex_states = ts_lex_states, \
.symbol_names = ts_symbol_names, \
.lex_fn = ts_lex, \
}; \
\
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_