Removed unwraps, added Rlyeh

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

View file

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

View file

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