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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
use crate::size::ScaleSize;
use super::*;
/// An argument or return value.
#[derive(Clone, PartialEq)]
pub enum Expr {
Ident(Ident),
Str(String),
Number(f64),
Size(Size),
Bool(bool),
Tuple(Tuple),
Object(Object),
}
impl Expr {
pub fn name(&self) -> &'static str {
use Expr::*;
match self {
Ident(_) => "identifier",
Str(_) => "string",
Number(_) => "number",
Size(_) => "size",
Bool(_) => "boolean",
Tuple(_) => "tuple",
Object(_) => "object",
}
}
}
impl Display for Expr {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
use Expr::*;
match self {
Ident(i) => write!(f, "{}", i),
Str(s) => write!(f, "{:?}", s),
Number(n) => write!(f, "{}", n),
Size(s) => write!(f, "{}", s),
Bool(b) => write!(f, "{}", b),
Tuple(t) => write!(f, "{}", t),
Object(o) => write!(f, "{}", o),
}
}
}
debug_display!(Expr);
/// An identifier.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Ident(pub String);
impl Ident {
pub fn new<S>(ident: S) -> Option<Ident> where S: AsRef<str> + Into<String> {
if is_identifier(ident.as_ref()) {
Some(Ident(ident.into()))
} else {
None
}
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl Display for Ident {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
debug_display!(Ident);
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct StringLike(pub String);
/// A sequence of expressions.
#[derive(Clone, PartialEq)]
pub struct Tuple {
pub items: Vec<Spanned<Expr>>,
}
impl Tuple {
pub fn new() -> Tuple {
Tuple { items: vec![] }
}
pub fn add(&mut self, item: Spanned<Expr>) {
self.items.push(item);
}
}
impl Display for Tuple {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "(")?;
let mut first = true;
for item in &self.items {
if !first {
write!(f, ", ")?;
}
write!(f, "{}", item.v)?;
first = false;
}
write!(f, ")")
}
}
debug_display!(Tuple);
/// A key-value collection of identifiers and associated expressions.
#[derive(Clone, PartialEq)]
pub struct Object {
pub pairs: Vec<Pair>,
}
impl Object {
pub fn new() -> Object {
Object { pairs: vec![] }
}
pub fn add(&mut self, key: Spanned<Ident>, value: Spanned<Expr>) {
self.pairs.push(Pair { key, value });
}
pub fn add_pair(&mut self, pair: Pair) {
self.pairs.push(pair);
}
}
impl Display for Object {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if self.pairs.len() == 0 {
return write!(f, "{{}}");
}
write!(f, "{{ ")?;
let mut first = true;
for pair in &self.pairs {
if !first {
write!(f, ", ")?;
}
write!(f, "{}", pair)?;
first = false;
}
write!(f, " }}")
}
}
debug_display!(Object);
/// A key-value pair in an object.
#[derive(Clone, PartialEq)]
pub struct Pair {
pub key: Spanned<Ident>,
pub value: Spanned<Expr>,
}
impl Display for Pair {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}: {}", self.key.v, self.value.v)
}
}
debug_display!(Pair);
pub trait ExprKind: Sized {
/// The name of the expression in an `expected <name>` error.
const NAME: &'static str;
/// Create from expression.
fn from_expr(expr: Spanned<Expr>) -> Result<Self, Error>;
}
impl<T> ExprKind for Spanned<T> where T: ExprKind {
const NAME: &'static str = T::NAME;
fn from_expr(expr: Spanned<Expr>) -> Result<Self, Error> {
let span = expr.span;
T::from_expr(expr).map(|v| Spanned { v, span })
}
}
/// Implements the expression kind trait for a type.
macro_rules! kind {
($type:ty, $name:expr, $($p:pat => $r:expr),* $(,)?) => {
impl ExprKind for $type {
const NAME: &'static str = $name;
fn from_expr(expr: Spanned<Expr>) -> Result<Self, Error> {
#[allow(unreachable_patterns)]
Ok(match expr.v {
$($p => $r),*,
_ => return Err(
err!("expected {}, found {}", Self::NAME, expr.v.name())
),
})
}
}
};
}
kind!(Expr, "expression", e => e);
kind!(Ident, "identifier", Expr::Ident(i) => i);
kind!(String, "string", Expr::Str(s) => s);
kind!(f64, "number", Expr::Number(n) => n);
kind!(bool, "boolean", Expr::Bool(b) => b);
kind!(Size, "size", Expr::Size(s) => s);
kind!(Tuple, "tuple", Expr::Tuple(t) => t);
kind!(Object, "object", Expr::Object(o) => o);
kind!(ScaleSize, "number or size",
Expr::Size(size) => ScaleSize::Absolute(size),
Expr::Number(scale) => ScaleSize::Scaled(scale as f32),
);
kind!(StringLike, "identifier or string",
Expr::Ident(Ident(s)) => StringLike(s),
Expr::Str(s) => StringLike(s),
);
|