mirror of
https://gitee.com/openharmony/third_party_rust_syn
synced 2024-11-30 03:10:24 +00:00
Added while loop parser and (currently ignored) tests.
This commit is contained in:
parent
795ee428ff
commit
3e562cc986
15
src/expr.rs
15
src/expr.rs
@ -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!(
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user