Parser cleanup

This commit is contained in:
Erin 2022-06-06 23:30:08 +02:00 committed by ondra05
parent 84f54f12d6
commit 7140082ff6

View file

@ -66,39 +66,15 @@ impl<'source> Parser<'source> {
let start = self.lexer.span().start;
match token {
Token::Unless => Ok(Spanned::new(
self.unless_flow()?,
start..self.lexer.span().end,
)),
Token::Functio => Ok(Spanned::new(
self.functio_flow()?,
start..self.lexer.span().end,
)),
Token::Bff => Ok(Spanned::new(self.bff_flow()?, start..self.lexer.span().end)),
Token::Melo => Ok(Spanned::new(
self.melo_flow()?,
start..self.lexer.span().end,
)),
Token::Loop => Ok(Spanned::new(
self.loop_flow()?,
start..self.lexer.span().end,
)),
Token::Enough => Ok(Spanned::new(
self.semicolon_terminated(Stmt::Enough)?,
start..self.lexer.span().end,
)),
Token::AndAgain => Ok(Spanned::new(
self.semicolon_terminated(Stmt::AndAgain)?,
start..self.lexer.span().end,
)),
Token::Rlyeh => Ok(Spanned::new(
self.semicolon_terminated(Stmt::Rlyeh)?,
start..self.lexer.span().end,
)),
Token::Rickroll => Ok(Spanned::new(
self.semicolon_terminated(Stmt::Rickroll)?,
start..self.lexer.span().end,
)),
Token::Unless => self.unless_flow(),
Token::Functio => self.functio_flow(),
Token::Bff => self.bff_flow(),
Token::Melo => self.melo_flow(),
Token::Loop => self.loop_flow(),
Token::Enough => self.semicolon_terminated(Stmt::Enough),
Token::AndAgain => self.semicolon_terminated(Stmt::AndAgain),
Token::Rlyeh => self.semicolon_terminated(Stmt::Rlyeh),
Token::Rickroll => self.semicolon_terminated(Stmt::Rickroll),
Token::Identifier(_)
| Token::String(_)
@ -106,16 +82,14 @@ impl<'source> Parser<'source> {
| Token::Char(_)
| Token::Aint
| Token::LeftBracket
| Token::LeftParen => Ok(Spanned::new(
self.value_flow(token)?,
start..self.lexer.span().end,
)),
| Token::LeftParen => self.value_flow(token),
t => Err(Error {
kind: ErrorKind::UnexpectedToken(t),
span: start..self.lexer.span().end,
}),
}
.map(|stmt| Spanned::new(stmt, start..self.lexer.span().end))
}
/// Require statement to be semicolon terminated
@ -161,43 +135,21 @@ impl<'source> Parser<'source> {
match token {
// Values
Token::Identifier(i) => Ok(Spanned::new(
Expr::Variable(self.tdark_subst(i)),
start..self.lexer.span().end,
)),
Token::Integer(i) => Ok(Spanned::new(
Expr::Literal(Literal::Int(i)),
start..self.lexer.span().end,
)),
Token::String(s) => Ok(Spanned::new(
Expr::Literal(Literal::Str(self.tdark_subst(s))),
start..self.lexer.span().end,
)),
Token::Char(c) => Ok(Spanned::new(
Expr::Literal(Literal::Char(c)),
start..self.lexer.span().end,
)),
Token::Identifier(i) => Ok(Expr::Variable(self.tdark_subst(i))),
Token::Integer(i) => Ok(Expr::Literal(Literal::Int(i))),
Token::String(s) => Ok(Expr::Literal(Literal::Str(self.tdark_subst(s)))),
Token::Char(c) => Ok(Expr::Literal(Literal::Char(c))),
Token::LeftBracket => match buf.take() {
Some(buf) => Ok(Spanned::new(
self.index_flow(buf)?,
start..self.lexer.span().end,
)),
None => Ok(Spanned::new(
self.cart_flow()?,
start..self.lexer.span().end,
)),
Some(buf) => self.index_flow(buf),
None => self.cart_flow(),
},
// Operations
Token::Aint if buf.is_none() => Ok(Spanned::new(
{
let next = self.checked_next()?;
Expr::Aint(Box::new(self.parse_expr(next, buf)?))
},
start..self.lexer.span().end,
)),
Token::Aint if buf.is_none() => {
let next = self.checked_next()?;
Ok(Expr::Aint(Box::new(self.parse_expr(next, buf)?)))
}
Token::Plus
| Token::Minus
| Token::Star
@ -205,17 +157,15 @@ impl<'source> Parser<'source> {
| Token::Equals
| Token::LessThan
| Token::GreaterThan
| Token::Aint => Ok(Spanned::new(
self.binop_flow(
BinOpKind::from_token(token).map_err(|e| Error::new(e, self.lexer.span()))?,
buf,
)?,
start..self.lexer.span().end,
)),
| Token::Aint => self.binop_flow(
BinOpKind::from_token(token).map_err(|e| Error::new(e, self.lexer.span()))?,
buf,
),
Token::LeftParen => self.expr_flow(Token::RightParen),
Token::LeftParen => return self.expr_flow(Token::RightParen),
t => Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())),
}
.map(|expr| Spanned::new(expr, start..self.lexer.span().end))
}
/// Flow for creating carts
@ -358,7 +308,7 @@ impl<'source> Parser<'source> {
/// will parse it to function call or print statement.
fn value_flow(&mut self, init: Token) -> Result<Stmt, Error> {
let mut buf = Some(self.parse_expr(init, &mut None)?);
let r = loop {
Ok(loop {
match self.checked_next()? {
// Print to stdout
Token::Print => {
@ -443,9 +393,7 @@ impl<'source> Parser<'source> {
t => buf = Some(self.parse_expr(t, &mut buf)?),
}
};
Ok(r)
})
}
/// Parse Unless flow