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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
//! Expressions in function headers.
use std::fmt::{self, Debug, Formatter};
use std::iter::FromIterator;
use std::ops::Deref;
use std::str::FromStr;
use std::u8;
use crate::problem::Problems;
use crate::size::Size;
use super::func::{Key, Value};
use super::span::{Span, Spanned};
use super::tokens::is_identifier;
/// An argument or return value.
#[derive(Clone, PartialEq)]
pub enum Expr {
/// An identifier: `ident`.
Ident(Ident),
/// A string: `"string"`.
Str(String),
/// A number: `1.2, 200%`.
Number(f64),
/// A size: `2cm, 5.2in`.
Size(Size),
/// A bool: `true, false`.
Bool(bool),
/// A color value, including the alpha channel: `#f79143ff`.
Color(RgbaColor),
/// A tuple: `(false, 12cm, "hi")`.
Tuple(Tuple),
/// A named tuple: `cmyk(37.7, 0, 3.9, 1.1)`.
NamedTuple(NamedTuple),
/// An object: `{ fit: false, size: 12pt }`.
Object(Object),
/// An operator that negates the contained expression.
Neg(Box<Spanned<Expr>>),
/// An operator that adds the contained expressions.
Add(Box<Spanned<Expr>>, Box<Spanned<Expr>>),
/// An operator that subtracts contained expressions.
Sub(Box<Spanned<Expr>>, Box<Spanned<Expr>>),
/// An operator that multiplies the contained expressions.
Mul(Box<Spanned<Expr>>, Box<Spanned<Expr>>),
/// An operator that divides the contained expressions.
Div(Box<Spanned<Expr>>, Box<Spanned<Expr>>),
}
impl Expr {
/// A natural-language name of the type of this expression, e.g. "identifier".
pub fn name(&self) -> &'static str {
use Expr::*;
match self {
Ident(_) => "identifier",
Str(_) => "string",
Number(_) => "number",
Size(_) => "size",
Bool(_) => "bool",
Color(_) => "color",
Tuple(_) => "tuple",
NamedTuple(_) => "named tuple",
Object(_) => "object",
Neg(_) => "negation",
Add(_, _) => "addition",
Sub(_, _) => "subtraction",
Mul(_, _) => "multiplication",
Div(_, _) => "division",
}
}
}
impl Debug for Expr {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
use Expr::*;
match self {
Ident(i) => i.fmt(f),
Str(s) => s.fmt(f),
Number(n) => n.fmt(f),
Size(s) => s.fmt(f),
Bool(b) => b.fmt(f),
Color(c) => c.fmt(f),
Tuple(t) => t.fmt(f),
NamedTuple(t) => t.fmt(f),
Object(o) => o.fmt(f),
Neg(e) => write!(f, "-{:?}", e),
Add(a, b) => write!(f, "({:?} + {:?})", a, b),
Sub(a, b) => write!(f, "({:?} - {:?})", a, b),
Mul(a, b) => write!(f, "({:?} * {:?})", a, b),
Div(a, b) => write!(f, "({:?} / {:?})", a, b),
}
}
}
/// A unicode identifier.
///
/// # Example
/// ```typst
/// [func: "hi", ident]
/// ^^^^ ^^^^^
/// ```
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Ident(pub String);
impl Ident {
/// Create a new identifier from a string checking that it is a valid
/// unicode identifier.
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
}
}
/// Return a reference to the underlying string.
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl Debug for Ident {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "`{}`", self.0)
}
}
/// An 8-bit RGBA color.
///
/// # Example
/// ```typst
/// [box: background=#423abaff]
/// ^^^^^^^^
/// ```
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct RgbaColor {
/// Red channel.
pub r: u8,
/// Green channel.
pub g: u8,
/// Blue channel.
pub b: u8,
/// Alpha channel.
pub a: u8,
/// Indicates whether this is a user-provided value or a
/// default value provided as a fail-over by the parser.
/// This color may be overwritten if this property is true.
pub healed: bool,
}
impl RgbaColor {
/// Constructs a new color.
pub fn new(r: u8, g: u8, b: u8, a: u8) -> RgbaColor {
RgbaColor { r, g, b, a, healed: false }
}
/// Constructs a new color with the healed property set to true.
pub fn new_healed(r: u8, g: u8, b: u8, a: u8) -> RgbaColor {
RgbaColor { r, g, b, a, healed: true }
}
}
impl FromStr for RgbaColor {
type Err = ParseColorError;
/// Constructs a new color from a hex string like `7a03c2`.
/// Do not specify a leading `#`.
fn from_str(hex_str: &str) -> Result<RgbaColor, Self::Err> {
if !hex_str.is_ascii() {
return Err(ParseColorError);
}
let len = hex_str.len();
let long = len == 6 || len == 8;
let short = len == 3 || len == 4;
let alpha = len == 4 || len == 8;
if !long && !short {
return Err(ParseColorError);
}
let mut values: [u8; 4] = [255; 4];
for elem in if alpha { 0..4 } else { 0..3 } {
let item_len = if long { 2 } else { 1 };
let pos = elem * item_len;
let item = &hex_str[pos..(pos+item_len)];
values[elem] = u8::from_str_radix(item, 16)
.map_err(|_| ParseColorError)?;
if short {
// Duplicate number for shorthand notation, i.e. `a` -> `aa`
values[elem] += values[elem] * 16;
}
}
Ok(RgbaColor::new(values[0], values[1], values[2], values[3]))
}
}
impl Debug for RgbaColor {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if f.alternate() {
write!(
f,
"rgba({:02}, {:02}, {:02}, {:02})",
self.r, self.g, self.b, self.a,
)?;
} else {
write!(
f,
"#{:02x}{:02x}{:02x}{:02x}",
self.r, self.g, self.b, self.a,
)?;
}
if self.healed {
f.write_str(" [healed]")?;
}
Ok(())
}
}
/// The error returned when parsing a [`RgbaColor`] from a string fails.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct ParseColorError;
impl std::error::Error for ParseColorError {}
impl fmt::Display for ParseColorError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str("invalid color")
}
}
/// An untyped sequence of expressions.
///
/// # Example
/// ```typst
/// (false, 12cm, "hi")
/// ```
#[derive(Default, Clone, PartialEq)]
pub struct Tuple {
/// The elements of the tuple.
pub items: Vec<Spanned<Expr>>,
}
impl Tuple {
/// Create an empty tuple.
pub fn new() -> Tuple {
Tuple { items: vec![] }
}
/// Add an element.
pub fn add(&mut self, item: Spanned<Expr>) {
self.items.push(item);
}
/// Extract (and remove) the first matching value and remove and generate
/// problems for all previous items that did not match.
pub fn get<V: Value>(&mut self, problems: &mut Problems) -> Option<V> {
while !self.items.is_empty() {
let expr = self.items.remove(0);
let span = expr.span;
match V::parse(expr) {
Ok(output) => return Some(output),
Err(v) => problems.push(Spanned { v, span }),
}
}
None
}
/// Extract and return an iterator over all values that match and generate
/// problems for all items that do not match.
pub fn get_all<'a, V: Value>(&'a mut self, problems: &'a mut Problems)
-> impl Iterator<Item=V> + 'a {
self.items.drain(..).filter_map(move |expr| {
let span = expr.span;
match V::parse(expr) {
Ok(output) => Some(output),
Err(v) => { problems.push(Spanned { v, span }); None }
}
})
}
/// Iterate over the items of this tuple.
pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, Spanned<Expr>> {
self.items.iter()
}
}
impl IntoIterator for Tuple {
type Item = Spanned<Expr>;
type IntoIter = std::vec::IntoIter<Spanned<Expr>>;
fn into_iter(self) -> Self::IntoIter {
self.items.into_iter()
}
}
impl<'a> IntoIterator for &'a Tuple {
type Item = &'a Spanned<Expr>;
type IntoIter = std::slice::Iter<'a, Spanned<Expr>>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl FromIterator<Spanned<Expr>> for Tuple {
fn from_iter<I: IntoIterator<Item=Spanned<Expr>>>(iter: I) -> Self {
Tuple { items: iter.into_iter().collect() }
}
}
impl Debug for Tuple {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_list()
.entries(&self.items)
.finish()
}
}
/// A named, untyped sequence of expressions.
///
/// # Example
/// ```typst
/// hsl(93, 10, 19.4)
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct NamedTuple {
/// The name of the tuple and where it is in the user source.
pub name: Spanned<Ident>,
/// The elements of the tuple.
pub tuple: Spanned<Tuple>,
}
impl NamedTuple {
/// Create a named tuple from a tuple.
pub fn new(name: Spanned<Ident>, tuple: Spanned<Tuple>) -> NamedTuple {
NamedTuple { name, tuple }
}
}
impl Deref for NamedTuple {
type Target = Tuple;
fn deref(&self) -> &Self::Target {
&self.tuple.v
}
}
/// A key-value collection of identifiers and associated expressions.
///
/// The pairs themselves are not spanned, but the combined spans can easily be
/// retrieved by merging the spans of key and value as happening in
/// [`FuncArg::span`](super::func::FuncArg::span).
///
/// # Example
/// ```typst
/// { fit: false, size: 12cm, items: (1, 2, 3) }
/// ```
#[derive(Default, Clone, PartialEq)]
pub struct Object {
/// The key-value pairs of the object.
pub pairs: Vec<Spanned<Pair>>,
}
/// A key-value pair in an object.
#[derive(Debug, Clone, PartialEq)]
pub struct Pair {
/// The key part.
/// ```typst
/// key: value
/// ^^^
/// ```
pub key: Spanned<Ident>,
/// The value part.
/// ```typst
/// key: value
/// ^^^^^
/// ```
pub value: Spanned<Expr>,
}
impl Object {
/// Create an empty object.
pub fn new() -> Object {
Object { pairs: vec![] }
}
/// Add a pair to object.
pub fn add(&mut self, pair: Spanned<Pair>) {
self.pairs.push(pair);
}
/// Extract (and remove) a pair with the given key string and matching
/// value.
///
/// Inserts an error if the value does not match. If the key is not
/// contained, no error is inserted.
pub fn get<V: Value>(&mut self, problems: &mut Problems, key: &str) -> Option<V> {
let index = self.pairs.iter().position(|pair| pair.v.key.v.as_str() == key)?;
self.get_index::<V>(problems, index)
}
/// Extract (and remove) a pair with a matching key and value.
///
/// Inserts an error if the value does not match. If no matching key is
/// found, no error is inserted.
pub fn get_with_key<K: Key, V: Value>(
&mut self,
problems: &mut Problems,
) -> Option<(K, V)> {
for (index, pair) in self.pairs.iter().enumerate() {
let key = Spanned { v: pair.v.key.v.as_str(), span: pair.v.key.span };
if let Some(key) = K::parse(key) {
return self.get_index::<V>(problems, index).map(|value| (key, value));
}
}
None
}
/// Extract (and remove) all pairs with matching keys and values.
///
/// Inserts errors for values that do not match.
pub fn get_all<'a, K: Key, V: Value>(
&'a mut self,
problems: &'a mut Problems,
) -> impl Iterator<Item=(K, V)> + 'a {
let mut index = 0;
std::iter::from_fn(move || {
if index < self.pairs.len() {
let key = &self.pairs[index].v.key;
let key = Spanned { v: key.v.as_str(), span: key.span };
Some(if let Some(key) = K::parse(key) {
self.get_index::<V>(problems, index).map(|v| (key, v))
} else {
index += 1;
None
})
} else {
None
}
}).filter_map(|x| x)
}
/// Extract all key value pairs with span information.
///
/// The spans are over both key and value, like so:
/// ```typst
/// { key: value }
/// ^^^^^^^^^^
/// ```
pub fn get_all_spanned<'a, K: Key + 'a, V: Value + 'a>(
&'a mut self,
problems: &'a mut Problems,
) -> impl Iterator<Item=Spanned<(K, V)>> + 'a {
self.get_all::<Spanned<K>, Spanned<V>>(problems)
.map(|(k, v)| Spanned::new((k.v, v.v), Span::merge(k.span, v.span)))
}
/// Extract the argument at the given index and insert an error if the value
/// does not match.
fn get_index<V: Value>(&mut self, problems: &mut Problems, index: usize) -> Option<V> {
let expr = self.pairs.remove(index).v.value;
let span = expr.span;
match V::parse(expr) {
Ok(output) => Some(output),
Err(v) => { problems.push(Spanned { v, span }); None }
}
}
/// Iterate over the pairs of this object.
pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, Spanned<Pair>> {
self.pairs.iter()
}
}
impl IntoIterator for Object {
type Item = Spanned<Pair>;
type IntoIter = std::vec::IntoIter<Spanned<Pair>>;
fn into_iter(self) -> Self::IntoIter {
self.pairs.into_iter()
}
}
impl<'a> IntoIterator for &'a Object {
type Item = &'a Spanned<Pair>;
type IntoIter = std::slice::Iter<'a, Spanned<Pair>>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl FromIterator<Spanned<Pair>> for Object {
fn from_iter<I: IntoIterator<Item=Spanned<Pair>>>(iter: I) -> Self {
Object { pairs: iter.into_iter().collect() }
}
}
impl Debug for Object {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_map()
.entries(self.pairs.iter().map(|p| (&p.v.key.v, &p.v.value.v)))
.finish()
}
}
|