simplify Token::eat

This commit is contained in:
Mazdak Farrokhzad 2022-05-31 16:15:53 +02:00
parent d43605538d
commit c623165742

View File

@ -171,18 +171,29 @@ impl Token {
/// Returns a tuple: [(token length, token)] if the next token can be eaten, otherwise returns [`None`]. /// Returns a tuple: [(token length, token)] if the next token can be eaten, otherwise returns [`None`].
/// The next token can be eaten if the bytes at the front of the given `input_tendril` string can be scanned into a token. /// The next token can be eaten if the bytes at the front of the given `input_tendril` string can be scanned into a token.
pub(crate) fn eat(input_tendril: &str) -> Result<(usize, Token)> { pub(crate) fn eat(input_tendril: &str) -> Result<(usize, Token)> {
if input_tendril.is_empty() {
return Err(ParserError::lexer_empty_input_tendril().into());
}
let mut input = input_tendril.chars().peekable(); let mut input = input_tendril.chars().peekable();
match input.peek() { // Error if empty or get the character.
Some(x) if x.is_ascii_whitespace() => { let c = input.peek().ok_or_else(|| ParserError::lexer_empty_input_tendril())?;
input.next();
return Ok((1, Token::WhiteSpace)); let single = |input: &mut Peekable<_>, token| {
input.next();
Ok((1, token))
};
let followed_by = |input: &mut Peekable<_>, on, then, els| {
input.next();
Ok(if input.next_if_eq(&on).is_some() {
(2, then)
} else {
(1, els)
})
};
match *c {
x if x.is_ascii_whitespace() => {
return single(&mut input, Token::WhiteSpace);
} }
Some('"') => { '"' => {
let mut string: Vec<leo_ast::Char> = Vec::new(); let mut string: Vec<leo_ast::Char> = Vec::new();
input.next(); input.next();
@ -205,69 +216,27 @@ impl Token {
return Err(ParserError::lexer_string_not_closed(leo_ast::Chars(string)).into()); return Err(ParserError::lexer_string_not_closed(leo_ast::Chars(string)).into());
} }
Some(x) if x.is_ascii_digit() => { x if x.is_ascii_digit() => {
return Self::eat_integer(&mut input); return Self::eat_integer(&mut input);
} }
Some('!') => { '!' => return followed_by(&mut input, '=', Token::NotEq, Token::Not),
input.next(); '?' => return single(&mut input, Token::Question),
if input.next_if_eq(&'=').is_some() { '&' => {
return Ok((2, Token::NotEq));
}
return Ok((1, Token::Not));
}
Some('?') => {
input.next();
return Ok((1, Token::Question));
}
Some('&') => {
input.next(); input.next();
if input.next_if_eq(&'&').is_some() { if input.next_if_eq(&'&').is_some() {
return Ok((2, Token::And)); return Ok((2, Token::And));
} }
return Err(ParserError::lexer_empty_input_tendril().into()); return Err(ParserError::lexer_empty_input_tendril().into());
} }
Some('(') => { '(' => return single(&mut input, Token::LeftParen),
input.next(); ')' => return single(&mut input, Token::RightParen),
return Ok((1, Token::LeftParen)); '_' => return single(&mut input, Token::Underscore),
} '*' => return followed_by(&mut input, '*', Token::Exp, Token::Mul),
Some(')') => { '+' => return single(&mut input, Token::Add),
input.next(); ',' => return single(&mut input, Token::Comma),
return Ok((1, Token::RightParen)); '-' => return followed_by(&mut input, '>', Token::Arrow, Token::Minus),
} '.' => return followed_by(&mut input, '.', Token::DotDot, Token::Dot),
Some('_') => { '/' => {
input.next();
return Ok((1, Token::Underscore));
}
Some('*') => {
input.next();
if input.next_if_eq(&'*').is_some() {
return Ok((2, Token::Exp));
}
return Ok((1, Token::Mul));
}
Some('+') => {
input.next();
return Ok((1, Token::Add));
}
Some(',') => {
input.next();
return Ok((1, Token::Comma));
}
Some('-') => {
input.next();
if input.next_if_eq(&'>').is_some() {
return Ok((2, Token::Arrow));
}
return Ok((1, Token::Minus));
}
Some('.') => {
input.next();
if input.next_if_eq(&'.').is_some() {
return Ok((2, Token::DotDot));
}
return Ok((1, Token::Dot));
}
Some(c) if c == &'/' => {
input.next(); input.next();
if input.next_if_eq(&'/').is_some() { if input.next_if_eq(&'/').is_some() {
let mut comment = String::from("//"); let mut comment = String::from("//");
@ -312,60 +281,24 @@ impl Token {
} }
return Ok((1, Token::Div)); return Ok((1, Token::Div));
} }
Some(':') => { ':' => return single(&mut input, Token::Colon),
';' => return single(&mut input, Token::Semicolon),
'<' => return followed_by(&mut input, '=', Token::LtEq, Token::Lt),
'>' => return followed_by(&mut input, '=', Token::GtEq, Token::Gt),
'=' => return followed_by(&mut input, '=', Token::Eq, Token::Assign),
'[' => return single(&mut input, Token::LeftSquare),
']' => return single(&mut input, Token::RightSquare),
'{' => return single(&mut input, Token::LeftCurly),
'}' => return single(&mut input, Token::RightCurly),
'|' => {
input.next(); input.next();
return Ok((1, Token::Colon)); return if input.next_if_eq(&'|').is_some() {
} Ok((2, Token::Or))
Some(';') => {
input.next();
return Ok((1, Token::Semicolon));
}
Some('<') => {
input.next();
if input.next_if_eq(&'=').is_some() {
return Ok((2, Token::LtEq));
}
return Ok((1, Token::Lt));
}
Some('>') => {
input.next();
if input.next_if_eq(&'=').is_some() {
return Ok((2, Token::GtEq));
}
return Ok((1, Token::Gt));
}
Some('=') => {
input.next();
if input.next_if_eq(&'=').is_some() {
return Ok((2, Token::Eq));
}
return Ok((1, Token::Assign));
}
Some('[') => {
input.next();
return Ok((1, Token::LeftSquare));
}
Some(']') => {
input.next();
return Ok((1, Token::RightSquare));
}
Some('{') => {
input.next();
return Ok((1, Token::LeftCurly));
}
Some('}') => {
input.next();
return Ok((1, Token::RightCurly));
}
Some('|') => {
input.next();
if input.next_if_eq(&'|').is_some() {
return Ok((2, Token::Or));
} else if let Some(found) = input.next() { } else if let Some(found) = input.next() {
return Err(ParserError::lexer_expected_but_found(found, '|').into()); Err(ParserError::lexer_expected_but_found(found, '|').into())
} else { } else {
return Err(ParserError::lexer_empty_input_tendril().into()); Err(ParserError::lexer_empty_input_tendril().into())
} };
} }
_ => (), _ => (),
} }