refactor(parser): unify Program and CommandSubstitution

Acked-by: cpli <dev@cpli.in>
Signed-off-by: Charlotte Meyer <dev@buffet.sh>
This commit is contained in:
buffet 2022-11-06 23:03:31 +00:00
parent c6a972c76b
commit fc3d52fe48

View file

@ -151,31 +151,28 @@ impl Iterator for Parser<'_> {
use TokenKind::*;
match self.stack.last()? {
// XXX: unify Program and CommandSubstitution to avoid duplication
NodeKind::Program => match self.lookahead.kind {
NodeKind::Program | NodeKind::CommandSubstitution => match self.lookahead.kind {
Whitespace => leaf!(Whitespace),
Newlines => leaf!(Newlines),
Semicolon => leaf!(Semicolon),
Comment => leaf!(Comment),
PlainText | DoubleQuote | OpeningParenthesis | EscapedChar => call!(Pipeline),
Pipe => error!(UnexpectedPipe),
ClosingParenthesis => error!(UnexpectedClosingParenthesis),
Eof => chain!(None, ret!()), // return silently
},
NodeKind::CommandSubstitution => match self.lookahead.kind {
Whitespace => leaf!(Whitespace),
Newlines => leaf!(Newlines),
Semicolon => leaf!(Semicolon),
Comment => leaf!(Comment),
PlainText | DoubleQuote | OpeningParenthesis | EscapedChar => call!(Pipeline),
ClosingParenthesis => match self.stack.get(self.stack.len() - 2) {
ClosingParenthesis => match self.stack.last().unwrap() {
NodeKind::Program => error!(UnexpectedClosingParenthesis),
NodeKind::CommandSubstitution => match self.stack.get(self.stack.len() - 2) {
Some(&NodeKind::DQuotedString) => {
chain_buf!(leaf!(ClosingParenthesis, String), ret!())
}
_ => chain_buf!(leaf!(ClosingParenthesis, Command), ret!()),
},
Pipe => error!(UnexpectedPipe),
Eof => chain_buf!(error!(UnexpectedEof), ret!()),
_ => unreachable!(),
},
Eof => match self.stack.last().unwrap() {
NodeKind::Program => chain!(None, ret!()), // return silently
NodeKind::CommandSubstitution => chain_buf!(error!(UnexpectedEof), ret!()),
_ => unreachable!(),
},
},
NodeKind::Pipeline => match self.lookahead.kind {
Whitespace => leaf!(Whitespace),