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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
use std::fmt::{self, Debug, Formatter, Write};
use std::iter;
use ecow::{eco_format, EcoString, EcoVec};
use super::{Content, Label, Node, NodeId, Vt};
use crate::diag::{SourceResult, Trace, Tracepoint};
use crate::eval::{cast_from_value, Args, Cast, Dict, Func, Regex, Value, Vm};
use crate::syntax::Span;
use crate::util::pretty_array_like;
/// A map of style properties.
#[derive(Default, Clone, Hash)]
pub struct StyleMap(Vec<Style>);
impl StyleMap {
/// Create a new, empty style map.
pub fn new() -> Self {
Self::default()
}
/// Whether this map contains no styles.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Set an inner value for a style property.
///
/// If the property needs folding and the value is already contained in the
/// style map, `self` contributes the outer values and `value` is the inner
/// one.
pub fn set(&mut self, style: impl Into<Style>) {
self.0.push(style.into());
}
/// Remove the style that was last set.
pub fn unset(&mut self) {
self.0.pop();
}
/// Apply outer styles. Like [`chain`](StyleChain::chain), but in-place.
pub fn apply(&mut self, outer: Self) {
self.0.splice(0..0, outer.0.iter().cloned());
}
/// Add an origin span to all contained properties.
pub fn spanned(mut self, span: Span) -> Self {
for entry in &mut self.0 {
if let Style::Property(property) = entry {
property.span = Some(span);
}
}
self
}
/// Returns `Some(_)` with an optional span if this map contains styles for
/// the given `node`.
pub fn interruption<T: Node>(&self) -> Option<Option<Span>> {
let node = NodeId::of::<T>();
self.0.iter().find_map(|entry| match entry {
Style::Property(property) => property.is_of(node).then(|| property.span),
Style::Recipe(recipe) => recipe.is_of(node).then(|| Some(recipe.span)),
})
}
}
impl From<Style> for StyleMap {
fn from(entry: Style) -> Self {
Self(vec![entry])
}
}
impl PartialEq for StyleMap {
fn eq(&self, other: &Self) -> bool {
crate::util::hash128(self) == crate::util::hash128(other)
}
}
impl Debug for StyleMap {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad("..")
}
}
/// A single style property or recipe.
#[derive(Clone, Hash)]
pub enum Style {
/// A style property originating from a set rule or constructor.
Property(Property),
/// A show rule recipe.
Recipe(Recipe),
}
impl Style {
/// If this is a property, return it.
pub fn property(&self) -> Option<&Property> {
match self {
Self::Property(property) => Some(property),
_ => None,
}
}
/// If this is a recipe, return it.
pub fn recipe(&self) -> Option<&Recipe> {
match self {
Self::Recipe(recipe) => Some(recipe),
_ => None,
}
}
}
impl Debug for Style {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Property(property) => property.fmt(f),
Self::Recipe(recipe) => recipe.fmt(f),
}
}
}
impl From<Property> for Style {
fn from(property: Property) -> Self {
Self::Property(property)
}
}
/// A style property originating from a set rule or constructor.
#[derive(Clone, Hash)]
pub struct Property {
/// The id of the node the property belongs to.
node: NodeId,
/// The property's name.
name: EcoString,
/// The property's value.
value: Value,
/// The span of the set rule the property stems from.
span: Option<Span>,
}
impl Property {
/// Create a new property from a key-value pair.
pub fn new(node: NodeId, name: EcoString, value: Value) -> Self {
Self { node, name, value, span: None }
}
/// Whether this property is the given one.
pub fn is(&self, node: NodeId, name: &str) -> bool {
self.node == node && self.name == name
}
/// Whether this property belongs to the node with the given id.
pub fn is_of(&self, node: NodeId) -> bool {
self.node == node
}
}
impl Debug for Property {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "set {}({}: {:?})", self.node.name, self.name, self.value)?;
Ok(())
}
}
/// A show rule recipe.
#[derive(Clone, Hash)]
pub struct Recipe {
/// The span errors are reported with.
pub span: Span,
/// Determines whether the recipe applies to a node.
pub selector: Option<Selector>,
/// The transformation to perform on the match.
pub transform: Transform,
}
impl Recipe {
/// Whether this recipe is for the given node.
pub fn is_of(&self, node: NodeId) -> bool {
match self.selector {
Some(Selector::Node(id, _)) => id == node,
_ => false,
}
}
/// Whether the recipe is applicable to the target.
pub fn applicable(&self, target: &Content) -> bool {
self.selector
.as_ref()
.map_or(false, |selector| selector.matches(target))
}
/// Apply the recipe to the given content.
pub fn apply_vm(&self, vm: &mut Vm, content: Content) -> SourceResult<Content> {
match &self.transform {
Transform::Content(content) => Ok(content.clone()),
Transform::Func(func) => {
let args = Args::new(self.span, [Value::Content(content.clone())]);
let mut result = func.call_vm(vm, args);
// For selector-less show rules, a tracepoint makes no sense.
if self.selector.is_some() {
let point = || Tracepoint::Show(content.id().name.into());
result = result.trace(vm.world(), point, content.span());
}
Ok(result?.display())
}
Transform::Style(styles) => Ok(content.styled_with_map(styles.clone())),
}
}
/// Apply the recipe to the given content.
pub fn apply_vt(&self, vt: &mut Vt, content: Content) -> SourceResult<Content> {
match &self.transform {
Transform::Content(content) => Ok(content.clone()),
Transform::Func(func) => {
let mut result = func.call_vt(vt, [Value::Content(content.clone())]);
if self.selector.is_some() {
let point = || Tracepoint::Show(content.id().name.into());
result = result.trace(vt.world, point, content.span());
}
Ok(result?.display())
}
Transform::Style(styles) => Ok(content.styled_with_map(styles.clone())),
}
}
}
impl Debug for Recipe {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str("show")?;
if let Some(selector) = &self.selector {
f.write_char(' ')?;
selector.fmt(f)?;
}
f.write_str(": ")?;
self.transform.fmt(f)
}
}
/// A selector in a show rule.
#[derive(Clone, PartialEq, Hash)]
pub enum Selector {
/// Matches a specific type of node.
///
/// If there is a dictionary, only nodes with the fields from the
/// dictionary match.
Node(NodeId, Option<Dict>),
/// Matches nodes with a specific label.
Label(Label),
/// Matches text nodes through a regular expression.
Regex(Regex),
/// Matches if any of the subselectors match.
Any(EcoVec<Self>),
}
impl Selector {
/// Define a simple node selector.
pub fn node<T: Node>() -> Self {
Self::Node(NodeId::of::<T>(), None)
}
/// Define a simple text selector.
pub fn text(text: &str) -> Self {
Self::Regex(Regex::new(®ex::escape(text)).unwrap())
}
/// Whether the selector matches for the target.
pub fn matches(&self, target: &Content) -> bool {
match self {
Self::Node(id, dict) => {
target.id() == *id
&& dict
.iter()
.flat_map(|dict| dict.iter())
.all(|(name, value)| target.field(name) == Some(value))
}
Self::Label(label) => target.label() == Some(label),
Self::Regex(regex) => {
target.id() == item!(text_id)
&& item!(text_str)(target).map_or(false, |text| regex.is_match(&text))
}
Self::Any(selectors) => selectors.iter().any(|sel| sel.matches(target)),
}
}
}
impl Debug for Selector {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Node(node, dict) => {
f.write_str(node.name)?;
if let Some(dict) = dict {
f.write_str(".where")?;
dict.fmt(f)?;
}
Ok(())
}
Self::Label(label) => label.fmt(f),
Self::Regex(regex) => regex.fmt(f),
Self::Any(selectors) => {
f.write_str("any")?;
let pieces: Vec<_> =
selectors.iter().map(|sel| eco_format!("{sel:?}")).collect();
f.write_str(&pretty_array_like(&pieces, false))
}
}
}
}
cast_from_value! {
Selector: "selector",
text: EcoString => Self::text(&text),
label: Label => Self::Label(label),
func: Func => func.select(None)?,
regex: Regex => Self::Regex(regex),
}
/// A show rule transformation that can be applied to a match.
#[derive(Clone, Hash)]
pub enum Transform {
/// Replacement content.
Content(Content),
/// A function to apply to the match.
Func(Func),
/// Apply styles to the content.
Style(StyleMap),
}
impl Debug for Transform {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Content(content) => content.fmt(f),
Self::Func(func) => func.fmt(f),
Self::Style(styles) => styles.fmt(f),
}
}
}
cast_from_value! {
Transform,
content: Content => Self::Content(content),
func: Func => Self::Func(func),
}
/// A chain of style maps, similar to a linked list.
///
/// A style chain allows to combine properties from multiple style maps in a
/// node hierarchy in a non-allocating way. Rather than eagerly merging the
/// maps, each access walks the hierarchy from the innermost to the outermost
/// map, trying to find a match and then folding it with matches further up the
/// chain.
#[derive(Default, Clone, Copy, Hash)]
pub struct StyleChain<'a> {
/// The first link of this chain.
head: &'a [Style],
/// The remaining links in the chain.
tail: Option<&'a Self>,
}
impl<'a> StyleChain<'a> {
/// Start a new style chain with a root map.
pub fn new(root: &'a StyleMap) -> Self {
Self { head: &root.0, tail: None }
}
/// Make the given map the first link of this chain.
///
/// The resulting style chain contains styles from `map` as well as
/// `self`. The ones from `map` take precedence over the ones from
/// `self`. For folded properties `map` contributes the inner value.
pub fn chain<'b>(&'b self, map: &'b StyleMap) -> StyleChain<'b> {
if map.is_empty() {
*self
} else {
StyleChain { head: &map.0, tail: Some(self) }
}
}
/// Make the given style the first link of the this chain.
pub fn chain_one<'b>(&'b self, style: &'b Style) -> StyleChain<'b> {
StyleChain {
head: std::slice::from_ref(style),
tail: Some(self),
}
}
/// Cast the first value for the given property in the chain.
pub fn get<T: Cast>(
self,
node: NodeId,
name: &'a str,
inherent: Option<Value>,
default: impl Fn() -> T,
) -> T {
self.properties::<T>(node, name, inherent)
.next()
.unwrap_or_else(default)
}
/// Cast the first value for the given property in the chain.
pub fn get_resolve<T: Cast + Resolve>(
self,
node: NodeId,
name: &'a str,
inherent: Option<Value>,
default: impl Fn() -> T,
) -> T::Output {
self.get(node, name, inherent, default).resolve(self)
}
/// Cast the first value for the given property in the chain.
pub fn get_fold<T: Cast + Fold>(
self,
node: NodeId,
name: &'a str,
inherent: Option<Value>,
default: impl Fn() -> T::Output,
) -> T::Output {
fn next<T: Fold>(
mut values: impl Iterator<Item = T>,
styles: StyleChain,
default: &impl Fn() -> T::Output,
) -> T::Output {
values
.next()
.map(|value| value.fold(next(values, styles, default)))
.unwrap_or_else(|| default())
}
next(self.properties::<T>(node, name, inherent), self, &default)
}
/// Cast the first value for the given property in the chain.
pub fn get_resolve_fold<T>(
self,
node: NodeId,
name: &'a str,
inherent: Option<Value>,
default: impl Fn() -> <T::Output as Fold>::Output,
) -> <T::Output as Fold>::Output
where
T: Cast + Resolve,
T::Output: Fold,
{
fn next<T>(
mut values: impl Iterator<Item = T>,
styles: StyleChain,
default: &impl Fn() -> <T::Output as Fold>::Output,
) -> <T::Output as Fold>::Output
where
T: Resolve,
T::Output: Fold,
{
values
.next()
.map(|value| value.resolve(styles).fold(next(values, styles, default)))
.unwrap_or_else(|| default())
}
next(self.properties::<T>(node, name, inherent), self, &default)
}
/// Iterate over all style recipes in the chain.
pub fn recipes(self) -> impl Iterator<Item = &'a Recipe> {
self.entries().filter_map(Style::recipe)
}
/// Iterate over all values for the given property in the chain.
pub fn properties<T: Cast + 'a>(
self,
node: NodeId,
name: &'a str,
inherent: Option<Value>,
) -> impl Iterator<Item = T> + '_ {
inherent
.into_iter()
.chain(
self.entries()
.filter_map(Style::property)
.filter(move |property| property.is(node, name))
.map(|property| property.value.clone()),
)
.map(move |value| {
value
.cast()
.unwrap_or_else(|err| panic!("{} (for {}.{})", err, node.name, name))
})
}
/// Convert to a style map.
pub fn to_map(self) -> StyleMap {
let mut suffix = StyleMap::new();
for link in self.links() {
suffix.0.splice(0..0, link.iter().cloned());
}
suffix
}
/// Iterate over the entries of the chain.
fn entries(self) -> Entries<'a> {
Entries { inner: [].as_slice().iter(), links: self.links() }
}
/// Iterate over the links of the chain.
fn links(self) -> Links<'a> {
Links(Some(self))
}
/// Build a style map from the suffix (all links beyond the `len`) of the
/// chain.
fn suffix(self, len: usize) -> StyleMap {
let mut suffix = StyleMap::new();
let take = self.links().count().saturating_sub(len);
for link in self.links().take(take) {
suffix.0.splice(0..0, link.iter().cloned());
}
suffix
}
/// Remove the last link from the chain.
fn pop(&mut self) {
*self = self.tail.copied().unwrap_or_default();
}
}
impl Debug for StyleChain<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
for entry in self.entries().collect::<Vec<_>>().into_iter().rev() {
writeln!(f, "{:?}", entry)?;
}
Ok(())
}
}
impl PartialEq for StyleChain<'_> {
fn eq(&self, other: &Self) -> bool {
crate::util::hash128(self) == crate::util::hash128(other)
}
}
/// An iterator over the entries in a style chain.
struct Entries<'a> {
inner: std::slice::Iter<'a, Style>,
links: Links<'a>,
}
impl<'a> Iterator for Entries<'a> {
type Item = &'a Style;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(entry) = self.inner.next_back() {
return Some(entry);
}
match self.links.next() {
Some(next) => self.inner = next.iter(),
None => return None,
}
}
}
}
/// An iterator over the links of a style chain.
struct Links<'a>(Option<StyleChain<'a>>);
impl<'a> Iterator for Links<'a> {
type Item = &'a [Style];
fn next(&mut self) -> Option<Self::Item> {
let StyleChain { head, tail } = self.0?;
self.0 = tail.copied();
Some(head)
}
}
/// A sequence of items with associated styles.
#[derive(Clone, Hash)]
pub struct StyleVec<T> {
items: Vec<T>,
maps: Vec<(StyleMap, usize)>,
}
impl<T> StyleVec<T> {
/// Whether there are any items in the sequence.
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
/// Number of items in the sequence.
pub fn len(&self) -> usize {
self.items.len()
}
/// Insert an element in the front. The element will share the style of the
/// current first element.
///
/// This method has no effect if the vector is empty.
pub fn push_front(&mut self, item: T) {
if !self.maps.is_empty() {
self.items.insert(0, item);
self.maps[0].1 += 1;
}
}
/// Map the contained items.
pub fn map<F, U>(&self, f: F) -> StyleVec<U>
where
F: FnMut(&T) -> U,
{
StyleVec {
items: self.items.iter().map(f).collect(),
maps: self.maps.clone(),
}
}
/// Iterate over references to the contained items and associated style maps.
pub fn iter(&self) -> impl Iterator<Item = (&T, &StyleMap)> + '_ {
self.items().zip(
self.maps
.iter()
.flat_map(|(map, count)| iter::repeat(map).take(*count)),
)
}
/// Iterate over the contained items.
pub fn items(&self) -> std::slice::Iter<'_, T> {
self.items.iter()
}
/// Iterate over the contained maps. Note that zipping this with `items()`
/// does not yield the same result as calling `iter()` because this method
/// only returns maps once that are shared by consecutive items. This method
/// is designed for use cases where you want to check, for example, whether
/// any of the maps fulfills a specific property.
pub fn styles(&self) -> impl Iterator<Item = &StyleMap> {
self.maps.iter().map(|(map, _)| map)
}
}
impl StyleVec<Content> {
pub fn to_vec(self) -> Vec<Content> {
self.items
.into_iter()
.zip(
self.maps
.iter()
.flat_map(|(map, count)| iter::repeat(map).take(*count)),
)
.map(|(content, map)| content.styled_with_map(map.clone()))
.collect()
}
}
impl<T> Default for StyleVec<T> {
fn default() -> Self {
Self { items: vec![], maps: vec![] }
}
}
impl<T> FromIterator<T> for StyleVec<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let items: Vec<_> = iter.into_iter().collect();
let maps = vec![(StyleMap::new(), items.len())];
Self { items, maps }
}
}
impl<T: Debug> Debug for StyleVec<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries(self.iter().map(|(item, map)| {
crate::util::debug(|f| {
map.fmt(f)?;
item.fmt(f)
})
}))
.finish()
}
}
/// Assists in the construction of a [`StyleVec`].
#[derive(Debug)]
pub struct StyleVecBuilder<'a, T> {
items: Vec<T>,
chains: Vec<(StyleChain<'a>, usize)>,
}
impl<'a, T> StyleVecBuilder<'a, T> {
/// Create a new style-vec builder.
pub fn new() -> Self {
Self { items: vec![], chains: vec![] }
}
/// Whether the builder is empty.
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
/// Push a new item into the style vector.
pub fn push(&mut self, item: T, styles: StyleChain<'a>) {
self.items.push(item);
if let Some((prev, count)) = self.chains.last_mut() {
if *prev == styles {
*count += 1;
return;
}
}
self.chains.push((styles, 1));
}
/// Iterate over the contained items.
pub fn items(&self) -> std::slice::Iter<'_, T> {
self.items.iter()
}
/// Finish building, returning a pair of two things:
/// - a style vector of items with the non-shared styles
/// - a shared prefix chain of styles that apply to all items
pub fn finish(self) -> (StyleVec<T>, StyleChain<'a>) {
let mut iter = self.chains.iter();
let mut trunk = match iter.next() {
Some(&(chain, _)) => chain,
None => return Default::default(),
};
let mut shared = trunk.links().count();
for &(mut chain, _) in iter {
let len = chain.links().count();
if len < shared {
for _ in 0..shared - len {
trunk.pop();
}
shared = len;
} else if len > shared {
for _ in 0..len - shared {
chain.pop();
}
}
while shared > 0 && chain != trunk {
trunk.pop();
chain.pop();
shared -= 1;
}
}
let maps = self
.chains
.into_iter()
.map(|(chain, count)| (chain.suffix(shared), count))
.collect();
(StyleVec { items: self.items, maps }, trunk)
}
}
impl<'a, T> Default for StyleVecBuilder<'a, T> {
fn default() -> Self {
Self::new()
}
}
/// A property that is resolved with other properties from the style chain.
pub trait Resolve {
/// The type of the resolved output.
type Output;
/// Resolve the value using the style chain.
fn resolve(self, styles: StyleChain) -> Self::Output;
}
impl<T: Resolve> Resolve for Option<T> {
type Output = Option<T::Output>;
fn resolve(self, styles: StyleChain) -> Self::Output {
self.map(|v| v.resolve(styles))
}
}
/// A property that is folded to determine its final value.
pub trait Fold {
/// The type of the folded output.
type Output;
/// Fold this inner value with an outer folded value.
fn fold(self, outer: Self::Output) -> Self::Output;
}
impl<T> Fold for Option<T>
where
T: Fold,
T::Output: Default,
{
type Output = Option<T::Output>;
fn fold(self, outer: Self::Output) -> Self::Output {
self.map(|inner| inner.fold(outer.unwrap_or_default()))
}
}
|