Skip to content

Commit

Permalink
Implement brace rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 31, 2024
1 parent 5d92bc7 commit 8847c83
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
20 changes: 15 additions & 5 deletions src/elements/subword/brace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ impl BraceSubword {
ans.words.push(w);
true
},
_ => false,
_ => {
if feeder.starts_with(",") || feeder.starts_with("}") {
ans.words.push(Word::new());
return true;
}
false
},
}
}
}

pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<BraceSubword> {
if ! feeder.starts_with("{") {
Expand All @@ -44,12 +50,12 @@ impl BraceSubword {
let mut ans = Self::new();
let mut closed = false;

/*
ans.text = feeder.consume(1); // {

while Self::eat_word(feeder, &mut ans, core) {
if feeder.starts_with(",") {
ans.text += &feeder.consume(1);
core.word_nest.push(",".to_string());
continue;
}

Expand All @@ -59,12 +65,16 @@ impl BraceSubword {
}
break;
}
*/

while core.word_nest.last().unwrap() == "," {
core.word_nest.pop();
}
core.word_nest.pop();

if closed {
if closed && ans.words.len() >= 1 {
feeder.pop_backup();
ans.text.insert(0, '<');
ans.text.push('>');
Some(ans)
}else{
feeder.rewind();
Expand Down
21 changes: 14 additions & 7 deletions src/elements/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ impl Word {
let left = core.word_nest.last().unwrap().to_string();
let mut ans = Word::new();

if core.word_nest.last().unwrap() == "" {
if feeder.starts_with("{}") {
let sw = UnquotedSubword::new("{}");
feeder.consume(2);
ans.subwords.push(Box::new(sw));
ans.text += &"{}";
}
if feeder.starts_with("{}") {
let sw = UnquotedSubword::new("{}");
feeder.consume(2);
ans.subwords.push(Box::new(sw));
ans.text += &"{}";
}else if feeder.starts_with("}") && left != "," {
let sw = UnquotedSubword::new("}");
feeder.consume(1);
ans.subwords.push(Box::new(sw));
ans.text += &"}";
}

loop {
Expand All @@ -53,6 +56,10 @@ impl Word {
continue;
}

if left == "{" && feeder.starts_with("}") {
break;
}

if left == "{" && feeder.starts_with(",") {
break;
}else if left == "," && ( feeder.starts_with(",") || feeder.starts_with("}") ) {
Expand Down
40 changes: 24 additions & 16 deletions test/test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -341,27 +341,27 @@ res=$($com <<< 'rm -f /tmp/rusty_bash ; while [ -f /tmp/rusty_bash ] ; do echo w
# brace

res=$($com <<< 'echo {a,b}c')
[ "$res" == "{a,b}c" ] || err $LINENO
[ "$res" == "<{a,b}>c" ] || err $LINENO
#[ "$res" == "ac bc" ] || err $LINENO

res=$($com <<< 'echo c{a,b}')
[ "$res" == "c{a,b}" ] || err $LINENO
[ "$res" == "c<{a,b}>" ] || err $LINENO
#[ "$res" == "ca cb" ] || err $LINENO

res=$($com <<< 'echo {{a},b}')
[ "$res" == "{{a},b}" ] || err $LINENO
[ "$res" == "<{<{a}>,b}>" ] || err $LINENO
#[ "$res" == "{a} b" ] || err $LINENO

res=$($com <<< 'echo {a,{b},c}')
[ "$res" == "{a,{b},c}" ] || err $LINENO
[ "$res" == "<{a,<{b}>,c}>" ] || err $LINENO
#[ "$res" == "a {b} c" ] || err $LINENO

res=$($com <<< 'echo {a,b,c{d,e}f,g{h,i{j,k}}}')
[ "$res" == "{a,b,c{d,e}f,g{h,i{j,k}}}" ] || err $LINENO
[ "$res" == "<{a,b,c<{d,e}>f,g<{h,i<{j,k}>}>}>" ] || err $LINENO
#[ "$res" == "a b cdf cef gh gij gik" ] || err $LINENO

res=$($com <<< 'echo {a,b,c{d,e}f,g{h,i{j,k}}')
[ "$res" == "{a,b,c{d,e}f,g{h,i{j,k}}" ] || err $LINENO
[ "$res" == "{a,b,c<{d,e}>f,g<{h,i<{j,k}>}>" ] || err $LINENO
#[ "$res" == "{a,b,cdf,gh {a,b,cdf,gij {a,b,cdf,gik {a,b,cef,gh {a,b,cef,gij {a,b,cef,gik" ] || err $LINENO

res=$($com <<< 'echo c{a,b')
Expand All @@ -371,44 +371,46 @@ res=$($com <<< 'echo c{a,b,')
[ "$res" == "c{a,b," ] || err $LINENO

res=$($com <<< 'echo {{a,b},{c,d},')
[ "$res" == "{{a,b},{c,d}," ] || err $LINENO
[ "$res" == "{<{a,b}>,<{c,d}>," ] || err $LINENO
#[ "$res" == "{a,c, {a,d, {b,c, {b,d," ] || err $LINENO

res=$($com <<< 'echo {{a,b},{c,d')
[ "$res" == "{{a,b},{c,d" ] || err $LINENO
[ "$res" == "{<{a,b}>,{c,d" ] || err $LINENO
#[ "$res" == "{a,{c,d {b,{c,d" ] || err $LINENO

res=$($com <<< 'echo {{a,b,{c,')
[ "$res" == "{{a,b,{c," ] || err $LINENO

res=$($com <<< 'echo {a}')
[ "$res" == "{a}" ] || err $LINENO
[ "$res" == "<{a}>" ] || err $LINENO
#[ "$res" == "{a}" ] || err $LINENO

res=$($com <<< 'echo {a,}')
[ "$res" == "{a,}" ] || err $LINENO
[ "$res" == "<{a,}>" ] || err $LINENO
#[ "$res" == "a" ] || err $LINENO

res=$($com <<< 'echo {a,b,}')
[ "$res" == "{a,b,}" ] || err $LINENO
[ "$res" == "<{a,b,}>" ] || err $LINENO
#[ "$res" == "a b" ] || err $LINENO

res=$($com <<< 'echo {a,b,}c')
[ "$res" == "{a,b,}c" ] || err $LINENO
[ "$res" == "<{a,b,}>c" ] || err $LINENO
#[ "$res" == "ac bc c" ] || err $LINENO

res=$($com <<< 'echo {}')
[ "$res" == "{}" ] || err $LINENO
#[ "$res" == "{}" ] || err $LINENO

res=$($com <<< 'echo {,}')
[ "$res" == "{,}" ] || err $LINENO
[ "$res" == "<{,}>" ] || err $LINENO
#[ "$res" == "" ] || err $LINENO

res=$($com <<< 'echo {,,}')
[ "$res" == "{,,}" ] || err $LINENO
[ "$res" == "<{,,}>" ] || err $LINENO
#[ "$res" == "" ] || err $LINENO

res=$($com <<< 'echo a{,,}b')
[ "$res" == "a{,,}b" ] || err $LINENO
[ "$res" == "a<{,,}>b" ] || err $LINENO
#[ "$res" == "ab ab ab" ] || err $LINENO

res=$($com <<< 'echo {')
Expand All @@ -418,9 +420,15 @@ res=$($com <<< 'echo }')
[ "$res" == "}" ] || err $LINENO

res=$($com <<< 'echo {a,}{b,}')
[ "$res" == "{a,}{b,}" ] || err $LINENO
[ "$res" == "<{a,}><{b,}>" ] || err $LINENO
#[ "$res" == "ab a b" ] || err $LINENO

res=$($com <<< 'echo {},b}')
[ "$res" == "{},b}" ] || err $LINENO

res=$($com <<< 'echo a{},b}')
[ "$res" == "a<{},b}>" ] || err $LINENO

### WHILE TEST ###

res=$($com <<< 'touch /tmp/rusty_bash ; while [ -f /tmp/rusty_bash ] ; do echo wait ; rm /tmp/rusty_bash ; done > /tmp/rusty_bash1'; cat /tmp/rusty_bash1 ; cat /tmp/rusty_bash1 )
Expand Down

0 comments on commit 8847c83

Please sign in to comment.