From ee9cf33689a7b63959ffd9f35f940752aeefecb4 Mon Sep 17 00:00:00 2001 From: Peter Ammon Date: Sun, 27 Apr 2025 11:58:42 -0700 Subject: [PATCH] ast: Remove Node::category This is now unused. --- src/ast.rs | 59 ------------------------------------- src/builtins/fish_indent.rs | 42 +++++++++++++------------- src/parse_util.rs | 6 ++-- src/reader.rs | 6 ++-- 4 files changed, 27 insertions(+), 86 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index e594005e9..646fec525 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -99,11 +99,6 @@ pub trait Node: Acceptor + ConcreteNode + AsNode + std::fmt::Debug { /// The type of this node. fn typ(&self) -> Type; - /// The category of this node. - fn category(&self) -> Category { - self.typ().category() - } - /// Return a helpful string description of this node. fn describe(&self) -> WString { let mut res = ast_type_to_string(self.typ()).to_owned(); @@ -3573,13 +3568,6 @@ fn test_ast_parse() { assert!(!ast.any_error); } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum Category { - branch, - leaf, - list, -} - #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum Type { token_base, @@ -3622,50 +3610,3 @@ pub enum Type { argument_list, job_list, } - -impl Type { - // Return the underlying category of this type. - pub fn category(self) -> Category { - match self { - Type::token_base => Category::leaf, - Type::keyword_base => Category::leaf, - Type::redirection => Category::branch, - Type::variable_assignment => Category::leaf, - Type::variable_assignment_list => Category::list, - Type::argument_or_redirection => Category::branch, - Type::argument_or_redirection_list => Category::list, - Type::statement => Category::branch, - Type::job_pipeline => Category::branch, - Type::job_conjunction => Category::branch, - Type::for_header => Category::branch, - Type::while_header => Category::branch, - Type::function_header => Category::branch, - Type::begin_header => Category::branch, - Type::block_statement => Category::branch, - Type::brace_statement => Category::branch, - Type::if_clause => Category::branch, - Type::elseif_clause => Category::branch, - Type::elseif_clause_list => Category::list, - Type::else_clause => Category::branch, - Type::if_statement => Category::branch, - Type::case_item => Category::branch, - Type::switch_statement => Category::branch, - Type::decorated_statement => Category::branch, - Type::not_statement => Category::branch, - Type::job_continuation => Category::branch, - Type::job_continuation_list => Category::list, - Type::job_conjunction_continuation => Category::branch, - Type::andor_job => Category::branch, - Type::andor_job_list => Category::list, - Type::freestanding_argument_list => Category::branch, // not a typo, wraps a list - Type::token_conjunction => Category::leaf, - Type::job_conjunction_continuation_list => Category::list, - Type::maybe_newlines => Category::leaf, - Type::token_pipe => Category::leaf, - Type::case_item_list => Category::list, - Type::argument => Category::leaf, - Type::argument_list => Category::list, - Type::job_list => Category::list, - } - } -} diff --git a/src/builtins/fish_indent.rs b/src/builtins/fish_indent.rs index 66ea4373b..e6bb487e7 100644 --- a/src/builtins/fish_indent.rs +++ b/src/builtins/fish_indent.rs @@ -15,7 +15,7 @@ use libc::LC_ALL; use super::prelude::*; -use crate::ast::{self, Ast, Category, Leaf, Node, NodeVisitor, SourceRangeList, Traversal, Type}; +use crate::ast::{self, Ast, Leaf, Node, NodeVisitor, SourceRangeList, Traversal, Type}; use crate::common::{ str2wcstring, unescape_string, wcs2string, UnescapeFlags, UnescapeStringStyle, PROGRAM_NAME, }; @@ -94,7 +94,6 @@ struct AstSizeMetrics { /// Note tokens and keywords are also counted as leaves. branch_count: usize, leaf_count: usize, - list_count: usize, token_count: usize, keyword_count: usize, // An estimate of the total allocated size of the ast in bytes. @@ -107,7 +106,6 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, " nodes: {}", self.node_count)?; writeln!(f, " branches: {}", self.branch_count)?; writeln!(f, " leaves: {}", self.leaf_count)?; - writeln!(f, " lists: {}", self.list_count)?; writeln!(f, " tokens: {}", self.token_count)?; writeln!(f, " keywords: {}", self.keyword_count)?; @@ -125,10 +123,10 @@ impl<'a> NodeVisitor<'a> for AstSizeMetrics { fn visit(&mut self, node: &'a dyn Node) { self.node_count += 1; self.memory_size += node.self_memory_size(); - match node.category() { - Category::branch => self.branch_count += 1, - Category::leaf => self.leaf_count += 1, - Category::list => self.list_count += 1, + if node.as_leaf().is_some() { + self.leaf_count += 1; + } else { + self.branch_count += 1; // treating lists as branches } if node.as_token().is_some() { self.token_count += 1; @@ -220,7 +218,7 @@ fn compute_gaps(&self) -> Vec { // Collect the token ranges into a list. let mut tok_ranges = vec![]; for node in Traversal::new(self.ast.top()) { - if node.category() == Category::leaf { + if let Some(node) = node.as_leaf() { let r = node.source_range(); if r.length() > 0 { tok_ranges.push(r); @@ -820,6 +818,7 @@ fn visit_begin_header(&mut self, node: &ast::BeginHeader) { // Prettify our ast traversal, populating the output. fn prettify_traversal(&mut self) { + use ast::Kind; while let Some(node) = self.traversal.next() { // Leaf nodes we just visit their text. if node.as_keyword().is_some() { @@ -835,29 +834,30 @@ fn prettify_traversal(&mut self) { } continue; } - match node.typ() { - Type::argument | Type::variable_assignment => { + + match node.kind() { + Kind::Argument(_) | Kind::VariableAssignment(_) => { self.emit_node_text(node); self.traversal.skip_children(node); } - Type::redirection => { - self.visit_redirection(node.as_redirection().unwrap()); + Kind::Redirection(node) => { + self.visit_redirection(node); self.traversal.skip_children(node); } - Type::maybe_newlines => { - self.visit_maybe_newlines(node.as_maybe_newlines().unwrap()); + Kind::MaybeNewlines(node) => { + self.visit_maybe_newlines(node); self.traversal.skip_children(node); } - Type::begin_header => { - self.visit_begin_header(node.as_begin_header().unwrap()); + Kind::BeginHeader(node) => { + self.visit_begin_header(node); self.traversal.skip_children(node); } _ => { - // For branch and list nodes, default is to visit their children. - if [Category::branch, Category::list].contains(&node.category()) { - continue; - } - panic!("unexpected node type"); + // Default is to visit children. We expect all leaves to have been handled above. + assert!( + node.as_leaf().is_none(), + "Should have handled all leaf nodes" + ); } } } diff --git a/src/parse_util.rs b/src/parse_util.rs index de17975be..cfc065e15 100644 --- a/src/parse_util.rs +++ b/src/parse_util.rs @@ -995,7 +995,7 @@ impl<'a> NodeVisitor<'a> for IndentVisitor<'a> { fn visit(&mut self, node: &'a dyn Node) { let mut inc = 0; let mut dec = 0; - use ast::{Category, Type}; + use ast::Type; match node.typ() { Type::job_list | Type::andor_job_list => { // Job lists are never unwound. @@ -1079,7 +1079,7 @@ fn visit(&mut self, node: &'a dyn Node) { } let range = node.source_range(); - if range.length() > 0 && node.category() == Category::leaf { + if range.length() > 0 && node.as_leaf().is_some() { self.record_line_continuations_until(range.start()); self.indents[self.last_leaf_end..range.start()].fill(self.last_indent); } @@ -1098,7 +1098,7 @@ fn visit(&mut self, node: &'a dyn Node) { } // If this is a leaf node, apply the current indentation. - if node.category() == Category::leaf && range.length() != 0 { + if node.as_leaf().is_some() && range.length() != 0 { let leading_spaces = self.src[..range.start()] .chars() .rev() diff --git a/src/reader.rs b/src/reader.rs index dfa0b330e..b4d9ab93a 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -45,7 +45,7 @@ use errno::{errno, Errno}; use crate::abbrs::abbrs_match; -use crate::ast::{is_same_node, Ast, Category}; +use crate::ast::{is_same_node, Ast}; use crate::builtins::shared::ErrorCode; use crate::builtins::shared::STATUS_CMD_ERROR; use crate::builtins::shared::STATUS_CMD_OK; @@ -5345,9 +5345,9 @@ fn extract_tokens(s: &wstr) -> Vec { let mut traversal = ast.walk(); while let Some(node) = traversal.next() { // We are only interested in leaf nodes with source. - if node.category() != Category::leaf { + if node.as_leaf().is_none() { continue; - } + }; let range = node.source_range(); if range.length() == 0 { continue;