// FILE: src/parser/core/utils.rs

use crate::parser::lexer::{Token, TokenKind};

pub struct Parser<'a> {
    pub tokens: &'a [Token],
    pub pos: usize,
    pub verbose: bool,
}

impl<'a> Parser<'a> {
    pub fn is_done(&self) -> bool {
        self.pos >= self.tokens.len()
    }

    pub fn current(&self) -> Option<&'a Token> {
        self.tokens.get(self.pos)
    }

    pub fn advance(&mut self) {
        if self.verbose {
            if let Some(t) = self.current() {
                eprintln!("[parser2] advance => consuming token at pos={} => {:?}", self.pos, t);
            }
        }
        self.pos += 1;
    }

    pub fn expect(&mut self, expected: &TokenKind) -> Result<(), crate::parser::core::driver::ParseError> {
        let cur = self.current().ok_or_else(|| crate::parser::core::driver::ParseError {
            msg: format!("Expected {:?}, found EOF", expected),
            line: 0,
            col: 0,
        })?;
        if std::mem::discriminant(&cur.kind) == std::mem::discriminant(expected) {
            self.advance();
            Ok(())
        } else {
            Err(crate::parser::core::driver::ParseError {
                msg: format!("Expected {:?}, found {:?}", expected, cur.kind),
                line: cur.line,
                col: cur.col,
            })
        }
    }
}
