From a0c1f93392b41d17700d7a75fef8b3e78ab9f037 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Mon, 7 Jun 2021 20:03:26 -0500 Subject: [PATCH] Remove 0..0 placeholder from unexpected_eof() --- src/error.rs | 6 ++++-- src/parser.rs | 60 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/error.rs b/src/error.rs index 6168f347..b1da3128 100644 --- a/src/error.rs +++ b/src/error.rs @@ -28,7 +28,9 @@ impl Error { Self { kind, span } } - pub fn unexpected_eof() -> Self { - Self::new(ErrorKind::UnexpectedEof, 0..0) + /// Create an UnexpectedEof error, where the EOF occurs at the + /// given index in the file. + pub fn unexpected_eof(index: usize) -> Self { + Self::new(ErrorKind::UnexpectedEof, index..index) } } diff --git a/src/parser.rs b/src/parser.rs index aea4f891..b52663a9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -109,13 +109,17 @@ impl<'source> Parser<'source> { match self.lexer.next() { Some(t) if t == expected => Ok(()), Some(t) => Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())), - None => Err(Error::unexpected_eof()), + None => Err(Error::unexpected_eof(self.lexer.span().start)), } } /// Get an Identifier fn get_iden(&mut self) -> Result { - match self.lexer.next().ok_or(Error::unexpected_eof())? { + match self + .lexer + .next() + .ok_or(Error::unexpected_eof(self.lexer.span().start))? + { Token::Identifier(iden) => Ok(Iden { iden: if self.tdark { iden.replace("lang", "script") @@ -194,7 +198,10 @@ impl<'source> Parser<'source> { Token::Not => Ok(Expr::new( { - let next = self.lexer.next().ok_or(Error::unexpected_eof())?; + let next = self + .lexer + .next() + .ok_or(Error::unexpected_eof(self.lexer.span().start))?; ExprKind::Not(Box::new(self.parse_expr(next, buf)?)) }, start..self.lexer.span().end, @@ -219,7 +226,10 @@ impl<'source> Parser<'source> { .ok_or(Error::new(ErrorKind::MissingLhs, self.lexer.span()))?, ), rhs: { - let next = self.lexer.next().ok_or(Error::unexpected_eof())?; + let next = self + .lexer + .next() + .ok_or(Error::unexpected_eof(self.lexer.span().start))?; Box::new(self.parse_expr(next, &mut None)?) }, kind, @@ -230,7 +240,11 @@ impl<'source> Parser<'source> { fn expr_flow(&mut self, terminate: Token) -> Result { let mut buf = None; Ok(loop { - match self.lexer.next().ok_or(Error::unexpected_eof())? { + match self + .lexer + .next() + .ok_or(Error::unexpected_eof(self.lexer.span().start))? + { t if t == terminate => { break buf .take() @@ -247,7 +261,11 @@ impl<'source> Parser<'source> { let mut block = vec![]; loop { - match self.lexer.next().ok_or(Error::unexpected_eof())? { + match self + .lexer + .next() + .ok_or(Error::unexpected_eof(self.lexer.span().start))? + { Token::RightCurly => break, t => block.push(self.parse(t)?), } @@ -261,7 +279,11 @@ impl<'source> Parser<'source> { fn value_flow(&mut self, init: Token) -> Result { let mut buf = Some(self.parse_expr(init, &mut None)?); let r = loop { - match self.lexer.next().ok_or(Error::unexpected_eof())? { + match self + .lexer + .next() + .ok_or(Error::unexpected_eof(self.lexer.span().start))? + { Token::Print => { break StmtKind::Print(buf.take().ok_or(Error::new( ErrorKind::UnexpectedToken(Token::Print), @@ -308,13 +330,21 @@ impl<'source> Parser<'source> { let mut args = vec![]; loop { - match self.lexer.next().ok_or(Error::unexpected_eof())? { + match self + .lexer + .next() + .ok_or(Error::unexpected_eof(self.lexer.span().start))? + { Token::RightParen => break, 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())? { + match self + .lexer + .next() + .ok_or(Error::unexpected_eof(self.lexer.span().start))? + { Token::Comma => continue, Token::RightParen => break, t => { @@ -339,7 +369,11 @@ impl<'source> Parser<'source> { let mut args = vec![]; let mut buf = None; loop { - match self.lexer.next().ok_or(Error::unexpected_eof())? { + match self + .lexer + .next() + .ok_or(Error::unexpected_eof(self.lexer.span().start))? + { // End of argument list Token::RightParen => { if let Some(expr) = buf.take() { @@ -369,7 +403,11 @@ impl<'source> Parser<'source> { /// Parse variable declaration fn var_flow(&mut self) -> Result { let iden = self.get_iden()?; - let init = match self.lexer.next().ok_or(Error::unexpected_eof())? { + let init = match self + .lexer + .next() + .ok_or(Error::unexpected_eof(self.lexer.span().start))? + { Token::Equal => Some(self.expr_flow(Token::Semicolon)?), Token::Semicolon => None, t => return Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())),