|
1 |
+ |
//! `for <binder> in copy "<file>" as <key>`: copy read at macro time.
|
|
2 |
+ |
//!
|
|
3 |
+ |
//! The production that lets a screen's words leave Rust. `policy.rs` held five
|
|
4 |
+ |
//! prose sections and seven document records as `const` arrays and looped over
|
|
5 |
+ |
//! them, which put the page's copy behind a Rust file and behind a `for` loop
|
|
6 |
+ |
//! the emitter cannot fold: a proc macro cannot evaluate a `const`, so the loop
|
|
7 |
+ |
//! survives into whatever is emitted, however the emitter is written.
|
|
8 |
+ |
//!
|
|
9 |
+ |
//! This reads the copy instead. The file is TOML, the key names an array of
|
|
10 |
+ |
//! tables, and the loop is unrolled here into one set of items per entry with
|
|
11 |
+ |
//! `binder.field` replaced by the value it holds. Nothing about the loop
|
|
12 |
+ |
//! reaches [`crate::ast`]: what the rest of the crate sees is the members
|
|
13 |
+ |
//! written out, exactly as if somebody had typed them.
|
|
14 |
+ |
//!
|
|
15 |
+ |
//! # What may live in a content file, and what may not
|
|
16 |
+ |
//!
|
|
17 |
+ |
//! Copy with no holes. The split is quasicoherent `98fbee62`'s: pure values
|
|
18 |
+ |
//! with no reference to code go in the file, structure with holes stays in the
|
|
19 |
+ |
//! macro, and computation stays a supplier function. So a value is substituted
|
|
20 |
+ |
//! as text and its braces are doubled on the way in, which is the escape the
|
|
21 |
+ |
//! ordinary string production already uses. A content file that wants a hole
|
|
22 |
+ |
//! has stopped being copy and is structure; move it back into the declaration
|
|
23 |
+ |
//! rather than growing the format.
|
|
24 |
+ |
//!
|
|
25 |
+ |
//! Values are strings and nothing else. A number or a bool in a content file
|
|
26 |
+ |
//! is refused naming the field, because a screen that reads copy is reading
|
|
27 |
+ |
//! words, and the moment the format grows types it is a description language
|
|
28 |
+ |
//! with two spellings.
|
|
29 |
+ |
|
|
30 |
+ |
use std::cell::RefCell;
|
|
31 |
+ |
use std::collections::BTreeMap;
|
|
32 |
+ |
use std::path::PathBuf;
|
|
33 |
+ |
|
|
34 |
+ |
use proc_macro2::{Group, Ident, Literal, TokenStream, TokenTree};
|
|
35 |
+ |
use syn::parse::{Parse, ParseStream};
|
|
36 |
+ |
use syn::{LitStr, Result, Token, braced};
|
|
37 |
+ |
|
|
38 |
+ |
use crate::ast::Item;
|
|
39 |
+ |
|
|
40 |
+ |
// The files this expansion read, absolute, in the order they were read.
|
|
41 |
+ |
//
|
|
42 |
+ |
// A thread-local because the reading happens inside a `Parse` impl, whose
|
|
43 |
+ |
// signature has nowhere to thread a collector through, and one macro expansion
|
|
44 |
+ |
// is one pass on one thread. `crate::declare` drains it directly after parsing
|
|
45 |
+ |
// and turns each path into an `include_bytes!`, which is what makes rustc
|
|
46 |
+ |
// rebuild the crate when the copy changes. Without that marker a content edit
|
|
47 |
+ |
// is invisible: the file is read by the macro and named nowhere the compiler
|
|
48 |
+ |
// looks.
|
|
49 |
+ |
thread_local! {
|
|
50 |
+ |
static READ: RefCell<Vec<PathBuf>> = const { RefCell::new(Vec::new()) };
|
|
51 |
+ |
}
|
|
52 |
+ |
|
|
53 |
+ |
/// The files read since the last drain.
|
|
54 |
+ |
pub fn taken() -> Vec<PathBuf> {
|
|
55 |
+ |
READ.with(|read| std::mem::take(&mut *read.borrow_mut()))
|
|
56 |
+ |
}
|
|
57 |
+ |
|
|
58 |
+ |
/// Whether the item ahead is a copy loop rather than an ordinary one.
|
|
59 |
+ |
///
|
|
60 |
+ |
/// Told apart by the word after `in`, which is the same rule the rest of the
|
|
61 |
+ |
/// grammar uses: `copy` is not a hole, because a hole is lowercase and bound,
|
|
62 |
+ |
/// and no binding can be named `copy` without this refusing it here.
|
|
63 |
+ |
pub fn ahead(input: ParseStream) -> bool {
|
|
64 |
+ |
let fork = input.fork();
|
|
65 |
+ |
if fork.parse::<Token![for]>().is_err() {
|
|
66 |
+ |
return false;
|
|
67 |
+ |
}
|
|
68 |
+ |
if fork.peek(Token![&]) && fork.parse::<Token![&]>().is_err() {
|
|
69 |
+ |
return false;
|
|
70 |
+ |
}
|
|
71 |
+ |
if fork.parse::<Ident>().is_err() || fork.parse::<Token![in]>().is_err() {
|
|
72 |
+ |
return false;
|
|
73 |
+ |
}
|
|
74 |
+ |
fork.parse::<Ident>().is_ok_and(|word| word == "copy")
|
|
75 |
+ |
}
|
|
76 |
+ |
|
|
77 |
+ |
/// Read the file and write the body out once per entry.
|
|
78 |
+ |
pub fn expand(input: ParseStream) -> Result<Vec<Item>> {
|
|
79 |
+ |
input.parse::<Token![for]>()?;
|
|
80 |
+ |
if input.peek(Token![&]) {
|
|
81 |
+ |
let ampersand = input.parse::<Token![&]>()?;
|
|
82 |
+ |
return Err(syn::Error::new(
|
|
83 |
+ |
ampersand.span,
|
|
84 |
+ |
"a copy loop binds text and not a reference: drop the `&`",
|
|
85 |
+ |
));
|
|
86 |
+ |
}
|
|
87 |
+ |
let binder: Ident = input.parse()?;
|
|
88 |
+ |
input.parse::<Token![in]>()?;
|
|
89 |
+ |
input.parse::<Ident>()?;
|
|
90 |
+ |
let file: LitStr = input.parse()?;
|
|
91 |
+ |
input.parse::<Token![as]>()?;
|
|
92 |
+ |
let key: Ident = input.parse()?;
|
|
93 |
+ |
|
|
94 |
+ |
let body;
|
|
95 |
+ |
braced!(body in input);
|
|
96 |
+ |
let template: TokenStream = body.parse()?;
|
|
97 |
+ |
|
|
98 |
+ |
let mut items = Vec::new();
|
|
99 |
+ |
for entry in read(&file, &key)? {
|
|
100 |
+ |
let filled = substitute(template.clone(), &binder, &entry, &file)?;
|
|
101 |
+ |
items.extend(syn::parse2::<Body>(filled)?.0);
|
|
102 |
+ |
}
|
|
103 |
+ |
Ok(items)
|
|
104 |
+ |
}
|
|
105 |
+ |
|
|
106 |
+ |
/// A body's items, so a filled template can be parsed as what it became.
|
|
107 |
+ |
struct Body(Vec<Item>);
|
|
108 |
+ |
|
|
109 |
+ |
impl Parse for Body {
|
|
110 |
+ |
fn parse(input: ParseStream) -> Result<Self> {
|
|
111 |
+ |
Ok(Self(crate::parse::items(input)?))
|
|
112 |
+ |
}
|
|
113 |
+ |
}
|
|
114 |
+ |
|
|
115 |
+ |
/// One entry's fields, by name.
|
|
116 |
+ |
type Entry = BTreeMap<String, String>;
|
|
117 |
+ |
|
|
118 |
+ |
/// The entries under `key`, read from the file the declaration named.
|
|
119 |
+ |
///
|
|
120 |
+ |
/// The path is relative to the manifest of the crate being compiled, which is
|
|
121 |
+ |
/// the only root a macro can resolve against that does not depend on which file
|
|
122 |
+ |
/// the invocation sits in.
|
|
123 |
+ |
fn read(file: &LitStr, key: &Ident) -> Result<Vec<Entry>> {
|
|
124 |
+ |
let root = std::env::var("CARGO_MANIFEST_DIR").map_err(|_| {
|
|
125 |
+ |
syn::Error::new(
|
|
126 |
+ |
file.span(),
|
|
127 |
+ |
"`copy` needs CARGO_MANIFEST_DIR, which cargo sets and a bare rustc does not",
|
|
128 |
+ |
)
|
|
129 |
+ |
})?;
|
|
130 |
+ |
let path = PathBuf::from(root).join(file.value());
|
|
131 |
+ |
let text = std::fs::read_to_string(&path).map_err(|error| {
|
|
132 |
+ |
syn::Error::new(
|
|
133 |
+ |
file.span(),
|
|
134 |
+ |
format!("cannot read {}: {error}", path.display()),
|
|
135 |
+ |
)
|
|
136 |
+ |
})?;
|
|
137 |
+ |
READ.with(|read| read.borrow_mut().push(path.clone()));
|
|
138 |
+ |
|
|
139 |
+ |
let table: toml::Table = text.parse().map_err(|error| {
|
|
140 |
+ |
syn::Error::new(
|
|
141 |
+ |
file.span(),
|
|
142 |
+ |
format!("{} is not TOML: {error}", path.display()),
|
|
143 |
+ |
)
|
|
144 |
+ |
})?;
|
|
145 |
+ |
let named = format!("`{key}` in {}", path.display());
|
|
146 |
+ |
let Some(value) = table.get(&key.to_string()) else {
|
|
147 |
+ |
return Err(syn::Error::new(key.span(), format!("no {named}")));
|
|
148 |
+ |
};
|
|
149 |
+ |
let Some(array) = value.as_array() else {
|
|
150 |
+ |
return Err(syn::Error::new(
|
|
151 |
+ |
key.span(),
|
|
152 |
+ |
format!("{named} is not an array of tables"),
|
|
153 |
+ |
));
|
|
154 |
+ |
};
|
|
155 |
+ |
|
|
156 |
+ |
let mut entries = Vec::new();
|
|
157 |
+ |
for (index, element) in array.iter().enumerate() {
|
|
158 |
+ |
let Some(fields) = element.as_table() else {
|
|
159 |
+ |
return Err(syn::Error::new(
|
|
160 |
+ |
key.span(),
|
|
161 |
+ |
format!("{named} entry {index} is not a table"),
|
|
162 |
+ |
));
|
|
163 |
+ |
};
|
|
164 |
+ |
let mut entry = Entry::new();
|
|
165 |
+ |
for (name, field) in fields {
|
|
166 |
+ |
let Some(text) = field.as_str() else {
|
|
167 |
+ |
return Err(syn::Error::new(
|
|
168 |
+ |
key.span(),
|
|
169 |
+ |
format!("{named} entry {index} field `{name}` is not a string: copy is words"),
|
|
170 |
+ |
));
|
|
171 |
+ |
};
|
|
172 |
+ |
entry.insert(name.clone(), text.to_owned());
|
|
173 |
+ |
}
|
|
174 |
+ |
entries.push(entry);
|
|
175 |
+ |
}
|
|
176 |
+ |
Ok(entries)
|
|
177 |
+ |
}
|
|
178 |
+ |
|
|
179 |
+ |
/// The template with every `binder.field` replaced by the text it names.
|
|
180 |
+ |
///
|
|
181 |
+ |
/// A token walk rather than a walk of the syntax tree, because the tree is not
|
|
182 |
+ |
/// built yet and building it first would mean a second traversal of every
|
|
183 |
+ |
/// production in the grammar, drifting from the first the way a second emitter
|
|
184 |
+ |
/// would. `binder.field` is three tokens in every position the grammar admits,
|
|
185 |
+ |
/// which is what makes the walk exact rather than approximate.
|
|
186 |
+ |
fn substitute(
|
|
187 |
+ |
template: TokenStream,
|
|
188 |
+ |
binder: &Ident,
|
|
189 |
+ |
entry: &Entry,
|
|
190 |
+ |
file: &LitStr,
|
|
191 |
+ |
) -> Result<TokenStream> {
|
|
192 |
+ |
let mut filled = Vec::new();
|
|
193 |
+ |
let mut tokens = template.into_iter().peekable();
|
|
194 |
+ |
|
|
195 |
+ |
while let Some(token) = tokens.next() {
|
|
196 |
+ |
match token {
|
|
197 |
+ |
TokenTree::Group(group) => {
|
|
198 |
+ |
let inner = substitute(group.stream(), binder, entry, file)?;
|
|
199 |
+ |
let mut replacement = Group::new(group.delimiter(), inner);
|
|
200 |
+ |
replacement.set_span(group.span());
|
|
201 |
+ |
filled.push(TokenTree::Group(replacement));
|
|
202 |
+ |
}
|
|
203 |
+ |
TokenTree::Ident(ident) if ident == *binder => {
|
|
204 |
+ |
let field = field_after(&mut tokens, &ident)?;
|
|
205 |
+ |
let Some(text) = entry.get(&field.to_string()) else {
|
|
206 |
+ |
return Err(syn::Error::new(
|
|
207 |
+ |
field.span(),
|
|
208 |
+ |
format!("{} has no `{field}` in {}", binder, file.value()),
|
|
209 |
+ |
));
|
|
210 |
+ |
};
|
|
211 |
+ |
let mut literal = Literal::string(&escaped(text));
|
|
212 |
+ |
literal.set_span(ident.span());
|
|
213 |
+ |
filled.push(TokenTree::Literal(literal));
|
|
214 |
+ |
}
|
|
215 |
+ |
other => filled.push(other),
|
|
216 |
+ |
}
|
|
217 |
+ |
}
|
|
218 |
+ |
Ok(filled.into_iter().collect())
|
|
219 |
+ |
}
|
|
220 |
+ |
|
|
221 |
+ |
/// The field named after the binder, or an error saying the binder is text.
|
|
222 |
+ |
fn field_after(
|
|
223 |
+ |
tokens: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
|
|
224 |
+ |
binder: &Ident,
|
|
225 |
+ |
) -> Result<Ident> {
|
|
226 |
+ |
let lone = || {
|
|
227 |
+ |
syn::Error::new(
|
|
228 |
+ |
binder.span(),
|
|
229 |
+ |
format!("`{binder}` is one entry's copy: name a field, as `{binder}.heading`"),
|
|
230 |
+ |
)
|
|
231 |
+ |
};
|
|
232 |
+ |
match tokens.next() {
|
|
233 |
+ |
Some(TokenTree::Punct(punct)) if punct.as_char() == '.' => {}
|
|
234 |
+ |
_ => return Err(lone()),
|
|
235 |
+ |
}
|
|
236 |
+ |
match tokens.next() {
|
|
237 |
+ |
Some(TokenTree::Ident(field)) => Ok(field),
|
|
238 |
+ |
_ => Err(lone()),
|
|
239 |
+ |
}
|
|
240 |
+ |
}
|
|
241 |
+ |
|
|
242 |
+ |
/// Copy with its braces doubled, so the string production reads it as text.
|
|
243 |
+ |
///
|
|
244 |
+ |
/// A brace in copy is a brace somebody typed. A hole in copy is structure that
|
|
245 |
+ |
/// wandered into the wrong file, and doubling is what makes the difference
|
|
246 |
+ |
/// impossible to reach by accident rather than a thing to remember.
|
|
247 |
+ |
fn escaped(text: &str) -> String {
|
|
248 |
+ |
text.replace('{', "{{").replace('}', "}}")
|
|
249 |
+ |
}
|