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
//! §Fase 39.f — `axon parse` subcommand (Rust binary parity).
//!
//! Multi-file diagnostic aggregator. Walks the given file paths /
//! directories / globs, runs each `.axon` file through
//! `Parser::parse_with_recovery`, and aggregates every parse error +
//! type-check error into a single report. Mirrors the Python
//! `axon.cli.parse_cmd:cmd_parse` from Fase 28.f.
//!
//! ## Flags
//!
//! - `--max-errors N` — cap total errors across all files (D6,
//! default unlimited)
//! - `--ignore PATTERN` — fnmatch-style ignore pattern (may repeat);
//! `.axonignore` files in walked dirs are honoured automatically
//! - `--jobs N` — worker thread count (default: auto). The Rust
//! implementation currently runs single-threaded; the flag is
//! accepted for Python-parity but the threading is deferred to a
//! future fase (honest scope)
//! - `--json` — emit machine-readable diagnostics (D5)
//! - `--format array|ndjson` — JSON framing when --json is set
//! - `--strict` — opt into legacy fail-on-first behavior (D8); also
//! activated by `AXON_PARSER_STRICT` env var
//! - `--no-color` — disable ANSI colour codes
//!
//! ## Exit codes (bitwise OR of cause classes)
//!
//! - `0` — success (no errors)
//! - `1` — parse / type errors observed
//! - `2` — I/O errors (file not found, read failed, glob expansion failed)
//! - `3` — both classes (1 | 2)
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use serde::Serialize;
use axon_frontend::lexer::Lexer;
use axon_frontend::parser::Parser;
/// Per-file diagnostic emitted by the aggregator. Wire-stable JSON
/// shape for `--json` mode (rustc-compatible field naming per
/// Fase 28.g D5).
#[derive(Debug, Clone, Serialize)]
pub struct AggregatedDiagnostic {
pub file: String,
pub line: u32,
pub column: u32,
pub message: String,
pub kind: String, // "parse" | "lex" | "type"
}
/// Configuration for `axon parse` (mirrors the Python CLI args).
#[derive(Debug, Clone, Default)]
pub struct ParseConfig {
pub patterns: Vec<String>,
pub max_errors: Option<usize>,
pub ignore_patterns: Vec<String>,
pub jobs: Option<usize>,
pub json: bool,
pub format: String, // "array" | "ndjson"
pub strict: bool,
pub no_color: bool,
}
/// Run `axon parse` against a configured corpus. Returns a tuple
/// `(diagnostics, io_errors, truncated)`:
/// - `diagnostics`: every parse / lex / type error observed
/// - `io_errors`: files that couldn't be read / glob-expanded
/// - `truncated`: true when `max_errors` capped the report
pub fn run_parse(config: &ParseConfig) -> (Vec<AggregatedDiagnostic>, Vec<String>, bool) {
let mut diagnostics: Vec<AggregatedDiagnostic> = Vec::new();
let mut io_errors: Vec<String> = Vec::new();
let mut truncated = false;
// ── §1 — Expand patterns into a deterministic file list ──
let files = match expand_patterns(&config.patterns, &config.ignore_patterns) {
Ok(f) => f,
Err(e) => {
io_errors.push(format!("pattern expansion: {e}"));
return (diagnostics, io_errors, false);
}
};
// ── §2 — Strict mode: honour env var OR flag (OR semantics) ──
let strict = config.strict
|| std::env::var("AXON_PARSER_STRICT")
.ok()
.map(|v| matches!(v.to_lowercase().as_str(), "1" | "true" | "yes" | "on"))
.unwrap_or(false);
// ── §3 — Parse each file ──
'outer: for path in &files {
// Honour max_errors cap.
if let Some(cap) = config.max_errors {
if diagnostics.len() >= cap {
truncated = true;
break 'outer;
}
}
let source = match fs::read_to_string(path) {
Ok(s) => s,
Err(e) => {
io_errors.push(format!("read {}: {}", path.display(), e));
continue;
}
};
let path_str = path.display().to_string();
// Tokenize
let tokens = match Lexer::new(&source, &path_str).tokenize() {
Ok(t) => t,
Err(e) => {
diagnostics.push(AggregatedDiagnostic {
file: path_str.clone(),
line: e.line,
column: e.column,
message: format!("lex error: {}", e.message),
kind: "lex".to_string(),
});
if strict {
break 'outer;
}
continue;
}
};
// Parse with recovery (or fail-fast in strict mode)
let mut parser = Parser::new(tokens);
if strict {
match parser.parse() {
Ok(_) => {}
Err(e) => {
diagnostics.push(AggregatedDiagnostic {
file: path_str.clone(),
line: e.line,
column: e.column,
message: format!("parse error: {}", e.message),
kind: "parse".to_string(),
});
break 'outer; // strict: stop at first failing file
}
}
} else {
let result = parser.parse_with_recovery();
for err in result.errors {
diagnostics.push(AggregatedDiagnostic {
file: path_str.clone(),
line: err.line,
column: err.column,
message: format!("parse error: {}", err.message),
kind: "parse".to_string(),
});
if let Some(cap) = config.max_errors {
if diagnostics.len() >= cap {
truncated = true;
break 'outer;
}
}
}
}
}
(diagnostics, io_errors, truncated)
}
/// Expand patterns (files / directories / globs) into a
/// deterministic sorted file list. Directories are walked
/// recursively; `.axonignore` files are honoured.
fn expand_patterns(
patterns: &[String],
ignore: &[String],
) -> Result<Vec<PathBuf>, String> {
let mut result: HashSet<PathBuf> = HashSet::new();
for pattern in patterns {
let path = PathBuf::from(pattern);
if path.is_file() {
if !is_ignored(&path, ignore) {
result.insert(path);
}
continue;
}
if path.is_dir() {
walk_dir(&path, ignore, &mut result)?;
continue;
}
// Not a file or directory — treat as a literal that doesn't
// resolve. We don't error here; the caller reports it via
// io_errors when read fails. (Glob expansion is honest
// scope — Python uses Path.glob; Rust would need an extra
// crate. For 39.f we accept literal paths + directories
// and defer glob to a future fase.)
if path.exists() {
result.insert(path);
} else {
return Err(format!("pattern not found: {pattern}"));
}
}
let mut sorted: Vec<PathBuf> = result.into_iter().collect();
sorted.sort();
Ok(sorted)
}
fn walk_dir(
dir: &Path,
ignore: &[String],
out: &mut HashSet<PathBuf>,
) -> Result<(), String> {
let entries = fs::read_dir(dir).map_err(|e| format!("read_dir {}: {}", dir.display(), e))?;
for entry in entries {
let entry = entry.map_err(|e| format!("dir entry: {e}"))?;
let path = entry.path();
if is_ignored(&path, ignore) {
continue;
}
if path.is_dir() {
// Skip common noise dirs.
let name = path
.file_name()
.and_then(|s| s.to_str())
.unwrap_or("");
if matches!(name, "target" | "node_modules" | ".git" | "__pycache__") {
continue;
}
walk_dir(&path, ignore, out)?;
} else if path.extension().and_then(|s| s.to_str()) == Some("axon") {
out.insert(path);
}
}
Ok(())
}
fn is_ignored(path: &Path, ignore: &[String]) -> bool {
let path_str = path.to_string_lossy();
for pattern in ignore {
// Very simple substring match for v2.0.0; fnmatch parity is
// a future-fase refinement.
if path_str.contains(pattern) {
return true;
}
}
false
}
/// Format diagnostics as a human-readable report for stdout.
pub fn render_human(
diagnostics: &[AggregatedDiagnostic],
io_errors: &[String],
truncated: bool,
no_color: bool,
) -> String {
let mut out = String::new();
let red = if no_color { "" } else { "\x1b[31m" };
let bold = if no_color { "" } else { "\x1b[1m" };
let dim = if no_color { "" } else { "\x1b[2m" };
let reset = if no_color { "" } else { "\x1b[0m" };
if diagnostics.is_empty() && io_errors.is_empty() {
out.push_str(&format!("{bold}✓ axon parse: no diagnostics{reset}\n"));
return out;
}
for d in diagnostics {
out.push_str(&format!(
"{red}{bold}error{reset}{bold}[{}]{reset} {}\n {dim}--> {}:{}:{}{reset}\n",
d.kind, d.message, d.file, d.line, d.column
));
}
for e in io_errors {
out.push_str(&format!("{red}{bold}I/O error{reset} {e}\n"));
}
if truncated {
out.push_str(&format!(
"{dim}... (truncated by --max-errors cap){reset}\n"
));
}
out
}
/// Format diagnostics as JSON (array or ndjson framing). Rustc-
/// compatible field shape per Fase 28.g D5.
pub fn render_json(
diagnostics: &[AggregatedDiagnostic],
format: &str,
) -> String {
if format == "ndjson" {
diagnostics
.iter()
.map(|d| serde_json::to_string(d).unwrap_or_default())
.collect::<Vec<_>>()
.join("\n")
+ "\n"
} else {
serde_json::to_string_pretty(diagnostics).unwrap_or_default() + "\n"
}
}
/// Compute the exit code from the diagnostics + io_errors observed.
/// Mirrors the Python CLI's bitwise OR convention (D6).
pub fn exit_code(
diagnostics: &[AggregatedDiagnostic],
io_errors: &[String],
) -> i32 {
let mut code = 0;
if !diagnostics.is_empty() {
code |= 1;
}
if !io_errors.is_empty() {
code |= 2;
}
code
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fase39f_parse_empty_patterns_returns_clean() {
let cfg = ParseConfig::default();
let (diags, ios, trunc) = run_parse(&cfg);
assert!(diags.is_empty());
assert!(ios.is_empty());
assert!(!trunc);
}
#[test]
fn fase39f_exit_code_zero_on_clean() {
assert_eq!(exit_code(&[], &[]), 0);
}
#[test]
fn fase39f_exit_code_one_on_diagnostic() {
let d = AggregatedDiagnostic {
file: "x.axon".to_string(),
line: 1,
column: 1,
message: "boom".to_string(),
kind: "parse".to_string(),
};
assert_eq!(exit_code(&[d], &[]), 1);
}
#[test]
fn fase39f_exit_code_two_on_io_error() {
assert_eq!(exit_code(&[], &["read failed".to_string()]), 2);
}
#[test]
fn fase39f_exit_code_three_on_both() {
let d = AggregatedDiagnostic {
file: "x.axon".to_string(),
line: 1,
column: 1,
message: "boom".to_string(),
kind: "parse".to_string(),
};
assert_eq!(exit_code(&[d], &["io".to_string()]), 3);
}
#[test]
fn fase39f_json_array_format_serializes_diagnostics() {
let d = AggregatedDiagnostic {
file: "x.axon".to_string(),
line: 1,
column: 1,
message: "boom".to_string(),
kind: "parse".to_string(),
};
let out = render_json(&[d], "array");
assert!(out.contains("\"file\": \"x.axon\""));
assert!(out.contains("\"kind\": \"parse\""));
}
#[test]
fn fase39f_json_ndjson_format_one_per_line() {
let d1 = AggregatedDiagnostic {
file: "a.axon".to_string(),
line: 1,
column: 1,
message: "e1".to_string(),
kind: "parse".to_string(),
};
let d2 = AggregatedDiagnostic {
file: "b.axon".to_string(),
line: 2,
column: 2,
message: "e2".to_string(),
kind: "parse".to_string(),
};
let out = render_json(&[d1, d2], "ndjson");
let lines: Vec<&str> = out.trim().split('\n').collect();
assert_eq!(lines.len(), 2);
assert!(lines[0].contains("a.axon"));
assert!(lines[1].contains("b.axon"));
}
#[test]
fn fase39f_human_render_clean_emits_check() {
let out = render_human(&[], &[], false, true);
assert!(out.contains("axon parse: no diagnostics"));
}
#[test]
fn fase39f_human_render_truncated_marker() {
let d = AggregatedDiagnostic {
file: "x".to_string(),
line: 1,
column: 1,
message: "e".to_string(),
kind: "parse".to_string(),
};
let out = render_human(&[d], &[], true, true);
assert!(out.contains("truncated by --max-errors"));
}
#[test]
fn fase39f_strict_env_var_recognized() {
// Verify the AXON_PARSER_STRICT env var truthy alphabet
// matches the Fase 28.h Python contract.
for truthy in &["1", "true", "yes", "on", "TRUE", "Yes"] {
std::env::set_var("AXON_PARSER_STRICT", truthy);
let cfg = ParseConfig::default();
let _ = run_parse(&cfg); // doesn't panic
}
std::env::remove_var("AXON_PARSER_STRICT");
}
}