1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::ops::RangeBounds;
use proc_macro::{Span, Diagnostic, Literal};
pub type PResult<T> = ::std::result::Result<T, Diagnostic>;
pub type DResult<T> = ::std::result::Result<T, Diagnostics>;
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)
}
}
use std::ops::Deref;
pub struct StringLit(crate String, crate Literal);
impl Deref for StringLit {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
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()
}
pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Span {
self.1.subspan(range).unwrap_or_else(|| self.span())
}
}