From b9baa0548afcfbf4882c5e63a03cc25c4f70de5a Mon Sep 17 00:00:00 2001 From: Ika Date: Sun, 14 Mar 2021 19:26:37 +0800 Subject: [PATCH] feat: upgrade to tree-sitter@0.19.3 BREAKING CHANGE: require tree-sitter 0.19+ --- .gitignore | 3 + .gitmodules | 2 +- binding.gyp | 2 +- {src => bindings/node}/binding.cc | 0 bindings/node/index.js | 19 ++++ index.js | 13 --- package.json | 7 +- src/grammar.json | 1 + src/parser.c | 28 +++--- src/tree_sitter/parser.h | 159 ++++++++++++++---------------- tree-sitter | 2 +- 11 files changed, 116 insertions(+), 120 deletions(-) rename {src => bindings/node}/binding.cc (100%) create mode 100644 bindings/node/index.js delete mode 100644 index.js diff --git a/.gitignore b/.gitignore index 7d5b7a9..39e4380 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ /build /node_modules + +/bindings/rust +/Cargo.toml diff --git a/.gitmodules b/.gitmodules index 738fb83..922f976 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "tree-sitter"] path = tree-sitter url = https://github.com/ikatyang/tree-sitter - branch = 0.18.0-custom + branch = v0.19.3-custom diff --git a/binding.gyp b/binding.gyp index d75fbbf..9234312 100644 --- a/binding.gyp +++ b/binding.gyp @@ -8,7 +8,7 @@ ], "sources": [ "src/parser.c", - "src/binding.cc", + "bindings/node/binding.cc", "src/scanner.cc" ], "cflags_c": [ diff --git a/src/binding.cc b/bindings/node/binding.cc similarity index 100% rename from src/binding.cc rename to bindings/node/binding.cc diff --git a/bindings/node/index.js b/bindings/node/index.js new file mode 100644 index 0000000..799f8fe --- /dev/null +++ b/bindings/node/index.js @@ -0,0 +1,19 @@ +try { + module.exports = require("../../build/Release/tree_sitter_markdown_binding"); +} catch (error1) { + if (error1.code !== 'MODULE_NOT_FOUND') { + throw error1; + } + try { + module.exports = require("../../build/Debug/tree_sitter_markdown_binding"); + } catch (error2) { + if (error2.code !== 'MODULE_NOT_FOUND') { + throw error2; + } + throw error1 + } +} + +try { + module.exports.nodeTypeInfo = require("../../src/node-types.json"); +} catch (_) {} diff --git a/index.js b/index.js deleted file mode 100644 index caf17a1..0000000 --- a/index.js +++ /dev/null @@ -1,13 +0,0 @@ -try { - module.exports = require("./build/Release/tree_sitter_markdown_binding"); -} catch (error) { - try { - module.exports = require("./build/Debug/tree_sitter_markdown_binding"); - } catch (_) { - throw error - } -} - -try { - module.exports.nodeTypeInfo = require("./src/node-types.json"); -} catch (_) {} diff --git a/package.json b/package.json index 65d10bd..377fa78 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "parser", "lexer" ], - "main": "index.js", + "main": "bindings/node", "repository": "https://github.com/ikatyang/tree-sitter-markdown", "homepage": "https://github.com/ikatyang/tree-sitter-markdown#readme", "author": { @@ -36,8 +36,7 @@ }, "files": [ "/src/", - "/binding.gyp", - "/grammar.js", - "/index.js" + "/bindings/node/", + "/grammar.js" ] } diff --git a/src/grammar.json b/src/grammar.json index 295cece..5275174 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -3200,6 +3200,7 @@ "_fen_cod_ctn" ] ], + "precedences": [], "externals": [ { "type": "SYMBOL", diff --git a/src/parser.c b/src/parser.c index d856c02..fbe1b4b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -5,7 +5,7 @@ #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -#define LANGUAGE_VERSION 12 +#define LANGUAGE_VERSION 13 #define STATE_COUNT 1009 #define LARGE_STATE_COUNT 4 #define SYMBOL_COUNT 228 @@ -14,6 +14,7 @@ #define EXTERNAL_TOKEN_COUNT 118 #define FIELD_COUNT 0 #define MAX_ALIAS_SEQUENCE_LENGTH 7 +#define PRODUCTION_ID_COUNT 14 enum { sym__eof = 1, @@ -1685,7 +1686,7 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { }, }; -static TSSymbol ts_alias_sequences[14][MAX_ALIAS_SEQUENCE_LENGTH] = { +static TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGTH] = { [0] = {0}, [1] = { [0] = alias_sym_line_break, @@ -35331,13 +35332,21 @@ extern const TSLanguage *tree_sitter_markdown(void) { .alias_count = ALIAS_COUNT, .token_count = TOKEN_COUNT, .external_token_count = EXTERNAL_TOKEN_COUNT, - .symbol_names = ts_symbol_names, - .symbol_metadata = ts_symbol_metadata, + .state_count = STATE_COUNT, + .large_state_count = LARGE_STATE_COUNT, + .production_id_count = PRODUCTION_ID_COUNT, + .field_count = FIELD_COUNT, + .max_alias_sequence_length = MAX_ALIAS_SEQUENCE_LENGTH, .parse_table = (const uint16_t *)ts_parse_table, + .small_parse_table = (const uint16_t *)ts_small_parse_table, + .small_parse_table_map = (const uint32_t *)ts_small_parse_table_map, .parse_actions = ts_parse_actions, - .lex_modes = ts_lex_modes, + .symbol_names = ts_symbol_names, + .symbol_metadata = ts_symbol_metadata, + .public_symbol_map = ts_symbol_map, + .alias_map = ts_non_terminal_alias_map, .alias_sequences = (const TSSymbol *)ts_alias_sequences, - .max_alias_sequence_length = MAX_ALIAS_SEQUENCE_LENGTH, + .lex_modes = ts_lex_modes, .lex_fn = ts_lex, .external_scanner = { (const bool *)ts_external_scanner_states, @@ -35348,13 +35357,6 @@ extern const TSLanguage *tree_sitter_markdown(void) { tree_sitter_markdown_external_scanner_serialize, tree_sitter_markdown_external_scanner_deserialize, }, - .field_count = FIELD_COUNT, - .large_state_count = LARGE_STATE_COUNT, - .small_parse_table = (const uint16_t *)ts_small_parse_table, - .small_parse_table_map = (const uint32_t *)ts_small_parse_table_map, - .public_symbol_map = ts_symbol_map, - .alias_map = ts_non_terminal_alias_map, - .state_count = STATE_COUNT, }; return &language; } diff --git a/src/tree_sitter/parser.h b/src/tree_sitter/parser.h index c5a788f..a3a87bd 100644 --- a/src/tree_sitter/parser.h +++ b/src/tree_sitter/parser.h @@ -13,6 +13,8 @@ extern "C" { #define ts_builtin_sym_end 0 #define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 +typedef uint16_t TSStateId; + #ifndef TREE_SITTER_API_H_ typedef uint16_t TSSymbol; typedef uint16_t TSFieldId; @@ -30,12 +32,10 @@ typedef struct { uint16_t length; } TSFieldMapSlice; -typedef uint16_t TSStateId; - typedef struct { - bool visible : 1; - bool named : 1; - bool supertype: 1; + bool visible; + bool named; + bool supertype; } TSSymbolMetadata; typedef struct TSLexer TSLexer; @@ -57,21 +57,21 @@ typedef enum { TSParseActionTypeRecover, } TSParseActionType; -typedef struct { - union { - struct { - TSStateId state; - bool extra : 1; - bool repetition : 1; - } shift; - struct { - TSSymbol symbol; - int16_t dynamic_precedence; - uint8_t child_count; - uint8_t production_id; - } reduce; - } params; - TSParseActionType type : 4; +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; } TSParseAction; typedef struct { @@ -83,7 +83,7 @@ typedef union { TSParseAction action; struct { uint8_t count; - bool reusable : 1; + bool reusable; } entry; } TSParseActionEntry; @@ -93,13 +93,24 @@ struct TSLanguage { uint32_t alias_count; uint32_t token_count; uint32_t external_token_count; - const char **symbol_names; - const TSSymbolMetadata *symbol_metadata; + uint32_t state_count; + uint32_t large_state_count; + uint32_t production_id_count; + uint32_t field_count; + uint16_t max_alias_sequence_length; const uint16_t *parse_table; + const uint16_t *small_parse_table; + const uint32_t *small_parse_table_map; const TSParseActionEntry *parse_actions; - const TSLexMode *lex_modes; + 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; const TSSymbol *alias_sequences; - uint16_t max_alias_sequence_length; + const TSLexMode *lex_modes; bool (*lex_fn)(TSLexer *, TSStateId); bool (*keyword_lex_fn)(TSLexer *, TSStateId); TSSymbol keyword_capture_token; @@ -112,16 +123,6 @@ struct TSLanguage { unsigned (*serialize)(void *, char *); void (*deserialize)(void *, const char *, unsigned); } external_scanner; - uint32_t field_count; - const TSFieldMapSlice *field_map_slices; - const TSFieldMapEntry *field_map_entries; - const char **field_names; - uint32_t large_state_count; - const uint16_t *small_parse_table; - const uint32_t *small_parse_table_map; - const TSSymbol *public_symbol_map; - const uint16_t *alias_map; - uint32_t state_count; }; /* @@ -170,66 +171,50 @@ struct TSLanguage { #define ACTIONS(id) id -#define SHIFT(state_value) \ - { \ - { \ - .params = { \ - .shift = { \ - .state = state_value \ - } \ - }, \ - .type = TSParseActionTypeShift \ - } \ - } +#define SHIFT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = state_value \ + } \ + }} #define SHIFT_REPEAT(state_value) \ - { \ - { \ - .params = { \ - .shift = { \ - .state = state_value, \ - .repetition = true \ - } \ - }, \ - .type = TSParseActionTypeShift \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = state_value, \ + .repetition = true \ } \ - } - -#define RECOVER() \ - { \ - { .type = TSParseActionTypeRecover } \ - } + }} #define SHIFT_EXTRA() \ - { \ - { \ - .params = { \ - .shift = { \ - .extra = true \ - } \ - }, \ - .type = TSParseActionTypeShift \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .extra = true \ } \ - } + }} #define REDUCE(symbol_val, child_count_val, ...) \ - { \ - { \ - .params = { \ - .reduce = { \ - .symbol = symbol_val, \ - .child_count = child_count_val, \ - __VA_ARGS__ \ - }, \ - }, \ - .type = TSParseActionTypeReduce \ - } \ - } - -#define ACCEPT_INPUT() \ - { \ - { .type = TSParseActionTypeAccept } \ - } + {{ \ + .reduce = { \ + .type = TSParseActionTypeReduce, \ + .symbol = symbol_val, \ + .child_count = child_count_val, \ + __VA_ARGS__ \ + }, \ + }} + +#define RECOVER() \ + {{ \ + .type = TSParseActionTypeRecover \ + }} + +#define ACCEPT_INPUT() \ + {{ \ + .type = TSParseActionTypeAccept \ + }} #ifdef __cplusplus } diff --git a/tree-sitter b/tree-sitter index 514f755..fc5a692 160000 --- a/tree-sitter +++ b/tree-sitter @@ -1 +1 @@ -Subproject commit 514f75506394d64b3fa7eac3d73fa45eb05d8f67 +Subproject commit fc5a692b7d5f144cc531f5823b1c5a3b2f06cf01