removed redundant code from group parsing

This commit is contained in:
0rphon 2022-02-14 18:39:22 -08:00
parent 0d6c258068
commit 136a441fc6

View File

@ -166,21 +166,15 @@ impl<'a> ParserContext<'a> {
/// the next token is not a [`GroupCoordinate`].
///
fn peek_group_coordinate(&self, i: &mut usize) -> Option<GroupCoordinate> {
if *i < 1 {
return None;
}
let token = self.tokens.get(*i - 1)?;
*i -= 1;
*i = i.checked_sub(1)?;
let token = self.tokens.get(*i)?;
Some(match &token.token {
Token::Add => GroupCoordinate::SignHigh,
Token::Minus if *i > 0 => match self.tokens.get(*i - 1) {
Token::Minus => match self.tokens.get(i.checked_sub(1)?) {
Some(SpannedToken {
token: Token::Int(value),
span,
}) => {
if *i < 1 {
return None;
}
*i -= 1;
GroupCoordinate::Number(format_tendril!("-{}", value), span.clone())
}
@ -213,56 +207,42 @@ impl<'a> ParserContext<'a> {
///
pub fn eat_group_partial(&mut self) -> Option<Result<(GroupCoordinate, GroupCoordinate, Span)>> {
let mut i = self.tokens.len();
if i < 1 {
return None;
}
let start_span = self.tokens.get(i - 1)?.span.clone();
let start_span = self.tokens.get(i.checked_sub(1)?)?.span.clone();
let first = self.peek_group_coordinate(&mut i)?;
if i < 1 {
i = i.checked_sub(1)?;
if !matches!(
self.tokens.get(i),
Some(SpannedToken {
token: Token::Comma,
..
})
) {
return None;
}
match self.tokens.get(i - 1) {
Some(SpannedToken {
token: Token::Comma, ..
}) => {
i -= 1;
}
_ => {
return None;
}
}
let second = self.peek_group_coordinate(&mut i)?;
if i < 1 {
return None;
}
let right_paren_span;
match self.tokens.get(i - 1) {
Some(SpannedToken {
token: Token::RightParen,
span,
}) => {
right_paren_span = span.clone();
i -= 1;
}
_ => {
return None;
}
}
if i < 1 {
i = i.checked_sub(1)?;
if let Some(SpannedToken {
token: Token::RightParen,
span,
}) = self.tokens.get(i)
{
right_paren_span = span.clone();
} else {
return None;
}
let end_span;
match self.tokens.get(i - 1) {
Some(SpannedToken {
token: Token::Group,
span,
}) => {
end_span = span.clone();
i -= 1;
}
_ => {
return None;
}
i = i.checked_sub(1)?;
if let Some(SpannedToken {
token: Token::Group,
span,
}) = self.tokens.get(i)
{
end_span = span.clone();
} else {
return None;
}
self.tokens.drain(i..);