Debian Codegraph @

debian/missing-sources/winnow/examples/c_expression/parser.rs

raw
1 //! Main points of the `c_expression` example:
2 //!
3 //! 1. [`Expr`], representing the AST of C-style expressions
4 //! 2. [`pratt_parser()`], the core parser.
5 //! 3. The [`test`]s, which demonstrate the expected input/outputs.
6 //!
7 //! There is one [`parse_example()`] function in this file, but the
8 //! associated test module for this example shows the other inputs/outputs.
9 //!
10 //! # Errata:
11 //! - There is a helper parser, [`identifier()`]
12 //! - Two print implementations: [`Expr::fmt_ast_with_indent()`] and [`Expr::fmt_delimited()`]
13 //! - For operator precedence, `1` has a low binding power while `13` has a high binding power.
14 //!
15 //! ## Printing
16 //!
17 //! `fmt_delimited()` is essentially prefix-notation.
18 //!
19 //! A short visual about the differences in printing, for parsing `1 + 1`:
20 //!
21 //! `fmt_ast_with_indent()` =
22 //! ~~~text
23 //! ADD
24 //! VAL 1
25 //! VAL 1
26 //! ~~~
27 //!
28 //! `fmt_delimited()` =
29 //! ~~~text
30 //! (+ 1 1)
31 //! ~~~
32 //!
33 //! ## Precedences
34 //!
35 //! Values uses an *inverted form* of the [C language operator precedence](c-precedence).
36 //!
37 //! In our implementation, a precedence of `1` is a very low precedence compared
38 //! to a value of `13`.
39 //!
40 //! In the linked C operator page, the table shows precedence in *descending precedence order*,
41 //! so `1` is the highest precedence, while `13` is a low precedence.
42 //!
43 //! The precedence levels you have to choose for your grammar depend on the language's
44 //! semantics.
45 //!
46 //! [c-precedence]: https://en.cppreference.com/w/c/language/operator_precedence.html
47 //!
48 //! ### Precedence Table
49 //!
50 //! An overview of the different operators for this C-style expression language.
51 //!
52 //! Note: this does not include the operands themselves, such as literals or
53 //! parenthesized expressions like: `(x)`
54 //!
55 //! Legend:
56 //! - Kind: one of Prefix, Postfix, or Infix
57 //! - Example: input text example
58 //! - Name: the [`Expr`] variant it corresponds to
59 //! - Power: the binding power/precedence of the operator
60 //! - Assoc: infix-only, represents the left/right associativity of an operator
61 //! - Recursive: only "TRUE" if parsing the operand invokes the parser again
62 //!
63 //! | Kind | Example | Name | Power | Assoc | Recursive |
64 //! |---------|------------|------------------|-------|---------|-----------|
65 //! | Prefix | `++x` | PreIncr | 18 | | |
66 //! | Prefix | `+x` | Positive (no-op) | 18 | | |
67 //! | Prefix | `--x` | PreDecr | 18 | | |
68 //! | Prefix | `-x` | Neg | 18 | | |
69 //! | Prefix | `&x` | Addr | 18 | | |
70 //! | Prefix | `*x` | Deref | 18 | | |
71 //! | Prefix | `!x` | Not | 18 | | |
72 //! | Prefix | `~x` | BitwiseNot | 18 | | |
73 //! | Postfix | `x!` | Fac | 19 | | |
74 //! | Postfix | `x?` | Ternary | 3 | | TRUE |
75 //! | Postfix | `[x]` | Index | 20 | | TRUE |
76 //! | Postfix | `(x)` | Function call | 20 | | TRUE |
77 //! | Postfix | `x++` | PostIncr | 20 | | |
78 //! | Postfix | `x--` | PostDecr | 20 | | |
79 //! | Infix | `a ** b` | Pow | 28 | Right | |
80 //! | Infix | `a * b` | Mul | 16 | Left | |
81 //! | Infix | `a / b` | Div | 16 | Left | |
82 //! | Infix | `a % b` | Rem | 16 | Left | |
83 //! | Infix | `a + b` | Add | 14 | Left | |
84 //! | Infix | `a -ne b` | NotEq | 10 | Neither | |
85 //! | Infix | `a -eq b` | Eq | 10 | Neither | |
86 //! | Infix | `a -gt b` | Greater | 12 | Neither | |
87 //! | Infix | `a -ge b` | GreaterEq | 12 | Neither | |
88 //! | Infix | `a -lt b` | Less | 12 | Neither | |
89 //! | Infix | `a -le b` | LessEqual | 12 | Neither | |
90 //! | Infix | `a->b` | ArrowOp | 20 | Left | |
91 //! | Infix | `a - b` | Sub | 14 | Left | |
92 //! | Infix | `a.b` | Dot | 20 | Left | |
93 //! | Infix | `a && b` | And | 6 | Left | |
94 //! | Infix | `a & b` | BitAnd | 12 | Left | |
95 //! | Infix | `a ^ b` | BitXor | 8 | Left | |
96 //! | Infix | `a == b` | Eq | 10 | Neither | |
97 //! | Infix | `a = b` | Assign | 2 | Right | |
98 //! | Infix | `a >= b` | GreaterEq | 12 | Neither | |
99 //! | Infix | `a > b` | Greater | 12 | Neither | |
100 //! | Infix | `a <= b` | LessEqual | 12 | Neither | |
101 //! | Infix | `a < b` | Less | 2 | Neither | |
102 //! | Infix | `a, b` | Comma | 0 | Left | |
103 //! | Infix | `a != b` | NotEq | 10 | Neither | |
104 //! | Infix | `a \|\| b` | Or | 4 | Left | |
105
106 use winnow::ascii::{digit1, multispace0};
107 use winnow::combinator::{
108 alt, cut_err, delimited, expression, fail, not, opt, peek, separated_pair, trace,
109 };
110 use winnow::combinator::{Infix, Postfix, Prefix};
111 use winnow::dispatch;
112 use winnow::error::{ContextError, ErrMode};
113 use winnow::prelude::*;
114 use winnow::token::{any, one_of, take, take_while};
115
116 #[cfg(test)]
117 mod test;
118
119 #[test]
120 fn parse_example() {
121 // Check out the [`crate::parser::test`] in this example for more samples.
122 let result = pratt_parser
123 .parse("a*a + b - c")
124 .map(|r| format!("{}", r))
125 .unwrap();
126
127 let expect = "\
128 SUB
129 ADD
130 MUL
131 NAME a
132 NAME a
133 NAME b
134 NAME c
135
136 (- (+ (* a a) b) c)";
137
138 assert_eq!(result, expect);
139 }
140
141 // Abstract syntax tree for an expression
142 pub(crate) enum Expr {
143 Name(String),
144 Value(i64),
145
146 Assign(Box<Expr>, Box<Expr>),
147
148 Addr(Box<Expr>),
149 Deref(Box<Expr>),
150
151 Dot(Box<Expr>, Box<Expr>),
152 ArrowOp(Box<Expr>, Box<Expr>),
153 Neg(Box<Expr>),
154 Add(Box<Expr>, Box<Expr>),
155 Sub(Box<Expr>, Box<Expr>),
156 Mul(Box<Expr>, Box<Expr>),
157 Div(Box<Expr>, Box<Expr>),
158 Pow(Box<Expr>, Box<Expr>),
159 Fac(Box<Expr>),
160
161 PreIncr(Box<Expr>),
162 PostIncr(Box<Expr>),
163 PreDecr(Box<Expr>),
164 PostDecr(Box<Expr>),
165
166 And(Box<Expr>, Box<Expr>),
167 Or(Box<Expr>, Box<Expr>),
168
169 // `==`
170 Eq(Box<Expr>, Box<Expr>),
171 // `!=`
172 NotEq(Box<Expr>, Box<Expr>),
173 // `!`
174 Not(Box<Expr>),
175 Greater(Box<Expr>, Box<Expr>),
176 GreaterEqual(Box<Expr>, Box<Expr>),
177 Less(Box<Expr>, Box<Expr>),
178 LessEqual(Box<Expr>, Box<Expr>),
179
180 // A parenthesized expression.
181 Paren(Box<Expr>),
182 FunctionCall(Box<Expr>, Option<Box<Expr>>),
183 Ternary(Box<Expr>, Box<Expr>, Box<Expr>),
184 // foo[...]
185 Index(Box<Expr>, Box<Expr>),
186 // a, b
187 Comma(Box<Expr>, Box<Expr>),
188
189 // %
190 Rem(Box<Expr>, Box<Expr>),
191 BitXor(Box<Expr>, Box<Expr>),
192 BitAnd(Box<Expr>, Box<Expr>),
193 BitwiseNot(Box<Expr>),
194 }
195
196 /// Parser definition
197 ///
198 /// We define a helper function `parser()` and call it at the very end.
199 ///
200 /// `parser()` accepts a minimum `precedence_level` for the entire expression,
201 /// and it returns a [`Parser`] that takes in a string and returns an [`Expr`] on
202 /// success.
203 pub(crate) fn pratt_parser(i: &mut &str) -> ModalResult<Expr> {
204 fn parser<'i>(precedence: i64) -> impl Parser<&'i str, Expr, ErrMode<ContextError>> {
205 move |i: &mut &str| {
206 use Infix::{Left, Neither, Right};
207 expression(
208 // parsing an operand, optionally surrounded by whitespace
209 delimited(
210 multispace0,
211 dispatch! {peek(any);
212 // Case one: Parenthesized expression
213 '(' => delimited('(', parser(0).map(|e| Expr::Paren(Box::new(e))), cut_err(')')),
214 _ => alt((
215 // Case two: A C-style identifier
216 identifier.map(|s| Expr::Name(s.into())),
217 // Case three: An integer
218 digit1.parse_to::<i64>().map(Expr::Value)
219 )),
220 },
221 multispace0,
222 )
223 )
224 .current_precedence_level(precedence)
225 .prefix(
226 // parsing prefix operators, optionally surrounded by whitespace
227 // for example: `++1`
228 // ||^ operand, called `a` below
229 // ^^ prefix 'pre-increment' operator
230 delimited(
231 multispace0,
232 dispatch! {any;
233 '+' => alt((
234 // ++
235 '+'.value(Prefix(18, |_: &mut _, a| Ok(Expr::PreIncr(Box::new(a))))),
236 Prefix(18, |_: &mut _, a| Ok(a) )
237 )),
238 '-' => alt((
239 // --
240 '-'.value(Prefix(18, |_: &mut _, a| Ok(Expr::PreDecr(Box::new(a))))),
241 Prefix(18, |_: &mut _, a| Ok(Expr::Neg(Box::new(a))))
242 )),
243 '&' => Prefix(18, |_: &mut _, a| Ok(Expr::Addr(Box::new(a)))),
244 '*' => Prefix(18, |_: &mut _, a| Ok(Expr::Deref(Box::new(a)))),
245 '!' => Prefix(18, |_: &mut _, a| Ok(Expr::Not(Box::new(a)))),
246 '~' => Prefix(18, |_: &mut _, a| Ok(Expr::BitwiseNot(Box::new(a)))),
247 _ => fail
248 },
249 multispace0,
250 )
251 )
252 .postfix(
253 // parsing postfix operators, optionally surrounded by whitespace
254 // for example: `(1 == 1) ? 100 : 0`
255 // |||||||| | \\\\\\\\___ remainder of the input, called `i` below
256 // |||||||| ^ postfix ternary operator
257 // ^^^^^^^^ operand, called `cond` below
258 delimited(
259 multispace0,
260 alt((
261 dispatch! {any;
262 '!' => not('=').value(Postfix(19, |_: &mut _, a| Ok(Expr::Fac(Box::new(a))))),
263 '?' => Postfix(3, |i: &mut &str, cond| {
264 let (left, right) = cut_err(separated_pair(parser(0), delimited(multispace0, ':', multispace0), parser(3))).parse_next(i)?;
265 Ok(Expr::Ternary(Box::new(cond), Box::new(left), Box::new(right)))
266 }),
267 '[' => Postfix(20, |i: &mut &str, a| {
268 let index = delimited(multispace0, parser(0), (multispace0, cut_err(']'), multispace0)).parse_next(i)?;
269 Ok(Expr::Index(Box::new(a), Box::new(index)))
270 }),
271 '(' => Postfix(20, |i: &mut &str, a| {
272 let args = delimited(multispace0, opt(parser(0)), (multispace0, cut_err(')'), multispace0)).parse_next(i)?;
273 Ok(Expr::FunctionCall(Box::new(a), args.map(Box::new)))
274 }),
275 _ => fail,
276 },
277 dispatch! {take(2usize);
278 "++" => Postfix(20, |_: &mut _, a| Ok(Expr::PostIncr(Box::new(a)))),
279 "--" => Postfix(20, |_: &mut _, a| Ok(Expr::PostDecr(Box::new(a)))),
280 _ => fail,
281 },
282 )),
283 multispace0,
284 ),
285 )
286 .infix(
287 // parsing infix operators
288 //
289 // we do not need to deal with whitespace here, it would be redundant
290 // since the operands already ignore whitespace
291 //
292 // an example infix operator: `123 + 456`
293 // operand, called `a` ^^^ | ^^^ operand, called `b` below
294 // |
295 // ^ infix addition operator
296 alt((
297 dispatch! {any;
298 '*' => alt((
299 // **
300 "*".value(Right(28, |_: &mut _, a, b| Ok(Expr::Pow(Box::new(a), Box::new(b))))),
301 Left(16, |_: &mut _, a, b| Ok(Expr::Mul(Box::new(a), Box::new(b)))),
302 )),
303 '/' => Left(16, |_: &mut _, a, b| Ok(Expr::Div(Box::new(a), Box::new(b)))),
304 '%' => Left(16, |_: &mut _, a, b| Ok(Expr::Rem(Box::new(a), Box::new(b)))),
305
306 '+' => Left(14, |_: &mut _, a, b| Ok(Expr::Add(Box::new(a), Box::new(b)))),
307 '-' => alt((
308 dispatch!{take(2usize);
309 "ne" => Neither(10, |_: &mut _, a, b| Ok(Expr::NotEq(Box::new(a), Box::new(b)))),
310 "eq" => Neither(10, |_: &mut _, a, b| Ok(Expr::Eq(Box::new(a), Box::new(b)))),
311 "gt" => Neither(12, |_: &mut _, a, b| Ok(Expr::Greater(Box::new(a), Box::new(b)))),
312 "ge" => Neither(12, |_: &mut _, a, b| Ok(Expr::GreaterEqual(Box::new(a), Box::new(b)))),
313 "lt" => Neither(12, |_: &mut _, a, b| Ok(Expr::Less(Box::new(a), Box::new(b)))),
314 "le" => Neither(12, |_: &mut _, a, b| Ok(Expr::LessEqual(Box::new(a), Box::new(b)))),
315 _ => fail
316 },
317 '>'.value(Left(20, |_: &mut _, a, b| Ok(Expr::ArrowOp(Box::new(a), Box::new(b))))),
318 Left(14, |_: &mut _, a, b| Ok(Expr::Sub(Box::new(a), Box::new(b))))
319 )),
320 '.' => Left(20, |_: &mut _, a, b| Ok(Expr::Dot(Box::new(a), Box::new(b)))),
321 '&' => alt((
322 // &&
323 "&".value(Left(6, |_: &mut _, a, b| Ok(Expr::And(Box::new(a), Box::new(b))))),
324
325 Left(12, |_: &mut _, a, b| Ok(Expr::BitAnd(Box::new(a), Box::new(b)))),
326 )),
327 '^' => Left(8, |_: &mut _, a, b| Ok(Expr::BitXor(Box::new(a), Box::new(b)))),
328 '=' => alt((
329 // ==
330 "=".value(Neither(10, |_: &mut _, a, b| Ok(Expr::Eq(Box::new(a), Box::new(b))))),
331 Right(2, |_: &mut _, a, b| Ok(Expr::Assign(Box::new(a), Box::new(b))))
332 )),
333
334 '>' => alt((
335 // >=
336 "=".value(Neither(12, |_: &mut _, a, b| Ok(Expr::GreaterEqual(Box::new(a), Box::new(b))))),
337 Neither(12, |_: &mut _, a, b| Ok(Expr::Greater(Box::new(a), Box::new(b))))
338 )),
339 '<' => alt((
340 // <=
341 "=".value(Neither(12, |_: &mut _, a, b| Ok(Expr::LessEqual(Box::new(a), Box::new(b))))),
342 Neither(12, |_: &mut _, a, b| Ok(Expr::Less(Box::new(a), Box::new(b))))
343 )),
344 ',' => Left(0, |_: &mut _, a, b| Ok(Expr::Comma(Box::new(a), Box::new(b)))),
345 _ => fail
346 },
347 dispatch! {take(2usize);
348 "!=" => Neither(10, |_: &mut _, a, b| Ok(Expr::NotEq(Box::new(a), Box::new(b)))),
349 "||" => Left(4, |_: &mut _, a, b| Ok(Expr::Or(Box::new(a), Box::new(b)))),
350 _ => fail
351 },
352 )),
353 )
354 .parse_next(i)
355 }
356 }
357
358 parser(0).parse_next(i)
359 }
360
361 // Helper parsers
362
363 fn identifier<'i>(i: &mut &'i str) -> ModalResult<&'i str> {
364 trace(
365 "identifier",
366 (
367 one_of(|c: char| c.is_alpha() || c == '_'),
368 take_while(0.., |c: char| c.is_alphanum() || c == '_'),
369 ),
370 )
371 .take()
372 .parse_next(i)
373 }
374
375 // Formatted print implementations
376 impl Expr {
377 fn fmt_ast_with_indent(
378 &self,
379 indent: u32,
380 f: &mut core::fmt::Formatter<'_>,
381 ) -> core::fmt::Result {
382 for _ in 0..indent {
383 write!(f, " ")?;
384 }
385 macro_rules! binary_fmt {
386 ($a:ident, $b:ident, $name:literal) => {{
387 writeln!(f, $name)?;
388 $a.fmt_ast_with_indent(indent + 1, f)?;
389 $b.fmt_ast_with_indent(indent + 1, f)
390 }};
391 }
392 macro_rules! unary_fmt {
393 ($a:ident, $name:literal) => {{
394 writeln!(f, $name)?;
395 $a.fmt_ast_with_indent(indent + 1, f)
396 }};
397 }
398 match self {
399 Self::Name(name) => writeln!(f, "NAME {name}"),
400 Self::Value(value) => writeln!(f, "VAL {value}"),
401 Self::Addr(a) => unary_fmt!(a, "ADDR"),
402 Self::Deref(a) => unary_fmt!(a, "DEREF"),
403 Self::Neg(a) => unary_fmt!(a, "NEG"),
404 Self::Fac(a) => unary_fmt!(a, "FAC"),
405 Self::PreIncr(a) => unary_fmt!(a, "PRE_INCR"),
406 Self::PostIncr(a) => unary_fmt!(a, "POST_INCR"),
407 Self::PreDecr(a) => unary_fmt!(a, "PRE_DECR"),
408 Self::PostDecr(a) => unary_fmt!(a, "POST_DECR"),
409 Self::Not(a) => unary_fmt!(a, "NOT"),
410 Self::BitwiseNot(a) => unary_fmt!(a, "BIT_NOT"),
411 Self::Paren(a) => unary_fmt!(a, "PAREN"),
412 Self::Assign(a, b) => binary_fmt!(a, b, "ASSIGN"),
413 Self::ArrowOp(a, b) => binary_fmt!(a, b, "ARROW"),
414 Self::Dot(a, b) => binary_fmt!(a, b, "ARROW"),
415 Self::FunctionCall(a, b) => {
416 writeln!(f, "CALL")?;
417 a.fmt_ast_with_indent(indent + 1, f)?;
418 if let Some(b) = b {
419 b.fmt_ast_with_indent(indent + 1, f)?;
420 }
421 Ok(())
422 }
423 Self::Add(a, b) => binary_fmt!(a, b, "ADD"),
424 Self::Sub(a, b) => binary_fmt!(a, b, "SUB"),
425 Self::Mul(a, b) => binary_fmt!(a, b, "MUL"),
426 Self::Div(a, b) => binary_fmt!(a, b, "DIV"),
427 Self::Pow(a, b) => binary_fmt!(a, b, "POW"),
428 Self::And(a, b) => binary_fmt!(a, b, "AND"),
429 Self::Or(a, b) => binary_fmt!(a, b, "OR"),
430 Self::Eq(a, b) => binary_fmt!(a, b, "EQ"),
431 Self::NotEq(a, b) => binary_fmt!(a, b, "NEQ"),
432 Self::Greater(a, b) => binary_fmt!(a, b, "GREATER"),
433 Self::GreaterEqual(a, b) => binary_fmt!(a, b, "GTEQ"),
434 Self::Less(a, b) => binary_fmt!(a, b, "LESS"),
435 Self::LessEqual(a, b) => binary_fmt!(a, b, "LESSEQ"),
436 Self::BitXor(a, b) => binary_fmt!(a, b, "BIT_XOR"),
437 Self::Rem(a, b) => binary_fmt!(a, b, "REM"),
438 Self::BitAnd(a, b) => binary_fmt!(a, b, "BIT_AND"),
439 Self::Index(a, b) => binary_fmt!(a, b, "INDEX"),
440 Self::Comma(a, b) => binary_fmt!(a, b, "COMMA"),
441 Self::Ternary(cond, a, b) => {
442 writeln!(f, "TERNARY")?;
443 cond.fmt_ast_with_indent(indent + 1, f)?;
444 a.fmt_ast_with_indent(indent + 2, f)?;
445 b.fmt_ast_with_indent(indent + 2, f)
446 }
447 }
448 }
449 fn fmt_delimited(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
450 match self {
451 Self::Name(name) => return write!(f, "{name}"),
452 Self::Value(value) => return write!(f, "{value}"),
453 Self::Paren(a) => return a.fmt_delimited(f),
454 _ => (),
455 }
456 macro_rules! unary {
457 ($op:literal, $a:ident) => {{
458 write!(f, $op)?;
459 $a.fmt_delimited(f)?;
460 }};
461 }
462 macro_rules! binary {
463 ($op:literal, $a:ident, $b:ident) => {{
464 write!(f, "{} ", $op)?;
465 $a.fmt_delimited(f)?;
466 write!(f, " ")?;
467 $b.fmt_delimited(f)?;
468 }};
469 }
470 write!(f, "(")?;
471 match self {
472 Self::Assign(a, b) => binary!("=", a, b),
473 Self::FunctionCall(a, b) => {
474 write!(f, "call ")?;
475 a.fmt_delimited(f)?;
476 if let Some(b) = b {
477 write!(f, " ")?;
478 b.fmt_delimited(f)?;
479 }
480 }
481 Self::ArrowOp(a, b) => binary!("->", a, b),
482 Self::Dot(a, b) => binary!(".", a, b),
483 Self::Addr(a) => unary!("&", a),
484 Self::Deref(a) => unary!("*", a),
485 Self::Neg(a) => unary!("-", a),
486 Self::Fac(a) => unary!("!", a),
487 Self::Not(a) => unary!("!", a),
488 Self::BitwiseNot(a) => unary!("~", a),
489 Self::PreIncr(a) => unary!("pre++", a),
490 Self::PostIncr(a) => unary!("post++", a),
491 Self::PreDecr(a) => unary!("pre--", a),
492 Self::PostDecr(a) => unary!("post--", a),
493 Self::Add(a, b) => binary!("+", a, b),
494 Self::Sub(a, b) => binary!("-", a, b),
495 Self::Mul(a, b) => binary!("*", a, b),
496 Self::Div(a, b) => binary!("/", a, b),
497 Self::Pow(a, b) => binary!("**", a, b),
498 Self::And(a, b) => binary!("&&", a, b),
499 Self::Or(a, b) => binary!("||", a, b),
500 Self::Eq(a, b) => binary!("==", a, b),
501 Self::NotEq(a, b) => binary!("!=", a, b),
502 Self::Greater(a, b) => binary!(">", a, b),
503 Self::GreaterEqual(a, b) => binary!(">=", a, b),
504 Self::Less(a, b) => binary!("<", a, b),
505 Self::LessEqual(a, b) => binary!("<=", a, b),
506 Self::BitXor(a, b) => binary!("^", a, b),
507 Self::Rem(a, b) => binary!("%", a, b),
508 Self::BitAnd(a, b) => binary!("&", a, b),
509 Self::Index(a, b) => binary!("[]", a, b),
510 Self::Comma(a, b) => binary!(",", a, b),
511 Self::Ternary(cond, a, b) => {
512 write!(f, "? ")?;
513 cond.fmt_delimited(f)?;
514 write!(f, " ")?;
515 a.fmt_delimited(f)?;
516 write!(f, " ")?;
517 b.fmt_delimited(f)?;
518 }
519 _ => unreachable!(),
520 }
521
522 write!(f, ")")
523 }
524 }
525
526 impl core::fmt::Display for Expr {
527 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
528 self.fmt_ast_with_indent(0, f)?;
529 writeln!(f)?;
530 self.fmt_delimited(f)
531 }
532 }