From e344837e35002c93d8805f3c9e0b51e24cca5f8f Mon Sep 17 00:00:00 2001 From: Will Lillis Date: Tue, 14 Oct 2025 17:52:32 -0400 Subject: [PATCH] fix(rust): minor cleanup in generate code --- crates/generate/src/build_tables/build_parse_table.rs | 9 +++------ crates/generate/src/build_tables/minimize_parse_table.rs | 4 +--- crates/generate/src/build_tables/token_conflicts.rs | 2 +- crates/generate/src/prepare_grammar/expand_tokens.rs | 4 ++-- .../src/prepare_grammar/extract_default_aliases.rs | 4 +--- crates/generate/src/prepare_grammar/extract_tokens.rs | 2 +- lib/binding_rust/lib.rs | 6 +++--- 7 files changed, 12 insertions(+), 19 deletions(-) diff --git a/crates/generate/src/build_tables/build_parse_table.rs b/crates/generate/src/build_tables/build_parse_table.rs index c832157a..66f29609 100644 --- a/crates/generate/src/build_tables/build_parse_table.rs +++ b/crates/generate/src/build_tables/build_parse_table.rs @@ -81,7 +81,7 @@ pub enum ParseTableBuilderError { StateCount(usize), } -#[derive(Default, Debug, Serialize)] +#[derive(Default, Debug, Serialize, Error)] pub struct ConflictError { pub symbol_sequence: Vec, pub conflicting_lookahead: String, @@ -89,7 +89,7 @@ pub struct ConflictError { pub possible_resolutions: Vec, } -#[derive(Default, Debug, Serialize)] +#[derive(Default, Debug, Serialize, Error)] pub struct Interpretation { pub preceding_symbols: Vec, pub variable_name: String, @@ -108,7 +108,7 @@ pub enum Resolution { AddConflict { symbols: Vec }, } -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Error)] pub struct AmbiguousExtraError { pub parent_symbols: Vec, } @@ -238,9 +238,6 @@ impl std::fmt::Display for AmbiguousExtraError { } } -impl std::error::Error for ConflictError {} -impl std::error::Error for AmbiguousExtraError {} - impl<'a> ParseTableBuilder<'a> { fn new( syntax_grammar: &'a SyntaxGrammar, diff --git a/crates/generate/src/build_tables/minimize_parse_table.rs b/crates/generate/src/build_tables/minimize_parse_table.rs index 1d70625e..6c26f1c4 100644 --- a/crates/generate/src/build_tables/minimize_parse_table.rs +++ b/crates/generate/src/build_tables/minimize_parse_table.rs @@ -306,9 +306,7 @@ impl Minimizer<'_> { return true; } - for (i, action1) in actions1.iter().enumerate() { - let action2 = &actions2[i]; - + for (action1, action2) in actions1.iter().zip(actions2.iter()) { // Two shift actions are equivalent if their destinations are in the same group. if let ( ParseAction::Shift { diff --git a/crates/generate/src/build_tables/token_conflicts.rs b/crates/generate/src/build_tables/token_conflicts.rs index bacac1b4..d72effd4 100644 --- a/crates/generate/src/build_tables/token_conflicts.rs +++ b/crates/generate/src/build_tables/token_conflicts.rs @@ -28,7 +28,7 @@ pub struct TokenConflictMap<'a> { impl<'a> TokenConflictMap<'a> { /// Create a token conflict map based on a lexical grammar, which describes the structure - /// each token, and a `following_token` map, which indicates which tokens may be appear + /// of each token, and a `following_token` map, which indicates which tokens may be appear /// immediately after each other token. /// /// This analyzes the possible kinds of overlap between each pair of tokens and stores diff --git a/crates/generate/src/prepare_grammar/expand_tokens.rs b/crates/generate/src/prepare_grammar/expand_tokens.rs index 2762b41c..4d8b4f11 100644 --- a/crates/generate/src/prepare_grammar/expand_tokens.rs +++ b/crates/generate/src/prepare_grammar/expand_tokens.rs @@ -27,7 +27,7 @@ pub enum ExpandTokensError { "The rule `{0}` matches the empty string. Tree-sitter does not support syntactic rules that match the empty string unless they are used only as the grammar's start rule. - " +" )] EmptyString(String), #[error(transparent)] @@ -189,7 +189,7 @@ impl NfaBuilder { } Rule::String(s) => { for c in s.chars().rev() { - self.push_advance(CharacterSet::empty().add_char(c), next_state_id); + self.push_advance(CharacterSet::from_char(c), next_state_id); next_state_id = self.nfa.last_state_id(); } Ok(!s.is_empty()) diff --git a/crates/generate/src/prepare_grammar/extract_default_aliases.rs b/crates/generate/src/prepare_grammar/extract_default_aliases.rs index 68ea1e48..cc977362 100644 --- a/crates/generate/src/prepare_grammar/extract_default_aliases.rs +++ b/crates/generate/src/prepare_grammar/extract_default_aliases.rs @@ -69,9 +69,7 @@ pub(super) fn extract_default_aliases( SymbolType::External => &mut external_status_list[symbol.index], SymbolType::NonTerminal => &mut non_terminal_status_list[symbol.index], SymbolType::Terminal => &mut terminal_status_list[symbol.index], - SymbolType::End | SymbolType::EndOfNonTerminalExtra => { - panic!("Unexpected end token") - } + SymbolType::End | SymbolType::EndOfNonTerminalExtra => panic!("Unexpected end token"), }; status.appears_unaliased = true; } diff --git a/crates/generate/src/prepare_grammar/extract_tokens.rs b/crates/generate/src/prepare_grammar/extract_tokens.rs index b74a8e84..a1fddbe6 100644 --- a/crates/generate/src/prepare_grammar/extract_tokens.rs +++ b/crates/generate/src/prepare_grammar/extract_tokens.rs @@ -153,7 +153,7 @@ pub(super) fn extract_tokens( } } - let mut external_tokens = Vec::new(); + let mut external_tokens = Vec::with_capacity(grammar.external_tokens.len()); for external_token in grammar.external_tokens { let rule = symbol_replacer.replace_symbols_in_rule(&external_token.rule); if let Rule::Symbol(symbol) = rule { diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index cf22eb72..a02fa173 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -3190,9 +3190,9 @@ impl QueryCursor { /// The zero max start depth value can be used as a special behavior and /// it helps to destructure a subtree by staying on a node and using /// captures for interested parts. Note that the zero max start depth - /// only limit a search depth for a pattern's root node but other nodes - /// that are parts of the pattern may be searched at any depth what - /// defined by the pattern structure. + /// only limits a search depth for a pattern's root node but other nodes + /// that are parts of the pattern may be searched at any depth depending on + /// what is defined by the pattern structure. /// /// Set to `None` to remove the maximum start depth. #[doc(alias = "ts_query_cursor_set_max_start_depth")]