quilt/langs/text/meta.rs
1//! The text meta-language: the *identity* host.
2//!
3//! Text holds the object-level code as unparsed lines — same tags, same
4//! `cmds`, same text — and refuses every operator with a diagnostic naming a
5//! real host. The mechanism is [`HoldMetaLanguage`](crate::langs::hold), shared
6//! with HTML (`langs::html::meta`); this file is the text dialect of it, and
7//! the tests that show what "holds the object level code as unparsed lines"
8//! *means*.
9//!
10//! `langs/omni.rs` leaves `text` out of its `metas` section, so `Omni` never
11//! reaches this. The type is `pub`, though, so a consumer can wire it into a
12//! `Single` or a `DictMulti` by hand — which used to hit three `todo!()`s and
13//! abort with no explanation (#174, finding J). That is the class of panic
14//! issue #11 set out to remove: `langs/text/lang.rs` beside it was converted at
15//! the time and this file was missed, while both `conformance/spec/text.toml`
16//! and `docs/wiki/concrete-languages.md` already documented the expansion as
17//! identity.
18
19pub use crate::langs::hold::{HoldMetaLanguage, Holds};
20
21/**************************************************************/
22
23/// Plain text: the original identity host.
24pub struct Text;
25
26impl Holds for Text {
27 const NAME: &'static str = "text";
28 const ANNO: &'static str = "txt";
29}
30
31pub type TextMetaLanguage = HoldMetaLanguage<Text>;
32
33/**************************************************************/
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38 use crate::lang::Arity;
39 use crate::meta::MetaLanguage as _;
40 use crate::prelude::*;
41 use crate::qterm::QTerm;
42 use crate::term::{CmdOrHole, STerm as _, Term as _};
43
44 /// The `cmds` the parser gives a quote: annotation, opening glyph, the
45 /// body's hole, closing glyph (see `Multi::build_nodes`).
46 fn bracket_cmds(anno: &str, open: &str, close: &str) -> Vec<CmdOrHole> {
47 vec![cmd(write(anno)), cmd(write(open)), HOLE, cmd(write(close))]
48 }
49
50 /// A tuple comes back as itself — same tag, same structure, same text. This
51 /// is the whole contract: the object-level code is held, not translated.
52 #[test]
53 fn holds_a_tuple_verbatim() -> Result<()> {
54 let meta = TextMetaLanguage::default();
55 let node = tb("text")
56 .w("a=")
57 .c(&leaf("text", "1"))
58 .w(" b=")
59 .c(&leaf("text", "2"))
60 .b();
61 let QTerm::Tuple { tag, terms, cmds } = &*node else {
62 unreachable!("built a tuple")
63 };
64
65 let held = meta.expand_tuple("text", tag, terms, cmds, Arity::Variadic)?;
66 assert_eq!(held, node, "expansion must be the identity");
67 assert_eq!(held.coparse(), "a=1 b=2");
68 Ok(())
69 }
70
71 /// A nested quote keeps its glyphs *and* its annotation, so the later stage
72 /// that owns it sees exactly what the author wrote.
73 #[test]
74 fn holds_a_nested_quote_verbatim() -> Result<()> {
75 let meta = TextMetaLanguage::default();
76 let cmds = bracket_cmds("rs", "↖", "↗");
77 let held = meta.expand_quote("text", "text", 1, "rs", &leaf("text", "x + 1"), &cmds)?;
78
79 assert_eq!(held.coparse(), "rs↖x + 1↗");
80 assert_eq!(held.tag(), crate::qterm::QTermTag::tuple("text"));
81 Ok(())
82 }
83
84 /// Same for an unquote that does not reach ground.
85 #[test]
86 fn holds_a_nested_unquote_verbatim() -> Result<()> {
87 let meta = TextMetaLanguage::default();
88 let cmds = bracket_cmds("", "↙", "↘");
89 let held = meta.expand_unquote("text", "text", 1, "text", &leaf("text", "v"), &cmds)?;
90
91 assert_eq!(held.coparse(), "↙v↘");
92 Ok(())
93 }
94
95 /// A hand-built node whose holes and children disagree is an error, not a
96 /// silently truncated fragment.
97 #[test]
98 fn mismatched_holes_are_an_error() {
99 let meta = TextMetaLanguage::default();
100 let err = meta
101 .expand_tuple(
102 "text",
103 "text",
104 &[leaf("text", "1")],
105 &[cmd(write("no holes here"))],
106 Arity::Variadic,
107 )
108 .expect_err("one child, no hole");
109 assert!(err.to_string().contains("0 hole(s) for 1"), "{err}");
110 }
111
112 /// Every operator spelling is an `Err` naming the alternative — never a
113 /// panic, and never a `__LIFT__`-style placeholder leaking into output.
114 /// The conformance battery (#144) treats a panic as a hard failure for
115 /// every language including the unsupported cases, which is what surfaced
116 /// the same bug in `langs/text/lang.rs`.
117 #[test]
118 fn operators_error_with_an_alternative() {
119 let meta = TextMetaLanguage::default();
120 let refusals = [
121 ("↑", meta.lift_str("text")),
122 ("↓", meta.reduce_str("")),
123 ("←", meta.emit_str()),
124 ("⟨T⟩", meta.type_str()),
125 ("⟨N⟩", meta.name_str()),
126 ];
127 for (glyph, refusal) in refusals {
128 let err = refusal
129 .expect_err("text has no host expressions")
130 .to_string();
131 assert!(err.starts_with("text "), "{glyph}: {err}");
132 assert!(err.contains(glyph), "should name the operator: {err}");
133 }
134 }
135}
136
137/**************************************************************/
138
139/// End-to-end: text wired up as a host by hand, the way a consumer outside
140/// `Omni` reaches this meta. These are the tests that show what "holds the
141/// object level code as unparsed lines" *means*.
142#[cfg(all(test, feature = "parse"))]
143mod host_tests {
144 use super::*;
145 use crate::langs::text::lang::DynTextLanguage;
146 use crate::multi::DictMulti;
147 use crate::prelude::*;
148 use crate::term::STerm as _;
149
150 /// A `DictMulti` with text as both the object language and the host.
151 fn text_host() -> DictMulti {
152 let mut multi = DictMulti::default();
153 multi.add_lang("text", bx(DynTextLanguage));
154 multi.add_meta("text", bx(TextMetaLanguage::default()));
155 multi.add_alias("txt", "text");
156 multi
157 }
158
159 fn expand(src: &str) -> Result<String> {
160 let mut multi = text_host();
161 let parsed = multi.parse_chain(&["text"], src)?;
162 Ok(multi.expand_lang("text", &parsed)?.coparse())
163 }
164
165 /// Ground text is its own output, and a quote at ground contributes its
166 /// body — the brackets are staging, not content.
167 #[test]
168 fn a_quote_contributes_its_text() -> Result<()> {
169 assert_eq!(expand("Hello ↖world↗!")?, "Hello world!");
170 Ok(())
171 }
172
173 /// Multiple lines survive as lines: the `cmds` that laid the fragment out
174 /// are the ones held.
175 #[test]
176 fn lines_stay_lines() -> Result<()> {
177 assert_eq!(
178 expand("↖alpha\nbeta\n\ndelta↗")?,
179 "alpha\nbeta\n\ndelta",
180 "held text keeps its line structure"
181 );
182 Ok(())
183 }
184
185 /// An unquote reaching ground splices the ground term, which in a text host
186 /// is ground text — so `↖…↙x↘…↗` reads straight through.
187 #[test]
188 fn a_ground_unquote_splices_its_text() -> Result<()> {
189 assert_eq!(expand("↖a ↙b↘ c↗")?, "a b c");
190 Ok(())
191 }
192
193 /// A quote *inside* a quote belongs to the next stage, so its glyphs are
194 /// still there afterwards — this is the `expand_quote` path.
195 #[test]
196 fn an_inner_quote_survives_expansion() -> Result<()> {
197 assert_eq!(expand("↖outer ↖inner↗ tail↗")?, "outer ↖inner↗ tail");
198 Ok(())
199 }
200
201 /// A ground operator is refused with a diagnostic rather than expanding to
202 /// a placeholder. `↑` is spelled while the fragment is being built, so this
203 /// covers the spelling accessors through the real parse path.
204 #[test]
205 fn a_ground_operator_is_refused() {
206 let err = expand("value: ↑\n")
207 .expect_err("text has no lift")
208 .to_string();
209 assert!(err.contains("text can't lift"), "{err}");
210 }
211}