From a76cd56b9f8cce132555f6c3b59d76da5ae86f6b Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 30 Mar 2021 16:51:51 +0530 Subject: add new catch-all error types --- src/lisp/parse.rs | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) (limited to 'src/lisp/parse.rs') diff --git a/src/lisp/parse.rs b/src/lisp/parse.rs index 6a8de9f..737e7ad 100644 --- a/src/lisp/parse.rs +++ b/src/lisp/parse.rs @@ -25,22 +25,28 @@ impl<'lex> Parser<'lex> { } } - pub fn parse_expr(&mut self) -> Result { + pub fn parse_expr(&mut self) -> Result { let mut stack = Vec::new(); let mut total_backticks = 0; loop { let (span, token) = self.next()?; - let r: Result = match token { + let r: Result = match token { Token::LeftParen => { stack.push(Group::Parens(Vec::new())); continue; } Token::RightParen => { - let group = stack.pop().ok_or_else( - || (ParseError::new(span, ParseErrorKind::UnmatchedParen)), // unmatched paren here - )?; + let group = stack + .pop() + .ok_or_else(|| (ParseError::new(span, ParseErrorKind::UnmatchedParen)))?; match group { - Group::Parens(v) => Ok(LispExpr::List(v)), + Group::Parens(v) => { + if v.len() == 0 { + Ok(LispExpr::Unit) + } else { + Ok(LispExpr::List(v)) + } + } _ => Err(From::from(ParseError::new( span, ParseErrorKind::UnexpectedToken { @@ -59,11 +65,11 @@ impl<'lex> Parser<'lex> { .map(|n| LispExpr::Number(LispNumber::Integer(n))) .map_err(|_| ParseError::new(span, ParseErrorKind::LiteralParseError).into()), Token::String(s) => Ok(LispExpr::StringLit(s[1..s.len() - 1].into())), - Token::Char(s) => Ok(LispExpr::Char(s.chars().nth(2).ok_or_else( - || -> LispError { - ParseError::new(span, ParseErrorKind::LiteralParseError).into() - }, - )?)), + Token::Char(s) => { + Ok(LispExpr::Char(s.chars().nth(2).ok_or_else(|| { + ParseError::new(span, ParseErrorKind::LiteralParseError) + })?)) + } Token::Name(n) => Ok(name_expr(n)), Token::BackQuote => { total_backticks += 1; @@ -76,7 +82,7 @@ impl<'lex> Parser<'lex> { } Token::Comma => { if total_backticks <= 0 { - return Err(ParseError::new(span, ParseErrorKind::UnbalancedComma).into()); + return Err(ParseError::new(span, ParseErrorKind::UnbalancedComma)); } total_backticks -= 1; if let Some(&mut Group::Backticks(ref mut n)) = stack.last_mut() { @@ -88,7 +94,7 @@ impl<'lex> Parser<'lex> { } Token::CommaAt => { if total_backticks <= 0 { - return Err(ParseError::new(span, ParseErrorKind::UnbalancedComma).into()); + return Err(ParseError::new(span, ParseErrorKind::UnbalancedComma)); } total_backticks -= 1; stack.push(Group::CommaAt); @@ -109,9 +115,9 @@ impl<'lex> Parser<'lex> { }); if any_paren { - Err(ParseError::new(span, ParseErrorKind::MissingCloseParen).into()) + Err(ParseError::new(span, ParseErrorKind::MissingCloseParen)) } else { - Err(ParseError::new(span, ParseErrorKind::UnexpectedEof).into()) + Err(ParseError::new(span, ParseErrorKind::UnexpectedEof)) } } }; @@ -148,13 +154,13 @@ impl<'lex> Parser<'lex> { } } - fn next(&mut self) -> Result<(Span, Token<'lex>), LispError> { + fn next(&mut self) -> Result<(Span, Token<'lex>), ParseError> { let r = self.peek()?; self.cur_token = None; Ok(r) } - fn peek(&mut self) -> Result<(Span, Token<'lex>), LispError> { + fn peek(&mut self) -> Result<(Span, Token<'lex>), ParseError> { if let Some(tok) = self.cur_token { Ok(tok) } else { @@ -164,7 +170,7 @@ impl<'lex> Parser<'lex> { } } - pub fn parse_single_expr(&mut self) -> Result { + pub fn parse_single_expr(&mut self) -> Result { let expr = self.parse_expr()?; match self.next()? { @@ -180,7 +186,7 @@ impl<'lex> Parser<'lex> { } } - pub fn parse_exprs(&mut self) -> Result, LispError> { + pub fn parse_exprs(&mut self) -> Result, ParseError> { let mut res = Vec::new(); loop { match self.peek()? { @@ -203,7 +209,7 @@ fn name_expr(input: &str) -> LispExpr { #[cfg(test)] mod tests { use super::*; - fn parse(input: &str) -> Result { + fn parse(input: &str) -> Result { let mut parser = Parser::new(Lexer::new(input, 0)); parser.parse_single_expr() -- cgit v1.2.3