Parser cleanup

This commit is contained in:
Erin 2022-06-06 23:30:08 +02:00 committed by ondra05
parent 8e6dd4bdcf
commit 16ac372c5d

View file

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