Skip to content

Commit 4e2b956

Browse files
committed
Simplify
1 parent 04e794e commit 4e2b956

File tree

2 files changed

+30
-46
lines changed

2 files changed

+30
-46
lines changed

src/elements/command/simple/alias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn set(
2222
com.continue_alias_check = w.ends_with(" ");
2323
let mut feeder_local = Feeder::new(&w);
2424

25-
while SimpleCommand::eat_substitution(&mut feeder_local, com, core)? {
25+
while com.eat_substitution(&mut feeder_local, core)? {
2626
command::eat_blank_with_comment(&mut feeder_local, core, &mut com.text);
2727
}
2828

src/elements/command/simple/parser.rs

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,81 +13,65 @@ use crate::elements::pipeline::Pipeline;
1313
use crate::elements::job::Job;
1414

1515
impl SimpleCommand {
16-
pub fn eat_substitution(
17-
feeder: &mut Feeder,
18-
ans: &mut Self,
19-
core: &mut ShellCore,
20-
) -> Result<bool, ParseError> {
21-
match Substitution::parse(feeder, core, false) {
22-
Ok(Some(s)) => {
23-
ans.text += &s.text;
24-
ans.substitutions.push(s);
16+
pub fn eat_substitution(&mut self, feeder: &mut Feeder, core: &mut ShellCore)
17+
-> Result<bool, ParseError> {
18+
match Substitution::parse(feeder, core, false)? {
19+
Some(s) => {
20+
self.text += &s.text;
21+
self.substitutions.push(s);
2522
Ok(true)
2623
}
27-
Ok(None) => Ok(false),
28-
Err(e) => {
29-
feeder.rewind();
30-
Err(e)
31-
}
24+
None => Ok(false),
3225
}
3326
}
3427

35-
pub fn eat_substitution_as_arg(
36-
feeder: &mut Feeder,
37-
ans: &mut Self,
38-
core: &mut ShellCore,
39-
) -> Result<bool, ParseError> {
28+
pub fn eat_substitution_as_arg(&mut self, feeder: &mut Feeder,core: &mut ShellCore)
29+
-> Result<bool, ParseError> {
4030
if let Some(s) = Substitution::parse_as_arg(feeder, core)? {
41-
ans.text += &s.text;
42-
ans.substitutions_as_args
31+
self.text += &s.text;
32+
self.substitutions_as_args
4333
.push(SubsArgType::Subs(Box::new(s)));
4434
return Ok(true);
4535
}
4636

4737
if let Some(w) = Word::parse(feeder, core, None)? {
48-
ans.text += &w.text;
49-
ans.substitutions_as_args.push(SubsArgType::Other(w));
38+
self.text += &w.text;
39+
self.substitutions_as_args.push(SubsArgType::Other(w));
5040
return Ok(true);
5141
}
5242

5343
Ok(false)
5444
}
5545

56-
fn eat_word(
57-
feeder: &mut Feeder,
58-
ans: &mut SimpleCommand,
59-
core: &mut ShellCore,
60-
) -> Result<bool, ParseError> {
46+
fn eat_word(&mut self, feeder: &mut Feeder, core: &mut ShellCore)
47+
-> Result<bool, ParseError> {
6148
let mut mode = None;
62-
if ans.command_name == "eval" || ans.command_name == "let" {
49+
if self.command_name == "eval" || self.command_name == "let" {
6350
mode = Some(WordMode::EvalLet);
6451
}
6552

66-
let w = match Word::parse(feeder, core, mode) {
67-
Ok(Some(w)) => w,
68-
Err(e) => {
69-
feeder.rewind();
70-
return Err(e);
71-
}
53+
let w = match Word::parse(feeder, core, mode)? {
54+
Some(w) => w,
7255
_ => return Ok(false),
7356
};
7457

75-
if ans.words.is_empty() {
76-
ans.lineno = feeder.lineno;
58+
if self.words.is_empty() {
59+
self.lineno = feeder.lineno;
7760
if utils::reserved(&w.text) {
7861
return Ok(false);
7962
}
8063

81-
ans.command_name = w.text.clone();
64+
self.command_name = w.text.clone();
8265
}
8366

84-
if (ans.words.is_empty() || ans.continue_alias_check) && alias::set(ans, &w, core, feeder)?
85-
{
67+
if (self.words.is_empty()
68+
|| self.continue_alias_check)
69+
&& alias::set(self, &w, core, feeder)? {
8670
return Ok(true);
8771
}
8872

89-
ans.text += &w.text;
90-
ans.words.push(w);
73+
self.text += &w.text;
74+
self.words.push(w);
9175

9276
Ok(true)
9377
}
@@ -121,19 +105,19 @@ impl SimpleCommand {
121105
feeder.set_backup();
122106

123107
while command::eat_redirects(feeder, core, &mut ans.redirects, &mut ans.text)?
124-
|| Self::eat_substitution(feeder, &mut ans, core)? {}
108+
|| ans.eat_substitution(feeder, core)? {}
125109

126110
loop {
127111
command::eat_redirects(feeder, core, &mut ans.redirects, &mut ans.text)?;
128112

129113
if core.subst_builtins.contains_key(&ans.command_name) {
130-
if Self::eat_substitution_as_arg(feeder, &mut ans, core)? {
114+
if ans.eat_substitution_as_arg(feeder, core)? {
131115
continue;
132116
}
133117
}
134118

135119
command::eat_blank_with_comment(feeder, core, &mut ans.text);
136-
if ! Self::eat_word(feeder, &mut ans, core)? {
120+
if ! ans.eat_word(feeder, core)? {
137121
break;
138122
}
139123
}

0 commit comments

Comments
 (0)