wdl_format/v1/
expr.rs

1//! Formatting of WDL v1.x expression elements.
2
3use wdl_ast::SyntaxKind;
4
5use crate::PreToken;
6use crate::TokenStream;
7use crate::Writable as _;
8use crate::element::FormatElement;
9
10/// Formats a [`SepOption`](wdl_ast::v1::SepOption).
11///
12/// # Panics
13///
14/// This will panic if the element does not have the expected children.
15pub fn format_sep_option(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
16    let mut children = element.children().expect("sep option children");
17
18    let sep_keyword = children.next().expect("sep keyword");
19    assert!(sep_keyword.element().kind() == SyntaxKind::Ident);
20    (&sep_keyword).write(stream);
21
22    let equals = children.next().expect("sep equals");
23    assert!(equals.element().kind() == SyntaxKind::Assignment);
24    (&equals).write(stream);
25
26    let sep_value = children.next().expect("sep value");
27    assert!(sep_value.element().kind() == SyntaxKind::LiteralStringNode);
28    (&sep_value).write(stream);
29    stream.end_word();
30}
31
32/// Formats a [`DefaultOption`](wdl_ast::v1::DefaultOption).
33///
34/// # Panics
35///
36/// This will panic if the element does not have the expected children.
37pub fn format_default_option(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
38    let mut children = element.children().expect("default option children");
39
40    let default_keyword = children.next().expect("default keyword");
41    assert!(default_keyword.element().kind() == SyntaxKind::Ident);
42    (&default_keyword).write(stream);
43
44    let equals = children.next().expect("default equals");
45    assert!(equals.element().kind() == SyntaxKind::Assignment);
46    (&equals).write(stream);
47
48    let default_value = children.next().expect("default value");
49    (&default_value).write(stream);
50    stream.end_word();
51}
52
53/// Formats a [`TrueFalseOption`](wdl_ast::v1::TrueFalseOption).
54///
55/// # Panics
56///
57/// This will panic if the element does not have the expected children.
58pub fn format_true_false_option(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
59    let mut children = element.children().expect("true false option children");
60
61    let first_keyword = children.next().expect("true false option first keyword");
62    let first_keyword_kind = first_keyword.element().kind();
63    assert!(
64        first_keyword_kind == SyntaxKind::TrueKeyword
65            || first_keyword_kind == SyntaxKind::FalseKeyword
66    );
67
68    let first_equals = children.next().expect("true false option first equals");
69    assert!(first_equals.element().kind() == SyntaxKind::Assignment);
70
71    let first_value = children.next().expect("true false option first value");
72
73    let second_keyword = children.next().expect("true false option second keyword");
74    let second_keyword_kind = second_keyword.element().kind();
75    assert!(
76        second_keyword_kind == SyntaxKind::TrueKeyword
77            || second_keyword_kind == SyntaxKind::FalseKeyword
78    );
79
80    let second_equals = children.next().expect("true false option second equals");
81    assert!(second_equals.element().kind() == SyntaxKind::Assignment);
82
83    let second_value = children.next().expect("true false option second value");
84
85    if first_keyword_kind == SyntaxKind::TrueKeyword {
86        assert!(second_keyword_kind == SyntaxKind::FalseKeyword);
87        (&first_keyword).write(stream);
88        (&first_equals).write(stream);
89        (&first_value).write(stream);
90        stream.end_word();
91        (&second_keyword).write(stream);
92        (&second_equals).write(stream);
93        (&second_value).write(stream);
94    } else {
95        assert!(second_keyword_kind == SyntaxKind::TrueKeyword);
96        (&second_keyword).write(stream);
97        (&second_equals).write(stream);
98        (&second_value).write(stream);
99        stream.end_word();
100        (&first_keyword).write(stream);
101        (&first_equals).write(stream);
102        (&first_value).write(stream);
103    }
104    stream.end_word();
105}
106
107/// Formats a [`Placeholder`](wdl_ast::v1::Placeholder).
108///
109/// # Panics
110///
111/// This will panic if the element does not have the expected children.
112pub fn format_placeholder(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
113    let mut children = element.children().expect("placeholder children");
114
115    let open = children.next().expect("placeholder open");
116    assert!(open.element().kind() == SyntaxKind::PlaceholderOpen);
117    let syntax = open.element().inner();
118    let text = syntax.as_token().expect("token").text();
119    match text {
120        "${" => {
121            stream.push_literal_in_place_of_token(
122                open.element().as_token().expect("token"),
123                "~{".to_owned(),
124            );
125        }
126        "~{" => {
127            (&open).write(stream);
128        }
129        _ => {
130            unreachable!("unexpected placeholder open: {:?}", text);
131        }
132    }
133
134    for child in children {
135        (&child).write(stream);
136    }
137}
138
139/// Formats a [`LiteralString`](wdl_ast::v1::LiteralString).
140///
141/// # Panics
142///
143/// This will panic if the element does not have the expected children.
144pub fn format_literal_string(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
145    for child in element.children().expect("literal string children") {
146        match child.element().kind() {
147            SyntaxKind::SingleQuote => {
148                stream.push_literal_in_place_of_token(
149                    child.element().as_token().expect("token"),
150                    "\"".to_owned(),
151                );
152            }
153            SyntaxKind::OpenHeredoc | SyntaxKind::CloseHeredoc | SyntaxKind::DoubleQuote => {
154                (&child).write(stream);
155            }
156            SyntaxKind::LiteralStringText => {
157                let mut replacement = String::new();
158                let syntax = child.element().inner();
159                let mut chars = syntax.as_token().expect("token").text().chars().peekable();
160                let mut prev_c = None;
161                while let Some(c) = chars.next() {
162                    match c {
163                        '\\' => {
164                            if let Some(next_c) = chars.peek() {
165                                if *next_c == '\'' {
166                                    // Do not write this backslash as single quotes don't need
167                                    // escaping in a double-quoted string (and we format all
168                                    // LiteralStrings as double-quoted strings).
169                                    prev_c = Some(c);
170                                    continue;
171                                }
172                            }
173                            replacement.push(c);
174                        }
175                        '"' => {
176                            if let Some(pc) = prev_c {
177                                if pc != '\\' {
178                                    // This double quote sign is not escaped, so we need to escape
179                                    // it. This happens when a single quoted string is re-formatted
180                                    // as a double quoted string.
181                                    replacement.push('\\');
182                                }
183                            }
184                            replacement.push(c);
185                        }
186                        _ => {
187                            replacement.push(c);
188                        }
189                    }
190                    prev_c = Some(c);
191                }
192
193                stream.push_literal_in_place_of_token(
194                    child.element().as_token().expect("token"),
195                    replacement,
196                );
197            }
198            SyntaxKind::PlaceholderNode => {
199                (&child).write(stream);
200            }
201            _ => {
202                unreachable!(
203                    "unexpected child in literal string: {:?}",
204                    child.element().kind()
205                );
206            }
207        }
208    }
209}
210
211/// Formats a [`LiteralNone`](wdl_ast::v1::LiteralNone).
212///
213/// # Panics
214///
215/// This will panic if the element does not have the expected children.
216pub fn format_literal_none(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
217    let mut children = element.children().expect("literal none children");
218    let none = children.next().expect("literal none token");
219    assert!(none.element().kind() == SyntaxKind::NoneKeyword);
220    (&none).write(stream);
221}
222
223/// Formats a [`LiteralPair`](wdl_ast::v1::LiteralPair).
224///
225/// # Panics
226///
227/// This will panic if the element does not have the expected children.
228pub fn format_literal_pair(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
229    let mut children = element.children().expect("literal pair children");
230
231    let open_paren = children.next().expect("literal pair open paren");
232    assert!(open_paren.element().kind() == SyntaxKind::OpenParen);
233    (&open_paren).write(stream);
234
235    let left = children.next().expect("literal pair left");
236    (&left).write(stream);
237
238    let comma = children.next().expect("literal pair comma");
239    assert!(comma.element().kind() == SyntaxKind::Comma);
240    (&comma).write(stream);
241    stream.end_word();
242
243    let right = children.next().expect("literal pair right");
244    (&right).write(stream);
245
246    let close_paren = children.next().expect("literal pair close paren");
247    assert!(close_paren.element().kind() == SyntaxKind::CloseParen);
248    (&close_paren).write(stream);
249}
250
251/// Formats a [`LiteralBoolean`](wdl_ast::v1::LiteralBoolean).
252///
253/// # Panics
254///
255/// This will panic if the element does not have the expected children.
256pub fn format_literal_boolean(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
257    let mut children = element.children().expect("literal boolean children");
258    let bool = children.next().expect("literal boolean token");
259    (&bool).write(stream);
260}
261
262/// Formats a [`NegationExpr`](wdl_ast::v1::NegationExpr).
263///
264/// # Panics
265///
266/// This will panic if the element does not have the expected children.
267pub fn format_negation_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
268    let mut children = element.children().expect("negation expr children");
269    let minus = children.next().expect("negation expr minus");
270    assert!(minus.element().kind() == SyntaxKind::Minus);
271    (&minus).write(stream);
272
273    let expr = children.next().expect("negation expr expr");
274    (&expr).write(stream);
275}
276
277/// Formats a [`LiteralInteger`](wdl_ast::v1::LiteralInteger).
278///
279/// # Panics
280///
281/// This will panic if the element does not have the expected children.
282pub fn format_literal_integer(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
283    for child in element.children().expect("literal integer children") {
284        (&child).write(stream);
285    }
286}
287
288/// Formats a [`LiteralFloat`](wdl_ast::v1::LiteralFloat).
289///
290/// # Panics
291///
292/// This will panic if the element does not have the expected children.
293pub fn format_literal_float(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
294    for child in element.children().expect("literal float children") {
295        (&child).write(stream);
296    }
297}
298
299/// Formats a [`NameRefExpr`](wdl_ast::v1::NameRefExpr).
300///
301/// # Panics
302///
303/// This will panic if the element does not have the expected children.
304pub fn format_name_ref_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
305    let mut children = element.children().expect("name ref children");
306    let name = children.next().expect("name ref name");
307    (&name).write(stream);
308}
309
310/// Formats a [`LiteralArray`](wdl_ast::v1::LiteralArray).
311///
312/// # Panics
313///
314/// This will panic if the element does not have the expected children.
315pub fn format_literal_array(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
316    let mut children = element.children().expect("literal array children");
317
318    let open_bracket = children.next().expect("literal array open bracket");
319    assert!(open_bracket.element().kind() == SyntaxKind::OpenBracket);
320    (&open_bracket).write(stream);
321
322    let mut items = Vec::new();
323    let mut commas = Vec::new();
324    let mut close_bracket = None;
325
326    for child in children {
327        match child.element().kind() {
328            SyntaxKind::CloseBracket => {
329                close_bracket = Some(child.to_owned());
330            }
331            SyntaxKind::Comma => {
332                commas.push(child.to_owned());
333            }
334            _ => {
335                items.push(child.to_owned());
336            }
337        }
338    }
339
340    let empty = items.is_empty();
341    if !empty {
342        stream.increment_indent();
343    }
344    let mut commas = commas.iter();
345    for item in items {
346        (&item).write(stream);
347        if let Some(comma) = commas.next() {
348            (comma).write(stream);
349        } else {
350            stream.push_literal(",".to_string(), SyntaxKind::Comma);
351        }
352        stream.end_line();
353    }
354
355    if !empty {
356        stream.decrement_indent();
357    }
358    (&close_bracket.expect("literal array close bracket")).write(stream);
359}
360
361/// Formats a [`LiteralMapItem`](wdl_ast::v1::LiteralMapItem).
362///
363/// # Panics
364///
365/// This will panic if the element does not have the expected children.
366pub fn format_literal_map_item(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
367    let mut children = element.children().expect("literal map item children");
368
369    let key = children.next().expect("literal map item key");
370    (&key).write(stream);
371
372    let colon = children.next().expect("literal map item colon");
373    assert!(colon.element().kind() == SyntaxKind::Colon);
374    (&colon).write(stream);
375    stream.end_word();
376
377    let value = children.next().expect("literal map item value");
378    (&value).write(stream);
379}
380
381/// Formats a [`LiteralMap`](wdl_ast::v1::LiteralMap).
382///
383/// # Panics
384///
385/// This will panic if the element does not have the expected children.
386pub fn format_literal_map(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
387    let mut children = element.children().expect("literal map children");
388
389    let open_brace = children.next().expect("literal map open brace");
390    assert!(open_brace.element().kind() == SyntaxKind::OpenBrace);
391    (&open_brace).write(stream);
392    stream.increment_indent();
393
394    let mut items = Vec::new();
395    let mut commas = Vec::new();
396    let mut close_brace = None;
397
398    for child in children {
399        match child.element().kind() {
400            SyntaxKind::CloseBrace => {
401                close_brace = Some(child.to_owned());
402            }
403            SyntaxKind::Comma => {
404                commas.push(child.to_owned());
405            }
406            _ => {
407                items.push(child.to_owned());
408            }
409        }
410    }
411
412    let mut commas = commas.iter();
413    for item in items {
414        (&item).write(stream);
415        if let Some(comma) = commas.next() {
416            (comma).write(stream);
417        } else {
418            stream.push_literal(",".to_string(), SyntaxKind::Comma);
419        }
420        stream.end_line();
421    }
422
423    stream.decrement_indent();
424    (&close_brace.expect("literal map close brace")).write(stream);
425}
426
427/// Formats a [`LiteralObjectItem`](wdl_ast::v1::LiteralObjectItem).
428///
429/// # Panics
430///
431/// This will panic if the element does not have the expected children.
432pub fn format_literal_object_item(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
433    let mut children = element.children().expect("literal object item children");
434
435    let key = children.next().expect("literal object item key");
436    assert!(key.element().kind() == SyntaxKind::Ident);
437    (&key).write(stream);
438
439    let colon = children.next().expect("literal object item colon");
440    assert!(colon.element().kind() == SyntaxKind::Colon);
441    (&colon).write(stream);
442    stream.end_word();
443
444    let value = children.next().expect("literal object item value");
445    (&value).write(stream);
446    assert!(children.next().is_none());
447}
448
449/// Formats a [`LiteralObject`](wdl_ast::v1::LiteralObject).
450///
451/// # Panics
452///
453/// This will panic if the element does not have the expected children.
454pub fn format_literal_object(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
455    let mut children = element.children().expect("literal object children");
456
457    let open_brace = children.next().expect("literal object open brace");
458    assert!(open_brace.element().kind() == SyntaxKind::OpenBrace);
459    (&open_brace).write(stream);
460    stream.increment_indent();
461
462    let mut members = Vec::new();
463    let mut commas = Vec::new();
464    let mut close_brace = None;
465
466    for child in children {
467        match child.element().kind() {
468            SyntaxKind::CloseBrace => {
469                close_brace = Some(child.to_owned());
470            }
471            SyntaxKind::Comma => {
472                commas.push(child.to_owned());
473            }
474            _ => {
475                members.push(child.to_owned());
476            }
477        }
478    }
479
480    let mut commas = commas.iter();
481    for member in members {
482        (&member).write(stream);
483        if let Some(comma) = commas.next() {
484            (comma).write(stream);
485        } else {
486            stream.push_literal(",".to_string(), SyntaxKind::Comma);
487        }
488        stream.end_line();
489    }
490
491    stream.decrement_indent();
492    (&close_brace.expect("literal object close brace")).write(stream);
493}
494
495/// Formats a [`AccessExpr`](wdl_ast::v1::AccessExpr).
496///
497/// # Panics
498///
499/// This will panic if the element does not have the expected children.
500pub fn format_access_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
501    for child in element.children().expect("access expr children") {
502        (&child).write(stream);
503    }
504}
505
506/// Formats a [`CallExpr`](wdl_ast::v1::CallExpr).
507///
508/// # Panics
509///
510/// This will panic if the element does not have the expected children.
511pub fn format_call_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
512    for child in element.children().expect("call expr children") {
513        (&child).write(stream);
514        if child.element().kind() == SyntaxKind::Comma {
515            stream.end_word();
516        }
517    }
518}
519
520/// Formats an [`IndexExpr`](wdl_ast::v1::IndexExpr).
521///
522/// # Panics
523///
524/// This will panic if the element does not have the expected children.
525pub fn format_index_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
526    for child in element.children().expect("index expr children") {
527        (&child).write(stream);
528    }
529}
530
531/// Formats an [`AdditionExpr`](wdl_ast::v1::AdditionExpr).
532///
533/// # Panics
534///
535/// This will panic if the element does not have the expected children.
536pub fn format_addition_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
537    for child in element.children().expect("addition expr children") {
538        let whitespace_wrapped = child.element().kind() == SyntaxKind::Plus;
539        if whitespace_wrapped {
540            stream.end_word();
541        }
542        (&child).write(stream);
543        if whitespace_wrapped {
544            stream.end_word();
545        }
546    }
547}
548
549/// Formats a [`SubtractionExpr`](wdl_ast::v1::SubtractionExpr).
550///
551/// # Panics
552///
553/// This will panic if the element does not have the expected children.
554pub fn format_subtraction_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
555    for child in element.children().expect("subtraction expr children") {
556        let whitespace_wrapped = child.element().kind() == SyntaxKind::Minus;
557        if whitespace_wrapped {
558            stream.end_word();
559        }
560        (&child).write(stream);
561        if whitespace_wrapped {
562            stream.end_word();
563        }
564    }
565}
566
567/// Formats a [`MultiplicationExpr`](wdl_ast::v1::MultiplicationExpr).
568///
569/// # Panics
570///
571/// This will panic if the element does not have the expected children.
572pub fn format_multiplication_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
573    for child in element.children().expect("multiplication expr children") {
574        let whitespace_wrapped = child.element().kind() == SyntaxKind::Asterisk;
575        if whitespace_wrapped {
576            stream.end_word();
577        }
578        (&child).write(stream);
579        if whitespace_wrapped {
580            stream.end_word();
581        }
582    }
583}
584
585/// Formats a [`DivisionExpr`](wdl_ast::v1::DivisionExpr).
586///
587/// # Panics
588///
589/// This will panic if the element does not have the expected children.
590pub fn format_division_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
591    for child in element.children().expect("division expr children") {
592        let whitespace_wrapped = child.element().kind() == SyntaxKind::Slash;
593        if whitespace_wrapped {
594            stream.end_word();
595        }
596        (&child).write(stream);
597        if whitespace_wrapped {
598            stream.end_word();
599        }
600    }
601}
602
603/// Formats a [`ModuloExpr`](wdl_ast::v1::ModuloExpr).
604///
605/// # Panics
606///
607/// This will panic if the element does not have the expected children.
608pub fn format_modulo_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
609    for child in element.children().expect("modulo expr children") {
610        let whitespace_wrapped = child.element().kind() == SyntaxKind::Percent;
611        if whitespace_wrapped {
612            stream.end_word();
613        }
614        (&child).write(stream);
615        if whitespace_wrapped {
616            stream.end_word();
617        }
618    }
619}
620
621/// Formats an [`ExponentiationExpr`](wdl_ast::v1::ExponentiationExpr).
622///
623/// # Panics
624///
625/// This will panic if the element does not have the expected children.
626pub fn format_exponentiation_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
627    for child in element.children().expect("exponentiation expr children") {
628        let whitespace_wrapped = child.element().kind() == SyntaxKind::Exponentiation;
629        if whitespace_wrapped {
630            stream.end_word();
631        }
632        (&child).write(stream);
633        if whitespace_wrapped {
634            stream.end_word();
635        }
636    }
637}
638
639/// Formats a [`LogicalAndExpr`](wdl_ast::v1::LogicalAndExpr).
640///
641/// # Panics
642///
643/// This will panic if the element does not have the expected children.
644pub fn format_logical_and_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
645    for child in element.children().expect("logical and expr children") {
646        let whitespace_wrapped = child.element().kind() == SyntaxKind::LogicalAnd;
647        if whitespace_wrapped {
648            stream.end_word();
649        }
650        (&child).write(stream);
651        if whitespace_wrapped {
652            stream.end_word();
653        }
654    }
655}
656
657/// Formats a [`LogicalNotExpr`](wdl_ast::v1::LogicalNotExpr).
658///
659/// # Panics
660///
661/// This will panic if the element does not have the expected children.
662pub fn format_logical_not_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
663    let mut children = element.children().expect("logical not expr children");
664    let not = children.next().expect("logical not expr not");
665    assert!(not.element().kind() == SyntaxKind::Exclamation);
666    (&not).write(stream);
667
668    let expr = children.next().expect("logical not expr expr");
669    (&expr).write(stream);
670}
671
672/// Formats a [`LogicalOrExpr`](wdl_ast::v1::LogicalOrExpr).
673///
674/// # Panics
675///
676/// This will panic if the element does not have the expected children.
677pub fn format_logical_or_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
678    for child in element.children().expect("logical or expr children") {
679        let whitespace_wrapped = child.element().kind() == SyntaxKind::LogicalOr;
680        if whitespace_wrapped {
681            stream.end_word();
682        }
683        (&child).write(stream);
684        if whitespace_wrapped {
685            stream.end_word();
686        }
687    }
688}
689
690/// Formats an [`EqualityExpr`](wdl_ast::v1::EqualityExpr).
691///
692/// # Panics
693///
694/// This will panic if the element does not have the expected children.
695pub fn format_equality_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
696    for child in element.children().expect("equality expr children") {
697        let whitespace_wrapped = child.element().kind() == SyntaxKind::Equal;
698        if whitespace_wrapped {
699            stream.end_word();
700        }
701        (&child).write(stream);
702        if whitespace_wrapped {
703            stream.end_word();
704        }
705    }
706}
707
708/// Formats a [`InequalityExpr`](wdl_ast::v1::InequalityExpr).
709///
710/// # Panics
711///
712/// This will panic if the element does not have the expected children.
713pub fn format_inequality_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
714    for child in element.children().expect("inequality expr children") {
715        let whitespace_wrapped = child.element().kind() == SyntaxKind::NotEqual;
716        if whitespace_wrapped {
717            stream.end_word();
718        }
719        (&child).write(stream);
720        if whitespace_wrapped {
721            stream.end_word();
722        }
723    }
724}
725
726/// Formats a [`LessExpr`](wdl_ast::v1::LessExpr).
727///
728/// # Panics
729///
730/// This will panic if the element does not have the expected children.
731pub fn format_less_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
732    for child in element.children().expect("less expr children") {
733        let whitespace_wrapped = child.element().kind() == SyntaxKind::Less;
734        if whitespace_wrapped {
735            stream.end_word();
736        }
737        (&child).write(stream);
738        if whitespace_wrapped {
739            stream.end_word();
740        }
741    }
742}
743
744/// Formats a [`LessEqualExpr`](wdl_ast::v1::LessEqualExpr).
745///
746/// # Panics
747///
748/// This will panic if the element does not have the expected children.
749pub fn format_less_equal_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
750    for child in element.children().expect("less equal expr children") {
751        let whitespace_wrapped = child.element().kind() == SyntaxKind::LessEqual;
752        if whitespace_wrapped {
753            stream.end_word();
754        }
755        (&child).write(stream);
756        if whitespace_wrapped {
757            stream.end_word();
758        }
759    }
760}
761
762/// Formats a [`GreaterExpr`](wdl_ast::v1::GreaterExpr).
763///
764/// # Panics
765///
766/// This will panic if the element does not have the expected children.
767pub fn format_greater_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
768    for child in element.children().expect("greater expr children") {
769        let whitespace_wrapped = child.element().kind() == SyntaxKind::Greater;
770        if whitespace_wrapped {
771            stream.end_word();
772        }
773        (&child).write(stream);
774        if whitespace_wrapped {
775            stream.end_word();
776        }
777    }
778}
779
780/// Formats a [`GreaterEqualExpr`](wdl_ast::v1::GreaterEqualExpr).
781///
782/// # Panics
783///
784/// This will panic if the element does not have the expected children.
785pub fn format_greater_equal_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
786    for child in element.children().expect("greater equal expr children") {
787        let whitespace_wrapped = child.element().kind() == SyntaxKind::GreaterEqual;
788        if whitespace_wrapped {
789            stream.end_word();
790        }
791        (&child).write(stream);
792        if whitespace_wrapped {
793            stream.end_word();
794        }
795    }
796}
797
798/// Formats a [`ParenthesizedExpr`](wdl_ast::v1::ParenthesizedExpr).
799///
800/// # Panics
801///
802/// This will panic if the element does not have the expected children.
803pub fn format_parenthesized_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
804    for child in element.children().expect("parenthesized expr children") {
805        (&child).write(stream);
806    }
807}
808
809/// Formats an [`IfExpr`](wdl_ast::v1::IfExpr).
810///
811/// # Panics
812///
813/// This will panic if the element does not have the expected children.
814pub fn format_if_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
815    for child in element.children().expect("if expr children") {
816        (&child).write(stream);
817        stream.end_word();
818    }
819    stream.trim_end(&PreToken::WordEnd);
820}