Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 20, 2025
1 parent 7114a09 commit 47e4120
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/core/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -149,7 +148,7 @@ pub fn eval(core: &mut ShellCore, args: &mut Vec<String>) -> 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),
_ => {},
}

Expand Down
3 changes: 1 addition & 2 deletions src/core/builtins/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,7 +47,7 @@ pub fn source(core: &mut ShellCore, args: &mut Vec<String>) -> 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),
_ => {},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/error/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl From<ExecError> 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,
Expand Down
4 changes: 2 additions & 2 deletions src/error/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub enum InputError {
Eof,
}

impl From<InputError> 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(),
Expand Down
30 changes: 12 additions & 18 deletions src/error/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,26 @@ use super::input::InputError;
pub enum ParseError {
UnexpectedSymbol(String),
Input(InputError),
/*
UnexpectedEof,
Interrupted,
*/
}

impl From<ParseError> 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::<ParseError>::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);
}
}
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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![])];
},
Expand Down Expand Up @@ -193,7 +193,7 @@ fn run_and_exit_c_option(args: &Vec<String>, c_parts: &Vec<String>) {
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)
Expand Down
3 changes: 1 addition & 2 deletions src/proc_ctrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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),
_ => {},
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/ok
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 47e4120

Please sign in to comment.