Builds a [mz_expr::MirRelationExpr] from a string.
(catalog: &TestCatalog, s: &str)
| 28 | |
| 29 | /// Builds a [mz_expr::MirRelationExpr] from a string. |
| 30 | pub fn try_parse_mir(catalog: &TestCatalog, s: &str) -> Result<mz_expr::MirRelationExpr, String> { |
| 31 | // Define a Parser that constructs a (read-only) parsing context `ctx` and |
| 32 | // delegates to `relation::parse_expr` by passing a `ctx` as a shared ref. |
| 33 | let parser = move |input: ParseStream| { |
| 34 | let ctx = Ctx { catalog }; |
| 35 | relation::parse_expr(&ctx, input) |
| 36 | }; |
| 37 | // Since the syn lexer doesn't parse comments, we replace all `// {` |
| 38 | // occurrences in the input string with `:: {`. |
| 39 | let s = s.replace("// {", ":: {"); |
| 40 | // Call the parser with the given input string. |
| 41 | let mut expr = parser.parse_str(&s).map_err(|err| { |
| 42 | let (line, column) = (err.span().start().line, err.span().start().column); |
| 43 | format!("parse error at {line}:{column}:\n{err}\n") |
| 44 | })?; |
| 45 | // Fix the types of the local let bindings of the parsed expression in a |
| 46 | // post-processing pass. |
| 47 | relation::fix_types(&mut expr, &mut relation::FixTypesCtx::default())?; |
| 48 | // Return the parsed, post-processed expression. |
| 49 | Ok(expr) |
| 50 | } |
| 51 | |
| 52 | /// Builds a source definition from a string. |
| 53 | pub fn try_parse_def(catalog: &TestCatalog, s: &str) -> Result<Def, String> { |