1
2
3
4
5
6
7
8
9
10
11
12
13
use crate::{lexer::Lexer, parser::Parser};

pub fn ast_from_code_str(code: &str, test_name: &str) -> String {
  let ast = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
    let lexer = Lexer::new(code, test_name).expect("Lexer to not fail in tests");
    Parser::new().parse(lexer)
  }));

  match ast.unwrap() {
    Ok(ast) => format!("{:#?}", ast),
    Err(err) => format!("{:#?}", err),
  }
}