Get rid of .into()'s in parser

This commit is contained in:
greg 2019-10-01 02:19:12 -07:00
parent f02d7cb924
commit f9a59838b0

View File

@ -353,7 +353,7 @@ impl Parser {
Keyword(Interface) => self.interface_declaration().map(|decl| StatementKind::Declaration(decl)), Keyword(Interface) => self.interface_declaration().map(|decl| StatementKind::Declaration(decl)),
Keyword(Impl) => self.impl_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)), 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() }) Ok(Statement { kind, id: self.id_store.fresh() })
} }
@ -484,7 +484,7 @@ impl Parser {
let default = match self.token_handler.peek_kind() { let default = match self.token_handler.peek_kind() {
Equals => { Equals => {
self.token_handler.next(); self.token_handler.next();
Some(self.expression()?.into()) Some(self.expression()?)
}, },
_ => None _ => None
}; };
@ -509,7 +509,7 @@ impl Parser {
}; };
expect!(self, Equals); expect!(self, Equals);
let expr = self.expression()?.into(); let expr = self.expression()?;
Ok(Declaration::Binding { name, constant, type_anno, expr }) Ok(Declaration::Binding { name, constant, type_anno, expr })
} }
@ -618,7 +618,7 @@ impl Parser {
None => unreachable!() None => unreachable!()
}; };
let rhs = self.precedence_expr(new_precedence)?; 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; self.parse_level -= 1;
Ok(lhs) Ok(lhs)
@ -636,7 +636,7 @@ impl Parser {
let prefix_op = PrefixOp::from_str(sigil.as_str()).unwrap(); let prefix_op = PrefixOp::from_str(sigil.as_str()).unwrap();
Ok(Expression::new( Ok(Expression::new(
self.id_store.fresh(), self.id_store.fresh(),
ExpressionKind::PrefixExp(prefix_op, bx!(expr.into())) ExpressionKind::PrefixExp(prefix_op, bx!(expr))
)) ))
}, },
_ => self.call_expr() _ => self.call_expr()
@ -648,8 +648,7 @@ impl Parser {
let mut expr = self.index_expr()?; let mut expr = self.index_expr()?;
while let LParen = self.token_handler.peek_kind() { while let LParen = self.token_handler.peek_kind() {
let arguments = delimited!(self, LParen, invocation_argument, Comma, RParen); 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), arguments }); //TODO no type anno is incorrect
expr = Expression::new(self.id_store.fresh(), ExpressionKind::Call { f: bx!(expr.into()), arguments }); //TODO no type anno is incorrect
} }
Ok(expr) Ok(expr)
@ -667,16 +666,16 @@ impl Parser {
Equals => { Equals => {
self.token_handler.next(); self.token_handler.next();
self.token_handler.next(); self.token_handler.next();
let expr = self.expression()?.into(); let expr = self.expression()?;
InvocationArgument::Keyword { name: s.clone(), expr } InvocationArgument::Keyword { name: s.clone(), expr }
}, },
_ => { _ => {
let expr = self.expression()?; 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<Expression> { fn index_expr(&mut self) -> ParseResult<Expression> {
let primary = self.primary()?; let primary = self.primary()?;
Ok(if let LSquareBracket = self.token_handler.peek_kind() { Ok(if let LSquareBracket = self.token_handler.peek_kind() {
let indexers = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket) let indexers = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket);
.into_iter().map(|ex| ex.into()).collect();
Expression::new(self.id_store.fresh(), ExpressionKind::Index { 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, indexers,
}) })
} else { } else {
@ -712,8 +710,7 @@ impl Parser {
#[recursive_descent_method] #[recursive_descent_method]
fn list_expr(&mut self) -> ParseResult<Expression> { fn list_expr(&mut self) -> ParseResult<Expression> {
let exprs = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket) let exprs = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket);
.into_iter().map(|ex| ex.into()).collect();
Ok(Expression::new(self.id_store.fresh(), ExpressionKind::ListLiteral(exprs))) Ok(Expression::new(self.id_store.fresh(), ExpressionKind::ListLiteral(exprs)))
} }
@ -754,10 +751,7 @@ impl Parser {
match inner.len() { match inner.len() {
0 => Ok(Expression::new(self.id_store.fresh(), TupleLiteral(vec![]))), 0 => Ok(Expression::new(self.id_store.fresh(), TupleLiteral(vec![]))),
1 => Ok(inner.pop().unwrap()), 1 => Ok(inner.pop().unwrap()),
_ => { _ => Ok(Expression::new(self.id_store.fresh(), TupleLiteral(inner)))
let inner: Vec<Expression> = inner.into_iter().map(|ex| ex.into()).collect();
Ok(Expression::new(self.id_store.fresh(), TupleLiteral(inner)))
}
} }
}; };
self.restrictions.no_struct_literal = old_struct_value; self.restrictions.no_struct_literal = old_struct_value;
@ -795,10 +789,7 @@ impl Parser {
#[recursive_descent_method] #[recursive_descent_method]
fn record_block(&mut self) -> ParseResult<Vec<(Rc<String>, Expression)>> { fn record_block(&mut self) -> ParseResult<Vec<(Rc<String>, Expression)>> {
Ok( Ok(delimited!(self, LCurlyBrace, record_entry, Comma, RCurlyBrace))
delimited!(self, LCurlyBrace, record_entry, Comma, RCurlyBrace)
.into_iter().map(|(s, ex)| (s, ex.into())).collect()
)
} }
#[recursive_descent_method] #[recursive_descent_method]
@ -833,9 +824,9 @@ impl Parser {
let lhs = self.prefix_expr()?; let lhs = self.prefix_expr()?;
let ref next = self.token_handler.peek_kind(); let ref next = self.token_handler.peek_kind();
Ok(if let Some(op) = BinOp::from_sigil_token(next) { Ok(if let Some(op) = BinOp::from_sigil_token(next) {
Discriminator::BinOp(lhs.into(), op) Discriminator::BinOp(lhs, op)
} else { } else {
Discriminator::Simple(lhs.into()) Discriminator::Simple(lhs)
}) })
} }
@ -1070,7 +1061,7 @@ impl Parser {
LCurlyBrace => self.block(), LCurlyBrace => self.block(),
_ => { _ => {
let expr = self.expression()?; 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]) Ok(vec![s])
} }
} }
@ -1084,7 +1075,7 @@ impl Parser {
self.restrictions.no_struct_literal = true; self.restrictions.no_struct_literal = true;
let x = self.while_cond(); let x = self.while_cond();
self.restrictions.no_struct_literal = false; self.restrictions.no_struct_literal = false;
x?.map(|expr| bx!(expr.into())) x?.map(|expr| bx!(expr))
}; };
let body = self.block()?; let body = self.block()?;
Ok(Expression::new(self.id_store.fresh(), WhileExpression {condition, body})) Ok(Expression::new(self.id_store.fresh(), WhileExpression {condition, body}))
@ -1120,7 +1111,7 @@ impl Parser {
fn enumerator(&mut self) -> ParseResult<Enumerator> { fn enumerator(&mut self) -> ParseResult<Enumerator> {
let id = self.identifier()?; let id = self.identifier()?;
expect!(self, Operator(ref c) if **c == "<-"); expect!(self, Operator(ref c) if **c == "<-");
let generator = self.expression()?.into(); let generator = self.expression()?;
Ok(Enumerator { id, generator }) Ok(Enumerator { id, generator })
} }
@ -1135,7 +1126,7 @@ impl Parser {
}, },
Keyword(Kw::Return) => { Keyword(Kw::Return) => {
self.token_handler.next(); 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), _ => return ParseError::new_with_token("for expressions must end in a block or 'return'", tok),
}) })