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
|
//! Value types for extracting function arguments.
use fontdock::{FontStyle, FontWeight, FontWidth};
use crate::Feedback;
use crate::layout::prelude::*;
use crate::length::{Length, ScaleLength};
use crate::paper::Paper;
use super::span::Spanned;
use super::expr::*;
/// Value types are used to extract values from functions, tuples and
/// objects. They represent the value part of an argument.
/// ```typst
/// [func: value, key=value]
/// ^^^^^ ^^^^^
/// ```
pub trait Value: Sized {
/// Try to parse this value from an expression.
///
/// Returns `None` and generates an appropriate error if the expression is
/// not valid for this value type
fn parse(expr: Spanned<Expr>, f: &mut Feedback) -> Option<Self>;
}
impl<V: Value> Value for Spanned<V> {
fn parse(expr: Spanned<Expr>, f: &mut Feedback) -> Option<Self> {
let span = expr.span;
V::parse(expr, f).map(|v| Spanned { v, span })
}
}
macro_rules! match_value {
($type:ty, $name:expr, $($p:pat => $r:expr),* $(,)?) => {
impl Value for $type {
fn parse(expr: Spanned<Expr>, f: &mut Feedback) -> Option<Self> {
#[allow(unreachable_patterns)]
match expr.v {
$($p => Some($r)),*,
other => {
error!(
@f, expr.span,
"expected {}, found {}", $name, other.name()
);
None
}
}
}
}
};
}
match_value!(Expr, "expression", e => e);
match_value!(Ident, "identifier", Expr::Ident(i) => i);
match_value!(String, "string", Expr::Str(s) => s);
match_value!(f64, "number", Expr::Number(n) => n);
match_value!(bool, "bool", Expr::Bool(b) => b);
match_value!(Length, "length", Expr::Length(l) => l);
match_value!(Tuple, "tuple", Expr::Tuple(t) => t);
match_value!(Object, "object", Expr::Object(o) => o);
match_value!(ScaleLength, "number or length",
Expr::Length(length) => ScaleLength::Absolute(length),
Expr::Number(scale) => ScaleLength::Scaled(scale),
);
/// A value type that matches [`Expr::Ident`] and [`Expr::Str`] and implements
/// `Into<String>`.
pub struct StringLike(pub String);
impl From<StringLike> for String {
fn from(like: StringLike) -> String {
like.0
}
}
match_value!(StringLike, "identifier or string",
Expr::Ident(Ident(s)) => StringLike(s),
Expr::Str(s) => StringLike(s),
);
macro_rules! ident_value {
($type:ty, $name:expr, $parse:expr) => {
impl Value for $type {
fn parse(expr: Spanned<Expr>, f: &mut Feedback) -> Option<Self> {
if let Expr::Ident(ident) = expr.v {
let val = $parse(ident.as_str());
if val.is_none() {
error!(@f, expr.span, "invalid {}", $name);
}
val
} else {
error!(
@f, expr.span,
"expected {}, found {}", $name, expr.v.name()
);
None
}
}
}
};
}
ident_value!(Dir, "direction", |s| match s {
"ltr" => Some(LTT),
"rtl" => Some(RTL),
"ttb" => Some(TTB),
"btt" => Some(BTT),
_ => None,
});
ident_value!(SpecAlign, "alignment", |s| match s {
"left" => Some(SpecAlign::Left),
"right" => Some(SpecAlign::Right),
"top" => Some(SpecAlign::Top),
"bottom" => Some(SpecAlign::Bottom),
"center" => Some(SpecAlign::Center),
_ => None,
});
ident_value!(FontStyle, "font style", FontStyle::from_name);
ident_value!(Paper, "paper", Paper::from_name);
impl Value for FontWeight {
fn parse(expr: Spanned<Expr>, f: &mut Feedback) -> Option<Self> {
match expr.v {
Expr::Number(weight) => {
const MIN: u16 = 100;
const MAX: u16 = 900;
Some(FontWeight(if weight < MIN as f64 {
error!(@f, expr.span, "the minimum font weight is {}", MIN);
MIN
} else if weight > MAX as f64 {
error!(@f, expr.span, "the maximum font weight is {}", MAX);
MAX
} else {
weight.round() as u16
}))
}
Expr::Ident(ident) => {
let weight = Self::from_name(ident.as_str());
if weight.is_none() {
error!(@f, expr.span, "invalid font weight");
}
weight
}
other => {
error!(
@f, expr.span,
"expected font weight (name or number), found {}",
other.name(),
);
None
}
}
}
}
impl Value for FontWidth {
fn parse(expr: Spanned<Expr>, f: &mut Feedback) -> Option<Self> {
match expr.v {
Expr::Number(width) => {
const MIN: u16 = 1;
const MAX: u16 = 9;
FontWidth::new(if width < MIN as f64 {
error!(@f, expr.span, "the minimum font width is {}", MIN);
MIN
} else if width > MAX as f64 {
error!(@f, expr.span, "the maximum font width is {}", MAX);
MAX
} else {
width.round() as u16
})
}
Expr::Ident(ident) => {
let width = Self::from_name(ident.as_str());
if width.is_none() {
error!(@f, expr.span, "invalid font width");
}
width
}
other => {
error!(
@f, expr.span,
"expected font width (name or number), found {}",
other.name(),
);
None
}
}
}
}
|