From f9a59838b00d2eb41b9e6995cfe59197e32369bd Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 1 Oct 2019 02:19:12 -0700 Subject: [PATCH] Get rid of .into()'s in parser --- schala-lang/language/src/parsing.rs | 49 ++++++++++++----------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 280ec53..3bf1d37 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -353,7 +353,7 @@ impl Parser { Keyword(Interface) => self.interface_declaration().map(|decl| StatementKind::Declaration(decl)), Keyword(Impl) => self.impl_declaration().map(|decl| StatementKind::Declaration(decl)), Keyword(Import) => self.import_declaration().map(|spec| StatementKind::Import(spec)), - _ => self.expression().map(|expr| { StatementKind::Expression(expr.into()) } ), + _ => self.expression().map(|expr| { StatementKind::Expression(expr) } ), }?; Ok(Statement { kind, id: self.id_store.fresh() }) } @@ -484,7 +484,7 @@ impl Parser { let default = match self.token_handler.peek_kind() { Equals => { self.token_handler.next(); - Some(self.expression()?.into()) + Some(self.expression()?) }, _ => None }; @@ -509,7 +509,7 @@ impl Parser { }; expect!(self, Equals); - let expr = self.expression()?.into(); + let expr = self.expression()?; Ok(Declaration::Binding { name, constant, type_anno, expr }) } @@ -618,7 +618,7 @@ impl Parser { None => unreachable!() }; let rhs = self.precedence_expr(new_precedence)?; - lhs = Expression::new(self.id_store.fresh(), ExpressionKind::BinExp(operation, bx!(lhs.into()), bx!(rhs.into()))); + lhs = Expression::new(self.id_store.fresh(), ExpressionKind::BinExp(operation, bx!(lhs), bx!(rhs))); } self.parse_level -= 1; Ok(lhs) @@ -636,7 +636,7 @@ impl Parser { let prefix_op = PrefixOp::from_str(sigil.as_str()).unwrap(); Ok(Expression::new( self.id_store.fresh(), - ExpressionKind::PrefixExp(prefix_op, bx!(expr.into())) + ExpressionKind::PrefixExp(prefix_op, bx!(expr)) )) }, _ => self.call_expr() @@ -648,8 +648,7 @@ impl Parser { let mut expr = self.index_expr()?; while let LParen = self.token_handler.peek_kind() { let arguments = delimited!(self, LParen, invocation_argument, Comma, RParen); - let arguments = arguments.into_iter().collect(); - expr = Expression::new(self.id_store.fresh(), ExpressionKind::Call { f: bx!(expr.into()), arguments }); //TODO no type anno is incorrect + expr = Expression::new(self.id_store.fresh(), ExpressionKind::Call { f: bx!(expr), arguments }); //TODO no type anno is incorrect } Ok(expr) @@ -667,16 +666,16 @@ impl Parser { Equals => { self.token_handler.next(); self.token_handler.next(); - let expr = self.expression()?.into(); + let expr = self.expression()?; InvocationArgument::Keyword { name: s.clone(), expr } }, _ => { let expr = self.expression()?; - InvocationArgument::Positional(expr.into()) + InvocationArgument::Positional(expr) } } }, - _ => InvocationArgument::Positional(self.expression()?.into()) + _ => InvocationArgument::Positional(self.expression()?) }) } @@ -684,10 +683,9 @@ impl Parser { fn index_expr(&mut self) -> ParseResult { let primary = self.primary()?; Ok(if let LSquareBracket = self.token_handler.peek_kind() { - let indexers = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket) - .into_iter().map(|ex| ex.into()).collect(); + let indexers = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket); Expression::new(self.id_store.fresh(), ExpressionKind::Index { - indexee: bx!(Expression::new(self.id_store.fresh(), primary.kind).into()), + indexee: bx!(Expression::new(self.id_store.fresh(), primary.kind)), indexers, }) } else { @@ -712,8 +710,7 @@ impl Parser { #[recursive_descent_method] fn list_expr(&mut self) -> ParseResult { - let exprs = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket) - .into_iter().map(|ex| ex.into()).collect(); + let exprs = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket); Ok(Expression::new(self.id_store.fresh(), ExpressionKind::ListLiteral(exprs))) } @@ -754,10 +751,7 @@ impl Parser { match inner.len() { 0 => Ok(Expression::new(self.id_store.fresh(), TupleLiteral(vec![]))), 1 => Ok(inner.pop().unwrap()), - _ => { - let inner: Vec = inner.into_iter().map(|ex| ex.into()).collect(); - Ok(Expression::new(self.id_store.fresh(), TupleLiteral(inner))) - } + _ => Ok(Expression::new(self.id_store.fresh(), TupleLiteral(inner))) } }; self.restrictions.no_struct_literal = old_struct_value; @@ -795,10 +789,7 @@ impl Parser { #[recursive_descent_method] fn record_block(&mut self) -> ParseResult, Expression)>> { - Ok( - delimited!(self, LCurlyBrace, record_entry, Comma, RCurlyBrace) - .into_iter().map(|(s, ex)| (s, ex.into())).collect() - ) + Ok(delimited!(self, LCurlyBrace, record_entry, Comma, RCurlyBrace)) } #[recursive_descent_method] @@ -833,9 +824,9 @@ impl Parser { let lhs = self.prefix_expr()?; let ref next = self.token_handler.peek_kind(); Ok(if let Some(op) = BinOp::from_sigil_token(next) { - Discriminator::BinOp(lhs.into(), op) + Discriminator::BinOp(lhs, op) } else { - Discriminator::Simple(lhs.into()) + Discriminator::Simple(lhs) }) } @@ -1070,7 +1061,7 @@ impl Parser { LCurlyBrace => self.block(), _ => { let expr = self.expression()?; - let s = Statement { id: self.id_store.fresh(), kind: StatementKind::Expression(expr.into()) }; + let s = Statement { id: self.id_store.fresh(), kind: StatementKind::Expression(expr) }; Ok(vec![s]) } } @@ -1084,7 +1075,7 @@ impl Parser { self.restrictions.no_struct_literal = true; let x = self.while_cond(); self.restrictions.no_struct_literal = false; - x?.map(|expr| bx!(expr.into())) + x?.map(|expr| bx!(expr)) }; let body = self.block()?; Ok(Expression::new(self.id_store.fresh(), WhileExpression {condition, body})) @@ -1120,7 +1111,7 @@ impl Parser { fn enumerator(&mut self) -> ParseResult { let id = self.identifier()?; expect!(self, Operator(ref c) if **c == "<-"); - let generator = self.expression()?.into(); + let generator = self.expression()?; Ok(Enumerator { id, generator }) } @@ -1135,7 +1126,7 @@ impl Parser { }, Keyword(Kw::Return) => { self.token_handler.next(); - MonadicReturn(self.expression()?.into()) + MonadicReturn(self.expression()?) }, _ => return ParseError::new_with_token("for expressions must end in a block or 'return'", tok), })