1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! The characters Quilt reserves, and the `\` escape that lets a fragment use
//! one as ordinary text.
//!
//! These are facts about Quilt's *surface syntax* and involve no parser, which
//! is why they live here rather than in [`crate::node`]: `node` is behind the
//! `parse` feature, and the writer in [`crate::strcmd`] — which is on the
//! runtime-only path that `quilt-wasm` and `nanobots-codegen` build for
//! `wasm32-unknown-unknown` (#162) — needs them to render a term back as
//! `.quilt` source (#223). `node` re-exports everything here, so
//! `quilt::node::GLYPHS` and friends keep working.
/// UTF-8 length of a Quilt glyph. Every glyph in [`GLYPHS`] is this wide (see
/// `node::tests::glyph_lengths_are_uniform`), which is what lets callers doing
/// byte arithmetic over the surface syntax — e.g. `quilt-lsp`'s `regions` — use
/// a single constant. Derived from a glyph rather than written as `3` so it
/// cannot drift from the glyph set it describes.
pub const ARROW_LEN: usize = '↖'.len_utf8;
/// UTF-8 length of the `\` that introduces an escape.
pub const ESCAPE_LEN: usize = '\\'.len_utf8;
/// The characters Quilt gives special meaning to, and hence the ones `\` can
/// escape: the four quote/unquote arrows, lift, reduce, emit, and the `⟨…⟩`
/// delimiters.
///
/// This is the single source of truth. `node::parse` reads it directly when
/// deciding what may appear bare in content and what a `\` escapes, so the
/// parser cannot drift from it — which it could when the same set was also
/// spelled out in three character classes in a tree-sitter grammar, and did
/// (`←` was Quilt's emit glyph but missing from the grammar's escape class, so
/// `\←` parsed as a literal backslash-arrow — issue #141).
pub const GLYPHS: = ;
/// The glyphs that are *operators* rather than brackets: `↑` lift, `↓` reduce,
/// `←` emit.
///
/// The distinction matters exactly once, in [`crate::qterm`]'s source renderer:
/// an operator inside an unresolved quote is deferred to the next stage and
/// plugged back into the term as a bare glyph (see `Multi::build_nodes`), so it
/// must reach the output unescaped, while the same glyph arriving as escaped
/// *content* must not. Brackets are never deferred, so they are never ambiguous.
pub const OPERATOR_GLYPHS: = ;
/// The machine glyph: `lang⟨M⟩` is an expression obtaining a machine speaking
/// `lang` (issue #273). It lives here rather than in `node::parse` next to
/// `⟨T⟩` and `⟨N⟩` because it is the one `⟨…⟩` spelling that can be *deferred*
/// — [`is_deferred_operator`] below has to recognise it, and that check is on
/// the runtime-only path where the parser is not.
pub const MACHINE: &str = "⟨M⟩";
/// Escape every Quilt glyph in `s` with a leading `\`.
///
/// This is the inverse of the grammar's `escape` rule and exists so that
/// `Node::coparse` round-trips: a `Node::Content` can only hold a glyph if it
/// came from a `\`-escape in the source (the grammar's `_char` class excludes
/// all of them), so writing the glyph bare would make it re-parse as the
/// operator instead of as content.
/// Strip the `\` from every escaped Quilt glyph in `s` — the inverse of
/// [`escape`]. Parsing does this structurally (via the grammar's `escape` rule),
/// so this is for callers holding raw source text.
/// Whether `s` is a language annotation: `[a-z][a-z0-9]*`, or empty.
///
/// The leading letter is load-bearing — without it `42↖…↗` would be a quote of
/// a language named "42" rather than the literal `42` followed by a bare quote
/// — and so are the digits, which is how `lean4` annotates anything at all.
/// This is the shape `node::parse` scans; it is spelled once, here, so the two
/// cannot drift.
/// A quote annotation split into its language and its representation:
/// `ts⟨str⟩` → `("ts", Some("str"))`, `⟨qt⟩` → `("", Some("qt"))`, `py` →
/// `("py", None)`. Either part may be empty; an empty language is inherited
/// (see `Multi::build_nodes`), and no representation means the default or an
/// inherited one.
/// The length of a representation `⟨name⟩` starting at `s`'s first byte, when
/// a `↖` follows it — the only place one can stand. `name` has an
/// annotation's shape, `[a-z][a-z0-9]*`, which keeps `⟨T⟩`, `⟨N⟩` and `⟨M⟩`
/// (uppercase) what they were.
/// Whether `s` is exactly one deferred Quilt operator, as
/// `Multi::build_nodes` plugs it back into a term: `↑`, `←`, or a reduce or
/// machine glyph with its optional language annotation (`↓`, `py↓`, `⟨M⟩`,
/// `sql⟨M⟩`).