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