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
#![allow(clippy::pedantic)] // Binary tool - focus on core clippy lints only
#![allow(clippy::unwrap_used, clippy::expect_used)]
use anyhow::Result;
use clap::{Parser, Subcommand};
use perl_corpus::{
build_inventory_from_paths, files::CorpusPaths, index::write_indices, parse_dir,
};
use std::path::PathBuf;
#[derive(Parser)]
#[command(name = "perl-corpus", version, about = "Perl test corpus management tool")]
struct Cli {
/// Path to test_corpus directory
#[arg(short, long, default_value = "test_corpus")]
corpus: PathBuf,
#[command(subcommand)]
cmd: Command,
}
#[derive(Subcommand)]
enum Command {
/// Validate metadata and structure
Lint {
/// Maximum sections per file
#[arg(long, default_value = "12")]
max_sections: usize,
/// Check for unknown tags
#[arg(long, default_value = "true")]
check_tags: bool,
/// Check for unknown flags
#[arg(long, default_value = "true")]
check_flags: bool,
},
/// Build _index.json and _tags.json
Index,
/// Print corpus statistics
Stats {
/// Show detailed statistics
#[arg(short, long)]
detailed: bool,
},
/// Generate test cases
Gen {
/// Generator to use
#[command(subcommand)]
generator: Generator,
/// Number of cases to generate
#[arg(short, long, default_value = "10")]
count: usize,
/// Random seed
#[arg(short, long)]
seed: Option<u64>,
},
/// Build a deterministic corpus inventory report
Inventory {
/// Output format
#[arg(long, default_value = "json")]
format: String,
/// Optional output path (prints to stdout if omitted)
#[arg(long)]
out: Option<PathBuf>,
},
}
#[derive(Subcommand)]
enum Generator {
/// Generate full programs with mixed statements
Program,
/// Generate qw expressions
Qw,
/// Generate quote-like operators
Quote,
/// Generate heredocs
Heredoc,
/// Generate whitespace-heavy code
Whitespace,
/// Generate loop control samples (next/redo/continue)
ControlFlow,
/// Generate format statements
Format,
/// Generate glob expressions
Glob,
/// Generate tie/untie samples
Tie,
/// Generate I/O and filehandle samples
Io,
/// Generate filetest operator samples
Filetest,
/// Generate built-in function call samples
Builtins,
/// Generate map/grep/sort list-operator samples
ListOps,
/// Generate package/subroutine declarations and method calls
Declarations,
/// Generate object-oriented patterns (bless, inheritance, overload)
ObjectOriented,
/// Generate expression-heavy statements
Expressions,
/// Generate regex match/substitution/transliteration statements
Regex,
/// Generate parser-ambiguity stress samples
Ambiguity,
/// Generate sigil and dereference samples
Sigils,
/// Generate compile-time phase blocks (BEGIN/CHECK/UNITCHECK/INIT/END)
Phasers,
/// Generate special variable and punctuation variable samples
SpecialVars,
}
fn main() -> Result<()> {
let args = Cli::parse();
match args.cmd {
Command::Lint { max_sections, check_tags, check_flags } => {
let sections = parse_dir(&args.corpus)?;
let config = perl_corpus::lint::LintConfig {
max_sections_per_file: max_sections,
check_unknown_tags: check_tags,
check_unknown_flags: check_flags,
require_perl_version: false,
};
perl_corpus::lint::lint_with_config(§ions, &config)?;
println!("✅ Corpus validation passed ({} sections)", sections.len());
}
Command::Index => {
let sections = parse_dir(&args.corpus)?;
write_indices(&args.corpus, §ions)?;
println!("✅ Generated index files:");
println!(" - {}", args.corpus.join("_index.json").display());
println!(" - {}", args.corpus.join("_tags.json").display());
println!(" - {}", args.corpus.join("COVERAGE_SUMMARY.md").display());
println!(" Total sections: {}", sections.len());
}
Command::Stats { detailed } => {
let sections = parse_dir(&args.corpus)?;
// Basic stats
let unique_files: std::collections::HashSet<_> =
sections.iter().map(|s| &s.file).collect();
let all_tags: std::collections::HashSet<_> =
sections.iter().flat_map(|s| s.tags.iter()).collect();
let all_flags: std::collections::HashSet<_> =
sections.iter().flat_map(|s| s.flags.iter()).collect();
println!("📊 Corpus Statistics");
println!("====================");
println!("Files: {}", unique_files.len());
println!("Sections: {}", sections.len());
println!("Tags: {}", all_tags.len());
println!("Flags: {}", all_flags.len());
if detailed {
println!("\n📁 Files:");
let mut file_counts: std::collections::BTreeMap<&str, usize> =
std::collections::BTreeMap::new();
for s in §ions {
*file_counts.entry(&s.file).or_default() += 1;
}
for (file, count) in file_counts {
println!(" {} ({})", file, count);
}
println!("\n🏷️ Top Tags:");
let mut tag_counts: std::collections::BTreeMap<&str, usize> =
std::collections::BTreeMap::new();
for s in §ions {
for tag in &s.tags {
*tag_counts.entry(tag).or_default() += 1;
}
}
let mut sorted_tags: Vec<_> = tag_counts.into_iter().collect();
sorted_tags.sort_by_key(|(_, c)| std::cmp::Reverse(*c));
for (tag, count) in sorted_tags.iter().take(10) {
println!(" {} ({})", tag, count);
}
if !all_flags.is_empty() {
println!("\n🚩 Flags:");
for flag in all_flags {
let count = sections.iter().filter(|s| s.has_flag(flag)).count();
println!(" {} ({})", flag, count);
}
}
}
}
Command::Gen { generator, count, seed } => {
use proptest::prelude::*;
use proptest::strategy::ValueTree;
use proptest::test_runner::{Config, TestRunner};
let seed = match seed {
Some(s) => s,
None => {
use std::time::{SystemTime, UNIX_EPOCH};
match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(duration) => duration.as_secs(),
Err(e) => {
eprintln!(
"Warning: system time appears to be before UNIX_EPOCH: {}",
e
);
0
}
}
}
};
let config = Config { cases: count as u32, ..Config::default() };
let mut runner = TestRunner::new_with_rng(
config,
proptest::test_runner::TestRng::from_seed(
proptest::test_runner::RngAlgorithm::ChaCha,
&seed.to_le_bytes(),
),
);
println!("# Generated with perl-corpus (seed: {})", seed);
println!();
match generator {
Generator::Program => {
let code = perl_corpus::generate_perl_code_with_seed(count, seed);
println!("# Program ({} statements)", count);
println!("{}", code);
}
Generator::Qw => {
use perl_corpus::r#gen::qw::qw_in_context;
for i in 0..count {
let value = qw_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (qw)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Quote => {
use perl_corpus::r#gen::quote_like::quote_like_single;
for i in 0..count {
let value = quote_like_single()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (quote-like)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Heredoc => {
use perl_corpus::r#gen::heredoc::heredoc_in_context;
for i in 0..count {
let value = heredoc_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (heredoc)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Whitespace => {
use perl_corpus::r#gen::whitespace::whitespace_stress_test;
for i in 0..count {
let value = whitespace_stress_test()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (whitespace-heavy)", i + 1);
println!("{}", value);
println!();
}
}
Generator::ControlFlow => {
use perl_corpus::r#gen::control_flow::loop_with_control;
for i in 0..count {
let value = loop_with_control()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (control-flow)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Format => {
use perl_corpus::r#gen::format_statements::format_statement;
for i in 0..count {
let value = format_statement()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (format)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Glob => {
use perl_corpus::r#gen::glob::glob_in_context;
for i in 0..count {
let value = glob_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (glob)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Tie => {
use perl_corpus::r#gen::tie::tie_in_context;
for i in 0..count {
let value = tie_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (tie)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Io => {
use perl_corpus::r#gen::io::io_in_context;
for i in 0..count {
let value = io_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (io)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Filetest => {
use perl_corpus::r#gen::filetest::filetest_in_context;
for i in 0..count {
let value = filetest_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (filetest)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Builtins => {
use perl_corpus::r#gen::builtins::builtin_in_context;
for i in 0..count {
let value = builtin_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (builtins)", i + 1);
println!("{}", value);
println!();
}
}
Generator::ListOps => {
use perl_corpus::r#gen::list_ops::list_op_in_context;
for i in 0..count {
let value = list_op_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (list-ops)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Declarations => {
use perl_corpus::r#gen::declarations::declaration_in_context;
for i in 0..count {
let value = declaration_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (declarations)", i + 1);
println!("{}", value);
println!();
}
}
Generator::ObjectOriented => {
use perl_corpus::r#gen::object_oriented::object_oriented_in_context;
for i in 0..count {
let value = object_oriented_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (object-oriented)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Expressions => {
use perl_corpus::r#gen::expressions::expression_in_context;
for i in 0..count {
let value = expression_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (expressions)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Regex => {
use perl_corpus::r#gen::regex::regex_in_context;
for i in 0..count {
let value = regex_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (regex)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Ambiguity => {
use perl_corpus::r#gen::ambiguity::ambiguity_in_context;
for i in 0..count {
let value = ambiguity_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (ambiguity)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Sigils => {
use perl_corpus::r#gen::sigils::sigil_in_context;
for i in 0..count {
let value = sigil_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (sigils)", i + 1);
println!("{}", value);
println!();
}
}
Generator::Phasers => {
use perl_corpus::r#gen::phasers::phaser_block;
for i in 0..count {
let value = phaser_block()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (phasers)", i + 1);
println!("{}", value);
println!();
}
}
Generator::SpecialVars => {
use perl_corpus::r#gen::special_vars::special_vars_in_context;
for i in 0..count {
let value = special_vars_in_context()
.new_tree(&mut runner)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.current();
println!("# Test case {} (special-vars)", i + 1);
println!("{}", value);
println!();
}
}
}
}
Command::Inventory { format, out } => {
if format != "json" {
anyhow::bail!("unsupported format '{format}', expected 'json'");
}
let paths = CorpusPaths::from_root(std::env::current_dir()?);
let inventory = build_inventory_from_paths(&paths)?;
let json = serde_json::to_string_pretty(&inventory)?;
if let Some(path) = out {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(&path, format!("{json}\n"))?;
println!("✅ Wrote inventory report to {}", path.display());
} else {
println!("{json}");
}
}
}
Ok(())
}