tree-sitter-bash/src/tree_sitter/parser.h

224 lines
5.2 KiB
C
Raw Normal View History

2017-07-14 19:28:54 +00:00
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
2017-08-01 17:12:23 +00:00
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
2017-07-14 19:28:54 +00:00
2021-03-04 22:15:19 +00:00
typedef uint16_t TSStateId;
#ifndef TREE_SITTER_API_H_
2018-05-24 18:46:57 +00:00
typedef uint16_t TSSymbol;
2019-06-19 00:00:42 +00:00
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
#endif
2018-05-24 18:46:57 +00:00
2019-06-19 00:00:42 +00:00
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
typedef struct {
uint16_t index;
uint16_t length;
} TSFieldMapSlice;
2017-07-14 19:28:54 +00:00
typedef struct {
2021-03-04 22:15:19 +00:00
bool visible;
bool named;
bool supertype;
2017-07-14 19:28:54 +00:00
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer {
2017-07-14 19:28:54 +00:00
int32_t lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
uint32_t (*get_column)(TSLexer *);
2019-12-10 20:39:14 +00:00
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
};
2017-07-14 19:28:54 +00:00
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
2021-03-04 22:15:19 +00:00
typedef union {
struct {
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
2017-07-14 19:28:54 +00:00
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
2021-03-04 22:15:19 +00:00
bool reusable;
2020-05-14 22:24:16 +00:00
} entry;
2017-07-14 19:28:54 +00:00
} TSParseActionEntry;
struct TSLanguage {
2017-07-14 19:28:54 +00:00
uint32_t version;
uint32_t symbol_count;
2017-08-01 17:12:23 +00:00
uint32_t alias_count;
2017-07-14 19:28:54 +00:00
uint32_t token_count;
uint32_t external_token_count;
2021-03-04 22:15:19 +00:00
uint32_t state_count;
uint32_t large_state_count;
uint32_t production_id_count;
uint32_t field_count;
uint16_t max_alias_sequence_length;
2017-07-14 19:28:54 +00:00
const uint16_t *parse_table;
2021-03-04 22:15:19 +00:00
const uint16_t *small_parse_table;
const uint32_t *small_parse_table_map;
2017-07-14 19:28:54 +00:00
const TSParseActionEntry *parse_actions;
2021-03-04 22:15:19 +00:00
const char **symbol_names;
const char **field_names;
const TSFieldMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
2017-08-01 17:12:23 +00:00
const TSSymbol *alias_sequences;
2021-03-04 22:15:19 +00:00
const TSLexMode *lex_modes;
2017-07-14 19:28:54 +00:00
bool (*lex_fn)(TSLexer *, TSStateId);
2018-03-28 18:18:32 +00:00
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
2017-07-14 19:28:54 +00:00
struct {
const bool *states;
const TSSymbol *symbol_map;
2019-06-19 00:00:42 +00:00
void *(*create)(void);
2017-07-14 19:28:54 +00:00
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
2017-08-01 17:12:23 +00:00
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
2017-07-14 19:28:54 +00:00
} external_scanner;
};
2017-07-14 19:28:54 +00:00
/*
* Lexer Macros
*/
#define START_LEXER() \
bool result = false; \
bool skip = false; \
2019-12-10 20:39:14 +00:00
bool eof = false; \
2017-07-14 19:28:54 +00:00
int32_t lookahead; \
goto start; \
2017-07-14 19:28:54 +00:00
next_state: \
lexer->advance(lexer, skip); \
start: \
skip = false; \
2017-07-14 19:28:54 +00:00
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
state = state_value; \
goto next_state; \
2017-07-14 19:28:54 +00:00
}
#define SKIP(state_value) \
{ \
skip = true; \
state = state_value; \
goto next_state; \
2017-07-14 19:28:54 +00:00
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
2019-12-10 20:39:14 +00:00
#define SMALL_STATE(id) id - LARGE_STATE_COUNT
2017-07-14 19:28:54 +00:00
#define STATE(id) id
2018-05-24 18:46:57 +00:00
2017-07-14 19:28:54 +00:00
#define ACTIONS(id) id
2021-03-04 22:15:19 +00:00
#define SHIFT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value \
} \
}}
2017-07-14 19:28:54 +00:00
2018-02-13 00:53:11 +00:00
#define SHIFT_REPEAT(state_value) \
2021-03-04 22:15:19 +00:00
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value, \
.repetition = true \
2018-02-13 00:53:11 +00:00
} \
2021-03-04 22:15:19 +00:00
}}
2017-07-14 19:28:54 +00:00
#define SHIFT_EXTRA() \
2021-03-04 22:15:19 +00:00
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.extra = true \
2017-07-14 19:28:54 +00:00
} \
2021-03-04 22:15:19 +00:00
}}
2017-07-14 19:28:54 +00:00
#define REDUCE(symbol_val, child_count_val, ...) \
2021-03-04 22:15:19 +00:00
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
}, \
}}
#define RECOVER() \
{{ \
.type = TSParseActionTypeRecover \
}}
#define ACCEPT_INPUT() \
{{ \
.type = TSParseActionTypeAccept \
}}
2017-07-14 19:28:54 +00:00
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_