Skip to content

Commit

Permalink
Apply parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 15, 2025
1 parent 44ca30f commit 92e6126
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/core/builtins/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn set_local(arg: &str, core: &mut ShellCore, layer: usize) -> Result<(), ExecEr
}

let mut sub = match Substitution::parse(&mut feeder, core) {
Some(s) => s,
Ok(Some(s)) => s,
_ => return Err(ExecError::VariableInvalid(arg.to_string())),
};

Expand All @@ -50,7 +50,7 @@ fn set_local_array(arg: &str, core: &mut ShellCore, layer: usize) -> Result<(),
}

let mut sub = match Substitution::parse(&mut feeder, core) {
Some(s) => s,
Ok(Some(s)) => s,
_ => return Err(ExecError::VariableInvalid(arg.to_string())),
};

Expand Down
14 changes: 7 additions & 7 deletions src/elements/command/simple/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::{ShellCore, Feeder, utils};
use super::{SimpleCommand};
use super::SimpleCommand;
use crate::elements::command;
use crate::elements::substitution::Substitution;
use crate::elements::word::Word;
use crate::error::ParseError;

impl SimpleCommand {
fn eat_substitution(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> bool {
if let Some(s) = Substitution::parse(feeder, core) {
fn eat_substitution(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> Result<bool, ParseError> {
if let Some(s) = Substitution::parse(feeder, core)? {
ans.text += &s.text;
match ans.permit_substitution_arg {
true => ans.substitutions_as_args.push(s),
false => ans.substitutions.push(s),
}
true
Ok(true)
}else{
false
Ok(false)
}
}

Expand Down Expand Up @@ -84,14 +84,14 @@ impl SimpleCommand {
let mut ans = Self::default();
feeder.set_backup();

while Self::eat_substitution(feeder, &mut ans, core) {
while Self::eat_substitution(feeder, &mut ans, core)? {
command::eat_blank_with_comment(feeder, core, &mut ans.text);
}

loop {
command::eat_redirects(feeder, core, &mut ans.redirects, &mut ans.text);
if ans.permit_substitution_arg
&& Self::eat_substitution(feeder, &mut ans, core) {
&& Self::eat_substitution(feeder, &mut ans, core)? {
continue;
}

Expand Down
18 changes: 5 additions & 13 deletions src/elements/substitution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::{ShellCore, Feeder};
use crate::error::ExecError;
use crate::error::{ExecError, ParseError};
use std::env;
use super::array::Array;
use super::subscript::Subscript;
Expand Down Expand Up @@ -161,17 +161,12 @@ impl Substitution {

let values = a.eval(core)?;
Ok([prev, values].concat())
/*
match a.eval(core) {
Some(values) => Some([prev, values].concat()),
None => None,
}*/
}

pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Self> {
pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Result<Option<Self>, ParseError> {
let len = feeder.scanner_name(core);
if len == 0 {
return None;
return Ok(None);
}

let mut ans = Self::default();
Expand All @@ -193,20 +188,17 @@ impl Substitution {
ans.text += &feeder.consume(1);
}else {
feeder.rewind();
return None;
return Ok(None);
}
feeder.pop_backup();

if let Some(a) = Array::parse(feeder, core) {
ans.text += &a.text;
ans.value = ParsedDataType::Array(a);
Some(ans)
}else if let Ok(Some(w)) = Word::parse(feeder, core, false) {
ans.text += &w.text;
ans.value = ParsedDataType::Single(w);
Some(ans)
}else {
Some(ans)
}
Ok(Some(ans))
}
}

0 comments on commit 92e6126

Please sign in to comment.