use std::fmt::Write;
use std::{fmt, sync::Arc};
use serde::{Deserialize, Serialize};
use crate::{formatting::source_lines, lexer::loc_to_pos};
#[derive(Clone)]
pub struct Context {
pub source: Arc<Source>,
pub left: usize,
pub right: usize,
}
impl fmt::Display for Context {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.source_position())?;
let lines = source_lines(&self.source, self.left, 0).replace('\n', "\n\t");
writeln!(f, ":\n\t{}", lines)?;
Ok(())
}
}
impl Context {
pub(crate) fn new(source: Arc<Source>, left: usize, right: usize) -> Self {
Self {
source,
left,
right,
}
}
pub(crate) fn source_position(&self) -> String {
let mut f = String::new();
let (row, column) = loc_to_pos(&self.source.src, self.left);
write!(f, " at line {}, column {}", row + 1, column + 1).unwrap();
if let Some(ref filename) = self.source.filename {
write!(f, " of file {}", filename).unwrap();
}
f
}
}
#[derive(Clone)]
pub enum SourceInfo {
Parser(Context),
TemporaryVariable,
Ffi,
Test,
}
impl fmt::Debug for SourceInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parser(_) => f.write_str("SourceInfo::Parser"),
Self::TemporaryVariable => f.write_str("SourceInfo::TemporaryVariable"),
Self::Ffi => f.write_str("SourceInfo::Ffi"),
Self::Test => f.write_str("SourceInfo::Test"),
}
}
}
impl SourceInfo {
pub fn ffi() -> Self {
Self::Ffi
}
pub(crate) fn parser(source: Arc<Source>, left: usize, right: usize) -> Self {
Self::Parser(Context::new(source, left, right))
}
}
#[derive(PartialEq, Eq, Serialize, Deserialize)]
pub struct Source {
pub filename: Option<String>,
pub src: String,
}
impl Source {
pub fn new<T: AsRef<str>>(src: T) -> Self {
Self {
filename: None,
src: src.as_ref().into(),
}
}
pub fn new_with_name<T: AsRef<str>, U: AsRef<str>>(filename: T, src: U) -> Self {
Self {
filename: Some(filename.as_ref().into()),
src: src.as_ref().into(),
}
}
}