diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 65e051d4..a4b3d145 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,6 +52,9 @@ jobs: toolchain: stable profile: minimal + - name: Check Rust code formatting + run: cargo fmt -- --check + - name: Install emscripten uses: mymindstorm/setup-emsdk@v7 with: diff --git a/Cargo.lock b/Cargo.lock index 26bfdb1a..f735619c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -634,7 +634,7 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tree-sitter" -version = "0.19.4" +version = "0.19.5" dependencies = [ "cc", "lazy_static", @@ -644,7 +644,7 @@ dependencies = [ [[package]] name = "tree-sitter-cli" -version = "0.19.4" +version = "0.19.5" dependencies = [ "ansi_term 0.12.1", "atty", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index a1884d2c..693ac817 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tree-sitter-cli" description = "CLI tool for developing, testing, and using Tree-sitter parsers" -version = "0.19.4" +version = "0.19.5" authors = ["Max Brunsfeld "] edition = "2018" license = "MIT" diff --git a/cli/npm/package.json b/cli/npm/package.json index 1d0fe136..2c5e364a 100644 --- a/cli/npm/package.json +++ b/cli/npm/package.json @@ -1,6 +1,6 @@ { "name": "tree-sitter-cli", - "version": "0.19.4", + "version": "0.19.5", "author": "Max Brunsfeld", "license": "MIT", "repository": { diff --git a/cli/src/generate/binding_files.rs b/cli/src/generate/binding_files.rs index ed56fcde..0a55e35a 100644 --- a/cli/src/generate/binding_files.rs +++ b/cli/src/generate/binding_files.rs @@ -95,7 +95,11 @@ pub fn generate_binding_files(repo_path: &Path, language_name: &str) -> Result<( write_file(&package_json_path, package_json_str)?; } } else { - generate_file(&package_json_path, PACKAGE_JSON_TEMPLATE, dashed_language_name)?; + generate_file( + &package_json_path, + PACKAGE_JSON_TEMPLATE, + dashed_language_name, + )?; } // Remove files from old node binding paths. diff --git a/cli/src/generate/render.rs b/cli/src/generate/render.rs index 9d22f4a3..78a07a22 100644 --- a/cli/src/generate/render.rs +++ b/cli/src/generate/render.rs @@ -355,7 +355,7 @@ impl Generator { } fn add_symbol_names_list(&mut self) { - add_line!(self, "static const char *ts_symbol_names[] = {{"); + add_line!(self, "static const char * const ts_symbol_names[] = {{"); indent!(self); for symbol in self.parse_table.symbols.iter() { let name = self.sanitize_string( @@ -417,7 +417,7 @@ impl Generator { } fn add_field_name_names_list(&mut self) { - add_line!(self, "static const char *ts_field_names[] = {{"); + add_line!(self, "static const char * const ts_field_names[] = {{"); indent!(self); add_line!(self, "[0] = NULL,"); for field_name in &self.field_names { @@ -543,7 +543,10 @@ impl Generator { let mut alias_ids_by_symbol = alias_ids_by_symbol.iter().collect::>(); alias_ids_by_symbol.sort_unstable_by_key(|e| e.0); - add_line!(self, "static const uint16_t ts_non_terminal_alias_map[] = {{"); + add_line!( + self, + "static const uint16_t ts_non_terminal_alias_map[] = {{" + ); indent!(self); for (symbol, alias_ids) in alias_ids_by_symbol { let symbol_id = &self.symbol_ids[symbol]; @@ -962,7 +965,10 @@ impl Generator { } fn add_lex_modes_list(&mut self) { - add_line!(self, "static const TSLexMode ts_lex_modes[STATE_COUNT] = {{"); + add_line!( + self, + "static const TSLexMode ts_lex_modes[STATE_COUNT] = {{" + ); indent!(self); for (i, state) in self.parse_table.states.iter().enumerate() { if state.is_end_of_non_terminal_extra() { @@ -1200,7 +1206,10 @@ impl Generator { add_line!(self, "}};"); add_line!(self, ""); - add_line!(self, "static const uint32_t ts_small_parse_table_map[] = {{"); + add_line!( + self, + "static const uint32_t ts_small_parse_table_map[] = {{" + ); indent!(self); for i in self.large_state_count..self.parse_table.states.len() { add_line!( @@ -1219,7 +1228,10 @@ impl Generator { } fn add_parse_action_list(&mut self, parse_table_entries: Vec<(usize, ParseTableEntry)>) { - add_line!(self, "static const TSParseActionEntry ts_parse_actions[] = {{"); + add_line!( + self, + "static const TSParseActionEntry ts_parse_actions[] = {{" + ); indent!(self); for (i, entry) in parse_table_entries { add!( @@ -1334,14 +1346,8 @@ impl Generator { // Parse table add_line!(self, ".parse_table = &ts_parse_table[0][0],"); if self.large_state_count < self.parse_table.states.len() { - add_line!( - self, - ".small_parse_table = ts_small_parse_table," - ); - add_line!( - self, - ".small_parse_table_map = ts_small_parse_table_map," - ); + add_line!(self, ".small_parse_table = ts_small_parse_table,"); + add_line!(self, ".small_parse_table_map = ts_small_parse_table_map,"); } add_line!(self, ".parse_actions = ts_parse_actions,"); @@ -1349,23 +1355,14 @@ impl Generator { add_line!(self, ".symbol_names = ts_symbol_names,"); if !self.field_names.is_empty() { add_line!(self, ".field_names = ts_field_names,"); - add_line!( - self, - ".field_map_slices = ts_field_map_slices," - ); - add_line!( - self, - ".field_map_entries = ts_field_map_entries," - ); + add_line!(self, ".field_map_slices = ts_field_map_slices,"); + add_line!(self, ".field_map_entries = ts_field_map_entries,"); } add_line!(self, ".symbol_metadata = ts_symbol_metadata,"); add_line!(self, ".public_symbol_map = ts_symbol_map,"); add_line!(self, ".alias_map = ts_non_terminal_alias_map,"); if !self.parse_table.production_infos.is_empty() { - add_line!( - self, - ".alias_sequences = &ts_alias_sequences[0][0]," - ); + add_line!(self, ".alias_sequences = &ts_alias_sequences[0][0],"); } // Lexing diff --git a/cli/src/main.rs b/cli/src/main.rs index e7600b30..a2d0a7da 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -122,7 +122,12 @@ fn run() -> error::Result<()> { .takes_value(true) .help("Only run corpus test cases whose name includes the given string"), ) - .arg(Arg::with_name("update").long("update").short("u").help("Update all syntax trees in corpus files with current parser output")) + .arg( + Arg::with_name("update") + .long("update") + .short("u") + .help("Update all syntax trees in corpus files with current parser output"), + ) .arg(Arg::with_name("debug").long("debug").short("d")) .arg(Arg::with_name("debug-graph").long("debug-graph").short("D")), ) diff --git a/cli/src/parse.rs b/cli/src/parse.rs index 5266b19f..e843663e 100644 --- a/cli/src/parse.rs +++ b/cli/src/parse.rs @@ -197,7 +197,8 @@ pub fn parse_file_at_path( did_visit_children = true; let start = node.start_byte(); let end = node.end_byte(); - let value = std::str::from_utf8(&source_code[start..end]).expect("has a string"); + let value = + std::str::from_utf8(&source_code[start..end]).expect("has a string"); write!(&mut stdout, "{}", html_escape::encode_text(value))?; } } diff --git a/cli/src/tests/helpers/random.rs b/cli/src/tests/helpers/random.rs index b0490e7e..4fc8f989 100644 --- a/cli/src/tests/helpers/random.rs +++ b/cli/src/tests/helpers/random.rs @@ -13,7 +13,7 @@ impl Rand { } pub fn unsigned(&mut self, max: usize) -> usize { - self.0.gen_range(0 .. max + 1) + self.0.gen_range(0..max + 1) } pub fn words(&mut self, max_count: usize) -> Vec { diff --git a/cli/src/tests/helpers/scope_sequence.rs b/cli/src/tests/helpers/scope_sequence.rs index 685fe91f..2f904025 100644 --- a/cli/src/tests/helpers/scope_sequence.rs +++ b/cli/src/tests/helpers/scope_sequence.rs @@ -47,7 +47,10 @@ impl ScopeSequence { if self.0.len() != text.len() { panic!( "Inconsistent scope sequence: {:?}", - self.0.iter().zip(text.iter().map(|c| *c as char)).collect::>() + self.0 + .iter() + .zip(text.iter().map(|c| *c as char)) + .collect::>() ); } diff --git a/docs/index.md b/docs/index.md index b5538c55..a773437b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -69,6 +69,7 @@ Parsers for these languages are in development: * [Go mod](https://github.com/camdencheek/tree-sitter-go-mod) * [Haskell](https://github.com/tree-sitter/tree-sitter-haskell) * [Julia](https://github.com/tree-sitter/tree-sitter-julia) +* [Kotlin](https://github.com/fwcd/tree-sitter-kotlin) * [Nix](https://github.com/cstrahan/tree-sitter-nix) * [Perl](https://github.com/ganezdragon/tree-sitter-perl) * [Scala](https://github.com/tree-sitter/tree-sitter-scala) diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 8e6fdced..dadc0367 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tree-sitter" description = "Rust bindings to the Tree-sitter parsing library" -version = "0.19.4" +version = "0.19.5" authors = ["Max Brunsfeld "] edition = "2018" license = "MIT" diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 7a8c7e24..22a0c2c1 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -1942,7 +1942,8 @@ impl error::Error for QueryError {} unsafe impl Send for Language {} unsafe impl Send for Parser {} unsafe impl Send for Query {} -unsafe impl Send for Tree {} unsafe impl Send for QueryCursor {} +unsafe impl Send for Tree {} unsafe impl Sync for Language {} unsafe impl Sync for Query {} +unsafe impl Sync for Tree {} diff --git a/lib/binding_web/exports.json b/lib/binding_web/exports.json index 67787b25..01f93434 100644 --- a/lib/binding_web/exports.json +++ b/lib/binding_web/exports.json @@ -26,7 +26,6 @@ "_memcpy", "_strlen", "_towupper", - "abort", "_ts_init", "_ts_language_field_count", diff --git a/lib/binding_web/package.json b/lib/binding_web/package.json index a3d11b55..b63a4072 100644 --- a/lib/binding_web/package.json +++ b/lib/binding_web/package.json @@ -1,6 +1,6 @@ { "name": "web-tree-sitter", - "version": "0.19.3", + "version": "0.19.4", "description": "Tree-sitter bindings for the web", "main": "tree-sitter.js", "types": "tree-sitter-web.d.ts", diff --git a/lib/include/tree_sitter/parser.h b/lib/include/tree_sitter/parser.h index a3a87bd1..cbbc7b4e 100644 --- a/lib/include/tree_sitter/parser.h +++ b/lib/include/tree_sitter/parser.h @@ -102,8 +102,8 @@ struct TSLanguage { const uint16_t *small_parse_table; const uint32_t *small_parse_table_map; const TSParseActionEntry *parse_actions; - const char **symbol_names; - const char **field_names; + const char * const *symbol_names; + const char * const *field_names; const TSFieldMapSlice *field_map_slices; const TSFieldMapEntry *field_map_entries; const TSSymbolMetadata *symbol_metadata; diff --git a/lib/src/node.c b/lib/src/node.c index ad360cfa..156b9a18 100644 --- a/lib/src/node.c +++ b/lib/src/node.c @@ -150,9 +150,6 @@ static inline TSNode ts_node__child( while (ts_node_child_iterator_next(&iterator, &child)) { if (ts_node__is_relevant(child, include_anonymous)) { if (index == child_index) { - if (ts_node__is_relevant(self, true)) { - ts_tree_set_cached_parent(self.tree, &child, &self); - } return child; } index++; @@ -355,7 +352,6 @@ static inline TSNode ts_node__descendant_for_byte_range( node = child; if (ts_node__is_relevant(node, include_anonymous)) { - ts_tree_set_cached_parent(self.tree, &child, &last_visible_node); last_visible_node = node; } did_descend = true; @@ -395,7 +391,6 @@ static inline TSNode ts_node__descendant_for_point_range( node = child; if (ts_node__is_relevant(node, include_anonymous)) { - ts_tree_set_cached_parent(self.tree, &child, &last_visible_node); last_visible_node = node; } did_descend = true; @@ -464,10 +459,7 @@ bool ts_node_has_error(TSNode self) { } TSNode ts_node_parent(TSNode self) { - TSNode node = ts_tree_get_cached_parent(self.tree, &self); - if (node.id) return node; - - node = ts_tree_root_node(self.tree); + TSNode node = ts_tree_root_node(self.tree); uint32_t end_byte = ts_node_end_byte(self); if (node.id == self.id) return ts_node__null(); @@ -486,7 +478,6 @@ TSNode ts_node_parent(TSNode self) { if (iterator.position.bytes >= end_byte) { node = child; if (ts_node__is_relevant(child, true)) { - ts_tree_set_cached_parent(self.tree, &node, &last_visible_node); last_visible_node = node; } did_descend = true; diff --git a/lib/src/tree.c b/lib/src/tree.c index 391fa7f5..f2cc85ef 100644 --- a/lib/src/tree.c +++ b/lib/src/tree.c @@ -5,8 +5,6 @@ #include "./tree_cursor.h" #include "./tree.h" -static const unsigned PARENT_CACHE_CAPACITY = 32; - TSTree *ts_tree_new( Subtree root, const TSLanguage *language, const TSRange *included_ranges, unsigned included_range_count @@ -14,9 +12,6 @@ TSTree *ts_tree_new( TSTree *result = ts_malloc(sizeof(TSTree)); result->root = root; result->language = language; - result->parent_cache = NULL; - result->parent_cache_start = 0; - result->parent_cache_size = 0; result->included_ranges = ts_calloc(included_range_count, sizeof(TSRange)); memcpy(result->included_ranges, included_ranges, included_range_count * sizeof(TSRange)); result->included_range_count = included_range_count; @@ -35,7 +30,6 @@ void ts_tree_delete(TSTree *self) { ts_subtree_release(&pool, self->root); ts_subtree_pool_delete(&pool); ts_free(self->included_ranges); - if (self->parent_cache) ts_free(self->parent_cache); ts_free(self); } @@ -78,8 +72,6 @@ void ts_tree_edit(TSTree *self, const TSInputEdit *edit) { SubtreePool pool = ts_subtree_pool_new(0); self->root = ts_subtree_edit(self->root, edit, &pool); - self->parent_cache_start = 0; - self->parent_cache_size = 0; ts_subtree_pool_delete(&pool); } @@ -111,38 +103,3 @@ TSRange *ts_tree_get_changed_ranges(const TSTree *self, const TSTree *other, uin void ts_tree_print_dot_graph(const TSTree *self, FILE *file) { ts_subtree_print_dot_graph(self->root, self->language, file); } - -TSNode ts_tree_get_cached_parent(const TSTree *self, const TSNode *node) { - for (uint32_t i = 0; i < self->parent_cache_size; i++) { - uint32_t index = (self->parent_cache_start + i) % PARENT_CACHE_CAPACITY; - ParentCacheEntry *entry = &self->parent_cache[index]; - if (entry->child == node->id) { - return ts_node_new(self, entry->parent, entry->position, entry->alias_symbol); - } - } - return ts_node_new(NULL, NULL, length_zero(), 0); -} - -void ts_tree_set_cached_parent(const TSTree *_self, const TSNode *node, const TSNode *parent) { - TSTree *self = (TSTree *)_self; - if (!self->parent_cache) { - self->parent_cache = ts_calloc(PARENT_CACHE_CAPACITY, sizeof(ParentCacheEntry)); - } - - uint32_t index = (self->parent_cache_start + self->parent_cache_size) % PARENT_CACHE_CAPACITY; - self->parent_cache[index] = (ParentCacheEntry) { - .child = node->id, - .parent = (const Subtree *)parent->id, - .position = { - parent->context[0], - {parent->context[1], parent->context[2]} - }, - .alias_symbol = parent->context[3], - }; - - if (self->parent_cache_size == PARENT_CACHE_CAPACITY) { - self->parent_cache_start++; - } else { - self->parent_cache_size++; - } -} diff --git a/lib/src/tree.h b/lib/src/tree.h index 92a7e641..0334b824 100644 --- a/lib/src/tree.h +++ b/lib/src/tree.h @@ -15,17 +15,12 @@ typedef struct { struct TSTree { Subtree root; const TSLanguage *language; - ParentCacheEntry *parent_cache; - uint32_t parent_cache_start; - uint32_t parent_cache_size; TSRange *included_ranges; unsigned included_range_count; }; TSTree *ts_tree_new(Subtree root, const TSLanguage *language, const TSRange *, unsigned); TSNode ts_node_new(const TSTree *, const Subtree *, Length, TSSymbol); -TSNode ts_tree_get_cached_parent(const TSTree *, const TSNode *); -void ts_tree_set_cached_parent(const TSTree *, const TSNode *, const TSNode *); #ifdef __cplusplus }