bfox/bfox-parser/src/lib.rs
2024-05-26 23:38:06 +03:00

56 lines
1.0 KiB
Rust

use types::{token::Token, Loc};
mod types;
pub struct Parser {
tokens: Vec<Token>,
chars: Vec<char>,
loc: Loc,
}
impl Parser {
pub fn new() -> Self {
Self {
tokens: Vec::new(),
chars: Vec::new(),
loc: Loc::default(),
}
}
pub fn parse(&mut self, code: String, file_name: String) -> anyhow::Result<()> {
self.loc.file = file_name;
self.loc.col = 0;
self.loc.line = 0;
self.chars = code.chars().collect();
// Parse here
Ok(())
}
/// Pops one token
fn next(&mut self) -> Option<char> {
if let Some(c) = self.chars.pop() {
match c {
'\n' => {
self.loc.line += 1;
self.loc.col = 0;
}
_ => {
self.loc.col += 1;
}
}
return Some(c);
}
None
}
pub fn tokens(&self) -> &[Token] {
&self.tokens
}
}