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
|
//! Parsing and tokenization.
mod collection;
mod lines;
mod parser;
mod resolve;
mod scanner;
mod tokens;
pub use lines::*;
pub use parser::*;
pub use resolve::*;
pub use scanner::*;
pub use tokens::*;
use std::str::FromStr;
use crate::color::RgbaColor;
use crate::diag::Pass;
use crate::syntax::*;
use collection::{arguments, parenthesized};
/// Parse a string of source code.
pub fn parse(src: &str) -> Pass<SynTree> {
let mut p = Parser::new(src);
Pass::new(tree(&mut p), p.finish())
}
/// Parse a syntax tree.
fn tree(p: &mut Parser) -> SynTree {
// We keep track of whether we are at the start of a block or paragraph
// to know whether headings are allowed.
let mut at_start = true;
let mut tree = vec![];
while !p.eof() {
if let Some(node) = p.span_if(|p| node(p, at_start)) {
match node.v {
SynNode::Parbreak => at_start = true,
SynNode::Space => {}
_ => at_start = false,
}
tree.push(node);
}
}
tree
}
/// Parse a syntax node.
fn node(p: &mut Parser, at_start: bool) -> Option<SynNode> {
let node = match p.peek()? {
Token::Space(newlines) => {
if newlines < 2 {
SynNode::Space
} else {
SynNode::Parbreak
}
}
Token::Text(text) => SynNode::Text(text.into()),
Token::LineComment(_) | Token::BlockComment(_) => {
p.eat();
return None;
}
Token::Star => SynNode::Strong,
Token::Underscore => SynNode::Emph,
Token::Tilde => SynNode::Text("\u{00A0}".into()),
Token::Backslash => SynNode::Linebreak,
Token::Hashtag => {
if at_start {
return Some(SynNode::Heading(heading(p)));
} else {
SynNode::Text(p.get(p.peek_span()).into())
}
}
Token::Raw(t) => SynNode::Raw(raw(p, t)),
Token::UnicodeEscape(t) => SynNode::Text(unicode_escape(p, t)),
Token::LeftBracket => {
return Some(SynNode::Expr(Expr::Call(bracket_call(p))));
}
Token::LeftBrace => {
return Some(SynNode::Expr(block_expr(p)?));
}
_ => {
p.diag_unexpected();
return None;
}
};
p.eat();
Some(node)
}
/// Parse a heading.
fn heading(p: &mut Parser) -> NodeHeading {
// Count hashtags.
let mut level = p.span(|p| {
p.eat_assert(Token::Hashtag);
let mut level = 0u8;
while p.eat_if(Token::Hashtag) {
level = level.saturating_add(1);
}
level
});
if level.v > 5 {
p.diag(warning!(level.span, "section depth should not exceed 6"));
level.v = 5;
}
// Parse the heading contents.
let mut contents = vec![];
while p.check(|t| !matches!(t, Token::Space(n) if n >= 1)) {
if let Some(node) = p.span_if(|p| node(p, false)) {
contents.push(node);
}
}
NodeHeading { level, contents }
}
/// Handle a raw block.
fn raw(p: &mut Parser, token: TokenRaw) -> NodeRaw {
let raw = resolve::resolve_raw(token.text, token.backticks);
if !token.terminated {
p.diag(error!(p.peek_span().end, "expected backtick(s)"));
}
raw
}
/// Handle a unicode escape sequence.
fn unicode_escape(p: &mut Parser, token: TokenUnicodeEscape) -> String {
let span = p.peek_span();
let text = if let Some(c) = resolve::resolve_hex(token.sequence) {
c.to_string()
} else {
// Print out the escape sequence verbatim if it is invalid.
p.diag(error!(span, "invalid unicode escape sequence"));
p.get(span).into()
};
if !token.terminated {
p.diag(error!(span.end, "expected closing brace"));
}
text
}
/// Parse a block expression.
fn block_expr(p: &mut Parser) -> Option<Expr> {
p.push_mode(TokenMode::Header);
p.start_group(Group::Brace);
let expr = expr(p);
while !p.eof() {
p.diag_unexpected();
}
p.pop_mode();
p.end_group();
expr
}
/// Parse a parenthesized function call.
fn paren_call(p: &mut Parser, name: Spanned<Ident>) -> ExprCall {
p.start_group(Group::Paren);
let args = p.span(arguments);
p.end_group();
ExprCall { name, args }
}
/// Parse a bracketed function call.
fn bracket_call(p: &mut Parser) -> ExprCall {
p.push_mode(TokenMode::Header);
p.start_group(Group::Bracket);
// One header is guaranteed, but there may be more (through chaining).
let mut outer = vec![];
let mut inner = p.span(bracket_subheader);
while p.eat_if(Token::Pipe) {
outer.push(inner);
inner = p.span(bracket_subheader);
}
p.pop_mode();
p.end_group();
if p.peek() == Some(Token::LeftBracket) {
let body = p.span(|p| Expr::Lit(Lit::Content(bracket_body(p))));
inner.span.expand(body.span);
inner.v.args.v.push(Argument::Pos(body));
}
while let Some(mut top) = outer.pop() {
let span = inner.span;
let node = inner.map(|c| SynNode::Expr(Expr::Call(c)));
let expr = Expr::Lit(Lit::Content(vec![node])).with_span(span);
top.v.args.v.push(Argument::Pos(expr));
inner = top;
}
inner.v
}
/// Parse one subheader of a bracketed function call.
fn bracket_subheader(p: &mut Parser) -> ExprCall {
p.start_group(Group::Subheader);
let start = p.next_start();
let name = p.span_if(ident).unwrap_or_else(|| {
let what = "function name";
if p.eof() {
p.diag_expected_at(what, start);
} else {
p.diag_expected(what);
}
Ident(String::new()).with_span(start)
});
let args = p.span(arguments);
p.end_group();
ExprCall { name, args }
}
/// Parse the body of a bracketed function call.
fn bracket_body(p: &mut Parser) -> SynTree {
p.push_mode(TokenMode::Body);
p.start_group(Group::Bracket);
let tree = tree(p);
p.pop_mode();
p.end_group();
tree
}
/// Parse an expression: `term (+ term)*`.
fn expr(p: &mut Parser) -> Option<Expr> {
binops(p, term, |token| match token {
Token::Plus => Some(BinOp::Add),
Token::Hyphen => Some(BinOp::Sub),
_ => None,
})
}
/// Parse a term: `factor (* factor)*`.
fn term(p: &mut Parser) -> Option<Expr> {
binops(p, factor, |token| match token {
Token::Star => Some(BinOp::Mul),
Token::Slash => Some(BinOp::Div),
_ => None,
})
}
/// Parse binary operations of the from `a (<op> b)*`.
fn binops(
p: &mut Parser,
operand: fn(&mut Parser) -> Option<Expr>,
op: fn(Token) -> Option<BinOp>,
) -> Option<Expr> {
let mut lhs = p.span_if(operand)?;
while let Some(op) = p.span_if(|p| p.eat_map(op)) {
if let Some(rhs) = p.span_if(operand) {
let span = lhs.span.join(rhs.span);
let expr = Expr::Binary(ExprBinary {
lhs: Box::new(lhs),
op,
rhs: Box::new(rhs),
});
lhs = expr.with_span(span);
} else {
break;
}
}
Some(lhs.v)
}
/// Parse a factor of the form `-?value`.
fn factor(p: &mut Parser) -> Option<Expr> {
let op = |token| match token {
Token::Hyphen => Some(UnOp::Neg),
_ => None,
};
if let Some(op) = p.span_if(|p| p.eat_map(op)) {
p.span_if(factor)
.map(|expr| Expr::Unary(ExprUnary { op, expr: Box::new(expr) }))
} else {
value(p)
}
}
/// Parse a value.
fn value(p: &mut Parser) -> Option<Expr> {
let expr = match p.peek() {
// Bracketed function call.
Some(Token::LeftBracket) => {
let node = p.span(|p| SynNode::Expr(Expr::Call(bracket_call(p))));
return Some(Expr::Lit(Lit::Content(vec![node])));
}
// Content expression.
Some(Token::LeftBrace) => {
return Some(Expr::Lit(Lit::Content(content(p))));
}
// Dictionary or just a parenthesized expression.
Some(Token::LeftParen) => {
return Some(parenthesized(p));
}
// Function or just ident.
Some(Token::Ident(id)) => {
p.eat();
let ident = Ident(id.into());
if p.peek() == Some(Token::LeftParen) {
let name = ident.with_span(p.peek_span());
return Some(Expr::Call(paren_call(p, name)));
} else {
return Some(Expr::Lit(Lit::Ident(ident)));
}
}
// Basic values.
Some(Token::Bool(b)) => Expr::Lit(Lit::Bool(b)),
Some(Token::Int(i)) => Expr::Lit(Lit::Int(i)),
Some(Token::Float(f)) => Expr::Lit(Lit::Float(f)),
Some(Token::Length(val, unit)) => Expr::Lit(Lit::Length(val, unit)),
Some(Token::Percent(p)) => Expr::Lit(Lit::Percent(p)),
Some(Token::Hex(hex)) => Expr::Lit(Lit::Color(color(p, hex))),
Some(Token::Str(token)) => Expr::Lit(Lit::Str(str(p, token))),
// No value.
_ => {
p.diag_expected("expression");
return None;
}
};
p.eat();
Some(expr)
}
// Parse a content value: `{...}`.
fn content(p: &mut Parser) -> SynTree {
p.push_mode(TokenMode::Body);
p.start_group(Group::Brace);
let tree = tree(p);
p.pop_mode();
p.end_group();
tree
}
/// Parse an identifier.
fn ident(p: &mut Parser) -> Option<Ident> {
p.eat_map(|token| match token {
Token::Ident(id) => Some(Ident(id.into())),
_ => None,
})
}
/// Parse a color.
fn color(p: &mut Parser, hex: &str) -> RgbaColor {
RgbaColor::from_str(hex).unwrap_or_else(|_| {
// Replace color with black.
p.diag(error!(p.peek_span(), "invalid color"));
RgbaColor::new(0, 0, 0, 255)
})
}
/// Parse a string.
fn str(p: &mut Parser, token: TokenStr) -> String {
if !token.terminated {
p.diag_expected_at("quote", p.peek_span().end);
}
resolve::resolve_string(token.string)
}
#[cfg(test)]
mod tests;
|