forked from Orchid/orchid
New macro system and stdlib additions
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
|
||||
use std::fmt;
|
||||
use std::hash::Hash;
|
||||
use std::ops::Range;
|
||||
use std::ops::{Add, AddAssign, Range};
|
||||
|
||||
use futures::future::join_all;
|
||||
use trait_set::trait_set;
|
||||
|
||||
use crate::error::ErrPos;
|
||||
@@ -25,6 +26,7 @@ pub enum Pos {
|
||||
Gen(CodeGenInfo),
|
||||
/// Range and file
|
||||
SrcRange(SrcRange),
|
||||
Multi(Vec<Pos>),
|
||||
}
|
||||
impl Pos {
|
||||
pub fn pretty_print(&self, get_src: &mut impl GetSrc) -> String {
|
||||
@@ -39,6 +41,7 @@ impl Pos {
|
||||
match_mapping!(api, api::Location => Pos {
|
||||
None, Inherit, SlotTarget,
|
||||
Gen(cgi => CodeGenInfo::from_api(cgi, i).await),
|
||||
Multi(v => join_all(v.iter().map(|l| Pos::from_api(l, i))).await)
|
||||
} {
|
||||
api::Location::SourceRange(sr) => Self::SrcRange(SrcRange::from_api(sr, i).await)
|
||||
})
|
||||
@@ -47,6 +50,7 @@ impl Pos {
|
||||
match_mapping!(self, Pos => api::Location {
|
||||
None, Inherit, SlotTarget,
|
||||
Gen(cgi.to_api()),
|
||||
Multi(v => v.iter().map(|pos| pos.to_api()).collect()),
|
||||
} {
|
||||
Self::SrcRange(sr) => api::Location::SourceRange(sr.to_api()),
|
||||
})
|
||||
@@ -60,9 +64,36 @@ impl fmt::Display for Pos {
|
||||
Pos::None => f.write_str("N/A"),
|
||||
Pos::Gen(g) => write!(f, "{g}"),
|
||||
Pos::SrcRange(sr) => write!(f, "{sr}"),
|
||||
Pos::Multi(posv) => {
|
||||
write!(f, "{}", posv[0])?;
|
||||
for pos in posv {
|
||||
write!(f, "+{}", pos)?;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Add for Pos {
|
||||
type Output = Pos;
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
match (self, rhs) {
|
||||
(Pos::Multi(l), Pos::Multi(r)) => Pos::Multi(l.into_iter().chain(r).collect()),
|
||||
(Pos::None, any) => any,
|
||||
(any, Pos::None) => any,
|
||||
(Pos::Multi(v), single) => Pos::Multi(v.into_iter().chain([single]).collect()),
|
||||
(single, Pos::Multi(v)) => Pos::Multi([single].into_iter().chain(v).collect()),
|
||||
(l, r) => Pos::Multi(vec![l, r]),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl AddAssign for Pos {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
let mut tmp = Pos::None;
|
||||
std::mem::swap(&mut tmp, self);
|
||||
*self = tmp + rhs;
|
||||
}
|
||||
}
|
||||
|
||||
/// Exact source code location. Includes where the code was loaded from, what
|
||||
/// the original source code was, and a byte range.
|
||||
@@ -77,7 +108,7 @@ impl SrcRange {
|
||||
}
|
||||
/// Create a dud [SourceRange] for testing. Its value is unspecified and
|
||||
/// volatile.
|
||||
pub async fn mock(i: &Interner) -> Self { Self { range: 0..1, path: sym!(test; i).await } }
|
||||
pub async fn mock(i: &Interner) -> Self { Self { range: 0..1, path: sym!(test; i) } }
|
||||
/// Path the source text was loaded from
|
||||
pub fn path(&self) -> Sym { self.path.clone() }
|
||||
/// Byte range
|
||||
|
||||
Reference in New Issue
Block a user