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
|
|
|
|
2014-01-11 18:14:24 -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>
|
2014-05-09 14:43:43 -07:00
|
|
|
#include "tree_sitter/runtime.h"
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2016-06-27 14:07:47 -07:00
|
|
|
#define TS_STATE_ERROR 0
|
2014-10-13 01:02:12 -07:00
|
|
|
#define TS_DEBUG_BUFFER_SIZE 512
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2014-10-17 16:20:01 -07:00
|
|
|
typedef unsigned short TSStateId;
|
|
|
|
|
|
2015-12-04 10:45:30 -08:00
|
|
|
typedef struct {
|
|
|
|
|
size_t bytes;
|
|
|
|
|
size_t chars;
|
2015-12-04 20:20:29 -08:00
|
|
|
size_t rows;
|
|
|
|
|
size_t columns;
|
2015-12-04 10:45:30 -08:00
|
|
|
} TSLength;
|
|
|
|
|
|
2015-11-22 13:32:20 -08:00
|
|
|
typedef struct {
|
|
|
|
|
bool visible : 1;
|
|
|
|
|
bool named : 1;
|
2015-12-02 07:36:31 -08:00
|
|
|
bool extra : 1;
|
2015-12-16 20:04:45 -08:00
|
|
|
bool structural : 1;
|
2015-11-22 13:32:20 -08:00
|
|
|
} TSSymbolMetadata;
|
2015-09-05 22:29:17 -07:00
|
|
|
|
2016-05-20 20:26:03 -07:00
|
|
|
typedef enum {
|
|
|
|
|
TSTransitionTypeMain,
|
|
|
|
|
TSTransitionTypeSeparator,
|
|
|
|
|
TSTransitionTypeError,
|
|
|
|
|
} TSTransitionType;
|
2014-09-26 16:15:07 -07:00
|
|
|
|
2016-05-20 20:26:03 -07:00
|
|
|
typedef struct TSLexer {
|
|
|
|
|
void (*advance)(struct TSLexer *, TSStateId, TSTransitionType);
|
2014-09-26 16:15:07 -07:00
|
|
|
|
|
|
|
|
TSLength current_position;
|
|
|
|
|
TSLength token_end_position;
|
|
|
|
|
TSLength token_start_position;
|
2016-05-20 20:26:03 -07:00
|
|
|
TSLength error_end_position;
|
|
|
|
|
|
|
|
|
|
const char *chunk;
|
|
|
|
|
size_t chunk_start;
|
|
|
|
|
size_t chunk_size;
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2014-09-13 00:15:24 -07:00
|
|
|
size_t lookahead_size;
|
|
|
|
|
int32_t lookahead;
|
2015-12-21 16:04:11 -08:00
|
|
|
TSStateId starting_state;
|
2016-05-20 20:26:03 -07:00
|
|
|
TSSymbol result_symbol;
|
|
|
|
|
bool result_follows_error;
|
|
|
|
|
int32_t first_unexpected_character;
|
2014-09-13 00:15:24 -07:00
|
|
|
|
2014-10-17 16:20:01 -07:00
|
|
|
TSInput input;
|
2014-10-13 01:02:12 -07:00
|
|
|
TSDebugger debugger;
|
|
|
|
|
char debug_buffer[TS_DEBUG_BUFFER_SIZE];
|
2014-07-30 23:40:02 -07:00
|
|
|
} TSLexer;
|
2014-07-10 13:14:52 -07:00
|
|
|
|
|
|
|
|
typedef enum {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSParseActionTypeShift,
|
|
|
|
|
TSParseActionTypeReduce,
|
|
|
|
|
TSParseActionTypeAccept,
|
2016-05-09 14:31:44 -07:00
|
|
|
TSParseActionTypeRecover,
|
2014-07-10 13:14:52 -07:00
|
|
|
} TSParseActionType;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2014-07-20 20:27:33 -07:00
|
|
|
union {
|
|
|
|
|
TSStateId to_state;
|
|
|
|
|
struct {
|
|
|
|
|
TSSymbol symbol;
|
|
|
|
|
unsigned short child_count;
|
|
|
|
|
};
|
2016-06-21 21:36:33 -07:00
|
|
|
};
|
2016-06-21 22:53:48 -07:00
|
|
|
TSParseActionType type : 4;
|
2015-12-17 12:48:55 -08:00
|
|
|
bool extra : 1;
|
|
|
|
|
bool fragile : 1;
|
2014-07-10 13:14:52 -07:00
|
|
|
} TSParseAction;
|
|
|
|
|
|
2015-12-29 11:20:52 -08:00
|
|
|
typedef union {
|
|
|
|
|
TSParseAction action;
|
2016-06-21 07:28:04 -07:00
|
|
|
struct {
|
|
|
|
|
unsigned short count;
|
|
|
|
|
bool reusable : 1;
|
|
|
|
|
bool depends_on_lookahead : 1;
|
|
|
|
|
};
|
2015-12-29 11:20:52 -08:00
|
|
|
} TSParseActionEntry;
|
|
|
|
|
|
2014-07-30 23:40:02 -07:00
|
|
|
struct TSLanguage {
|
2014-07-20 20:27:33 -07:00
|
|
|
size_t symbol_count;
|
|
|
|
|
const char **symbol_names;
|
2015-11-22 13:32:20 -08:00
|
|
|
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;
|
2016-06-17 21:26:03 -07:00
|
|
|
bool (*lex_fn)(TSLexer *, TSStateId, bool);
|
2014-07-10 13:14:52 -07:00
|
|
|
};
|
|
|
|
|
|
2014-10-17 16:20:01 -07:00
|
|
|
/*
|
|
|
|
|
* Lexer Macros
|
|
|
|
|
*/
|
2014-06-09 21:14:38 -07:00
|
|
|
|
2016-05-20 20:26:03 -07:00
|
|
|
#define START_LEXER() \
|
|
|
|
|
int32_t lookahead; \
|
|
|
|
|
next_state: \
|
2014-10-17 16:20:01 -07:00
|
|
|
lookahead = lexer->lookahead;
|
2014-03-28 13:51:32 -07:00
|
|
|
|
2015-12-30 09:37:40 -08:00
|
|
|
#define GO_TO_STATE(state_value) \
|
2015-07-27 18:29:48 -07:00
|
|
|
{ \
|
2016-01-10 20:04:41 -08: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
|
|
|
|
2016-05-20 20:26:03 -07:00
|
|
|
#define ADVANCE(state_value) \
|
|
|
|
|
{ \
|
|
|
|
|
lexer->advance(lexer, state_value, TSTransitionTypeMain); \
|
|
|
|
|
GO_TO_STATE(state_value); \
|
2016-05-19 16:25:44 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 20:26:03 -07:00
|
|
|
#define SKIP(state_value) \
|
|
|
|
|
{ \
|
|
|
|
|
lexer->advance(lexer, state_value, TSTransitionTypeSeparator); \
|
|
|
|
|
GO_TO_STATE(state_value); \
|
2015-06-12 13:13:43 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 20:26:03 -07:00
|
|
|
#define ACCEPT_TOKEN(symbol_value) \
|
|
|
|
|
{ \
|
|
|
|
|
lexer->result_symbol = symbol_value; \
|
2016-06-17 21:26:03 -07:00
|
|
|
return true; \
|
2016-05-20 20:26:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define LEX_ERROR() \
|
|
|
|
|
if (error_mode) { \
|
2016-06-27 14:07:47 -07:00
|
|
|
if (state == TS_STATE_ERROR) \
|
2016-05-20 20:26:03 -07:00
|
|
|
lexer->advance(lexer, state, TSTransitionTypeError); \
|
2016-06-27 14:07:47 -07:00
|
|
|
GO_TO_STATE(TS_STATE_ERROR); \
|
2016-05-20 20:26:03 -07:00
|
|
|
} else { \
|
2016-06-17 21:26:03 -07:00
|
|
|
return false; \
|
|
|
|
|
}
|
2016-05-20 20:26:03 -07:00
|
|
|
|
2014-10-17 16:20:01 -07:00
|
|
|
/*
|
|
|
|
|
* Parse Table Macros
|
|
|
|
|
*/
|
|
|
|
|
|
2016-06-21 21:36:33 -07:00
|
|
|
#define SHIFT(to_state_value) \
|
|
|
|
|
{ \
|
|
|
|
|
{ .type = TSParseActionTypeShift, .to_state = to_state_value } \
|
2015-12-29 21:17:31 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-27 14:07:47 -07:00
|
|
|
#define RECOVER(to_state_value) \
|
|
|
|
|
{ \
|
|
|
|
|
{ .type = TSParseActionTypeRecover, .to_state = to_state_value } \
|
|
|
|
|
}
|
2016-06-18 20:35:33 -07:00
|
|
|
|
2015-12-29 21:17:31 -08:00
|
|
|
#define SHIFT_EXTRA() \
|
|
|
|
|
{ \
|
|
|
|
|
{ .type = TSParseActionTypeShift, .extra = true } \
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 21:36:33 -07:00
|
|
|
#define REDUCE_EXTRA(symbol_val) \
|
|
|
|
|
{ \
|
|
|
|
|
{ \
|
|
|
|
|
.type = TSParseActionTypeReduce, .symbol = symbol_val, .child_count = 1, \
|
|
|
|
|
.extra = true, \
|
|
|
|
|
} \
|
2015-12-29 21:17:31 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-21 21:36:33 -07:00
|
|
|
#define REDUCE(symbol_val, child_count_val) \
|
|
|
|
|
{ \
|
|
|
|
|
{ \
|
|
|
|
|
.type = TSParseActionTypeReduce, .symbol = symbol_val, \
|
|
|
|
|
.child_count = child_count_val, \
|
|
|
|
|
} \
|
2016-06-21 07:28:04 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-21 21:36:33 -07:00
|
|
|
#define REDUCE_FRAGILE(symbol_val, child_count_val) \
|
|
|
|
|
{ \
|
|
|
|
|
{ \
|
|
|
|
|
.type = TSParseActionTypeReduce, .symbol = symbol_val, \
|
|
|
|
|
.child_count = child_count_val, .fragile = true, \
|
|
|
|
|
} \
|
2015-12-29 21:17:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define ACCEPT_INPUT() \
|
|
|
|
|
{ \
|
|
|
|
|
{ .type = TSParseActionTypeAccept } \
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
#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, \
|
|
|
|
|
.lex_states = ts_lex_states, \
|
|
|
|
|
.symbol_names = ts_symbol_names, \
|
|
|
|
|
.lex_fn = ts_lex, \
|
|
|
|
|
}; \
|
|
|
|
|
\
|
|
|
|
|
const TSLanguage *language_name() { \
|
|
|
|
|
return &language; \
|
2015-07-31 15:47:48 -07:00
|
|
|
}
|
2014-05-09 15:37:30 -07:00
|
|
|
|
2014-01-05 15:43:00 -08: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_
|