Skip to main content

quilt/lift/
gen.rs

1//! `LiftTo` impls: the shape each target's parser builds for a literal.
2//!
3//! @generated by `bin/gen-lifts` from `quilt/src/lift/mk_lifts.rs.quilt` —
4//! do not edit by hand. `bin/check-lifts` regenerates it and fails if it
5//! differs, so a lift cannot drift from the grammar it is meant to match.
6//!
7//! Every shape below was read off a *parsed sample literal*. The generator
8//! parses e.g. `py↖"s"↗` with the vendored grammar and lifts
9//! with `↑`, which prints the builder calls that rebuild it; the sample's
10//! own text is then replaced by the runtime expression. So the tags, the
11//! child structure and the layout are the parser's, not a transcription of
12//! it. Two hand surveys had to be done before this existed (#176, then
13//! `fed278b`); generating it is issue #203.
14//!
15//! Three families are still deliberately *not* the parser's shape:
16//!
17//! * WGSL literals parse wrapped in a `const_literal`; these lifts produce
18//!   the inner node, which is the tag `conformance/spec/wgsl.toml` pins.
19//!   Adopting the wrapper moves a declared claim — the open call in #174.
20//! * A negative Python or Nix integer parses as a `unary_operator` /
21//!   `unary_expression` over a positive literal; these lift one signed
22//!   literal. Same call. Lean's negatives *are* the parser's shape.
23//! * A string containing escapes parses with `escape_sequence` children
24//!   inside its content, which no runtime reproduces (#174, finding A2).
25
26use super::{
27    escape_html, lean_dquote_escape, mysql_squote_escape, nix_dquote_escape, py_dquote_escape,
28    sh_dquote_escape, sql_squote_escape, ts_dquote_escape, Bash, Html, Lean, LiftTo, MySql, Nix,
29    Python, Sql, TypeScript, Wgsl, Zsh,
30};
31use crate::qterm::{leaf, sym, tb, QTerm};
32use std::sync::Arc;
33
34/// WGSL `3u` / `3i`. The parser wraps a literal in a
35/// `const_literal`; this is the inner node, which is what
36/// `conformance/spec/wgsl.toml` pins — see issue #174.
37///
38/// WGSL has no 64-bit integers, so `u64`/`i64` get no impl
39/// below: lifting one is a compile error in the expanded
40/// program rather than a silent truncation.
41pub fn wgsl_int_term(text: &str) -> Arc<QTerm> {
42    leaf("int_literal", text)
43}
44impl LiftTo<Wgsl> for u8 {
45    fn lift_to(&self) -> Arc<QTerm> {
46        wgsl_int_term(&format!("{self}u"))
47    }
48}
49impl LiftTo<Wgsl> for u16 {
50    fn lift_to(&self) -> Arc<QTerm> {
51        wgsl_int_term(&format!("{self}u"))
52    }
53}
54impl LiftTo<Wgsl> for u32 {
55    fn lift_to(&self) -> Arc<QTerm> {
56        wgsl_int_term(&format!("{self}u"))
57    }
58}
59impl LiftTo<Wgsl> for usize {
60    fn lift_to(&self) -> Arc<QTerm> {
61        wgsl_int_term(&format!("{self}u"))
62    }
63}
64impl LiftTo<Wgsl> for i8 {
65    fn lift_to(&self) -> Arc<QTerm> {
66        wgsl_int_term(&format!("{self}i"))
67    }
68}
69impl LiftTo<Wgsl> for i16 {
70    fn lift_to(&self) -> Arc<QTerm> {
71        wgsl_int_term(&format!("{self}i"))
72    }
73}
74impl LiftTo<Wgsl> for i32 {
75    fn lift_to(&self) -> Arc<QTerm> {
76        wgsl_int_term(&format!("{self}i"))
77    }
78}
79impl LiftTo<Wgsl> for isize {
80    fn lift_to(&self) -> Arc<QTerm> {
81        wgsl_int_term(&format!("{self}i"))
82    }
83}
84/// WGSL `1.5f`.
85pub fn wgsl_float_term(text: &str) -> Arc<QTerm> {
86    leaf("float_literal", text)
87}
88impl LiftTo<Wgsl> for f32 {
89    fn lift_to(&self) -> Arc<QTerm> {
90        wgsl_float_term(&format!("{self}f"))
91    }
92}
93/// WGSL `true` / `false`. The keyword is a child token of the
94/// `bool_literal`, not its text — a lift that spelled it as
95/// text coparsed identically and matched nothing.
96pub fn wgsl_bool_term(b: bool) -> Arc<QTerm> {
97    if b {
98        tb("bool_literal").c(&sym("true")).b()
99    } else {
100        tb("bool_literal").c(&sym("false")).b()
101    }
102}
103impl LiftTo<Wgsl> for bool {
104    fn lift_to(&self) -> Arc<QTerm> {
105        wgsl_bool_term(*self)
106    }
107}
108/// Python `3`. Python integers are arbitrary-precision, so
109/// every Rust integer width lifts losslessly.
110pub fn py_int_term(text: &str) -> Arc<QTerm> {
111    leaf("integer", text)
112}
113impl LiftTo<Python> for u8 {
114    fn lift_to(&self) -> Arc<QTerm> {
115        py_int_term(&self.to_string())
116    }
117}
118impl LiftTo<Python> for u16 {
119    fn lift_to(&self) -> Arc<QTerm> {
120        py_int_term(&self.to_string())
121    }
122}
123impl LiftTo<Python> for u32 {
124    fn lift_to(&self) -> Arc<QTerm> {
125        py_int_term(&self.to_string())
126    }
127}
128impl LiftTo<Python> for u64 {
129    fn lift_to(&self) -> Arc<QTerm> {
130        py_int_term(&self.to_string())
131    }
132}
133impl LiftTo<Python> for u128 {
134    fn lift_to(&self) -> Arc<QTerm> {
135        py_int_term(&self.to_string())
136    }
137}
138impl LiftTo<Python> for usize {
139    fn lift_to(&self) -> Arc<QTerm> {
140        py_int_term(&self.to_string())
141    }
142}
143impl LiftTo<Python> for i8 {
144    fn lift_to(&self) -> Arc<QTerm> {
145        py_int_term(&self.to_string())
146    }
147}
148impl LiftTo<Python> for i16 {
149    fn lift_to(&self) -> Arc<QTerm> {
150        py_int_term(&self.to_string())
151    }
152}
153impl LiftTo<Python> for i32 {
154    fn lift_to(&self) -> Arc<QTerm> {
155        py_int_term(&self.to_string())
156    }
157}
158impl LiftTo<Python> for i64 {
159    fn lift_to(&self) -> Arc<QTerm> {
160        py_int_term(&self.to_string())
161    }
162}
163impl LiftTo<Python> for i128 {
164    fn lift_to(&self) -> Arc<QTerm> {
165        py_int_term(&self.to_string())
166    }
167}
168impl LiftTo<Python> for isize {
169    fn lift_to(&self) -> Arc<QTerm> {
170        py_int_term(&self.to_string())
171    }
172}
173/// Python `1.5`.
174pub fn py_float_term(text: &str) -> Arc<QTerm> {
175    leaf("float", text)
176}
177impl LiftTo<Python> for f32 {
178    fn lift_to(&self) -> Arc<QTerm> {
179        py_float_term(&format!("{self:?}"))
180    }
181}
182impl LiftTo<Python> for f64 {
183    fn lift_to(&self) -> Arc<QTerm> {
184        py_float_term(&format!("{self:?}"))
185    }
186}
187/// Python `True` / `False` — two different node kinds, not one
188/// kind with two spellings.
189pub fn py_bool_term(b: bool) -> Arc<QTerm> {
190    if b {
191        leaf("true", "True")
192    } else {
193        leaf("false", "False")
194    }
195}
196impl LiftTo<Python> for bool {
197    fn lift_to(&self) -> Arc<QTerm> {
198        py_bool_term(*self)
199    }
200}
201/// Python `"s"` — a three-child `string`, and for the empty
202/// string a two-child one, because the parser emits no
203/// `string_content` when there is no content.
204///
205/// Public because `quilt-python`'s own `qlift` needs exactly
206/// this shape: there were two implementations of "lift a string
207/// into Python", and they disagreed (#176). Sharing the
208/// constructor is what keeps them from drifting apart again —
209/// and generating it is what keeps *this* one honest.
210pub fn py_string_term(s: &str) -> Arc<QTerm> {
211    if s.is_empty() {
212        tb("string")
213            .c(&leaf("string_start", "\""))
214            .c(&leaf("string_end", "\""))
215            .b()
216    } else {
217        tb("string")
218            .c(&leaf("string_start", "\""))
219            .c(&leaf("string_content", &py_dquote_escape(s)))
220            .c(&leaf("string_end", "\""))
221            .b()
222    }
223}
224impl LiftTo<Python> for str {
225    fn lift_to(&self) -> Arc<QTerm> {
226        py_string_term(self)
227    }
228}
229impl LiftTo<Python> for String {
230    fn lift_to(&self) -> Arc<QTerm> {
231        py_string_term(self)
232    }
233}
234/// The container shape, with the elements as a parameter. Derived from
235/// the empty, one- and two-element samples: what surrounds the first
236/// element is the opener, what sits between two of them is the
237/// separator, and what the empty container still writes is neither.
238pub fn py_list_term(items: &[Arc<QTerm>]) -> Arc<QTerm> {
239    let mut b = tb("list");
240    b.child(&sym("["));
241    for (i, x) in items.iter().enumerate() {
242        if i > 0 {
243            b.child(&sym(","));
244            b.write(" ");
245        }
246        b.child(x);
247    }
248    b.child(&sym("]"));
249    b.b()
250}
251impl<T: LiftTo<Python>> LiftTo<Python> for [T] {
252    fn lift_to(&self) -> Arc<QTerm> {
253        py_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
254    }
255}
256impl<T: LiftTo<Python>> LiftTo<Python> for Vec<T> {
257    fn lift_to(&self) -> Arc<QTerm> {
258        self.as_slice().lift_to()
259    }
260}
261/// Bash `3` — a bare `number` word.
262pub fn bash_number_term(text: &str) -> Arc<QTerm> {
263    leaf("number", text)
264}
265impl LiftTo<Bash> for u8 {
266    fn lift_to(&self) -> Arc<QTerm> {
267        bash_number_term(&self.to_string())
268    }
269}
270impl LiftTo<Bash> for u16 {
271    fn lift_to(&self) -> Arc<QTerm> {
272        bash_number_term(&self.to_string())
273    }
274}
275impl LiftTo<Bash> for u32 {
276    fn lift_to(&self) -> Arc<QTerm> {
277        bash_number_term(&self.to_string())
278    }
279}
280impl LiftTo<Bash> for u64 {
281    fn lift_to(&self) -> Arc<QTerm> {
282        bash_number_term(&self.to_string())
283    }
284}
285impl LiftTo<Bash> for usize {
286    fn lift_to(&self) -> Arc<QTerm> {
287        bash_number_term(&self.to_string())
288    }
289}
290impl LiftTo<Bash> for i8 {
291    fn lift_to(&self) -> Arc<QTerm> {
292        bash_number_term(&self.to_string())
293    }
294}
295impl LiftTo<Bash> for i16 {
296    fn lift_to(&self) -> Arc<QTerm> {
297        bash_number_term(&self.to_string())
298    }
299}
300impl LiftTo<Bash> for i32 {
301    fn lift_to(&self) -> Arc<QTerm> {
302        bash_number_term(&self.to_string())
303    }
304}
305impl LiftTo<Bash> for i64 {
306    fn lift_to(&self) -> Arc<QTerm> {
307        bash_number_term(&self.to_string())
308    }
309}
310impl LiftTo<Bash> for isize {
311    fn lift_to(&self) -> Arc<QTerm> {
312        bash_number_term(&self.to_string())
313    }
314}
315/// Bash `"s"`. Double-quoted so the value is inert data:
316/// everything the shell would interpret inside `"…"` is
317/// backslash-escaped first.
318pub fn bash_string_term(s: &str) -> Arc<QTerm> {
319    if s.is_empty() {
320        tb("string").c(&sym("\"")).c(&sym("\"")).b()
321    } else {
322        tb("string")
323            .c(&sym("\""))
324            .c(&leaf("string_content", &sh_dquote_escape(s)))
325            .c(&sym("\""))
326            .b()
327    }
328}
329impl LiftTo<Bash> for str {
330    fn lift_to(&self) -> Arc<QTerm> {
331        bash_string_term(self)
332    }
333}
334impl LiftTo<Bash> for String {
335    fn lift_to(&self) -> Arc<QTerm> {
336        bash_string_term(self)
337    }
338}
339/// Zsh `3` — derived from the zsh grammar, not assumed equal to
340/// bash's. Eleven constructs the two shells were assumed to
341/// share had quietly drifted apart once already (#150).
342pub fn zsh_number_term(text: &str) -> Arc<QTerm> {
343    leaf("number", text)
344}
345impl LiftTo<Zsh> for u8 {
346    fn lift_to(&self) -> Arc<QTerm> {
347        zsh_number_term(&self.to_string())
348    }
349}
350impl LiftTo<Zsh> for u16 {
351    fn lift_to(&self) -> Arc<QTerm> {
352        zsh_number_term(&self.to_string())
353    }
354}
355impl LiftTo<Zsh> for u32 {
356    fn lift_to(&self) -> Arc<QTerm> {
357        zsh_number_term(&self.to_string())
358    }
359}
360impl LiftTo<Zsh> for u64 {
361    fn lift_to(&self) -> Arc<QTerm> {
362        zsh_number_term(&self.to_string())
363    }
364}
365impl LiftTo<Zsh> for usize {
366    fn lift_to(&self) -> Arc<QTerm> {
367        zsh_number_term(&self.to_string())
368    }
369}
370impl LiftTo<Zsh> for i8 {
371    fn lift_to(&self) -> Arc<QTerm> {
372        zsh_number_term(&self.to_string())
373    }
374}
375impl LiftTo<Zsh> for i16 {
376    fn lift_to(&self) -> Arc<QTerm> {
377        zsh_number_term(&self.to_string())
378    }
379}
380impl LiftTo<Zsh> for i32 {
381    fn lift_to(&self) -> Arc<QTerm> {
382        zsh_number_term(&self.to_string())
383    }
384}
385impl LiftTo<Zsh> for i64 {
386    fn lift_to(&self) -> Arc<QTerm> {
387        zsh_number_term(&self.to_string())
388    }
389}
390impl LiftTo<Zsh> for isize {
391    fn lift_to(&self) -> Arc<QTerm> {
392        zsh_number_term(&self.to_string())
393    }
394}
395/// Zsh `"s"`.
396pub fn zsh_string_term(s: &str) -> Arc<QTerm> {
397    if s.is_empty() {
398        tb("string").c(&sym("\"")).c(&sym("\"")).b()
399    } else {
400        tb("string")
401            .c(&sym("\""))
402            .c(&leaf("string_content", &sh_dquote_escape(s)))
403            .c(&sym("\""))
404            .b()
405    }
406}
407impl LiftTo<Zsh> for str {
408    fn lift_to(&self) -> Arc<QTerm> {
409        zsh_string_term(self)
410    }
411}
412impl LiftTo<Zsh> for String {
413    fn lift_to(&self) -> Arc<QTerm> {
414        zsh_string_term(self)
415    }
416}
417/// Nix `3`. Nix integers are 64-bit, matching the shell widths.
418pub fn nix_int_term(text: &str) -> Arc<QTerm> {
419    leaf("integer_expression", text)
420}
421impl LiftTo<Nix> for u8 {
422    fn lift_to(&self) -> Arc<QTerm> {
423        nix_int_term(&self.to_string())
424    }
425}
426impl LiftTo<Nix> for u16 {
427    fn lift_to(&self) -> Arc<QTerm> {
428        nix_int_term(&self.to_string())
429    }
430}
431impl LiftTo<Nix> for u32 {
432    fn lift_to(&self) -> Arc<QTerm> {
433        nix_int_term(&self.to_string())
434    }
435}
436impl LiftTo<Nix> for u64 {
437    fn lift_to(&self) -> Arc<QTerm> {
438        nix_int_term(&self.to_string())
439    }
440}
441impl LiftTo<Nix> for usize {
442    fn lift_to(&self) -> Arc<QTerm> {
443        nix_int_term(&self.to_string())
444    }
445}
446impl LiftTo<Nix> for i8 {
447    fn lift_to(&self) -> Arc<QTerm> {
448        nix_int_term(&self.to_string())
449    }
450}
451impl LiftTo<Nix> for i16 {
452    fn lift_to(&self) -> Arc<QTerm> {
453        nix_int_term(&self.to_string())
454    }
455}
456impl LiftTo<Nix> for i32 {
457    fn lift_to(&self) -> Arc<QTerm> {
458        nix_int_term(&self.to_string())
459    }
460}
461impl LiftTo<Nix> for i64 {
462    fn lift_to(&self) -> Arc<QTerm> {
463        nix_int_term(&self.to_string())
464    }
465}
466impl LiftTo<Nix> for isize {
467    fn lift_to(&self) -> Arc<QTerm> {
468        nix_int_term(&self.to_string())
469    }
470}
471/// Nix `1.5`.
472pub fn nix_float_term(text: &str) -> Arc<QTerm> {
473    leaf("float_expression", text)
474}
475impl LiftTo<Nix> for f32 {
476    fn lift_to(&self) -> Arc<QTerm> {
477        nix_float_term(&format!("{self:?}"))
478    }
479}
480impl LiftTo<Nix> for f64 {
481    fn lift_to(&self) -> Arc<QTerm> {
482        nix_float_term(&format!("{self:?}"))
483    }
484}
485/// Nix `true` / `false`. They are builtins rather than
486/// keywords, so the parser reads them as a variable reference
487/// and the name is its own node.
488pub fn nix_bool_term(b: bool) -> Arc<QTerm> {
489    if b {
490        tb("variable_expression").c(&leaf("identifier", "true")).b()
491    } else {
492        tb("variable_expression")
493            .c(&leaf("identifier", "false"))
494            .b()
495    }
496}
497impl LiftTo<Nix> for bool {
498    fn lift_to(&self) -> Arc<QTerm> {
499        nix_bool_term(*self)
500    }
501}
502/// Nix `"s"`. Besides `"` and `\`, the antiquotation opener is
503/// escaped, so the value stays data rather than interpolating.
504pub fn nix_string_term(s: &str) -> Arc<QTerm> {
505    if s.is_empty() {
506        tb("string_expression").c(&sym("\"")).c(&sym("\"")).b()
507    } else {
508        tb("string_expression")
509            .c(&sym("\""))
510            .c(&leaf("string_fragment", &nix_dquote_escape(s)))
511            .c(&sym("\""))
512            .b()
513    }
514}
515impl LiftTo<Nix> for str {
516    fn lift_to(&self) -> Arc<QTerm> {
517        nix_string_term(self)
518    }
519}
520impl LiftTo<Nix> for String {
521    fn lift_to(&self) -> Arc<QTerm> {
522        nix_string_term(self)
523    }
524}
525/// The container shape, with the elements as a parameter. Derived from
526/// the empty, one- and two-element samples: what surrounds the first
527/// element is the opener, what sits between two of them is the
528/// separator, and what the empty container still writes is neither.
529pub fn nix_list_term(items: &[Arc<QTerm>]) -> Arc<QTerm> {
530    let mut b = tb("list_expression");
531    b.child(&sym("["));
532    b.write(" ");
533    for (i, x) in items.iter().enumerate() {
534        if i > 0 {
535            b.write(" ");
536        }
537        b.child(x);
538    }
539    if !items.is_empty() {
540        b.write(" ");
541    }
542    b.child(&sym("]"));
543    b.b()
544}
545impl<T: LiftTo<Nix>> LiftTo<Nix> for [T] {
546    fn lift_to(&self) -> Arc<QTerm> {
547        nix_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
548    }
549}
550impl<T: LiftTo<Nix>> LiftTo<Nix> for Vec<T> {
551    fn lift_to(&self) -> Arc<QTerm> {
552        self.as_slice().lift_to()
553    }
554}
555/// Lean `3` and `-3`. `Nat`/`Int` are arbitrary-precision, so
556/// every Rust integer width lifts losslessly; `num_lit` itself
557/// is unsigned, so the sign is a node rather than a character.
558pub fn lean_num_term(negative: bool, text: &str) -> Arc<QTerm> {
559    if negative {
560        tb("unary_op").c(&sym("-")).c(&leaf("num_lit", text)).b()
561    } else {
562        leaf("num_lit", text)
563    }
564}
565impl LiftTo<Lean> for u8 {
566    fn lift_to(&self) -> Arc<QTerm> {
567        lean_num_term(false, &self.to_string())
568    }
569}
570impl LiftTo<Lean> for u16 {
571    fn lift_to(&self) -> Arc<QTerm> {
572        lean_num_term(false, &self.to_string())
573    }
574}
575impl LiftTo<Lean> for u32 {
576    fn lift_to(&self) -> Arc<QTerm> {
577        lean_num_term(false, &self.to_string())
578    }
579}
580impl LiftTo<Lean> for u64 {
581    fn lift_to(&self) -> Arc<QTerm> {
582        lean_num_term(false, &self.to_string())
583    }
584}
585impl LiftTo<Lean> for u128 {
586    fn lift_to(&self) -> Arc<QTerm> {
587        lean_num_term(false, &self.to_string())
588    }
589}
590impl LiftTo<Lean> for usize {
591    fn lift_to(&self) -> Arc<QTerm> {
592        lean_num_term(false, &self.to_string())
593    }
594}
595impl LiftTo<Lean> for i8 {
596    fn lift_to(&self) -> Arc<QTerm> {
597        lean_num_term(*self < 0, &self.unsigned_abs().to_string())
598    }
599}
600impl LiftTo<Lean> for i16 {
601    fn lift_to(&self) -> Arc<QTerm> {
602        lean_num_term(*self < 0, &self.unsigned_abs().to_string())
603    }
604}
605impl LiftTo<Lean> for i32 {
606    fn lift_to(&self) -> Arc<QTerm> {
607        lean_num_term(*self < 0, &self.unsigned_abs().to_string())
608    }
609}
610impl LiftTo<Lean> for i64 {
611    fn lift_to(&self) -> Arc<QTerm> {
612        lean_num_term(*self < 0, &self.unsigned_abs().to_string())
613    }
614}
615impl LiftTo<Lean> for i128 {
616    fn lift_to(&self) -> Arc<QTerm> {
617        lean_num_term(*self < 0, &self.unsigned_abs().to_string())
618    }
619}
620impl LiftTo<Lean> for isize {
621    fn lift_to(&self) -> Arc<QTerm> {
622        lean_num_term(*self < 0, &self.unsigned_abs().to_string())
623    }
624}
625/// Lean `1.5` and `-1.5`.
626pub fn lean_float_term(negative: bool, text: &str) -> Arc<QTerm> {
627    if negative {
628        tb("unary_op")
629            .c(&sym("-"))
630            .c(&leaf("scientific_lit", text))
631            .b()
632    } else {
633        leaf("scientific_lit", text)
634    }
635}
636impl LiftTo<Lean> for f32 {
637    fn lift_to(&self) -> Arc<QTerm> {
638        {
639            let s = format!("{self:?}");
640            match s.strip_prefix('-') {
641                Some(mag) => lean_float_term(true, mag),
642                None => lean_float_term(false, &s),
643            }
644        }
645    }
646}
647impl LiftTo<Lean> for f64 {
648    fn lift_to(&self) -> Arc<QTerm> {
649        {
650            let s = format!("{self:?}");
651            match s.strip_prefix('-') {
652                Some(mag) => lean_float_term(true, mag),
653                None => lean_float_term(false, &s),
654            }
655        }
656    }
657}
658/// Lean `true` / `false`.
659pub fn lean_bool_term(b: bool) -> Arc<QTerm> {
660    if b {
661        tb("true_const").c(&sym("true")).b()
662    } else {
663        tb("false_const").c(&sym("false")).b()
664    }
665}
666impl LiftTo<Lean> for bool {
667    fn lift_to(&self) -> Arc<QTerm> {
668        lean_bool_term(*self)
669    }
670}
671/// Lean `"s"`. The quote tokens are children and the body sits
672/// between them as raw text, not as a node of its own.
673pub fn lean_string_term(s: &str) -> Arc<QTerm> {
674    if s.is_empty() {
675        tb("str_lit").c(&sym("\"")).c(&sym("\"")).b()
676    } else {
677        tb("str_lit")
678            .c(&sym("\""))
679            .w(&lean_dquote_escape(s))
680            .c(&sym("\""))
681            .b()
682    }
683}
684impl LiftTo<Lean> for str {
685    fn lift_to(&self) -> Arc<QTerm> {
686        lean_string_term(self)
687    }
688}
689impl LiftTo<Lean> for String {
690    fn lift_to(&self) -> Arc<QTerm> {
691        lean_string_term(self)
692    }
693}
694/// The container shape, with the elements as a parameter. Derived from
695/// the empty, one- and two-element samples: what surrounds the first
696/// element is the opener, what sits between two of them is the
697/// separator, and what the empty container still writes is neither.
698pub fn lean_list_term(items: &[Arc<QTerm>]) -> Arc<QTerm> {
699    let mut b = tb("list_lit");
700    b.child(&sym("["));
701    for (i, x) in items.iter().enumerate() {
702        if i > 0 {
703            b.child(&sym(","));
704            b.write(" ");
705        }
706        b.child(x);
707    }
708    b.child(&sym("]"));
709    b.b()
710}
711/// SQL `7` and `-7`. The sign is a child token of the
712/// `literal` rather than part of its text, because `_integer`
713/// is `seq(optional(choice("-", "+")), …)` — so a lift that
714/// spelled `-7` as one leaf built a tree the parser never
715/// produces, which `smatch` and `rewrite` can see (#174).
716pub fn sql_number_term(negative: bool, text: &str) -> Arc<QTerm> {
717    if negative {
718        tb("literal").c(&sym("-")).w(text).b()
719    } else {
720        leaf("literal", text)
721    }
722}
723impl LiftTo<Sql> for u8 {
724    fn lift_to(&self) -> Arc<QTerm> {
725        sql_number_term(false, &self.to_string())
726    }
727}
728impl LiftTo<Sql> for u16 {
729    fn lift_to(&self) -> Arc<QTerm> {
730        sql_number_term(false, &self.to_string())
731    }
732}
733impl LiftTo<Sql> for u32 {
734    fn lift_to(&self) -> Arc<QTerm> {
735        sql_number_term(false, &self.to_string())
736    }
737}
738impl LiftTo<Sql> for u64 {
739    fn lift_to(&self) -> Arc<QTerm> {
740        sql_number_term(false, &self.to_string())
741    }
742}
743impl LiftTo<Sql> for u128 {
744    fn lift_to(&self) -> Arc<QTerm> {
745        sql_number_term(false, &self.to_string())
746    }
747}
748impl LiftTo<Sql> for usize {
749    fn lift_to(&self) -> Arc<QTerm> {
750        sql_number_term(false, &self.to_string())
751    }
752}
753impl LiftTo<Sql> for i8 {
754    fn lift_to(&self) -> Arc<QTerm> {
755        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
756    }
757}
758impl LiftTo<Sql> for i16 {
759    fn lift_to(&self) -> Arc<QTerm> {
760        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
761    }
762}
763impl LiftTo<Sql> for i32 {
764    fn lift_to(&self) -> Arc<QTerm> {
765        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
766    }
767}
768impl LiftTo<Sql> for i64 {
769    fn lift_to(&self) -> Arc<QTerm> {
770        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
771    }
772}
773impl LiftTo<Sql> for i128 {
774    fn lift_to(&self) -> Arc<QTerm> {
775        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
776    }
777}
778impl LiftTo<Sql> for isize {
779    fn lift_to(&self) -> Arc<QTerm> {
780        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
781    }
782}
783impl LiftTo<Sql> for f32 {
784    fn lift_to(&self) -> Arc<QTerm> {
785        {
786            let s = format!("{self:?}");
787            match s.strip_prefix('-') {
788                Some(mag) => sql_number_term(true, mag),
789                None => sql_number_term(false, &s),
790            }
791        }
792    }
793}
794impl LiftTo<Sql> for f64 {
795    fn lift_to(&self) -> Arc<QTerm> {
796        {
797            let s = format!("{self:?}");
798            match s.strip_prefix('-') {
799                Some(mag) => sql_number_term(true, mag),
800                None => sql_number_term(false, &s),
801            }
802        }
803    }
804}
805/// SQL `TRUE` / `FALSE`. The keyword is its own node inside the
806/// `literal`, unlike the numeric and string forms.
807pub fn sql_bool_term(b: bool) -> Arc<QTerm> {
808    if b {
809        tb("literal").c(&leaf("keyword_true", "TRUE")).b()
810    } else {
811        tb("literal").c(&leaf("keyword_false", "FALSE")).b()
812    }
813}
814impl LiftTo<Sql> for bool {
815    fn lift_to(&self) -> Arc<QTerm> {
816        sql_bool_term(*self)
817    }
818}
819/// SQL `'s'` — one `literal` token, quotes included. The empty
820/// string needs no separate branch and an escaped quote needs
821/// no exemption, because nothing inside the quotes is a node of
822/// its own: `'O''Hara'` is still a single token.
823pub fn sql_string_term(s: &str) -> Arc<QTerm> {
824    leaf("literal", &format!("'{}'", sql_squote_escape(s)))
825}
826impl LiftTo<Sql> for str {
827    fn lift_to(&self) -> Arc<QTerm> {
828        sql_string_term(self)
829    }
830}
831impl LiftTo<Sql> for String {
832    fn lift_to(&self) -> Arc<QTerm> {
833        sql_string_term(self)
834    }
835}
836/// The container shape, with the elements as a parameter. Derived from
837/// the empty, one- and two-element samples: what surrounds the first
838/// element is the opener, what sits between two of them is the
839/// separator, and what the empty container still writes is neither.
840pub fn sql_list_term(items: &[Arc<QTerm>]) -> Arc<QTerm> {
841    let mut b = tb("list");
842    b.child(&sym("("));
843    for (i, x) in items.iter().enumerate() {
844        if i > 0 {
845            b.child(&sym(","));
846            b.write(" ");
847        }
848        b.child(x);
849    }
850    b.child(&sym(")"));
851    b.b()
852}
853impl<T: LiftTo<Sql>> LiftTo<Sql> for [T] {
854    fn lift_to(&self) -> Arc<QTerm> {
855        sql_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
856    }
857}
858impl<T: LiftTo<Sql>> LiftTo<Sql> for Vec<T> {
859    fn lift_to(&self) -> Arc<QTerm> {
860        self.as_slice().lift_to()
861    }
862}
863impl LiftTo<MySql> for u8 {
864    fn lift_to(&self) -> Arc<QTerm> {
865        sql_number_term(false, &self.to_string())
866    }
867}
868impl LiftTo<MySql> for u16 {
869    fn lift_to(&self) -> Arc<QTerm> {
870        sql_number_term(false, &self.to_string())
871    }
872}
873impl LiftTo<MySql> for u32 {
874    fn lift_to(&self) -> Arc<QTerm> {
875        sql_number_term(false, &self.to_string())
876    }
877}
878impl LiftTo<MySql> for u64 {
879    fn lift_to(&self) -> Arc<QTerm> {
880        sql_number_term(false, &self.to_string())
881    }
882}
883impl LiftTo<MySql> for u128 {
884    fn lift_to(&self) -> Arc<QTerm> {
885        sql_number_term(false, &self.to_string())
886    }
887}
888impl LiftTo<MySql> for usize {
889    fn lift_to(&self) -> Arc<QTerm> {
890        sql_number_term(false, &self.to_string())
891    }
892}
893impl LiftTo<MySql> for i8 {
894    fn lift_to(&self) -> Arc<QTerm> {
895        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
896    }
897}
898impl LiftTo<MySql> for i16 {
899    fn lift_to(&self) -> Arc<QTerm> {
900        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
901    }
902}
903impl LiftTo<MySql> for i32 {
904    fn lift_to(&self) -> Arc<QTerm> {
905        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
906    }
907}
908impl LiftTo<MySql> for i64 {
909    fn lift_to(&self) -> Arc<QTerm> {
910        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
911    }
912}
913impl LiftTo<MySql> for i128 {
914    fn lift_to(&self) -> Arc<QTerm> {
915        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
916    }
917}
918impl LiftTo<MySql> for isize {
919    fn lift_to(&self) -> Arc<QTerm> {
920        sql_number_term(*self < 0, &self.unsigned_abs().to_string())
921    }
922}
923impl LiftTo<MySql> for f32 {
924    fn lift_to(&self) -> Arc<QTerm> {
925        {
926            let s = format!("{self:?}");
927            match s.strip_prefix('-') {
928                Some(mag) => sql_number_term(true, mag),
929                None => sql_number_term(false, &s),
930            }
931        }
932    }
933}
934impl LiftTo<MySql> for f64 {
935    fn lift_to(&self) -> Arc<QTerm> {
936        {
937            let s = format!("{self:?}");
938            match s.strip_prefix('-') {
939                Some(mag) => sql_number_term(true, mag),
940                None => sql_number_term(false, &s),
941            }
942        }
943    }
944}
945impl LiftTo<MySql> for bool {
946    fn lift_to(&self) -> Arc<QTerm> {
947        sql_bool_term(*self)
948    }
949}
950/// `MySQL` `'s'`. Identical in shape to `sql_string_term` and
951/// different only in the escaper it calls — see
952/// `mysql_squote_escape` for why that difference exists.
953pub fn mysql_string_term(s: &str) -> Arc<QTerm> {
954    leaf("literal", &format!("'{}'", mysql_squote_escape(s)))
955}
956impl LiftTo<MySql> for str {
957    fn lift_to(&self) -> Arc<QTerm> {
958        mysql_string_term(self)
959    }
960}
961impl LiftTo<MySql> for String {
962    fn lift_to(&self) -> Arc<QTerm> {
963        mysql_string_term(self)
964    }
965}
966impl<T: LiftTo<MySql>> LiftTo<MySql> for [T] {
967    fn lift_to(&self) -> Arc<QTerm> {
968        sql_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
969    }
970}
971impl<T: LiftTo<MySql>> LiftTo<MySql> for Vec<T> {
972    fn lift_to(&self) -> Arc<QTerm> {
973        self.as_slice().lift_to()
974    }
975}
976/// TypeScript `3`, `1.5`, `-3`. One `number` node for integers
977/// and floats; unsigned, so the sign is a `unary_expression`
978/// rather than a character.
979///
980/// Public because `quilt-python`'s `qlift_ts` builds the same
981/// literal, and a shape written twice is how #176 started.
982pub fn ts_number_term(negative: bool, text: &str) -> Arc<QTerm> {
983    if negative {
984        tb("unary_expression")
985            .c(&sym("-"))
986            .c(&leaf("number", text))
987            .b()
988    } else {
989        leaf("number", text)
990    }
991}
992impl LiftTo<TypeScript> for u8 {
993    fn lift_to(&self) -> Arc<QTerm> {
994        ts_number_term(false, &self.to_string())
995    }
996}
997impl LiftTo<TypeScript> for u16 {
998    fn lift_to(&self) -> Arc<QTerm> {
999        ts_number_term(false, &self.to_string())
1000    }
1001}
1002impl LiftTo<TypeScript> for u32 {
1003    fn lift_to(&self) -> Arc<QTerm> {
1004        ts_number_term(false, &self.to_string())
1005    }
1006}
1007impl LiftTo<TypeScript> for u64 {
1008    fn lift_to(&self) -> Arc<QTerm> {
1009        ts_number_term(false, &self.to_string())
1010    }
1011}
1012impl LiftTo<TypeScript> for u128 {
1013    fn lift_to(&self) -> Arc<QTerm> {
1014        ts_number_term(false, &self.to_string())
1015    }
1016}
1017impl LiftTo<TypeScript> for usize {
1018    fn lift_to(&self) -> Arc<QTerm> {
1019        ts_number_term(false, &self.to_string())
1020    }
1021}
1022impl LiftTo<TypeScript> for i8 {
1023    fn lift_to(&self) -> Arc<QTerm> {
1024        ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1025    }
1026}
1027impl LiftTo<TypeScript> for i16 {
1028    fn lift_to(&self) -> Arc<QTerm> {
1029        ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1030    }
1031}
1032impl LiftTo<TypeScript> for i32 {
1033    fn lift_to(&self) -> Arc<QTerm> {
1034        ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1035    }
1036}
1037impl LiftTo<TypeScript> for i64 {
1038    fn lift_to(&self) -> Arc<QTerm> {
1039        ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1040    }
1041}
1042impl LiftTo<TypeScript> for i128 {
1043    fn lift_to(&self) -> Arc<QTerm> {
1044        ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1045    }
1046}
1047impl LiftTo<TypeScript> for isize {
1048    fn lift_to(&self) -> Arc<QTerm> {
1049        ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1050    }
1051}
1052impl LiftTo<TypeScript> for f32 {
1053    fn lift_to(&self) -> Arc<QTerm> {
1054        {
1055            let s = format!("{self:?}");
1056            match s.strip_prefix('-') {
1057                Some(mag) => ts_number_term(true, mag),
1058                None => ts_number_term(false, &s),
1059            }
1060        }
1061    }
1062}
1063impl LiftTo<TypeScript> for f64 {
1064    fn lift_to(&self) -> Arc<QTerm> {
1065        {
1066            let s = format!("{self:?}");
1067            match s.strip_prefix('-') {
1068                Some(mag) => ts_number_term(true, mag),
1069                None => ts_number_term(false, &s),
1070            }
1071        }
1072    }
1073}
1074/// TypeScript `true` / `false` — node kinds of their own.
1075pub fn ts_bool_term(b: bool) -> Arc<QTerm> {
1076    if b {
1077        sym("true")
1078    } else {
1079        sym("false")
1080    }
1081}
1082impl LiftTo<TypeScript> for bool {
1083    fn lift_to(&self) -> Arc<QTerm> {
1084        ts_bool_term(*self)
1085    }
1086}
1087/// TypeScript `"s"`. Like Python's, the empty string has no
1088/// `string_fragment` child, because there is no content.
1089pub fn ts_string_term(s: &str) -> Arc<QTerm> {
1090    if s.is_empty() {
1091        tb("string").c(&sym("\"")).c(&sym("\"")).b()
1092    } else {
1093        tb("string")
1094            .c(&sym("\""))
1095            .c(&leaf("string_fragment", &ts_dquote_escape(s)))
1096            .c(&sym("\""))
1097            .b()
1098    }
1099}
1100impl LiftTo<TypeScript> for str {
1101    fn lift_to(&self) -> Arc<QTerm> {
1102        ts_string_term(self)
1103    }
1104}
1105impl LiftTo<TypeScript> for String {
1106    fn lift_to(&self) -> Arc<QTerm> {
1107        ts_string_term(self)
1108    }
1109}
1110/// The container shape, with the elements as a parameter. Derived from
1111/// the empty, one- and two-element samples: what surrounds the first
1112/// element is the opener, what sits between two of them is the
1113/// separator, and what the empty container still writes is neither.
1114pub fn ts_list_term(items: &[Arc<QTerm>]) -> Arc<QTerm> {
1115    let mut b = tb("array");
1116    b.child(&sym("["));
1117    for (i, x) in items.iter().enumerate() {
1118        if i > 0 {
1119            b.child(&sym(","));
1120            b.write(" ");
1121        }
1122        b.child(x);
1123    }
1124    b.child(&sym("]"));
1125    b.b()
1126}
1127impl<T: LiftTo<TypeScript>> LiftTo<TypeScript> for [T] {
1128    fn lift_to(&self) -> Arc<QTerm> {
1129        ts_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
1130    }
1131}
1132impl<T: LiftTo<TypeScript>> LiftTo<TypeScript> for Vec<T> {
1133    fn lift_to(&self) -> Arc<QTerm> {
1134        self.as_slice().lift_to()
1135    }
1136}
1137/// HTML text content. Every value is a `text` node; a string's
1138/// is entity-escaped by `escape_html` first, the rule the
1139/// runtimes' `qlift_html` share. No list impl: HTML has no
1140/// literal syntax for a sequence to borrow.
1141pub fn html_text_term(text: &str) -> Arc<QTerm> {
1142    leaf("text", text)
1143}
1144impl LiftTo<Html> for u8 {
1145    fn lift_to(&self) -> Arc<QTerm> {
1146        html_text_term(&self.to_string())
1147    }
1148}
1149impl LiftTo<Html> for u16 {
1150    fn lift_to(&self) -> Arc<QTerm> {
1151        html_text_term(&self.to_string())
1152    }
1153}
1154impl LiftTo<Html> for u32 {
1155    fn lift_to(&self) -> Arc<QTerm> {
1156        html_text_term(&self.to_string())
1157    }
1158}
1159impl LiftTo<Html> for u64 {
1160    fn lift_to(&self) -> Arc<QTerm> {
1161        html_text_term(&self.to_string())
1162    }
1163}
1164impl LiftTo<Html> for u128 {
1165    fn lift_to(&self) -> Arc<QTerm> {
1166        html_text_term(&self.to_string())
1167    }
1168}
1169impl LiftTo<Html> for usize {
1170    fn lift_to(&self) -> Arc<QTerm> {
1171        html_text_term(&self.to_string())
1172    }
1173}
1174impl LiftTo<Html> for i8 {
1175    fn lift_to(&self) -> Arc<QTerm> {
1176        html_text_term(&self.to_string())
1177    }
1178}
1179impl LiftTo<Html> for i16 {
1180    fn lift_to(&self) -> Arc<QTerm> {
1181        html_text_term(&self.to_string())
1182    }
1183}
1184impl LiftTo<Html> for i32 {
1185    fn lift_to(&self) -> Arc<QTerm> {
1186        html_text_term(&self.to_string())
1187    }
1188}
1189impl LiftTo<Html> for i64 {
1190    fn lift_to(&self) -> Arc<QTerm> {
1191        html_text_term(&self.to_string())
1192    }
1193}
1194impl LiftTo<Html> for i128 {
1195    fn lift_to(&self) -> Arc<QTerm> {
1196        html_text_term(&self.to_string())
1197    }
1198}
1199impl LiftTo<Html> for isize {
1200    fn lift_to(&self) -> Arc<QTerm> {
1201        html_text_term(&self.to_string())
1202    }
1203}
1204impl LiftTo<Html> for bool {
1205    fn lift_to(&self) -> Arc<QTerm> {
1206        html_text_term(&self.to_string())
1207    }
1208}
1209impl LiftTo<Html> for f32 {
1210    fn lift_to(&self) -> Arc<QTerm> {
1211        html_text_term(&format!("{self:?}"))
1212    }
1213}
1214impl LiftTo<Html> for f64 {
1215    fn lift_to(&self) -> Arc<QTerm> {
1216        html_text_term(&format!("{self:?}"))
1217    }
1218}
1219impl LiftTo<Html> for str {
1220    fn lift_to(&self) -> Arc<QTerm> {
1221        html_text_term(&escape_html(self))
1222    }
1223}
1224impl LiftTo<Html> for String {
1225    fn lift_to(&self) -> Arc<QTerm> {
1226        html_text_term(&escape_html(self))
1227    }
1228}
1229impl<T: LiftTo<Lean>> LiftTo<Lean> for [T] {
1230    fn lift_to(&self) -> Arc<QTerm> {
1231        lean_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
1232    }
1233}
1234impl<T: LiftTo<Lean>> LiftTo<Lean> for Vec<T> {
1235    fn lift_to(&self) -> Arc<QTerm> {
1236        self.as_slice().lift_to()
1237    }
1238}