regenerate.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. use matzo::grammar;
  2. use matzo::lexer;
  3. use std::io::Write;
  4. fn main() -> Result<(), Box<dyn std::error::Error>> {
  5. for exp in std::fs::read_dir("tests")? {
  6. let exp = exp?.path().canonicalize()?;
  7. let fname = exp.file_name().ok_or("bad file name")?.to_string_lossy();
  8. if let Some(prefix) = fname.strip_suffix(".matzo") {
  9. println!("regenerating {}.matzo", prefix);
  10. let exp_filename = |new_suffix| {
  11. let mut f = exp.clone();
  12. f.pop();
  13. f.push(format!("{}.{}", prefix, new_suffix));
  14. f
  15. };
  16. let mut ast = matzo::ast::ASTArena::new();
  17. let src = std::fs::read_to_string(&exp)?;
  18. let tokens = lexer::tokens(&src);
  19. if let Ok(stmts) = grammar::StmtsParser::new().parse(&mut ast, tokens) {
  20. let mut f = std::fs::File::create(exp_filename("parsed"))?;
  21. for stmt in stmts {
  22. writeln!(f, "{:#?}", stmt.show(&ast))?;
  23. }
  24. }
  25. }
  26. }
  27. Ok(())
  28. }