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,58 +254,48 @@ 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 {
),
b"err>" | b"e>" => (
Token {
contents: TokenContents::ErrGreaterThan, contents: TokenContents::ErrGreaterThan,
span, span,
}, },
None, b"out+err>" | b"err+out>" | b"o+e>" | b"e+o>" => Token {
),
b"out+err>" | b"err+out>" | b"o+e>" | b"e+o>" => (
Token {
contents: TokenContents::OutErrGreaterThan, contents: TokenContents::OutErrGreaterThan,
span, span,
}, },
None, b"&&" => {
), err = Some(ParseError::ShellAndAnd(span));
b"&&" => (
Token { Token {
contents: TokenContents::Item, contents: TokenContents::Item,
span, span,
},
Some(ParseError::ShellAndAnd(span)),
),
b"2>" => (
Token {
contents: TokenContents::Item,
span,
},
Some(ParseError::ShellErrRedirect(span)),
),
b"2>&1" => (
Token {
contents: TokenContents::Item,
span,
},
Some(ParseError::ShellOutErrRedirect(span)),
),
_ => (
Token {
contents: TokenContents::Item,
span,
},
None,
),
} }
} }
b"2>" => {
err = Some(ParseError::ShellErrRedirect(span));
Token {
contents: TokenContents::Item,
span,
}
}
b"2>&1" => {
err = Some(ParseError::ShellOutErrRedirect(span));
Token {
contents: TokenContents::Item,
span,
}
}
_ => Token {
contents: TokenContents::Item,
span,
},
};
(output, err)
}
pub fn lex_signature( pub fn lex_signature(
input: &[u8], input: &[u8],