Added while loop parser and (currently ignored) tests.

This commit is contained in:
Gregory Katz 2016-09-28 18:33:02 -04:00
parent 795ee428ff
commit 3e562cc986
2 changed files with 46 additions and 1 deletions

View File

@ -357,7 +357,8 @@ pub mod parsing {
|
expr_if
// TODO: IfLet
// TODO: While
|
expr_while
// TODO: WhileLet
// TODO: ForLoop
// TODO: Loop
@ -561,6 +562,18 @@ pub mod parsing {
))
));
named!(expr_while -> Expr, do_parse!(
lt: option!(terminated!(lifetime, punct!(":"))) >>
punct!("while") >>
cond: expr >>
while_block: block >>
(Expr::While(
Box::new(cond),
Box::new(while_block),
lt.map(|lt| lt.ident),
))
));
named!(expr_block -> Expr, map!(block, |b| Expr::Block(Box::new(b))));
named!(block -> Block, do_parse!(

View File

@ -41,3 +41,35 @@ fn test_named_loop() {
assert_eq!(expected, parse_expr(raw).unwrap());
}
#[test]
// Ignore test until bool parsing is available
#[ignore]
fn test_unnamed_while() {
let block = match parse_expr("{ ( 1, 3, 8 ) }").unwrap() {
Expr::Block(b) => b,
_ => panic!("Could not run test_unnamed_while: error in block parse."),
};
let raw = "while true {(1, 3, 8 )}";
let expected = Expr::While(Box::new(Expr::Lit(Lit::Bool(true))), block, None);
assert_eq!(expected, parse_expr(raw).unwrap());
}
#[test]
// Ignore test until bool parsing is available
#[ignore]
fn test_named_while() {
let block = match parse_expr("{ ( 1, 5, 9, 11) }").unwrap() {
Expr::Block(b) => b,
_ => panic!("Could not run named_while: error in block parse."),
};
let raw = "' test : while true {(1, 5, 9, 11)}";
let expected = Expr::While(Box::new(Expr::Lit(Lit::Bool(true))), block, Some("'test".into()));
assert_eq!(expected, parse_expr(raw).unwrap());
}