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