Refactor: simplify lex_item impl (#10744)

In the final match of `lex_item`, we'll return `Err(ParseError)` in rare
case, normally we'll return None.

So I think making error part mutable can reduce some code, and it's
better if we want to add more lex items.
This commit is contained in:
WindSoilder 2023-10-19 05:23:17 +08:00 committed by GitHub
parent ed8dee04b6
commit 9e7f84afb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -254,57 +254,47 @@ pub fn lex_item(
); );
} }
match &input[(span.start - span_offset)..(span.end - span_offset)] { let mut err = None;
b"out>" | b"o>" => ( let output = match &input[(span.start - span_offset)..(span.end - span_offset)] {
Token { b"out>" | b"o>" => Token {
contents: TokenContents::OutGreaterThan, contents: TokenContents::OutGreaterThan,
span, span,
}, },
None, b"err>" | b"e>" => Token {
), contents: TokenContents::ErrGreaterThan,
b"err>" | b"e>" => ( span,
Token { },
contents: TokenContents::ErrGreaterThan, b"out+err>" | b"err+out>" | b"o+e>" | b"e+o>" => Token {
span, contents: TokenContents::OutErrGreaterThan,
}, span,
None, },
), b"&&" => {
b"out+err>" | b"err+out>" | b"o+e>" | b"e+o>" => ( err = Some(ParseError::ShellAndAnd(span));
Token {
contents: TokenContents::OutErrGreaterThan,
span,
},
None,
),
b"&&" => (
Token { Token {
contents: TokenContents::Item, contents: TokenContents::Item,
span, span,
}, }
Some(ParseError::ShellAndAnd(span)), }
), b"2>" => {
b"2>" => ( err = Some(ParseError::ShellErrRedirect(span));
Token { Token {
contents: TokenContents::Item, contents: TokenContents::Item,
span, span,
}, }
Some(ParseError::ShellErrRedirect(span)), }
), b"2>&1" => {
b"2>&1" => ( err = Some(ParseError::ShellOutErrRedirect(span));
Token { Token {
contents: TokenContents::Item, contents: TokenContents::Item,
span, span,
}, }
Some(ParseError::ShellOutErrRedirect(span)), }
), _ => Token {
_ => ( contents: TokenContents::Item,
Token { span,
contents: TokenContents::Item, },
span, };
}, (output, err)
None,
),
}
} }
pub fn lex_signature( pub fn lex_signature(