Skip to content

Commit 0e9be2d

Browse files
committed
Merge branch 'sd/202412_2' into sd/202412_3
2 parents d3f2872 + e218a02 commit 0e9be2d

File tree

6 files changed

+62
-81
lines changed

6 files changed

+62
-81
lines changed

src/elements/io/redirect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Redirect {
114114
Redirect {
115115
text: String::new(),
116116
symbol: String::new(),
117-
right: Word::new(),
117+
right: Word::new(vec![]),
118118
left: String::new(),
119119
left_fd: -1,
120120
left_backup: -1,

src/elements/job.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ pub struct Job {
1616

1717
impl Job {
1818
pub fn exec(&mut self, core: &mut ShellCore, bg: bool) {
19-
let pgid = if core.is_subshell { //17〜21行目を追加
20-
unistd::getpgrp() //自身のPGID
21-
}else{
22-
Pid::from_raw(0)
19+
let pgid = match core.is_subshell {
20+
true => unistd::getpgrp(),
21+
false => Pid::from_raw(0),
2322
};
2423

25-
if bg {
26-
self.exec_bg(core, pgid);
27-
}else{
28-
self.exec_fg(core, pgid);
24+
match bg {
25+
true => self.exec_bg(core, pgid),
26+
false => self.exec_fg(core, pgid),
2927
}
3028
}
3129

@@ -136,6 +134,9 @@ impl Job {
136134
}
137135
}
138136
}
137+
138+
let com_num = feeder.scanner_comment();
139+
ans.text += &feeder.consume(com_num);
139140

140141
if ans.pipelines.len() > 0 {
141142
// dbg!("{:?}", &ans); // デバッグ用にansの内容を出力

src/elements/script.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,26 @@ impl Script {
5252
len != 0
5353
}
5454

55-
fn check_nest(feeder: &mut Feeder, jobnum: usize) -> Status {
55+
fn check_nest(&self, feeder: &mut Feeder) -> Status {
5656
let nest = feeder.nest.last().expect("SUSHI INTERNAL ERROR (empty nest)");
5757

58-
match ( nest.1.iter().find(|e| feeder.starts_with(e)), jobnum ) {
58+
if nest.0 == "" && feeder.len() == 0 {
59+
return Status::NormalEnd;
60+
}
61+
62+
match ( nest.1.iter().find(|e| feeder.starts_with(e)), self.jobs.len() ) {
5963
( Some(end), 0 ) => return Status::UnexpectedSymbol(end.to_string()),
6064
( Some(_), _) => return Status::NormalEnd,
6165
( None, _) => {},
6266
}
6367

64-
let ng_ends = vec!["(", ")", "}", "then", "else", "if", "fi", "elif", "do", "done", "while", "||", "&&", "|", "&", ";"];
65-
match ( ng_ends.iter().find(|e| feeder.starts_with(e)), nest.1.len() ) {
66-
(Some(end), _) => return Status::UnexpectedSymbol(end.to_string()),
67-
(None, 0) => return Status::NormalEnd,
68-
(None, _) => return Status::NeedMoreLine,
68+
if feeder.len() > 0 {
69+
let remaining = feeder.consume(feeder.len());
70+
let first_token = remaining.split(" ").nth(0).unwrap().to_string();
71+
return Status::UnexpectedSymbol(first_token);
6972
}
73+
74+
Status::NeedMoreLine
7075
}
7176

7277
pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Script> {
@@ -76,10 +81,8 @@ impl Script {
7681
while Self::eat_job(feeder, core, &mut ans)
7782
&& Self::eat_job_end(feeder, &mut ans) {}
7883

79-
match Self::check_nest(feeder, ans.jobs.len()){
80-
Status::NormalEnd => {
81-
return Some(ans)
82-
},
84+
match ans.check_nest(feeder){
85+
Status::NormalEnd => return Some(ans),
8386
Status::UnexpectedSymbol(s) => {
8487
eprintln!("Unexpected token: {}", s);
8588
core.set_param("?", "2");

src/elements/subword.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
//SPDX-License-Identifier: BSD-3-Clause
33

44
mod escaped_char;
5-
mod parameter;
65
mod simple;
76
mod single_quoted;
87

98
use crate::{Feeder, ShellCore};
109
use std::fmt;
1110
use self::escaped_char::EscapedChar;
12-
use self::parameter::Parameter;
1311
use self::simple::SimpleSubword;
1412
use self::single_quoted::SingleQuoted;
1513
use std::fmt::Debug;
@@ -43,7 +41,6 @@ pub trait Subword {
4341
pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Box<dyn Subword>> {
4442
if let Some(a) = SingleQuoted::parse(feeder, core){ Some(Box::new(a)) }
4543
else if let Some(a) = EscapedChar::parse(feeder, core){ Some(Box::new(a)) }
46-
else if let Some(a) = Parameter::parse(feeder, core){ Some(Box::new(a)) }
4744
else if let Some(a) = SimpleSubword::parse(feeder){ Some(Box::new(a)) }
4845
else{ None }
4946
}

src/elements/word.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ impl Word {
2222
Some( Self::make_args(&mut ws) )
2323
}
2424

25-
pub fn make_args(words: &mut Vec<Word>) -> Vec<String> {
25+
fn make_args(words: &mut Vec<Word>) -> Vec<String> {
2626
words.iter_mut()
2727
.map(|w| w.make_unquoted_word())
2828
.filter(|w| *w != None)
2929
.map(|w| w.unwrap())
3030
.collect()
3131
}
3232

33-
pub fn make_unquoted_word(&mut self) -> Option<String> {
33+
fn make_unquoted_word(&mut self) -> Option<String> {
3434
let sw: Vec<Option<String>> = self.subwords.iter_mut()
3535
.map(|s| s.make_unquoted_string()) //""や''はNoneにならずに空文字として残る
3636
.filter(|s| *s != None)
@@ -43,32 +43,27 @@ impl Word {
4343
Some(sw.into_iter().map(|s| s.unwrap()).collect::<String>())
4444
}
4545

46-
pub fn new() -> Word {
46+
pub fn new(subwords: Vec<Box::<dyn Subword>>) -> Word {
4747
Word {
48-
text: String::new(),
49-
subwords: vec![],
48+
text: subwords.iter().map(|s| s.get_text()).collect(),
49+
subwords: subwords,
5050
}
5151
}
5252

53-
fn push(&mut self, subword: &Box<dyn Subword>) {
54-
self.text += &subword.get_text().to_string();
55-
self.subwords.push(subword.clone());
56-
}
57-
5853
pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Word> {
5954
if feeder.starts_with("#") {
6055
return None;
6156
}
6257

63-
let mut ans = Word::new();
58+
let mut subwords = vec![];
6459
while let Some(sw) = subword::parse(feeder, core) {
65-
ans.push(&sw);
60+
subwords.push(sw);
6661
}
6762

68-
if ans.text.len() == 0 {
69-
None
70-
}else{
71-
Some(ans)
63+
let ans = Word::new(subwords);
64+
match ans.text.len() {
65+
0 => None,
66+
_ => Some(ans),
7267
}
7368
}
7469
}

src/elements/word/brace_expansion.rs

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//SPDX-License-Identifier: BSD-3-Clause
33

44
use crate::elements::subword::Subword;
5-
use crate::elements::word::Word;
5+
use super::Word;
66

77
fn after_dollar(s: &str) -> bool {
88
s == "$" || s == "$$"
@@ -14,21 +14,19 @@ pub fn eval(word: &mut Word) -> Vec<Word> {
1414
let mut skip_until = 0;
1515
for i in open_brace_pos(word) {
1616
if i < skip_until { //ブレース展開の終わりまで処理をスキップ
17-
continue;
17+
continue;
1818
}
1919

20-
if let Some(d) = parse(&word.subwords[i..]) {
21-
let shift_d: Vec<usize> = d.iter().map(|e| e+i).collect();
22-
23-
if i > 0 && after_dollar(word.subwords[i-1].get_text()) {
24-
skip_until = *shift_d.last().unwrap();
25-
continue;
26-
}
20+
let d = parse(&word.subwords[i..], i);
21+
if d.len() <= 2 {
22+
continue;
23+
}
2724

28-
return expand(&word.subwords, &shift_d);
25+
match i > 0 && after_dollar(word.subwords[i-1].get_text()) {
26+
true => skip_until = *d.last().unwrap(),
27+
false => return expand(&word.subwords, &d),
2928
}
3029
}
31-
3230
vec![word.clone()]
3331
}
3432

@@ -52,56 +50,43 @@ fn open_brace_pos(w: &Word) -> Vec<usize> {
5250
.collect()
5351
}
5452

55-
pub fn parse(subwords: &[Box<dyn Subword>]) -> Option<Vec<usize>> {
53+
fn parse(subwords: &[Box<dyn Subword>], start: usize) -> Vec<usize> {
5654
let mut stack = vec![];
5755
for sw in subwords {
58-
stack.push(Some(sw.get_text()));
56+
stack.push(sw.get_text());
5957
if sw.get_text() == "}" {
60-
match get_delimiters(&mut stack) {
61-
Some(ds) => return Some(ds),
62-
_ => {},
58+
let ds = get_delimiters(&mut stack, start);
59+
if ds.len() > 2 {
60+
return ds;
6361
}
6462
}
6563
}
66-
None
64+
vec![]
6765
}
6866

69-
fn get_delimiters(stack: &mut Vec<Option<&str>>) -> Option<Vec<usize>> {
70-
let mut comma_pos = vec![];
67+
fn get_delimiters(stack: &mut Vec<&str>, start: usize) -> Vec<usize> {
68+
let mut delimiter_pos = vec![start, stack.len()-1+start];
7169
for i in (1..stack.len()-1).rev() {
72-
if stack[i] == Some(",") {
73-
comma_pos.push(i);
74-
}else if stack[i] == Some("{") { // find an inner brace expcomma_posion
75-
stack[i..].iter_mut().for_each(|e| *e = None);
76-
return None;
70+
if stack[i] == "," {
71+
delimiter_pos.insert(1, start+i);
72+
}else if stack[i] == "{" { // find an inner brace expcomma_posion
73+
stack[i..].iter_mut().for_each(|e| *e = "");
74+
return vec![];
7775
}
7876
}
79-
80-
if comma_pos.len() > 0 {
81-
comma_pos.reverse();
82-
comma_pos.insert(0, 0); // add "{" pos
83-
comma_pos.push(stack.len()-1); // add "}" pos
84-
Some(comma_pos)
85-
}else{
86-
None
87-
}
77+
delimiter_pos
8878
}
8979

90-
pub fn expand(subwords: &Vec<Box<dyn Subword>>, delimiters: &Vec<usize>) -> Vec<Word> {
80+
fn expand(subwords: &Vec<Box<dyn Subword>>, delimiters: &Vec<usize>) -> Vec<Word> {
9181
let left = &subwords[..delimiters[0]];
9282
let mut right = subwords[(delimiters.last().unwrap()+1)..].to_vec();
9383
invalidate_brace(&mut right);
9484

9585
let mut ans = vec![];
96-
let mut from = delimiters[0] + 1;
97-
for to in &delimiters[1..] {
98-
let mut w = Word::new();
99-
w.subwords.extend(left.to_vec());
100-
w.subwords.extend(subwords[from..*to].to_vec());
101-
w.subwords.extend(right.to_vec());
102-
w.text = w.subwords.iter().map(|s| s.get_text()).collect();
86+
for i in 0..(delimiters.len()-1) {
87+
let center = &subwords[ (delimiters[i]+1)..delimiters[i+1] ];
88+
let mut w = Word::new([ left, &center, &right ].concat() );
10389
ans.append(&mut eval(&mut w));
104-
from = *to + 1;
10590
}
10691
ans
10792
}

0 commit comments

Comments
 (0)