diff --git a/crates/oyster_parser/src/parser.rs b/crates/oyster_parser/src/parser.rs index 34756cd..50e6a5a 100644 --- a/crates/oyster_parser/src/parser.rs +++ b/crates/oyster_parser/src/parser.rs @@ -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) { - Some(&NodeKind::DQuotedString) => { - chain_buf!(leaf!(ClosingParenthesis, String), ret!()) - } - _ => chain_buf!(leaf!(ClosingParenthesis, Command), ret!()), + 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!()), + }, + _ => unreachable!(), + }, + Eof => match self.stack.last().unwrap() { + NodeKind::Program => chain!(None, ret!()), // return silently + NodeKind::CommandSubstitution => chain_buf!(error!(UnexpectedEof), ret!()), + _ => unreachable!(), }, - Pipe => error!(UnexpectedPipe), - Eof => chain_buf!(error!(UnexpectedEof), ret!()), }, NodeKind::Pipeline => match self.lookahead.kind { Whitespace => leaf!(Whitespace),