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 {
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) => { Some(&NodeKind::DQuotedString) => {
chain_buf!(leaf!(ClosingParenthesis, String), ret!()) chain_buf!(leaf!(ClosingParenthesis, String), ret!())
} }
_ => chain_buf!(leaf!(ClosingParenthesis, Command), ret!()), _ => chain_buf!(leaf!(ClosingParenthesis, Command), ret!()),
}, },
Pipe => error!(UnexpectedPipe), _ => unreachable!(),
Eof => chain_buf!(error!(UnexpectedEof), ret!()), },
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 { NodeKind::Pipeline => match self.lookahead.kind {
Whitespace => leaf!(Whitespace), Whitespace => leaf!(Whitespace),