1use std::{2 any::Any,3 fmt::{self, Debug, Display},4 hash::{Hash, Hasher},5 path::{Path, PathBuf},6 rc::Rc,7};89use jrsonnet_gcmodule::Acyclic;10use jrsonnet_interner::{IBytes, IStr};1112use crate::location::{CodeLocation, location_to_offset, offset_to_location};1314macro_rules! any_ext_methods {15 ($T:ident) => {16 fn as_any(&self) -> &dyn Any;17 fn dyn_hash(&self, hasher: &mut dyn Hasher);18 fn dyn_eq(&self, other: &dyn $T) -> bool;19 fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;20 };21}22macro_rules! any_ext_impl {23 ($T:ident) => {24 fn as_any(&self) -> &dyn Any {25 self26 }27 fn dyn_hash(&self, mut hasher: &mut dyn Hasher) {28 self.hash(&mut hasher)29 }30 fn dyn_eq(&self, other: &dyn $T) -> bool {31 let Some(other) = other.as_any().downcast_ref::<Self>() else {32 return false;33 };34 let this = <Self as $T>::as_any(self)35 .downcast_ref::<Self>()36 .expect("restricted by impl");37 this == other38 }39 fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {40 <Self as std::fmt::Debug>::fmt(self, fmt)41 }42 };43}44macro_rules! any_ext {45 ($T:ident) => {46 impl Hash for dyn $T {47 fn hash<H: Hasher>(&self, state: &mut H) {48 self.dyn_hash(state)49 }50 }51 impl PartialEq for dyn $T {52 fn eq(&self, other: &Self) -> bool {53 self.dyn_eq(other)54 }55 }56 impl Eq for dyn $T {}57 };58}59pub trait SourcePathT: Acyclic + Debug + Display {60 61 62 fn is_default(&self) -> bool;63 fn path(&self) -> Option<&Path>;64 any_ext_methods!(SourcePathT);65}66any_ext!(SourcePathT);67686970717273747576777879808182#[derive(Eq, Clone, Acyclic)]83pub struct SourcePath(Rc<dyn SourcePathT>);84impl SourcePath {85 pub fn new(inner: impl SourcePathT) -> Self {86 Self(Rc::new(inner))87 }88 pub fn downcast_ref<T: SourcePathT>(&self) -> Option<&T> {89 self.0.as_any().downcast_ref()90 }91 pub fn is_default(&self) -> bool {92 self.0.is_default()93 }94 pub fn path(&self) -> Option<&Path> {95 self.0.path()96 }97}98impl Hash for SourcePath {99 fn hash<H: Hasher>(&self, state: &mut H) {100 self.0.hash(state);101 }102}103impl PartialEq for SourcePath {104 #[allow(clippy::op_ref)]105 fn eq(&self, other: &Self) -> bool {106 &*self.0 == &*other.0107 }108}109impl Display for SourcePath {110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {111 write!(f, "{}", self.0)112 }113}114impl Debug for SourcePath {115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {116 write!(f, "{:?}", self.0)117 }118}119impl Default for SourcePath {120 fn default() -> Self {121 Self(Rc::new(SourceDefault))122 }123}124125#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]126struct SourceDefault;127impl Display for SourceDefault {128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {129 write!(f, "<default>")130 }131}132impl SourcePathT for SourceDefault {133 fn is_default(&self) -> bool {134 true135 }136 fn path(&self) -> Option<&Path> {137 None138 }139 any_ext_impl!(SourcePathT);140}141142#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]143pub struct SourceDefaultIgnoreJpath;144impl Display for SourceDefaultIgnoreJpath {145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {146 write!(f, "<default (ignoring jpath)>")147 }148}149impl SourcePathT for SourceDefaultIgnoreJpath {150 fn is_default(&self) -> bool {151 true152 }153 fn path(&self) -> Option<&Path> {154 None155 }156 any_ext_impl!(SourcePathT);157}158159160161162163164#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]165pub struct SourceFile(PathBuf);166impl SourceFile {167 pub fn new(path: PathBuf) -> Self {168 Self(path)169 }170 pub fn path(&self) -> &Path {171 &self.0172 }173}174impl Display for SourceFile {175 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {176 write!(f, "{}", self.0.display())177 }178}179impl SourcePathT for SourceFile {180 fn is_default(&self) -> bool {181 false182 }183 fn path(&self) -> Option<&Path> {184 Some(&self.0)185 }186 any_ext_impl!(SourcePathT);187}188189190191192#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]193pub struct SourceDirectory(PathBuf);194impl SourceDirectory {195 pub fn new(path: PathBuf) -> Self {196 Self(path)197 }198 pub fn path(&self) -> &Path {199 &self.0200 }201}202impl Display for SourceDirectory {203 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {204 write!(f, "{}", self.0.display())205 }206}207impl SourcePathT for SourceDirectory {208 fn is_default(&self) -> bool {209 false210 }211 fn path(&self) -> Option<&Path> {212 Some(&self.0)213 }214 any_ext_impl!(SourcePathT);215}216217218219220221#[derive(Acyclic, Hash, PartialEq, Eq, Clone)]222pub struct SourceVirtual(pub IStr);223impl Display for SourceVirtual {224 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {225 write!(f, "virtual:{}", self.0)226 }227}228impl fmt::Debug for SourceVirtual {229 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {230 write!(f, "virtual:{}", self.0)231 }232}233impl SourcePathT for SourceVirtual {234 fn is_default(&self) -> bool {235 true236 }237 fn path(&self) -> Option<&Path> {238 None239 }240 any_ext_impl!(SourcePathT);241}242243244245246247248#[allow(clippy::derived_hash_with_manual_eq)]249#[derive(Acyclic, Debug, Hash)]250pub struct SourceFifo(pub String, pub IBytes);251impl PartialEq for SourceFifo {252 fn eq(&self, other: &Self) -> bool {253 std::ptr::eq(self, other)254 }255}256impl fmt::Display for SourceFifo {257 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {258 write!(f, "fifo({:?})", self.0)259 }260}261impl SourcePathT for SourceFifo {262 fn is_default(&self) -> bool {263 264 true265 }266267 fn path(&self) -> Option<&Path> {268 None269 }270271 any_ext_impl!(SourcePathT);272}273274275276#[derive(Clone, PartialEq, Eq, Acyclic)]277pub struct Source(pub Rc<(SourcePath, IStr)>);278279impl Source {280 pub fn new(path: SourcePath, code: IStr) -> Self {281 Self(Rc::new((path, code)))282 }283284 pub fn new_virtual(name: IStr, code: IStr) -> Self {285 Self::new(SourcePath::new(SourceVirtual(name)), code)286 }287288 pub fn code(&self) -> &str {289 &self.0.1290 }291292 pub fn source_path(&self) -> &SourcePath {293 &self.0.0294 }295296 pub fn map_source_locations<const S: usize>(&self, locs: &[u32; S]) -> [CodeLocation; S] {297 offset_to_location(&self.0.1, locs)298 }299 pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {300 location_to_offset(&self.0.1, line, column)301 }302}303impl fmt::Debug for Source {304 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {305 write!(f, "{:?}", self.0.0)306 }307}