ast: Remove Node::category

This is now unused.
This commit is contained in:
Peter Ammon
2025-04-27 11:58:42 -07:00
parent 11b6bf31c0
commit ee9cf33689
4 changed files with 27 additions and 86 deletions

View File

@@ -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,
}
}
}

View File

@@ -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<SourceRange> {
// 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"
);
}
}
}

View File

@@ -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()

View File

@@ -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<PositionedToken> {
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;