From 47e4120f406b5060d21f02bdcd52e7a9e0a60b82 Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Mon, 20 Jan 2025 16:19:18 +0900 Subject: [PATCH] Simplify --- src/core/builtins.rs | 3 +-- src/core/builtins/source.rs | 3 +-- src/error/exec.rs | 2 +- src/error/input.rs | 4 ++-- src/error/parse.rs | 30 ++++++++++++------------------ src/main.rs | 6 +++--- src/proc_ctrl.rs | 3 +-- test/ok | 4 ++-- 8 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/core/builtins.rs b/src/core/builtins.rs index bb590f3e..3b96576c 100644 --- a/src/core/builtins.rs +++ b/src/core/builtins.rs @@ -18,7 +18,6 @@ mod loop_control; mod unset; use crate::{error, proc_ctrl, Feeder, Script, ShellCore}; -use crate::error::parse; use crate::elements::command::simple::SimpleCommand; use crate::elements::io::pipe::Pipe; use crate::utils::{arg, exit, file}; @@ -149,7 +148,7 @@ pub fn eval(core: &mut ShellCore, args: &mut Vec) -> i32 { core.eval_level += 1; match Script::parse(&mut feeder, core, false){ Ok(Some(mut s)) => {let _ = s.exec(core); }, - Err(e) => parse::print_error(e, core), + Err(e) => e.print(core), _ => {}, } diff --git a/src/core/builtins/source.rs b/src/core/builtins/source.rs index 86f7f200..492817dc 100644 --- a/src/core/builtins/source.rs +++ b/src/core/builtins/source.rs @@ -3,7 +3,6 @@ use crate::{file_check, Script, ShellCore, Feeder}; use crate::elements::io; -use crate::error::parse; use std::fs::File; use std::os::fd::IntoRawFd; @@ -48,7 +47,7 @@ pub fn source(core: &mut ShellCore, args: &mut Vec) -> i32 { match Script::parse(&mut feeder, core, false){ Ok(Some(mut s)) => {let _ = s.exec(core); }, - Err(e) => parse::print_error(e, core), + Err(e) => e.print(core), _ => {}, } } diff --git a/src/error/exec.rs b/src/error/exec.rs index 7a738fc1..3e28cc54 100644 --- a/src/error/exec.rs +++ b/src/error/exec.rs @@ -43,7 +43,7 @@ impl From for String { ExecError::VariableReadOnly(name) => format!("{}: readonly variable", name), ExecError::VariableInvalid(name) => format!("`{}': not a valid identifier", name), ExecError::OperandExpected(token) => format!("{0}: syntax error: operand expected (error token is \"{0}\")", token), - ExecError::ParseError(p) => From::from(p), + ExecError::ParseError(p) => From::from(&p), ExecError::Recursion(token) => format!("{0}: expression recursion level exceeded (error token is \"{0}\")", token), ExecError::SubstringMinus(n) => format!("{}: substring expression < 0", n), ExecError::Other(name) => name, diff --git a/src/error/input.rs b/src/error/input.rs index d29acd8f..82dbd754 100644 --- a/src/error/input.rs +++ b/src/error/input.rs @@ -7,8 +7,8 @@ pub enum InputError { Eof, } -impl From for String { - fn from(e: InputError) -> String { +impl From<&InputError> for String { + fn from(e: &InputError) -> String { match e { InputError::Eof => "syntax error: unexpected end of file".to_string(), InputError::Interrupt => "interrupted".to_string(), diff --git a/src/error/parse.rs b/src/error/parse.rs index c252b769..f06818b0 100644 --- a/src/error/parse.rs +++ b/src/error/parse.rs @@ -8,32 +8,26 @@ use super::input::InputError; pub enum ParseError { UnexpectedSymbol(String), Input(InputError), - /* - UnexpectedEof, - Interrupted, - */ } -impl From for String { - fn from(e: ParseError) -> String { +impl From<&ParseError> for String { + fn from(e: &ParseError) -> String { match e { ParseError::UnexpectedSymbol(s) => format!("Unexpected token: {}", s), ParseError::Input(e) => From::from(e), - /* - ParseError::UnexpectedEof => "syntax error: unexpected end of file".to_string(), - ParseError::Interrupted => "interrupted".to_string(), - */ } } } -pub fn print_error(e: ParseError, core: &mut ShellCore) { - let name = core.db.get_param("0").unwrap(); - let s: String = From::::from(e); - if core.db.flags.contains('i') { - eprintln!("{}: {}", &name, &s); - }else{ - let lineno = core.db.get_param("LINENO").unwrap_or("".to_string()); - eprintln!("{}: line {}: {}", &name, &lineno, s); +impl ParseError { + pub fn print(&self, core: &mut ShellCore) { + let name = core.db.get_param("0").unwrap(); + let s: String = From::<&ParseError>::from(self); + if core.db.flags.contains('i') { + eprintln!("{}: {}", &name, &s); + }else{ + let lineno = core.db.get_param("LINENO").unwrap_or("".to_string()); + eprintln!("{}: line {}: {}", &name, &lineno, s); + } } } diff --git a/src/main.rs b/src/main.rs index 3f78738a..bfbefc00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use builtins::{option, parameter}; use std::{env, process}; use std::sync::atomic::Ordering::Relaxed; use crate::core::{builtins, ShellCore}; -use crate::error::{exec, parse}; +use crate::error::exec; use crate::elements::script::Script; use crate::feeder::Feeder; use utils::{exit, file_check, arg}; @@ -146,7 +146,7 @@ fn main_loop(core: &mut ShellCore) { set_history(core, &s.get_text()); }, Err(e) => { - parse::print_error(e, core); + e.print(core); feeder.consume(feeder.len()); feeder.nest = vec![("".to_string(), vec![])]; }, @@ -193,7 +193,7 @@ fn run_and_exit_c_option(args: &Vec, c_parts: &Vec) { let mut feeder = Feeder::new(&c_parts[1]); match Script::parse(&mut feeder, &mut core, false){ Ok(Some(mut s)) => {let _ = s.exec(&mut core);}, - Err(e) => parse::print_error(e, &mut core), + Err(e) => e.print(&mut core), _ => {}, } exit::normal(&mut core) diff --git a/src/proc_ctrl.rs b/src/proc_ctrl.rs index e5fa6194..4dd6bf7c 100644 --- a/src/proc_ctrl.rs +++ b/src/proc_ctrl.rs @@ -3,7 +3,6 @@ use crate::{exit, Feeder, Script, ShellCore, signal}; use crate::error; -use crate::error::parse; use nix::unistd; use nix::errno::Errno; use nix::sys::{resource, wait}; @@ -157,7 +156,7 @@ fn run_command_not_found(arg: &String, core: &mut ShellCore) -> ! { let mut f = Feeder::new(&s); match Script::parse(&mut f, core, false) { Ok(Some(mut script)) => {let _ = script.exec(core);}, - Err(e) => parse::print_error(e, core), + Err(e) => e.print(core), _ => {}, } } diff --git a/test/ok b/test/ok index 1bd62e29..3b519473 100644 --- a/test/ok +++ b/test/ok @@ -1,11 +1,11 @@ ./test_script.bash ./test_options.bash -./test_redirects.bash ./test_glob.bash +./test_redirects.bash ./test_brace.bash ./test_builtins.bash ./test_others.bash -./test_calculation.bash ./test_parameters.bash +./test_calculation.bash ./test_compound.bash ./test_job.bash