From 846eeae04caa445797d6304cc830be4deb1ea6c2 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 5 Jan 2019 17:50:54 -0800 Subject: [PATCH] More use of Token instead of TokenKind --- schala-lang/language/src/parsing.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 73c5680..f30b39f 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -73,8 +73,8 @@ impl TokenHandler { fn peek_token(&mut self) -> Token { self.tokens.peek().map(|t: &Token| { t.clone()}).unwrap_or(Token { kind: TokenKind::EOF, offset: self.end_of_file}) } - fn next(&mut self) -> TokenKind { - self.tokens.next().map(|ref t| { t.kind.clone() }).unwrap_or(TokenKind::EOF) + fn next(&mut self) -> Token { + self.tokens.next().unwrap_or(Token { kind: TokenKind::EOF, offset: self.end_of_file}) } } @@ -91,7 +91,7 @@ impl Parser { fn peek(&mut self) -> TokenKind { self.token_handler.peek() } - fn next(&mut self) -> TokenKind { + fn next(&mut self) -> Token { self.token_handler.next() } @@ -572,7 +572,8 @@ impl Parser { if precedence >= new_precedence { break; } - let operation = match BinOp::from_sigil_token(&self.next()) { + let next_tok = self.next(); + let operation = match BinOp::from_sigil_token(&next_tok.kind) { Some(sigil) => sigil, None => unreachable!() }; @@ -587,7 +588,7 @@ impl Parser { fn prefix_expr(&mut self) -> ParseResult { match self.peek() { Operator(ref op) if PrefixOp::is_prefix(&*op) => { - let sigil = match self.next() { + let sigil = match self.next().kind { Operator(op) => op, _ => unreachable!(), }; @@ -826,7 +827,7 @@ impl Parser { Guard::Pat(pat) }, ref tok if BinOp::from_sigil_token(tok).is_some() => { - let op = BinOp::from_sigil_token(&self.next()).unwrap(); + let op = BinOp::from_sigil_token(&self.next().kind).unwrap(); let precedence = op.get_precedence(); let Expression(expr, _) = self.precedence_expr(precedence)?; Guard::HalfExpr(HalfExpr { op: Some(op), expr }) @@ -1001,9 +1002,10 @@ impl Parser { #[recursive_descent_method] fn identifier(&mut self) -> ParseResult> { - match self.next() { + let tok = self.next(); + match tok.get_kind() { Identifier(s) => Ok(s), - p => ParseError::new(&format!("Expected an identifier, got {:?}", p)), + p => ParseError::new_with_token(&format!("Expected an identifier, got {:?}", p), tok), } } @@ -1041,7 +1043,8 @@ impl Parser { #[recursive_descent_method] fn int_literal(&mut self) -> ParseResult { use self::ExpressionType::*; - match self.next() { + let tok = self.next(); + match tok.get_kind() { BinNumberSigil => { let digits = self.digits()?; let n = parse_binary(digits)?; @@ -1052,7 +1055,7 @@ impl Parser { let n = parse_hex(digits)?; Ok(Expression(NatLiteral(n), None)) }, - _ => return ParseError::new("Expected '0x' or '0b'"), + _ => return ParseError::new_with_token("Expected '0x' or '0b'", tok), } }