1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use clap::Args;

use crate::{ast::Statement, lexer::Lexer, parser::Parser};

#[derive(Args)]
pub struct EmitASTOptions {
  /// The ETAC file to parse
  #[arg(short = 'f')]
  pub filepath: String,

  /// Turn debugging information on
  #[arg(short, long, action = clap::ArgAction::Count)]
  debug: u8,
}

pub fn ast(
  EmitASTOptions { filepath, debug }: &EmitASTOptions,
) -> Result<Vec<Statement>, Box<dyn std::error::Error>> {
  let source_code = std::fs::read_to_string(filepath)?;
  let lexer = Lexer::new(&source_code[..], filepath).map_err(|e| e.to_string())?;

  if *debug > 0 {
    println!("{}", lexer);
  }

  let ast = Parser::new().parse(lexer)?;

  if *debug > 0 {
    println!("{:#?}", ast);
  }

  Ok(ast)
}