From 2b338fd3c99d253764878e630074f412536f0438 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 5 Jan 2019 18:29:24 -0800 Subject: [PATCH] Move .next() onto token_handler --- schala-lang/language/src/parsing.rs | 63 ++++++++++++++--------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 50613b4..06f4129 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -91,9 +91,6 @@ impl Parser { fn peek(&mut self) -> TokenKind { self.token_handler.peek() } - fn next(&mut self) -> Token { - self.token_handler.next() - } pub fn parse(&mut self) -> ParseResult { self.program() @@ -126,7 +123,7 @@ macro_rules! expect { { let tok = $self.token_handler.peek_token(); match tok.get_kind() { - $expected_kind if $cond => $self.next(), + $expected_kind if $cond => $self.token_handler.next(), actual_kind => { let msg = format!("Expected {}, got {:?}", print_token_pattern!($expected_kind), actual_kind); return ParseError::new_with_token(&msg, tok); @@ -155,13 +152,13 @@ macro_rules! delimited { } if !$strictness { match peek { - $( $delim )|* => { $self.next(); continue }, + $( $delim )|* => { $self.token_handler.next(); continue }, _ => () } } acc.push($self.$parse_fn()?); match $self.peek() { - $( $delim )|* => { $self.next(); continue }, + $( $delim )|* => { $self.token_handler.next(); continue }, _ if $strictness => break, _ => continue, }; @@ -302,7 +299,7 @@ impl Parser { match self.peek() { EOF => break, Newline | Semicolon => { - self.next(); + self.token_handler.next(); continue; }, _ => statements.push( @@ -338,7 +335,7 @@ impl Parser { self.type_alias() } else { let mutable = if let Keyword(Mut) = self.peek() { - self.next(); + self.token_handler.next(); true } else { false @@ -365,7 +362,7 @@ impl Parser { variants.push(self.variant_specifier()?); loop { if let Pipe = self.peek() { - self.next(); + self.token_handler.next(); variants.push(self.variant_specifier()?); } else { break; @@ -417,7 +414,7 @@ impl Parser { let (name, operator) = match self.peek() { Operator(s) => { let name = s.clone(); - self.next(); + self.token_handler.next(); (name, true) }, _ => (self.identifier()?, false) @@ -456,7 +453,7 @@ impl Parser { expect!(self, Keyword(Kw::Let)); let constant = match self.peek() { Keyword(Kw::Mut) => { - self.next(); + self.token_handler.next(); false } _ => true @@ -486,7 +483,7 @@ impl Parser { expect!(self, Keyword(Impl)); let first = self.type_name()?; let second = if let Keyword(For) = self.peek() { - self.next(); + self.token_handler.next(); Some(self.type_name()?) } else { None @@ -572,7 +569,7 @@ impl Parser { if precedence >= new_precedence { break; } - let next_tok = self.next(); + let next_tok = self.token_handler.next(); let operation = match BinOp::from_sigil_token(&next_tok.kind) { Some(sigil) => sigil, None => unreachable!() @@ -588,7 +585,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().kind { + let sigil = match self.token_handler.next().kind { Operator(op) => op, _ => unreachable!(), }; @@ -773,7 +770,7 @@ impl Parser { #[recursive_descent_method] fn else_clause(&mut self) -> ParseResult> { Ok(if let Keyword(Kw::Else) = self.peek() { - self.next(); + self.token_handler.next(); Some(self.expr_or_block()?) } else { None @@ -790,13 +787,13 @@ impl Parser { loop { match self.peek() { RCurlyBrace | EOF => break, - Semicolon | Newline => { self.next(); continue}, + Semicolon | Newline => { self.token_handler.next(); continue}, _ => { let guard_arm = self.guard_arm()?; guards.push(guard_arm); loop { match self.peek() { - Semicolon | Newline => { self.next(); continue; }, + Semicolon | Newline => { self.token_handler.next(); continue; }, _ => break, } } @@ -823,12 +820,12 @@ impl Parser { fn guard(&mut self) -> ParseResult { Ok(match self.peek() { Keyword(Kw::Is) => { - self.next(); + self.token_handler.next(); let pat = self.pattern()?; Guard::Pat(pat) }, ref tok if BinOp::from_sigil_token(tok).is_some() => { - let op = BinOp::from_sigil_token(&self.next().kind).unwrap(); + let op = BinOp::from_sigil_token(&self.token_handler.next().kind).unwrap(); let precedence = op.get_precedence(); let Expression(expr, _) = self.precedence_expr(precedence)?; Guard::HalfExpr(HalfExpr { op: Some(op), expr }) @@ -871,21 +868,21 @@ impl Parser { } }, Keyword(Kw::True) => { - self.next(); + self.token_handler.next(); Pattern::Literal(PatternLiteral::BoolPattern(true)) }, Keyword(Kw::False) => { - self.next(); + self.token_handler.next(); Pattern::Literal(PatternLiteral::BoolPattern(false)) }, StrLiteral(s) => { - self.next(); + self.token_handler.next(); Pattern::Literal(PatternLiteral::StringPattern(s)) }, DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.signed_number_literal()?, Operator(ref op) if **op == "-" => self.signed_number_literal()?, Underscore => { - self.next(); + self.token_handler.next(); Pattern::Ignored }, other => return ParseError::new_with_token(&format!("{:?} is not a valid Pattern", other), tok) @@ -897,7 +894,7 @@ impl Parser { fn signed_number_literal(&mut self) -> ParseResult { let neg = match self.peek() { Operator(ref op) if **op == "-" => { - self.next(); + self.token_handler.next(); true }, _ => false @@ -994,7 +991,7 @@ impl Parser { StatementBlock(statements.into_iter().map(|s| Node::new(s)).collect()) }, Keyword(Kw::Return) => { - self.next(); + self.token_handler.next(); MonadicReturn(self.expression()?) }, _ => return ParseError::new_with_token("for expressions must end in a block or 'return'", tok), @@ -1003,7 +1000,7 @@ impl Parser { #[recursive_descent_method] fn identifier(&mut self) -> ParseResult> { - let tok = self.next(); + let tok = self.token_handler.next(); match tok.get_kind() { Identifier(s) => Ok(s), p => ParseError::new_with_token(&format!("Expected an identifier, got {:?}", p), tok), @@ -1018,15 +1015,15 @@ impl Parser { match tok.get_kind() { DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.number_literal(), Keyword(Kw::True) => { - self.next(); + self.token_handler.next(); Ok(Expression(BoolLiteral(true), None)) }, Keyword(Kw::False) => { - self.next(); + self.token_handler.next(); Ok(Expression(BoolLiteral(false), None)) }, StrLiteral(s) => { - self.next(); + self.token_handler.next(); Ok(Expression(StringLiteral(s.clone()), None)) } e => ParseError::new_with_token(&format!("Expected a literal expression, got {:?}", e), tok), @@ -1044,7 +1041,7 @@ impl Parser { #[recursive_descent_method] fn int_literal(&mut self) -> ParseResult { use self::ExpressionType::*; - let tok = self.next(); + let tok = self.token_handler.next(); match tok.get_kind() { BinNumberSigil => { let digits = self.digits()?; @@ -1065,7 +1062,7 @@ impl Parser { use self::ExpressionType::*; let mut digits = self.digits()?; if let Period = self.peek() { - self.next(); + self.token_handler.next(); digits.push_str("."); digits.push_str(&self.digits()?); match digits.parse::() { @@ -1086,8 +1083,8 @@ impl Parser { let mut ds = String::new(); loop { match self.peek() { - Underscore => { self.next(); continue; }, - DigitGroup(ref s) => { self.next(); ds.push_str(s)}, + Underscore => { self.token_handler.next(); continue; }, + DigitGroup(ref s) => { self.token_handler.next(); ds.push_str(s)}, _ => break, } }