use crate::compiling::Token;
use crate::compiling::failing::position_info::PositionInfo;
pub struct DefaultMetadata {
pub index: usize,
pub expr: Vec<Token>,
pub path: Option<String>,
pub code: Option<String>,
pub indent: Option<usize>
}
impl Metadata for DefaultMetadata {
fn new(tokens: Vec<Token>, path: Option<String>, code: Option<String>) -> Self {
DefaultMetadata {
index: 0,
expr: tokens,
path,
code,
indent: None
}
}
fn get_token_at(&self, index: usize) -> Option<Token> {
self.expr.get(index).cloned()
}
fn set_index(&mut self, index: usize) {
self.index = index
}
fn get_index(&self) -> usize {
self.index
}
fn get_debug(&mut self) -> Option<usize> {
self.indent
}
fn set_debug(&mut self, indent: usize) {
self.indent = Some(indent)
}
fn get_path(&self) -> Option<String> {
self.path.clone()
}
fn get_code(&self) -> Option<&String> {
self.code.as_ref()
}
}
pub trait Metadata {
fn new(tokens: Vec<Token>, path: Option<String>, code: Option<String>) -> Self;
fn get_token_at(&self, index: usize) -> Option<Token>;
fn get_index(&self) -> usize;
fn set_index(&mut self, index: usize);
fn get_debug(&mut self) -> Option<usize>;
fn set_debug(&mut self, indent: usize);
fn get_path(&self) -> Option<String>;
fn get_code(&self) -> Option<&String>;
fn get_trace(&self) -> Vec<PositionInfo> {
vec![]
}
fn increment_index(&mut self) {
let index = self.get_index();
self.set_index(index + 1);
}
fn get_current_token(&self) -> Option<Token> {
let index = self.get_index();
self.get_token_at(index)
}
fn offset_index(&mut self, offset: isize) {
let index = self.get_index();
self.set_index((index as isize + offset) as usize);
}
}