regenerate.rs 946 B

1234567891011121314151617181920212223242526272829
  1. use matzo::lexer;
  2. use matzo::grammar;
  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 src = std::fs::read_to_string(&exp)?;
  17. let tokens = lexer::tokens(&src);
  18. if let Ok(ast) = grammar::StmtsParser::new().parse(tokens) {
  19. let mut f = std::fs::File::create(exp_filename("parsed"))?;
  20. writeln!(f, "{:#?}", ast)?;
  21. }
  22. }
  23. }
  24. Ok(())
  25. }