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
// ClaimExtractor implementation: claim extraction from documentation text,
// entity recognition (languages, capabilities), and regex-based pattern matching.
impl ClaimExtractor {
/// Create new claim extractor with default patterns
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn new() -> Self {
let capability_patterns = vec![
// Positive capabilities: "PMAT can analyze X"
Regex::new(r"(?i)PMAT can ([a-z]+)\s+(.+?)(?:\.|$)").expect("internal error"),
// Negative capabilities: "PMAT cannot compile"
Regex::new(r"(?i)PMAT cannot ([a-z]+)\s+(.+?)(?:\.|$)").expect("internal error"),
// Alternative patterns: "PMAT supports X"
Regex::new(r"(?i)PMAT supports? (.+?)(?:\.|$)").expect("internal error"),
];
let known_languages = vec![
"Rust",
"TypeScript",
"JavaScript",
"Python",
"C",
"C++",
"Go",
"Java",
"Kotlin",
"Ruby",
"PHP",
"Swift",
"C#",
"Bash",
"WASM",
"Haskell",
"Elixir",
"Erlang",
"OCaml",
]
.into_iter()
.map(|s| s.to_string())
.collect();
// Repository-relative paths: a recognised top-level directory, then a
// filename with an extension. Anchoring on the directory keeps prose
// like "and/or" or "24/7" out of the claim set.
let file_pattern = Regex::new(
r#"(?:^|[\s`"(\[,])((?:\./)?(?:src|docs|tests|benches|examples|crates|server|scripts|assets|\.github)/[A-Za-z0-9_+./-]+\.[A-Za-z0-9]{1,10})"#,
)
.expect("internal error");
// Function references written the way documentation writes them: an
// identifier immediately followed by ().
let function_pattern =
Regex::new(r#"\b([a-z_][a-z0-9_]{2,})\(\)"#).expect("internal error");
Self {
capability_patterns,
known_languages,
file_pattern,
function_pattern,
}
}
/// Extract all claims from documentation text
///
/// Three families of claim are recognised, all of them checkable against
/// the repository: capability claims ("PMAT can …"), file references
/// (`src/foo.rs`) and function references (`do_thing()`). Prose that
/// asserts nothing checkable yields no claim — but see
/// `ValidateReadmeCmd::execute`, which treats an empty claim set as "this
/// document was not checked", never as a pass.
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn extract_claims(&self, documentation: &str) -> Vec<Claim> {
let mut claims = Vec::new();
let mut in_code_block = false;
// Documents repeat the same path or function on many lines; one
// verdict per distinct reference keeps the report readable.
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
for (line_number, line) in documentation.lines().enumerate() {
let trimmed = line.trim();
// Track markdown fenced code blocks (```)
if trimmed.starts_with("```") {
in_code_block = !in_code_block;
continue;
}
// Skip lines inside code blocks
if in_code_block {
continue;
}
// Skip empty lines and headers
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
let line_number = line_number + 1;
// Try to extract capability claims
if let Some(claim) = self.extract_capability_claim(line, line_number) {
claims.push(claim);
}
claims.extend(self.extract_reference_claims(line, line_number, &mut seen));
}
claims
}
/// Standard-library and universal method names. Documentation mentioning
/// `unwrap()` or `len()` is not claiming the project defines them, and the
/// fact database (which indexes project functions) would always report
/// them missing — a guaranteed false finding.
fn is_ubiquitous_method(name: &str) -> bool {
const UBIQUITOUS: &[&str] = &[
"unwrap",
"expect",
"clone",
"len",
"new",
"default",
"to_string",
"into",
"from",
"is_empty",
"iter",
"collect",
"push",
"insert",
"get",
"as_str",
"to_owned",
"to_vec",
"next",
"main",
];
UBIQUITOUS.contains(&name)
}
/// Extract file-path and function references from a single line.
fn extract_reference_claims(
&self,
line: &str,
line_number: usize,
seen: &mut std::collections::HashSet<String>,
) -> Vec<Claim> {
let mut claims = Vec::new();
for caps in self.file_pattern.captures_iter(line) {
let Some(path) = caps.get(1).map(|m| m.as_str()) else {
continue;
};
if !seen.insert(format!("file:{path}")) {
continue;
}
claims.push(Claim {
source_file: PathBuf::from(""),
line_number,
text: format!("references file {path}"),
claim_type: ClaimType::Structure,
entities: vec![Entity::File(path.to_string())],
is_negative: false,
});
}
for caps in self.function_pattern.captures_iter(line) {
let Some(name) = caps.get(1).map(|m| m.as_str()) else {
continue;
};
if Self::is_ubiquitous_method(name) {
continue;
}
if !seen.insert(format!("fn:{name}")) {
continue;
}
claims.push(Claim {
source_file: PathBuf::from(""),
line_number,
text: format!("references function {name}()"),
claim_type: ClaimType::Api,
entities: vec![Entity::Function(name.to_string())],
is_negative: false,
});
}
claims
}
/// Extract capability claim from a line of text
fn extract_capability_claim(&self, line: &str, line_number: usize) -> Option<Claim> {
// Check for "PMAT can" pattern
if let Some(caps) = self.capability_patterns[0].captures(line) {
let verb = caps.get(1)?.as_str();
let object = caps.get(2)?.as_str();
let text = format!("PMAT can {} {}", verb, object);
let entities = self.extract_entities(&text);
return Some(Claim {
source_file: PathBuf::from(""),
line_number,
text: text.trim_end_matches('.').to_string(),
claim_type: ClaimType::Capability,
entities,
is_negative: false,
});
}
// Check for "PMAT cannot" pattern (negative capability)
if let Some(caps) = self.capability_patterns[1].captures(line) {
let verb = caps.get(1)?.as_str();
let object = caps.get(2)?.as_str();
let text = format!("PMAT cannot {} {}", verb, object);
let entities = self.extract_entities(&text);
return Some(Claim {
source_file: PathBuf::from(""),
line_number,
text: text.trim_end_matches('.').to_string(),
claim_type: ClaimType::Capability,
entities,
is_negative: true,
});
}
// Check for "PMAT supports" pattern
if let Some(caps) = self.capability_patterns[2].captures(line) {
let object = caps.get(1)?.as_str();
let text = format!("PMAT supports {}", object);
let entities = self.extract_entities(&text);
return Some(Claim {
source_file: PathBuf::from(""),
line_number,
text: text.trim_end_matches('.').to_string(),
claim_type: ClaimType::Capability,
entities,
is_negative: false,
});
}
None
}
/// Extract entities (languages, capabilities) from claim text
fn extract_entities(&self, text: &str) -> Vec<Entity> {
let mut entities = Vec::new();
// Extract language entities
for language in &self.known_languages {
if text.contains(language) {
entities.push(Entity::Language(language.clone()));
}
}
// Extract capability entities (verbs)
let capability_verbs = vec![
"analyze", "compile", "support", "detect", "generate", "validate", "parse", "extract",
"format", "refactor",
];
for verb in capability_verbs {
if text.to_lowercase().contains(verb) {
entities.push(Entity::Capability(verb.to_string()));
}
}
entities
}
}
impl Default for ClaimExtractor {
fn default() -> Self {
Self::new()
}
}