Removed unwraps, added Rlyeh

This commit is contained in:
Erin 2021-06-07 11:07:50 +02:00 committed by ondra05
parent b7464f6ad3
commit 2084ebeed1
2 changed files with 21 additions and 2 deletions

View file

@ -65,6 +65,7 @@ pub enum StmtKind {
}, },
Print(Expr), Print(Expr),
Melo(Iden), Melo(Iden),
Rlyeh,
} }
impl Stmt { impl Stmt {

View file

@ -59,6 +59,10 @@ impl<'source> Parser<'source> {
self.semi_terminated(StmtKind::HopBack)?, self.semi_terminated(StmtKind::HopBack)?,
start..self.lexer.span().end, start..self.lexer.span().end,
)), )),
Token::Rlyeh => Ok(Stmt::new(
self.semi_terminated(StmtKind::HopBack)?,
start..self.lexer.span().end,
)),
Token::Identifier(_) Token::Identifier(_)
| Token::Char | Token::Char
@ -198,7 +202,11 @@ impl<'source> Parser<'source> {
let mut buf = None; let mut buf = None;
Ok(loop { Ok(loop {
match self.lexer.next().ok_or(Error::unexpected_eof())? { match self.lexer.next().ok_or(Error::unexpected_eof())? {
t if t == terminate => break buf.take().unwrap(), t if t == terminate => {
break buf
.take()
.ok_or(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span()))?
}
t => buf = Some(self.parse_expr(t, &mut buf)?), t => buf = Some(self.parse_expr(t, &mut buf)?),
} }
}) })
@ -225,7 +233,12 @@ impl<'source> Parser<'source> {
let mut buf = Some(self.parse_expr(init, &mut None)?); let mut buf = Some(self.parse_expr(init, &mut None)?);
let r = loop { let r = loop {
match self.lexer.next().ok_or(Error::unexpected_eof())? { match self.lexer.next().ok_or(Error::unexpected_eof())? {
Token::Print => break StmtKind::Print(buf.take().unwrap()), Token::Print => {
break StmtKind::Print(buf.take().ok_or(Error::new(
ErrorKind::UnexpectedToken(Token::Print),
self.lexer.span(),
))?)
}
Token::LeftParen => { Token::LeftParen => {
if let Some(Expr { if let Some(Expr {
kind: ExprKind::Variable(iden), kind: ExprKind::Variable(iden),
@ -271,6 +284,7 @@ impl<'source> Parser<'source> {
Token::Identifier(i) => { Token::Identifier(i) => {
args.push(Iden::new(i, self.lexer.span())); args.push(Iden::new(i, self.lexer.span()));
// Require comma (next) or right paren (end) after identifier
match self.lexer.next().ok_or(Error::unexpected_eof())? { match self.lexer.next().ok_or(Error::unexpected_eof())? {
Token::Comma => continue, Token::Comma => continue,
Token::RightParen => break, Token::RightParen => break,
@ -297,14 +311,18 @@ impl<'source> Parser<'source> {
let mut buf = None; let mut buf = None;
loop { loop {
match self.lexer.next().ok_or(Error::unexpected_eof())? { match self.lexer.next().ok_or(Error::unexpected_eof())? {
// End of argument list
Token::RightParen => { Token::RightParen => {
if let Some(expr) = buf.take() { if let Some(expr) = buf.take() {
args.push(expr) args.push(expr)
} }
break; break;
} }
// Next argument
Token::Comma => match buf.take() { Token::Comma => match buf.take() {
Some(expr) => args.push(expr), Some(expr) => args.push(expr),
// Comma alone
None => { None => {
return Err(Error::new( return Err(Error::new(
ErrorKind::UnexpectedToken(Token::Comma), ErrorKind::UnexpectedToken(Token::Comma),