2018-11-27 16:01:47 +00:00
|
|
|
use std::ops::RangeBounds;
|
2018-10-05 04:44:42 +00:00
|
|
|
|
2018-11-24 01:15:13 +00:00
|
|
|
use proc_macro::{Span, Diagnostic, Literal};
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2019-06-13 01:59:25 +00:00
|
|
|
pub type PResult<T> = std::result::Result<T, Diagnostic>;
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2019-06-13 01:59:25 +00:00
|
|
|
pub type DResult<T> = std::result::Result<T, Diagnostics>;
|
2018-09-20 04:14:30 +00:00
|
|
|
|
|
|
|
// An experiment.
|
|
|
|
pub struct Diagnostics(Vec<Diagnostic>);
|
|
|
|
|
|
|
|
impl Diagnostics {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Diagnostics(vec![])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push(&mut self, diag: Diagnostic) {
|
|
|
|
self.0.push(diag);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn join(mut self, mut diags: Diagnostics) -> Self {
|
|
|
|
self.0.append(&mut diags.0);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn emit_head(self) -> Diagnostic {
|
|
|
|
let mut iter = self.0.into_iter();
|
|
|
|
let mut last = iter.next().expect("Diagnostic::emit_head empty");
|
|
|
|
for diag in iter {
|
|
|
|
last.emit();
|
|
|
|
last = diag;
|
|
|
|
}
|
|
|
|
|
|
|
|
last
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn head_err_or<T>(self, ok: T) -> PResult<T> {
|
|
|
|
match self.0.is_empty() {
|
|
|
|
true => Ok(ok),
|
|
|
|
false => Err(self.emit_head())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn err_or<T>(self, ok: T) -> DResult<T> {
|
|
|
|
match self.0.is_empty() {
|
|
|
|
true => Ok(ok),
|
|
|
|
false => Err(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Diagnostic> for Diagnostics {
|
|
|
|
fn from(diag: Diagnostic) -> Self {
|
|
|
|
Diagnostics(vec![diag])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<Diagnostic>> for Diagnostics {
|
|
|
|
fn from(diags: Vec<Diagnostic>) -> Self {
|
|
|
|
Diagnostics(diags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 01:15:13 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2019-09-20 20:43:05 +00:00
|
|
|
pub struct StringLit(pub String, pub Literal);
|
2018-11-24 01:15:13 +00:00
|
|
|
|
|
|
|
impl Deref for StringLit {
|
|
|
|
type Target = str;
|
|
|
|
|
|
|
|
fn deref(&self) -> &str {
|
|
|
|
&self.0
|
|
|
|
}
|
2018-09-20 04:14:30 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 01:15:13 +00:00
|
|
|
impl StringLit {
|
|
|
|
pub fn new<S: Into<String>>(string: S, span: Span) -> Self {
|
|
|
|
let string = string.into();
|
|
|
|
let mut lit = Literal::string(&string);
|
|
|
|
lit.set_span(span);
|
|
|
|
StringLit(string, lit)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
self.1.span()
|
|
|
|
}
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2019-01-22 10:03:37 +00:00
|
|
|
/// Attempt to obtain a subspan, or, failing that, produce the full span.
|
|
|
|
/// This will create suboptimal diagnostics, but better than failing to build entirely.
|
|
|
|
pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Span {
|
|
|
|
self.1.subspan(range).unwrap_or_else(|| self.span())
|
2018-09-20 04:14:30 +00:00
|
|
|
}
|
|
|
|
}
|