| 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 |
use proc_macro2::Span; |
| 41 |
use quote::format_ident; |
| 42 |
use syn::{Ident, Result}; |
| 43 |
|
| 44 |
use crate::ast::{ |
| 45 |
Arg, Declaration, Emission, Guard, Hole, HoleRoot, Interpolated, Item, Param, Predicate, |
| 46 |
RegionKind, Source, StrPart, |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
pub const FLAG: &str = "staged"; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
pub const CONSTANT: &str = "constant"; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub fn constant_name(name: &Ident) -> Ident { |
| 74 |
format_ident!("{}_constant", name, span = name.span()) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
fn fixed(arg: &Arg) -> bool { |
| 84 |
match arg { |
| 85 |
Arg::Str(text) => text |
| 86 |
.parts |
| 87 |
.iter() |
| 88 |
.all(|part| matches!(part, StrPart::Lit(_))), |
| 89 |
Arg::Int(_) | Arg::Bool(_) => true, |
| 90 |
Arg::List(items) => items.iter().all(fixed), |
| 91 |
Arg::Hole(_) | Arg::Borrow(_) => false, |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
pub fn constant(declaration: &Declaration) -> bool { |
| 97 |
declaration.flags.iter().any(|flag| flag == CONSTANT) |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
pub fn promise(declaration: &Declaration) -> proc_macro2::TokenStream { |
| 102 |
if !constant(declaration) { |
| 103 |
return proc_macro2::TokenStream::new(); |
| 104 |
} |
| 105 |
let name = &declaration.name; |
| 106 |
let shim = constant_name(name); |
| 107 |
let vis = declaration |
| 108 |
.vis |
| 109 |
.clone() |
| 110 |
.unwrap_or(syn::Visibility::Inherited); |
| 111 |
let params: Vec<_> = declaration |
| 112 |
.params |
| 113 |
.iter() |
| 114 |
.map(|param| { |
| 115 |
let name = ¶m.name; |
| 116 |
let ty = ¶m.ty; |
| 117 |
quote::quote!(#name: #ty) |
| 118 |
}) |
| 119 |
.collect(); |
| 120 |
let arguments = declaration.params.iter().map(|param| ¶m.name); |
| 121 |
let Ok(returns) = crate::emit::returns_type(&declaration.returns) else { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
return proc_macro2::TokenStream::new(); |
| 126 |
}; |
| 127 |
let doc = format!( |
| 128 |
" [`{name}`], and the promise that it answers the same thing every time.\n\n Emitted by `#[constant]`. A staged `include` whose arguments are all\n literals calls this rather than opening a scope, so what this returns is\n built once while a residual is derived instead of once per request. A\n shape that made no such promise has no function here, and the call site\n fails to resolve rather than quietly baking one evaluation in forever." |
| 129 |
); |
| 130 |
quote::quote! { |
| 131 |
#[doc = #doc] |
| 132 |
#[allow(dead_code)] |
| 133 |
#vis fn #shim(#(#params),*) -> #returns { |
| 134 |
#name(#(#arguments),*) |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
pub(crate) const PLAN: &str = "plan"; |
| 141 |
|
| 142 |
|
| 143 |
pub fn staged_name(name: &Ident) -> Ident { |
| 144 |
format_ident!("{}_staged", name, span = name.span()) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
fn counts_name(name: &Ident) -> Ident { |
| 149 |
format_ident!( |
| 150 |
"{}_STAGED", |
| 151 |
name.to_string().to_uppercase(), |
| 152 |
span = name.span() |
| 153 |
) |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
pub fn wanted(declaration: &Declaration) -> bool { |
| 158 |
declaration.flags.iter().any(|flag| flag == FLAG) |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
pub enum Fill { |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
Hole { id: u16, hole: Hole }, |
| 173 |
|
| 174 |
Branch { guard: Guard, body: Vec<Fill> }, |
| 175 |
|
| 176 |
Repeat { |
| 177 |
dereferenced: bool, |
| 178 |
binder: Ident, |
| 179 |
iterable: Hole, |
| 180 |
body: Vec<Fill>, |
| 181 |
}, |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
Swap { guard: Guard }, |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
Arms { |
| 204 |
scrutinee: Hole, |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
patterns: Vec<crate::ast::Pattern>, |
| 209 |
}, |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
Include { |
| 217 |
callee: syn::Path, |
| 218 |
args: Vec<Arg>, |
| 219 |
site: u16, |
| 220 |
}, |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
#[derive(Default)] |
| 230 |
struct Counters { |
| 231 |
guards: u16, |
| 232 |
loops: u16, |
| 233 |
holes: u16, |
| 234 |
|
| 235 |
sites: u16, |
| 236 |
|
| 237 |
arms: u16, |
| 238 |
|
| 239 |
stack: Vec<Vec<Fill>>, |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
swapping: Option<u16>, |
| 247 |
} |
| 248 |
|
| 249 |
impl Counters { |
| 250 |
|
| 251 |
fn enter(&mut self) { |
| 252 |
self.stack.push(Vec::new()); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
fn exit(&mut self) -> Vec<Fill> { |
| 257 |
self.stack.pop().unwrap_or_default() |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
fn wrote(&mut self, fill: Fill) { |
| 262 |
if let Some(body) = self.stack.last_mut() { |
| 263 |
body.push(fill); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
fn plan_read(method: &str, id: u16) -> Hole { |
| 270 |
Hole { |
| 271 |
root: HoleRoot::Binding(Ident::new(PLAN, Span::call_site())), |
| 272 |
steps: vec![crate::ast::Step::Method { |
| 273 |
name: Ident::new(method, Span::call_site()), |
| 274 |
args: vec![Arg::Int(i64::from(id))], |
| 275 |
}], |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
fn sentinel(id: u16) -> Hole { |
| 286 |
plan_read("hole", id) |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
fn plan_arm(id: u16, count: usize) -> Hole { |
| 294 |
Hole { |
| 295 |
root: HoleRoot::Binding(Ident::new(PLAN, Span::call_site())), |
| 296 |
steps: vec![crate::ast::Step::Method { |
| 297 |
name: Ident::new("arm", Span::call_site()), |
| 298 |
args: vec![Arg::Int(i64::from(id)), Arg::Int(count as i64)], |
| 299 |
}], |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
fn counted(id: u16) -> Hole { |
| 309 |
plan_read("number", id) |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
pub struct Staged { |
| 314 |
pub declaration: Declaration, |
| 315 |
|
| 316 |
pub fill: Vec<Fill>, |
| 317 |
pub counts: (u16, u16, u16, u16), |
| 318 |
pub counts_ident: Ident, |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
pub fn stage(declaration: &Declaration) -> Result<Staged> { |
| 323 |
let mut counters = Counters::default(); |
| 324 |
counters.enter(); |
| 325 |
let items = items(&declaration.items, &mut counters)?; |
| 326 |
let fill = counters.exit(); |
| 327 |
|
| 328 |
let name = staged_name(&declaration.name); |
| 329 |
let doc = format!( |
| 330 |
" The staged twin of [`{}`], evaluated with no request.", |
| 331 |
declaration.name |
| 332 |
); |
| 333 |
|
| 334 |
Ok(Staged { |
| 335 |
fill, |
| 336 |
counts: ( |
| 337 |
counters.guards, |
| 338 |
counters.loops, |
| 339 |
counters.holes, |
| 340 |
counters.sites, |
| 341 |
), |
| 342 |
counts_ident: counts_name(&declaration.name), |
| 343 |
declaration: Declaration { |
| 344 |
docs: vec![doc], |
| 345 |
|
| 346 |
|
| 347 |
flags: declaration |
| 348 |
.flags |
| 349 |
.iter() |
| 350 |
.filter(|flag| *flag != FLAG) |
| 351 |
.cloned() |
| 352 |
.collect(), |
| 353 |
vis: declaration.vis.clone(), |
| 354 |
name, |
| 355 |
params: vec![Param { |
| 356 |
name: Ident::new(PLAN, Span::call_site()), |
| 357 |
ty: syn::parse_quote!(&::quasi_router::stage::Plan), |
| 358 |
}], |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
returns: listed(&declaration.returns), |
| 364 |
items, |
| 365 |
}, |
| 366 |
}) |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
fn listed(returns: &syn::Type) -> syn::Type { |
| 375 |
let syn::Type::Path(path) = returns else { |
| 376 |
return returns.clone(); |
| 377 |
}; |
| 378 |
match path.path.segments.last() { |
| 379 |
Some(last) if last.ident == "Vec" => { |
| 380 |
syn::parse_quote!(::quasi_router::stage::Staged<#returns>) |
| 381 |
} |
| 382 |
_ => returns.clone(), |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
fn items(items: &[Item], counters: &mut Counters) -> Result<Vec<Item>> { |
| 387 |
items |
| 388 |
.iter() |
| 389 |
.map(|item| self::item(item, counters)) |
| 390 |
.collect() |
| 391 |
} |
| 392 |
|
| 393 |
fn item(item: &Item, counters: &mut Counters) -> Result<Item> { |
| 394 |
Ok(match item { |
| 395 |
Item::Bind { name, source } => Item::Bind { |
| 396 |
name: name.clone(), |
| 397 |
source: self::source(source, counters)?, |
| 398 |
}, |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
Item::Marked { .. } => { |
| 403 |
return Err(syn::Error::new( |
| 404 |
Span::call_site(), |
| 405 |
"a staged twin cannot be staged again", |
| 406 |
)); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
Item::Attribute { |
| 411 |
name, |
| 412 |
args, |
| 413 |
guard, |
| 414 |
body, |
| 415 |
} => { |
| 416 |
if let Some(guard) = guard { |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
if !placing(name) |
| 421 |
&& let Some(site) = counters.swapping |
| 422 |
{ |
| 423 |
return Ok(Item::Attribute { |
| 424 |
name: name.clone(), |
| 425 |
args: staged_args(name, args, counters)?, |
| 426 |
guard: Some(Guard { |
| 427 |
negated: false, |
| 428 |
predicate: Predicate::Truth(plan_read("swap", site)), |
| 429 |
span: guard.span, |
| 430 |
}), |
| 431 |
body: items(body, counters)?, |
| 432 |
}); |
| 433 |
} |
| 434 |
if !placing(name) { |
| 435 |
return Err(syn::Error::new( |
| 436 |
guard.span, |
| 437 |
"a staged shape cannot guard a settling setting: it varies \ |
| 438 |
markup INSIDE the member its own statement produces, and \ |
| 439 |
a mark covers a run of members. The answer is two arms \ |
| 440 |
over that member -- built with the setting and without it \ |
| 441 |
-- which is the shape `given` already compiles to", |
| 442 |
)); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
let site = counters.guards; |
| 449 |
counters.guards += 1; |
| 450 |
counters.enter(); |
| 451 |
let args = staged_args(name, args, counters)?; |
| 452 |
|
| 453 |
|
| 454 |
let inner = items(body, counters)?; |
| 455 |
let recorded = counters.exit(); |
| 456 |
counters.wrote(Fill::Branch { |
| 457 |
guard: (*guard).clone(), |
| 458 |
body: recorded, |
| 459 |
}); |
| 460 |
return Ok(Item::Marked { |
| 461 |
site, |
| 462 |
varies: crate::ast::Varies::Absent, |
| 463 |
body: vec![Item::Attribute { |
| 464 |
name: name.clone(), |
| 465 |
args, |
| 466 |
guard: None, |
| 467 |
body: inner, |
| 468 |
}], |
| 469 |
}); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
let settling: Vec<&Guard> = body |
| 477 |
.iter() |
| 478 |
.filter_map(|item| match item { |
| 479 |
Item::Attribute { |
| 480 |
guard: Some(guard), |
| 481 |
name, |
| 482 |
.. |
| 483 |
} if !placing(name) => Some(guard), |
| 484 |
_ => None, |
| 485 |
}) |
| 486 |
.collect(); |
| 487 |
if let Some(guard) = settling.first() { |
| 488 |
if settling.len() > 1 { |
| 489 |
return Err(syn::Error::new( |
| 490 |
guard.span, |
| 491 |
"two settling settings on one member would be four arms of \ |
| 492 |
it, and nothing has wanted that: say one of them another \ |
| 493 |
way, or teach the twin the combinations", |
| 494 |
)); |
| 495 |
} |
| 496 |
let site = counters.arms; |
| 497 |
counters.arms += 1; |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
let args = staged_args(name, args, counters)?; |
| 505 |
let inner = swapped(body, site, counters)?; |
| 506 |
counters.wrote(Fill::Swap { |
| 507 |
guard: (*guard).clone(), |
| 508 |
}); |
| 509 |
|
| 510 |
return Ok(Item::Marked { |
| 511 |
site, |
| 512 |
varies: crate::ast::Varies::Arm { |
| 513 |
of: 2, |
| 514 |
taken: plan_arm(site, 2), |
| 515 |
}, |
| 516 |
body: vec![Item::Attribute { |
| 517 |
name: name.clone(), |
| 518 |
args, |
| 519 |
guard: None, |
| 520 |
body: inner, |
| 521 |
}], |
| 522 |
}); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
Item::Attribute { |
| 529 |
name: name.clone(), |
| 530 |
args: staged_args(name, args, counters)?, |
| 531 |
guard: None, |
| 532 |
body: items(body, counters)?, |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
Item::For { |
| 540 |
binder, |
| 541 |
body, |
| 542 |
dereferenced, |
| 543 |
iterable, |
| 544 |
} => { |
| 545 |
let id = counters.loops; |
| 546 |
counters.loops += 1; |
| 547 |
counters.enter(); |
| 548 |
let staged = items(body, counters)?; |
| 549 |
let inner = counters.exit(); |
| 550 |
counters.wrote(Fill::Repeat { |
| 551 |
dereferenced: *dereferenced, |
| 552 |
binder: binder.clone(), |
| 553 |
iterable: iterable.clone(), |
| 554 |
body: inner, |
| 555 |
}); |
| 556 |
Item::Marked { |
| 557 |
site: id, |
| 558 |
varies: crate::ast::Varies::Repeated, |
| 559 |
body: vec![Item::For { |
| 560 |
dereferenced: false, |
| 561 |
binder: format_ident!("_{}", binder, span = binder.span()), |
| 562 |
iterable: plan_read("rows", id), |
| 563 |
body: staged, |
| 564 |
}], |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
Item::Emit(Emission::Guarded { guard, inner }) => { |
| 574 |
let site = counters.guards; |
| 575 |
counters.guards += 1; |
| 576 |
counters.enter(); |
| 577 |
let inner = self::emission(inner, counters)?; |
| 578 |
let body = counters.exit(); |
| 579 |
counters.wrote(Fill::Branch { |
| 580 |
guard: (*guard).clone(), |
| 581 |
body, |
| 582 |
}); |
| 583 |
Item::Marked { |
| 584 |
site, |
| 585 |
varies: crate::ast::Varies::Absent, |
| 586 |
body: vec![Item::Emit(inner)], |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
Item::Emit(Emission::Given { |
| 594 |
scrutinee, |
| 595 |
arms, |
| 596 |
otherwise, |
| 597 |
}) => { |
| 598 |
let Some(otherwise) = otherwise else { |
| 599 |
return Err(syn::Error::new( |
| 600 |
scrutinee.span(), |
| 601 |
"a staged dispatch says what it does when nothing matches: \ |
| 602 |
a residual holds one arm per position and there has to be \ |
| 603 |
one to hold", |
| 604 |
)); |
| 605 |
}; |
| 606 |
let site = counters.arms; |
| 607 |
counters.arms += 1; |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
let staged = arms |
| 613 |
.iter() |
| 614 |
.map(|(pattern, arm)| { |
| 615 |
Ok((pattern.clone(), Box::new(self::emission(arm, counters)?))) |
| 616 |
}) |
| 617 |
.collect::<Result<Vec<_>>>()?; |
| 618 |
let last = Box::new(self::emission(otherwise, counters)?); |
| 619 |
|
| 620 |
counters.wrote(Fill::Arms { |
| 621 |
scrutinee: (*scrutinee).clone(), |
| 622 |
patterns: arms.iter().map(|(pattern, _)| pattern.clone()).collect(), |
| 623 |
}); |
| 624 |
|
| 625 |
let of = staged.len() + 1; |
| 626 |
let taken = plan_arm(site, of); |
| 627 |
Item::Marked { |
| 628 |
site, |
| 629 |
varies: crate::ast::Varies::Arm { |
| 630 |
of, |
| 631 |
taken: taken.clone(), |
| 632 |
}, |
| 633 |
body: vec![Item::Emit(Emission::Given { |
| 634 |
scrutinee: taken, |
| 635 |
arms: staged |
| 636 |
.into_iter() |
| 637 |
.enumerate() |
| 638 |
.map(|(at, (_, arm))| (crate::ast::Pattern::Int(at as i64), arm)) |
| 639 |
.collect(), |
| 640 |
otherwise: Some(last), |
| 641 |
})], |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
Item::Emit(emission) if settling_guard(emission).is_some() => { |
| 649 |
let guard = settling_guard(emission).expect("just asked"); |
| 650 |
let site = counters.arms; |
| 651 |
counters.arms += 1; |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
let held = counters.swapping.replace(site); |
| 656 |
let staged = self::emission(emission, counters)?; |
| 657 |
counters.swapping = held; |
| 658 |
counters.wrote(Fill::Swap { |
| 659 |
guard: guard.clone(), |
| 660 |
}); |
| 661 |
|
| 662 |
Item::Marked { |
| 663 |
site, |
| 664 |
varies: crate::ast::Varies::Arm { |
| 665 |
of: 2, |
| 666 |
taken: plan_arm(site, 2), |
| 667 |
}, |
| 668 |
body: vec![Item::Emit(staged)], |
| 669 |
} |
| 670 |
} |
| 671 |
Item::Emit(emission) => Item::Emit(self::emission(emission, counters)?), |
| 672 |
}) |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
fn settling_guard(emission: &Emission) -> Option<&Guard> { |
| 685 |
body_of(emission)?.iter().find_map(|item| match item { |
| 686 |
Item::Attribute { |
| 687 |
guard: Some(guard), |
| 688 |
name, |
| 689 |
.. |
| 690 |
} if !placing(name) => Some(guard), |
| 691 |
_ => None, |
| 692 |
}) |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
fn body_of(emission: &Emission) -> Option<&[Item]> { |
| 701 |
Some(match emission { |
| 702 |
Emission::Simple { body, .. } |
| 703 |
| Emission::Chip { body, .. } |
| 704 |
| Emission::Screen { body, .. } |
| 705 |
| Emission::Row { body, .. } |
| 706 |
| Emission::Form { body, .. } |
| 707 |
| Emission::Field { body, .. } |
| 708 |
| Emission::Act { body, .. } |
| 709 |
| Emission::Offers { body, .. } |
| 710 |
| Emission::Column { body, .. } |
| 711 |
| Emission::Cell { body, .. } |
| 712 |
| Emission::Offering { body, .. } |
| 713 |
| Emission::Removes { body, .. } |
| 714 |
| Emission::Repeats { body, .. } |
| 715 |
| Emission::Region { body, .. } |
| 716 |
| Emission::Across { body, .. } => body, |
| 717 |
Emission::List(body) | Emission::Table(body) | Emission::Cells(body) => body, |
| 718 |
Emission::Framed { inner, .. } |
| 719 |
| Emission::Beside { inner, .. } |
| 720 |
| Emission::At { inner, .. } => { |
| 721 |
return body_of(inner); |
| 722 |
} |
| 723 |
Emission::Guarded { .. } |
| 724 |
| Emission::Given { .. } |
| 725 |
| Emission::Activate(_) |
| 726 |
| Emission::Include(_) |
| 727 |
| Emission::IncludeEach(_) |
| 728 |
| Emission::Link { .. } => return None, |
| 729 |
}) |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
fn swapped(body: &[Item], site: u16, counters: &mut Counters) -> Result<Vec<Item>> { |
| 740 |
body.iter() |
| 741 |
.map(|item| match item { |
| 742 |
Item::Attribute { |
| 743 |
name, |
| 744 |
args, |
| 745 |
guard: Some(_), |
| 746 |
body, |
| 747 |
} if !placing(name) => Ok(Item::Attribute { |
| 748 |
name: name.clone(), |
| 749 |
args: staged_args(name, args, counters)?, |
| 750 |
guard: Some(Guard { |
| 751 |
negated: false, |
| 752 |
predicate: Predicate::Truth(plan_read("swap", site)), |
| 753 |
span: Span::call_site(), |
| 754 |
}), |
| 755 |
body: items(body, counters)?, |
| 756 |
}), |
| 757 |
other => self::item(other, counters), |
| 758 |
}) |
| 759 |
.collect() |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
fn guard(guard: &Guard, counters: &mut Counters) -> Guard { |
| 770 |
let id = counters.guards; |
| 771 |
counters.guards += 1; |
| 772 |
Guard { |
| 773 |
negated: false, |
| 774 |
predicate: Predicate::Truth(plan_read("guard", id)), |
| 775 |
span: guard.span, |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
fn source(source: &Source, counters: &mut Counters) -> Result<Source> { |
| 780 |
Ok(match source { |
| 781 |
Source::Str(text) => Source::Str(interpolated(text, counters)), |
| 782 |
Source::Hole(hole) => Source::Hole(value(hole, counters)), |
| 783 |
|
| 784 |
|
| 785 |
Source::Choose { scrutinee, .. } => { |
| 786 |
return Err(syn::Error::new( |
| 787 |
scrutinee.span(), |
| 788 |
"a staged shape cannot dispatch: only one arm reaches the \ |
| 789 |
residual, so the others are lost rather than compiled. Say \ |
| 790 |
the arms as guards -- `A when x; B unless x` -- which renders \ |
| 791 |
the same and derives a branch for each", |
| 792 |
)); |
| 793 |
} |
| 794 |
}) |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
fn value(hole: &Hole, counters: &mut Counters) -> Hole { |
| 804 |
stand_in(hole, counters, false) |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
fn number(hole: &Hole, counters: &mut Counters) -> Hole { |
| 809 |
stand_in(hole, counters, true) |
| 810 |
} |
| 811 |
|
| 812 |
fn stand_in(hole: &Hole, counters: &mut Counters, counting: bool) -> Hole { |
| 813 |
if matches!(hole.root, HoleRoot::Path(_)) && hole.steps.is_empty() { |
| 814 |
return Hole { |
| 815 |
root: match &hole.root { |
| 816 |
HoleRoot::Path(path) => HoleRoot::Path(path.clone()), |
| 817 |
_ => unreachable!("checked above"), |
| 818 |
}, |
| 819 |
steps: Vec::new(), |
| 820 |
}; |
| 821 |
} |
| 822 |
let id = counters.holes; |
| 823 |
counters.holes += 1; |
| 824 |
counters.wrote(Fill::Hole { |
| 825 |
id, |
| 826 |
hole: hole.clone(), |
| 827 |
}); |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
if counting { counted(id) } else { sentinel(id) } |
| 832 |
} |
| 833 |
|
| 834 |
fn interpolated(text: &Interpolated, counters: &mut Counters) -> Interpolated { |
| 835 |
Interpolated { |
| 836 |
parts: text |
| 837 |
.parts |
| 838 |
.iter() |
| 839 |
.map(|part| match part { |
| 840 |
StrPart::Lit(literal) => StrPart::Lit(literal.clone()), |
| 841 |
StrPart::Hole(hole) => StrPart::Hole(value(hole, counters)), |
| 842 |
}) |
| 843 |
.collect(), |
| 844 |
span: text.span, |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
const STRUCTURED: &[&str] = &[ |
| 870 |
"token", "meter", "stats", "option", "figure", "more", "back", "forward", "jumping", "chart", |
| 871 |
"bar", |
| 872 |
]; |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
const PLACING: &[&str] = &[ |
| 895 |
"figure", "bar", "more", "back", "forward", "jumping", "option", |
| 896 |
]; |
| 897 |
|
| 898 |
|
| 899 |
fn placing(name: &Ident) -> bool { |
| 900 |
PLACING.contains(&name.to_string().as_str()) |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
fn staged_args(name: &Ident, args: &[Arg], counters: &mut Counters) -> Result<Vec<Arg>> { |
| 909 |
if structured(name) { |
| 910 |
through(args, counters) |
| 911 |
} else if numbered(name) { |
| 912 |
counting_args(args, counters) |
| 913 |
} else { |
| 914 |
self::args(args, counters) |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
fn structured(name: &Ident) -> bool { |
| 920 |
STRUCTURED.contains(&name.to_string().as_str()) |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
const NUMBERED: &[&str] = &["of"]; |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
const NUMBERED_CALLS: &[&str] = &[ |
| 942 |
"Rest::page", |
| 943 |
"Rest::showing", |
| 944 |
"Rest::more", |
| 945 |
"Jump::new", |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
"Chart::new", |
| 951 |
]; |
| 952 |
|
| 953 |
|
| 954 |
fn numbered(name: &Ident) -> bool { |
| 955 |
NUMBERED.contains(&name.to_string().as_str()) |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
fn numbered_call(path: &syn::Path) -> bool { |
| 960 |
let mut tail = path |
| 961 |
.segments |
| 962 |
.iter() |
| 963 |
.rev() |
| 964 |
.take(2) |
| 965 |
.map(|segment| segment.ident.to_string()) |
| 966 |
.collect::<Vec<_>>(); |
| 967 |
tail.reverse(); |
| 968 |
NUMBERED_CALLS.contains(&tail.join("::").as_str()) |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
fn through(args: &[Arg], counters: &mut Counters) -> Result<Vec<Arg>> { |
| 976 |
args.iter().map(|one| one_through(one, counters)).collect() |
| 977 |
} |
| 978 |
|
| 979 |
fn one_through(arg: &Arg, counters: &mut Counters) -> Result<Arg> { |
| 980 |
Ok(match arg { |
| 981 |
Arg::List(items) => Arg::List(through(items, counters)?), |
| 982 |
Arg::Borrow(inner) => Arg::Borrow(Box::new(one_through(inner, counters)?)), |
| 983 |
Arg::Hole(hole) => match &hole.root { |
| 984 |
HoleRoot::Call { path, args } => Arg::Hole(Hole { |
| 985 |
root: HoleRoot::Call { |
| 986 |
path: path.clone(), |
| 987 |
args: if numbered_call(path) { |
| 988 |
counting_args(args, counters)? |
| 989 |
} else { |
| 990 |
self::args(args, counters)? |
| 991 |
}, |
| 992 |
}, |
| 993 |
|
| 994 |
|
| 995 |
steps: hole |
| 996 |
.steps |
| 997 |
.iter() |
| 998 |
.map(|step| { |
| 999 |
Ok(match step { |
| 1000 |
crate::ast::Step::Field(name) => crate::ast::Step::Field(name.clone()), |
| 1001 |
crate::ast::Step::Method { name, args } => crate::ast::Step::Method { |
| 1002 |
name: name.clone(), |
| 1003 |
args: if numbered(name) { |
| 1004 |
counting_args(args, counters)? |
| 1005 |
} else { |
| 1006 |
self::args(args, counters)? |
| 1007 |
}, |
| 1008 |
}, |
| 1009 |
}) |
| 1010 |
}) |
| 1011 |
.collect::<Result<Vec<_>>>()?, |
| 1012 |
}), |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
_ => Arg::Hole(hole.clone()), |
| 1019 |
}, |
| 1020 |
other => self::arg(other, counters)?, |
| 1021 |
}) |
| 1022 |
} |
| 1023 |
|
| 1024 |
fn args(args: &[Arg], counters: &mut Counters) -> Result<Vec<Arg>> { |
| 1025 |
args.iter().map(|arg| self::arg(arg, counters)).collect() |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
fn counting_args(args: &[Arg], counters: &mut Counters) -> Result<Vec<Arg>> { |
| 1030 |
args.iter() |
| 1031 |
.map(|arg| match arg { |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
Arg::Hole(hole) if matches!(hole.root, HoleRoot::Call { .. }) => { |
| 1036 |
one_through(arg, counters) |
| 1037 |
} |
| 1038 |
Arg::Hole(hole) => Ok(Arg::Hole(number(hole, counters))), |
| 1039 |
other => self::arg(other, counters), |
| 1040 |
}) |
| 1041 |
.collect() |
| 1042 |
} |
| 1043 |
|
| 1044 |
fn arg(arg: &Arg, counters: &mut Counters) -> Result<Arg> { |
| 1045 |
Ok(match arg { |
| 1046 |
Arg::Str(text) => Arg::Str(interpolated(text, counters)), |
| 1047 |
Arg::Hole(hole) => Arg::Hole(value(hole, counters)), |
| 1048 |
Arg::List(items) => Arg::List(args(items, counters)?), |
| 1049 |
Arg::Borrow(inner) => Arg::Borrow(Box::new(self::arg(inner, counters)?)), |
| 1050 |
Arg::Int(value) => Arg::Int(*value), |
| 1051 |
Arg::Bool(value) => Arg::Bool(*value), |
| 1052 |
}) |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
fn action(action: &crate::ast::Action, counters: &mut Counters) -> Result<crate::ast::Action> { |
| 1060 |
Ok(crate::ast::Action { |
| 1061 |
verb: action.verb.clone(), |
| 1062 |
target: action |
| 1063 |
.target |
| 1064 |
.as_ref() |
| 1065 |
.map(|target| arg(target, counters)) |
| 1066 |
.transpose()?, |
| 1067 |
modifiers: action |
| 1068 |
.modifiers |
| 1069 |
.iter() |
| 1070 |
.map(|modifier| { |
| 1071 |
Ok(crate::ast::Modifier { |
| 1072 |
name: modifier.name.clone(), |
| 1073 |
args: args(&modifier.args, counters)?, |
| 1074 |
}) |
| 1075 |
}) |
| 1076 |
.collect::<Result<Vec<_>>>()?, |
| 1077 |
}) |
| 1078 |
} |
| 1079 |
|
| 1080 |
fn emission(emission: &Emission, counters: &mut Counters) -> Result<Emission> { |
| 1081 |
Ok(match emission { |
| 1082 |
Emission::Simple { member, args, body } => Emission::Simple { |
| 1083 |
member: member.clone(), |
| 1084 |
args: if structured(member) { |
| 1085 |
through(args, counters)? |
| 1086 |
} else { |
| 1087 |
self::args(args, counters)? |
| 1088 |
}, |
| 1089 |
body: items(body, counters)?, |
| 1090 |
}, |
| 1091 |
Emission::Chip { |
| 1092 |
value, |
| 1093 |
action, |
| 1094 |
removable, |
| 1095 |
body, |
| 1096 |
} => Emission::Chip { |
| 1097 |
value: arg(value, counters)?, |
| 1098 |
action: self::action(action, counters)?, |
| 1099 |
removable: *removable, |
| 1100 |
body: items(body, counters)?, |
| 1101 |
}, |
| 1102 |
Emission::Screen { |
| 1103 |
arrangement, |
| 1104 |
args, |
| 1105 |
body, |
| 1106 |
} => Emission::Screen { |
| 1107 |
arrangement: arrangement.clone(), |
| 1108 |
args: self::args(args, counters)?, |
| 1109 |
body: items(body, counters)?, |
| 1110 |
}, |
| 1111 |
Emission::Row { primary, body } => Emission::Row { |
| 1112 |
primary: arg(primary, counters)?, |
| 1113 |
body: items(body, counters)?, |
| 1114 |
}, |
| 1115 |
Emission::Form { action, body } => Emission::Form { |
| 1116 |
action: self::action(action, counters)?, |
| 1117 |
body: items(body, counters)?, |
| 1118 |
}, |
| 1119 |
Emission::Field { |
| 1120 |
kind, |
| 1121 |
name, |
| 1122 |
label, |
| 1123 |
body, |
| 1124 |
} => Emission::Field { |
| 1125 |
kind: kind.clone(), |
| 1126 |
name: arg(name, counters)?, |
| 1127 |
label: arg(label, counters)?, |
| 1128 |
body: items(body, counters)?, |
| 1129 |
}, |
| 1130 |
Emission::List(body) => Emission::List(items(body, counters)?), |
| 1131 |
Emission::Act { |
| 1132 |
label, |
| 1133 |
action, |
| 1134 |
body, |
| 1135 |
} => Emission::Act { |
| 1136 |
label: arg(label, counters)?, |
| 1137 |
action: self::action(action, counters)?, |
| 1138 |
body: items(body, counters)?, |
| 1139 |
}, |
| 1140 |
Emission::Offers { |
| 1141 |
label, |
| 1142 |
action, |
| 1143 |
body, |
| 1144 |
} => Emission::Offers { |
| 1145 |
label: arg(label, counters)?, |
| 1146 |
action: self::action(action, counters)?, |
| 1147 |
body: items(body, counters)?, |
| 1148 |
}, |
| 1149 |
Emission::Table(body) => Emission::Table(items(body, counters)?), |
| 1150 |
Emission::Column { name, body } => Emission::Column { |
| 1151 |
name: arg(name, counters)?, |
| 1152 |
body: items(body, counters)?, |
| 1153 |
}, |
| 1154 |
Emission::Cells(body) => Emission::Cells(items(body, counters)?), |
| 1155 |
Emission::Cell { |
| 1156 |
column, |
| 1157 |
value, |
| 1158 |
body, |
| 1159 |
} => Emission::Cell { |
| 1160 |
column: column.as_ref().map(|it| arg(it, counters)).transpose()?, |
| 1161 |
value: arg(value, counters)?, |
| 1162 |
body: items(body, counters)?, |
| 1163 |
}, |
| 1164 |
Emission::Offering { |
| 1165 |
label, |
| 1166 |
action, |
| 1167 |
body, |
| 1168 |
} => Emission::Offering { |
| 1169 |
label: arg(label, counters)?, |
| 1170 |
action: self::action(action, counters)?, |
| 1171 |
body: items(body, counters)?, |
| 1172 |
}, |
| 1173 |
Emission::Removes { |
| 1174 |
label, |
| 1175 |
action, |
| 1176 |
body, |
| 1177 |
} => Emission::Removes { |
| 1178 |
label: arg(label, counters)?, |
| 1179 |
action: self::action(action, counters)?, |
| 1180 |
body: items(body, counters)?, |
| 1181 |
}, |
| 1182 |
Emission::Repeats { |
| 1183 |
one, |
| 1184 |
label, |
| 1185 |
action, |
| 1186 |
body, |
| 1187 |
} => Emission::Repeats { |
| 1188 |
one: arg(one, counters)?, |
| 1189 |
label: arg(label, counters)?, |
| 1190 |
action: self::action(action, counters)?, |
| 1191 |
body: items(body, counters)?, |
| 1192 |
}, |
| 1193 |
Emission::Activate(action) => Emission::Activate(self::action(action, counters)?), |
| 1194 |
Emission::Link { text, action } => Emission::Link { |
| 1195 |
text: arg(text, counters)?, |
| 1196 |
action: self::action(action, counters)?, |
| 1197 |
}, |
| 1198 |
Emission::Include(hole) => Emission::Include(include(hole, counters)?), |
| 1199 |
|
| 1200 |
|
| 1201 |
Emission::IncludeEach(hole) => Emission::IncludeEach(include(hole, counters)?), |
| 1202 |
Emission::Region { name, kind, body } => Emission::Region { |
| 1203 |
name: arg(name, counters)?, |
| 1204 |
kind: match kind { |
| 1205 |
RegionKind::Variant(variant) => RegionKind::Variant(variant.clone()), |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
RegionKind::Supplied(hole) => { |
| 1213 |
let fixed_kind = match &hole.root { |
| 1214 |
HoleRoot::Path(_) => hole.steps.is_empty(), |
| 1215 |
HoleRoot::Call { args, .. } => { |
| 1216 |
hole.steps.is_empty() && args.iter().all(fixed) |
| 1217 |
} |
| 1218 |
HoleRoot::Binding(_) => false, |
| 1219 |
}; |
| 1220 |
if !fixed_kind { |
| 1221 |
return Err(syn::Error::new( |
| 1222 |
hole.span(), |
| 1223 |
"a staged shape cannot take its region kind from a supplier \ |
| 1224 |
that reads the request: a `RegionKind` has no sentinel to \ |
| 1225 |
stand in for it", |
| 1226 |
)); |
| 1227 |
} |
| 1228 |
RegionKind::Supplied(hole.clone()) |
| 1229 |
} |
| 1230 |
}, |
| 1231 |
body: items(body, counters)?, |
| 1232 |
}, |
| 1233 |
Emission::Across { fallback, body } => Emission::Across { |
| 1234 |
fallback: fallback.clone(), |
| 1235 |
body: items(body, counters)?, |
| 1236 |
}, |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
Emission::Framed { label, inner } => Emission::Framed { |
| 1241 |
label: label.clone(), |
| 1242 |
inner: Box::new(self::emission(inner, counters)?), |
| 1243 |
}, |
| 1244 |
Emission::Beside { |
| 1245 |
priority, |
| 1246 |
width, |
| 1247 |
inner, |
| 1248 |
} => Emission::Beside { |
| 1249 |
priority: arg(priority, counters)?, |
| 1250 |
width: width.as_ref().map(|held| arg(held, counters)).transpose()?, |
| 1251 |
inner: Box::new(self::emission(inner, counters)?), |
| 1252 |
}, |
| 1253 |
Emission::At { at, inner } => Emission::At { |
| 1254 |
at: arg(at, counters)?, |
| 1255 |
inner: Box::new(self::emission(inner, counters)?), |
| 1256 |
}, |
| 1257 |
Emission::Guarded { guard, inner } => { |
| 1258 |
let staged = self::guard(guard, counters); |
| 1259 |
counters.enter(); |
| 1260 |
let inner = Box::new(self::emission(inner, counters)?); |
| 1261 |
let body = counters.exit(); |
| 1262 |
counters.wrote(Fill::Branch { |
| 1263 |
guard: (*guard).clone(), |
| 1264 |
body, |
| 1265 |
}); |
| 1266 |
Emission::Guarded { |
| 1267 |
guard: staged, |
| 1268 |
inner, |
| 1269 |
} |
| 1270 |
} |
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
Emission::Given { |
| 1290 |
scrutinee, |
| 1291 |
arms, |
| 1292 |
otherwise, |
| 1293 |
} => { |
| 1294 |
let Some(otherwise) = otherwise else { |
| 1295 |
return Err(syn::Error::new( |
| 1296 |
scrutinee.span(), |
| 1297 |
"a staged dispatch says what it does when nothing matches: \ |
| 1298 |
a residual holds one arm per position and there has to be \ |
| 1299 |
one to hold", |
| 1300 |
)); |
| 1301 |
}; |
| 1302 |
let id = counters.arms; |
| 1303 |
counters.arms += 1; |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
let staged = arms |
| 1309 |
.iter() |
| 1310 |
.map(|(pattern, arm)| { |
| 1311 |
Ok((pattern.clone(), Box::new(self::emission(arm, counters)?))) |
| 1312 |
}) |
| 1313 |
.collect::<Result<Vec<_>>>()?; |
| 1314 |
let last = Box::new(self::emission(otherwise, counters)?); |
| 1315 |
|
| 1316 |
counters.wrote(Fill::Arms { |
| 1317 |
scrutinee: (*scrutinee).clone(), |
| 1318 |
patterns: arms.iter().map(|(pattern, _)| pattern.clone()).collect(), |
| 1319 |
}); |
| 1320 |
|
| 1321 |
let count = staged.len() + 1; |
| 1322 |
Emission::Given { |
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
scrutinee: plan_arm(id, count), |
| 1328 |
arms: staged |
| 1329 |
.into_iter() |
| 1330 |
.enumerate() |
| 1331 |
.map(|(at, (_, arm))| (crate::ast::Pattern::Int(at as i64), arm)) |
| 1332 |
.collect(), |
| 1333 |
otherwise: Some(last), |
| 1334 |
} |
| 1335 |
} |
| 1336 |
}) |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
fn include(hole: &Hole, counters: &mut Counters) -> Result<Hole> { |
| 1356 |
let HoleRoot::Call { path, .. } = &hole.root else { |
| 1357 |
return Err(syn::Error::new( |
| 1358 |
hole.span(), |
| 1359 |
"a staged `include` names a shape to call: a value supplied from a \ |
| 1360 |
binding has no sentinel to stand in for it", |
| 1361 |
)); |
| 1362 |
}; |
| 1363 |
if !hole.steps.is_empty() { |
| 1364 |
return Err(syn::Error::new( |
| 1365 |
hole.span(), |
| 1366 |
"a staged `include` calls a shape and reads nothing off it", |
| 1367 |
)); |
| 1368 |
} |
| 1369 |
|
| 1370 |
let HoleRoot::Call { args, .. } = &hole.root else { |
| 1371 |
unreachable!("checked above"); |
| 1372 |
}; |
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
if args.iter().all(fixed) { |
| 1388 |
let mut path = path.clone(); |
| 1389 |
let last = path |
| 1390 |
.segments |
| 1391 |
.last_mut() |
| 1392 |
.ok_or_else(|| syn::Error::new(hole.span(), "an empty path"))?; |
| 1393 |
last.ident = constant_name(&last.ident); |
| 1394 |
return Ok(Hole { |
| 1395 |
root: HoleRoot::Call { |
| 1396 |
path, |
| 1397 |
args: args.clone(), |
| 1398 |
}, |
| 1399 |
steps: Vec::new(), |
| 1400 |
}); |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
let site = counters.sites; |
| 1407 |
counters.sites += 1; |
| 1408 |
|
| 1409 |
counters.wrote(Fill::Include { |
| 1410 |
callee: path.clone(), |
| 1411 |
args: args.clone(), |
| 1412 |
site, |
| 1413 |
}); |
| 1414 |
|
| 1415 |
let mut path = path.clone(); |
| 1416 |
let last = path |
| 1417 |
.segments |
| 1418 |
.last_mut() |
| 1419 |
.ok_or_else(|| syn::Error::new(hole.span(), "an empty path"))?; |
| 1420 |
last.ident = staged_name(&last.ident); |
| 1421 |
|
| 1422 |
Ok(Hole { |
| 1423 |
root: HoleRoot::Call { |
| 1424 |
path, |
| 1425 |
args: vec![Arg::Borrow(Box::new(Arg::Hole(Hole { |
| 1426 |
root: HoleRoot::Binding(Ident::new(PLAN, Span::call_site())), |
| 1427 |
steps: vec![crate::ast::Step::Method { |
| 1428 |
name: Ident::new("enter", Span::call_site()), |
| 1429 |
args: vec![Arg::Int(i64::from(site))], |
| 1430 |
}], |
| 1431 |
})))], |
| 1432 |
}, |
| 1433 |
steps: Vec::new(), |
| 1434 |
}) |
| 1435 |
} |
| 1436 |
|
| 1437 |
impl Hole { |
| 1438 |
|
| 1439 |
pub(crate) fn span(&self) -> Span { |
| 1440 |
match &self.root { |
| 1441 |
HoleRoot::Binding(name) => name.span(), |
| 1442 |
HoleRoot::Path(path) | HoleRoot::Call { path, .. } => syn::spanned::Spanned::span(path), |
| 1443 |
} |
| 1444 |
} |
| 1445 |
} |
| 1446 |
|