Entry point of statement and expression parsing
(syn_stmts: &[syn::Stmt])
| 6 | |
| 7 | // Entry point of statement and expression parsing |
| 8 | pub(crate) fn parse_body(syn_stmts: &[syn::Stmt]) -> Vec<StmtOrExpr> { |
| 9 | syn_stmts.iter().fold(Vec::new(), |mut acc, syn_stmt| { |
| 10 | match syn_stmt { |
| 11 | // Parse `let a;` let or `let a = 1;` let assign declaration |
| 12 | syn::Stmt::Local(local) => acc.extend(local.parse_body()), |
| 13 | // Parse statements or expressions |
| 14 | // - a statement should ends with a `;` |
| 15 | // - a expression does NOT end with a `;` |
| 16 | syn::Stmt::Expr(expr, semi) => acc.extend((expr, semi).parse_body()), |
| 17 | // Parse items, we only interested in these two items |
| 18 | // - `const PI_CONST: f64 = 3.14;` |
| 19 | // - `static PI_STATIC: f64 = 3.14;` |
| 20 | syn::Stmt::Item(item) => acc.extend(item.parse_body()), |
| 21 | // Parse macro invocation, it can be a |
| 22 | // - a statement: `println!("HI");` |
| 23 | // - a expression: `format!("HI")` |
| 24 | syn::Stmt::Macro(stmt_macro) => acc.extend(stmt_macro.parse_body()), |
| 25 | } |
| 26 | acc |
| 27 | }) |
| 28 | } |
| 29 | |
| 30 | // Parse any AST into statement or expression |
| 31 | pub(crate) trait ParseBody { |
no test coverage detected