#![allow(clippy::upper_case_acronyms)] mod new; mod test; use std::fmt; use crate::{ ast::{ASTItem, AST}, identifier::{Id, IdStore}, }; pub struct Parser { id_store: IdStore, } impl Parser { pub(crate) fn new() -> Self { Self { id_store: IdStore::new() } } pub(crate) fn parse(&mut self, input: &str) -> Result { use peg::str::LineCol; new::schala_parser::program(input, self).map_err(|err: peg::error::ParseError| { let msg = err.to_string(); ParseError { msg, location: err.location.offset.into() } }) } fn fresh(&mut self) -> Id { self.id_store.fresh() } } /// Represents a parsing error #[derive(Debug)] pub struct ParseError { pub msg: String, pub location: Location, } #[derive(Debug, Clone, Copy, PartialEq, Default)] pub struct Location { pub(crate) offset: usize, } impl From for Location { fn from(offset: usize) -> Self { Self { offset } } } impl fmt::Display for Location { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.offset) } }