use crate::model::{CompilerError, Program};
use common::logging::Logger;
use parser::{ParseResult, Parser};
// use semantic_analyser::Analyser;

pub mod lexer;
pub mod parser;
// pub mod semantic_analyser;

pub fn generate_ast(input: &str, logger: &Logger) -> Result<Program, CompilerError> {
    logger.info("Tokenising Input...");

    let lexer = lexer::Lexer::new(&input);
    let tokens = lexer.collect::<Vec<_>>();

    // println!("{tokens:#?}");

    logger.info(&format!("Parsing {} Tokens...", tokens.len()));

    let mut parser = Parser::new(tokens);
    let ast = match parser.parse() {
        ParseResult::Accept(ast) => ast,
        ParseResult::Reject(e) => return Err(e),
        ParseResult::Deny => {
            return Err(CompilerError::Generic("Parser used ::Deny".to_string()));
        }
    };
    // println!("{ast:#?}");

    logger.info("Analyzing AST...");
    logger.info("Checking Type Information...");

    // let mut analyser = Analyser::new();
    // analyser.analyse(ast.clone()).unwrap();

    logger.info("Type Checking Complete...");
    Ok(ast)
}
