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
//! The walk of a section whose blocks are in any order: every rule that
//! admits a block is tried, and where none is clean the rule whose refusal
//! the walk keeps claims the block.
use std::borrow::Cow;
use super::{
Checker, blocks,
claim::{self, Around, Claimed, Docs},
};
use crate::{grammar::Block as Rule, markdown::BlockKind};
/// Blocks in any order: every admitting rule is tried, and where none is
/// clean the rule whose refusal the walk keeps claims the block and reports.
// sokf-implements: TLMD_62
pub(super) fn match_free(
c: &mut Checker<'_>,
rules: &[&Rule],
docs: &mut Docs<'_>,
section: Option<usize>,
at: usize,
) {
// How many claims each rule made, how many repetitions each group
// holds, which its `min` counts at its next claim, and the line of each
// rule's first claim.
let mut used = vec![0usize; rules.len()];
let mut held = vec![0usize; rules.len()];
let mut first = vec![at; rules.len()];
let mut di = 0;
while di < docs.len() {
let free: Vec<usize> = (0..rules.len())
// A group always repeats, so it is free after its first claim
// too, as it takes every repetition in a walk in order.
.filter(|&ri| used[ri] == 0 || rules[ri].repeat || rules[ri].kind == BlockKind::Group)
.collect();
let mut taken = None;
let mut refusal = None;
let fit: Vec<usize> = free
.iter()
.copied()
.filter(|&ri| c.matches_rule(&docs[di], rules[ri]))
.collect();
for ri in fit {
// The positions after a rule are the other free rules, and not
// the rule itself, whose next repetition is its own business.
// sokf-implements: TLMD_71
let later: Vec<&Rule> = free
.iter()
.filter(|&&k| k != ri)
.map(|&k| rules[k])
.collect();
let kinds = claim::opened(&later, c);
let around = Around {
later: &later,
kinds: &kinds,
held: held[ri],
ordered: false,
dividing: &[],
};
// Dividing a run needs an order to divide it along, so the
// free walk takes whole blocks (LANG-5_AC-4).
let before = c.save();
match claim::claim(c, docs, di, rules[ri], section, false, false, around) {
// A group that took no block leaves it to the rules after
// it, and to the report of a block nobody claimed, and keeps
// nothing that it wrote, such as its gate.
Ok(Claimed { blocks: 0, .. }) => {
c.rollback(&before);
}
Ok(claimed) => {
taken = Some((ri, claimed));
break;
}
Err(dropped) => {
if refusal.is_none() {
refusal = Some((ri, dropped));
}
}
}
}
// Nothing clean: the rule whose refusal the walk keeps takes the
// block, so a malformed block keeps its own finding, with every
// block that the refusal covers, and the block and its findings go
// to one position.
// sokf-implements: TLMD_228
let taken = taken.or_else(|| {
let (ri, dropped) = refusal.take()?;
let blocks = dropped.blocks;
c.keep(dropped);
Some((
ri,
Claimed {
blocks,
repetitions: 0,
},
))
});
if let Some((ri, claimed)) = taken {
// The count is reported at the group's first repetition.
if held[ri] == 0 && claimed.repetitions > 0 {
first[ri] = docs[di].line;
}
used[ri] += 1;
held[ri] += claimed.repetitions;
di += claimed.blocks;
} else {
claim::unclaimed(c, &docs[di], matches!(docs[di], Cow::Owned(_)));
di += 1;
}
}
for (ri, rule) in rules.iter().enumerate() {
// A group's repetitions are counted once, over every claim of the
// walk, so that its bounds read every repetition wherever it stands.
// A group that holds none took its blocks only with the reason that
// it refused them, which says why it is short.
// sokf-implements: TLMD_146
// sokf-implements: TLMD_160
if rule.kind == BlockKind::Group && held[ri] > 0 {
c.located(claim::at_block(&c.at, rule), |c| {
blocks::check_count(c, rule, held[ri], first[ri]);
});
}
if used[ri] == 0 && !c.block_optional(rule) {
let at_rule = claim::at_block(&c.at, rule);
c.located(at_rule, |c| {
// sokf-implements: TLMD_152
c.emit(
&rule.common,
Some(at),
"TLMD_152",
format!(
"{} block is missing",
rule.kind.spoken_in(&c.host.dialect())
),
);
});
}
}
}