blue-lang-cli 0.0.56

The blue command line: run, fmt, ast, erase, check.
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
//! How `blue check` shows what the check stage found: text for a person,
//! JSON Lines for a tool, and `--fix` for the machine-applicable repairs.
//!
//! ## `--format json`: one object per line, per diagnostic
//!
//! The field names are a stable interface; `docs/DIAGNOSTICS.md` documents
//! each one and `tests/check_json.rs` pins the exact output for a fixture, so
//! a rename is a red test rather than a silent break in someone's tool.
//!
//! ```json
//! {"code":"B0001","slug":"unbound-name","severity":"error",
//!  "message":"unbound name `lenght`","file":"f.b",
//!  "line":2,"column":3,"end_line":2,"end_column":9,"byte_start":12,"byte_end":18,
//!  "help":"did you mean `length` (builtin)?","related":[],
//!  "fixes":[{"message":"replace with `length` (builtin)","applicability":"maybe-incorrect",
//!            "edits":[{"file":"f.b","line":2,"column":3,"end_line":2,"end_column":9,
//!                      "byte_start":12,"byte_end":18,"original":"lenght","replacement":"length"}]}],
//!  "waiver":null}
//! ```

use std::path::Path;

use blue_lang_check::{Applicability, Diagnostic, Outcome};
use blue_lang_runtime::uses::{ResolvedProgram, SourceFile};
use blue_lang_syntax::Span;
use serde::Serialize;

/// A position range in one file. Lines and columns are 1-based; columns count
/// characters, not bytes. `byte_start`/`byte_end` are the half-open byte range.
#[derive(Serialize)]
pub struct Location {
    pub file: Option<String>,
    pub line: Option<usize>,
    pub column: Option<usize>,
    pub end_line: Option<usize>,
    pub end_column: Option<usize>,
    pub byte_start: Option<usize>,
    pub byte_end: Option<usize>,
}

impl Location {
    fn of(file: Option<&SourceFile>, span: Span) -> Self {
        let name = file.map(|f| {
            f.path
                .as_ref()
                .map_or_else(|| "<anonymous>".to_string(), |p| p.display().to_string())
        });
        let text = file.map(|f| f.text.as_str());
        match text {
            Some(t) if !span.is_synthetic() && span.end <= t.len() => {
                let (line, column) = Span::line_col(t, span.start);
                let (end_line, end_column) = Span::line_col(t, span.end);
                Self {
                    file: name,
                    line: Some(line),
                    column: Some(column),
                    end_line: Some(end_line),
                    end_column: Some(end_column),
                    byte_start: Some(span.start),
                    byte_end: Some(span.end),
                }
            }
            _ => Self {
                file: name,
                line: None,
                column: None,
                end_line: None,
                end_column: None,
                byte_start: None,
                byte_end: None,
            },
        }
    }
}

#[derive(Serialize)]
pub struct JsonRelated {
    #[serde(flatten)]
    pub at: Location,
    pub message: String,
}

#[derive(Serialize)]
pub struct JsonEdit {
    #[serde(flatten)]
    pub at: Location,
    pub original: String,
    pub replacement: String,
}

#[derive(Serialize)]
pub struct JsonFix {
    pub message: String,
    pub applicability: &'static str,
    pub edits: Vec<JsonEdit>,
}

#[derive(Serialize)]
pub struct JsonWaiver {
    pub reason: String,
    pub line: Option<usize>,
}

/// One line of `blue check --format json`.
#[derive(Serialize)]
pub struct JsonDiagnostic {
    pub code: &'static str,
    pub slug: &'static str,
    pub severity: &'static str,
    pub message: String,
    #[serde(flatten)]
    pub at: Location,
    pub help: Option<String>,
    pub related: Vec<JsonRelated>,
    pub fixes: Vec<JsonFix>,
    /// Set when an in-source waiver suppressed this diagnostic.
    pub waiver: Option<JsonWaiver>,
}

fn file_of(program: &ResolvedProgram, top_level: usize) -> Option<&SourceFile> {
    program.owner_of(top_level).and_then(|id| program.file(id))
}

/// A diagnostic as its JSON object.
#[must_use]
pub fn to_json(
    program: &ResolvedProgram,
    d: &Diagnostic,
    waiver: Option<JsonWaiver>,
) -> JsonDiagnostic {
    let file = file_of(program, d.top_level);
    let rule = d.code.rule();
    JsonDiagnostic {
        code: d.code.as_str(),
        slug: rule.slug,
        severity: d.severity.label(),
        message: d.message.clone(),
        at: Location::of(file, d.span),
        help: d.help.clone(),
        related: d
            .related
            .iter()
            .map(|r| JsonRelated {
                at: Location::of(file, r.span),
                message: r.message.clone(),
            })
            .collect(),
        fixes: d
            .fixes
            .iter()
            .map(|f| JsonFix {
                message: f.message.clone(),
                applicability: f.applicability.label(),
                edits: f
                    .edits
                    .iter()
                    .map(|e| JsonEdit {
                        at: Location::of(file, e.span),
                        original: e.original.clone(),
                        replacement: e.replacement.clone(),
                    })
                    .collect(),
            })
            .collect(),
        waiver,
    }
}

/// Every diagnostic in `outcome` — reported and waived — as JSON Lines.
///
/// # Errors
///
/// Serialisation cannot fail for these types; the `Result` is serde's.
pub fn json_lines(program: &ResolvedProgram, outcome: &Outcome) -> serde_json::Result<String> {
    let mut out = String::new();
    for d in &outcome.diagnostics {
        out.push_str(&serde_json::to_string(&to_json(program, d, None))?);
        out.push('\n');
    }
    for w in &outcome.waived {
        let file = file_of(program, w.waiver.top_level);
        let line = file.map(|f| Span::line_col(&f.text, w.waiver.span.start).0);
        let waiver = JsonWaiver {
            reason: w.waiver.reason.clone(),
            line,
        };
        out.push_str(&serde_json::to_string(&to_json(
            program,
            &w.diagnostic,
            Some(waiver),
        ))?);
        out.push('\n');
    }
    Ok(out)
}

/// A syntax error, which has no resolved program behind it, as JSON.
///
/// # Errors
///
/// As [`json_lines`].
pub fn syntax_json(path: &Path, text: &str, d: &Diagnostic) -> serde_json::Result<String> {
    let file = SourceFile {
        id: ResolvedProgram::ENTRY,
        path: Some(path.to_path_buf()),
        package: None,
        text: text.to_string(),
    };
    let rule = d.code.rule();
    let j = JsonDiagnostic {
        code: d.code.as_str(),
        slug: rule.slug,
        severity: d.severity.label(),
        message: d.message.clone(),
        at: Location::of(Some(&file), d.span),
        help: d.help.clone(),
        related: Vec::new(),
        fixes: Vec::new(),
        waiver: None,
    };
    let mut s = serde_json::to_string(&j)?;
    s.push('\n');
    Ok(s)
}

/// Apply every MACHINE-APPLICABLE fix whose edits land in the entry file.
///
/// An edit applies only when its span still holds `original`, and only when it
/// overlaps no edit already taken; the rest are skipped, never forced. Returns
/// the rewritten text and the number of fixes applied.
#[must_use]
pub fn apply_machine_fixes(
    program: &ResolvedProgram,
    outcome: &Outcome,
    text: &str,
) -> (String, usize) {
    let mut edits: Vec<(Span, &str)> = Vec::new();
    let mut applied = 0;
    for d in &outcome.diagnostics {
        if program.owner_of(d.top_level) != Some(ResolvedProgram::ENTRY) {
            continue;
        }
        for fix in d
            .fixes
            .iter()
            .filter(|f| f.applicability == Applicability::MachineApplicable)
        {
            let fits = fix.edits.iter().all(|e| {
                text.get(e.span.start..e.span.end) == Some(e.original.as_str())
                    && !edits
                        .iter()
                        .any(|(s, _)| e.span.start < s.end && s.start < e.span.end)
            });
            if !fits {
                continue;
            }
            edits.extend(fix.edits.iter().map(|e| (e.span, e.replacement.as_str())));
            applied += 1;
            // One fix per diagnostic: the best one.
            break;
        }
    }
    edits.sort_by_key(|(s, _)| std::cmp::Reverse(s.start));
    let mut out = text.to_string();
    for (span, replacement) in edits {
        out.replace_range(span.start..span.end, replacement);
    }
    (out, applied)
}

// ---- `blue ast --resolved --json` -------------------------------------------

/// What one reference binds to, as `ast --resolved --json` prints it.
#[derive(Serialize)]
struct TargetJson {
    /// `local`, `def`, `builtin` or `unbound`.
    kind: &'static str,
    /// The defining bidama for a `def`; `null` for the root namespace and
    /// every other kind.
    namespace: Option<String>,
    name: Option<String>,
    /// The symbol the resolved tree writes: `retsu/first`, `count`.
    key: Option<String>,
}

fn target_json(t: &blue_lang_check::names::Target) -> TargetJson {
    use blue_lang_check::names::Target;
    use blue_lang_check::Namespace;
    let key = t.resolved_symbol();
    match t {
        Target::Local => TargetJson {
            kind: "local",
            namespace: None,
            name: None,
            key,
        },
        Target::Def(ns, n) => TargetJson {
            kind: "def",
            namespace: match ns {
                Namespace::Bidama(p) => Some(p.clone()),
                _ => None,
            },
            name: Some(n.clone()),
            key,
        },
        Target::Builtin(n) => TargetJson {
            kind: "builtin",
            namespace: None,
            name: Some(n.clone()),
            key,
        },
        Target::Ambiguous(_) => TargetJson {
            kind: "ambiguous",
            namespace: None,
            name: None,
            key,
        },
        Target::Unbound => TargetJson {
            kind: "unbound",
            namespace: None,
            name: None,
            key,
        },
    }
}

#[derive(Serialize)]
struct ReferenceJson {
    #[serde(flatten)]
    at: Location,
    top_level: usize,
    written: String,
    opaque: bool,
    flat: TargetJson,
    ns: TargetJson,
    /// The locals in scope where it is written, innermost first.
    locals: Vec<String>,
}

#[derive(Serialize)]
struct ImportJson {
    #[serde(flatten)]
    at: Location,
    package: String,
    /// The names listed for bare use; empty for a whole-package `use`.
    names: Vec<String>,
}

#[derive(Serialize)]
struct ResolvedJson {
    /// The entry file's bidama, or `null` for the root namespace.
    namespace: Option<String>,
    /// The entry file's top-level forms, resolved under the flat rule.
    flat: Vec<String>,
    /// The same forms under per-bidama namespaces.
    ns: Vec<String>,
    /// Every non-local reference in the entry file.
    references: Vec<ReferenceJson>,
    /// The entry file's `use` declarations.
    imports: Vec<ImportJson>,
    /// The first line of the entry file's first top-level form, `null` for
    /// a file with none: where a first `use` goes.
    first_line: Option<usize>,
    /// The entry file's top-level definitions: name and where the name is.
    definitions: Vec<DefinitionJson>,
    /// Every name the interpreter binds, sorted: what a bare name falls to.
    builtins: Vec<String>,
    /// The reserved words, which no definition can be named.
    reserved: Vec<&'static str>,
}

#[derive(Serialize)]
struct DefinitionJson {
    #[serde(flatten)]
    at: Location,
    name: String,
}

/// The entry file's resolution, as one JSON object.
///
/// # Errors
///
/// Serialization only.
pub fn resolved_json(
    checked: &blue_lang_runtime::pipeline::Checked,
    resolved: &blue_lang_check::names::Resolved,
) -> Result<String, serde_json::Error> {
    use blue_lang_check::names::Rule;
    let program = &checked.program;
    let entry = |i: usize| program.owner_of(i) == Some(ResolvedProgram::ENTRY);
    let tree = |rule| -> Vec<String> {
        resolved
            .resolved_tree(program.forms(), rule)
            .iter()
            .enumerate()
            .filter(|(i, _)| entry(*i))
            .map(|(_, f)| f.to_sexp().to_string())
            .collect()
    };
    let file = program.file(ResolvedProgram::ENTRY);
    let out = ResolvedJson {
        namespace: file.and_then(|f| f.package.clone()),
        flat: tree(Rule::Flat),
        ns: tree(Rule::Namespaced),
        references: resolved
            .references
            .iter()
            .filter(|r| entry(r.top_level))
            .map(|r| ReferenceJson {
                at: Location::of(file, r.span),
                top_level: r.top_level,
                written: r.written.clone(),
                opaque: r.opaque,
                flat: target_json(&r.flat),
                ns: target_json(&r.ns),
                locals: r.locals.clone(),
            })
            .collect(),
        imports: program
            .imports()
            .iter()
            .filter(|(f, _)| *f == ResolvedProgram::ENTRY)
            .map(|(_, u)| ImportJson {
                at: Location::of(file, u.span),
                package: u.package.clone(),
                names: u.names.iter().map(|(n, _)| n.clone()).collect(),
            })
            .collect(),
        first_line: program
            .forms()
            .iter()
            .enumerate()
            .find(|(i, _)| entry(*i))
            .and_then(|(_, f)| Location::of(file, f.span).line),
        definitions: program
            .forms()
            .iter()
            .enumerate()
            .filter(|(i, _)| entry(*i))
            .flat_map(|(_, f)| blue_lang_check::names::definitions_of(f))
            .map(|(name, span, _)| DefinitionJson {
                at: Location::of(file, span),
                name,
            })
            .collect(),
        builtins: {
            let mut b: Vec<String> = checked
                .names
                .scopes()
                .iter()
                .filter(|s| {
                    !matches!(
                        s.namespace,
                        blue_lang_check::Namespace::File(_) | blue_lang_check::Namespace::Bidama(_)
                    )
                })
                .flat_map(|s| s.bindings().map(|b| b.name.clone()))
                .collect();
            b.sort();
            b.dedup();
            b
        },
        reserved: blue_lang_syntax::SURFACE_KEYWORDS
            .iter()
            .chain(blue_lang_syntax::BLOCK_KEYWORDS)
            .copied()
            .collect(),
    };
    serde_json::to_string(&out)
}

// ---- cross-tier overrides ----------------------------------------------------

fn tier_word(t: blue_lang_check::names::Tier) -> &'static str {
    use blue_lang_check::names::Tier;
    match t {
        Tier::Local => "local",
        Tier::Own => "own",
        Tier::Imported => "imported",
        Tier::Builtin => "builtin",
    }
}

fn namespace_word(n: &blue_lang_check::Namespace) -> String {
    use blue_lang_check::Namespace;
    match n {
        Namespace::Bidama(p) => p.clone(),
        Namespace::File(_) => "this file".to_string(),
        other => other.to_string(),
    }
}

#[derive(Serialize)]
struct ShadowedJson {
    tier: &'static str,
    namespace: String,
}

#[derive(Serialize)]
struct OverrideJson {
    kind: &'static str,
    name: String,
    #[serde(flatten)]
    at: Location,
    tier: &'static str,
    namespace: String,
    shadowed: Vec<ShadowedJson>,
}

/// One JSON line per cross-tier override in the entry file: a bare name the
/// resolution order bound in a higher tier while a lower tier also had it.
/// Not a diagnostic — each tier is something the author wrote — and recorded
/// so a tool can see every place the order decided.
///
/// # Errors
///
/// Serialization only.
pub fn overrides_json(
    checked: &blue_lang_runtime::pipeline::Checked,
    resolved: &blue_lang_check::names::Resolved,
) -> Result<String, serde_json::Error> {
    use blue_lang_check::names::Target;
    let program = &checked.program;
    let file = program.file(ResolvedProgram::ENTRY);
    let mut out = String::new();
    let mut seen = std::collections::BTreeSet::new();
    for r in &resolved.references {
        if r.shadowed.is_empty() || program.owner_of(r.top_level) != Some(ResolvedProgram::ENTRY) {
            continue;
        }
        if !seen.insert((r.span.start, r.span.end, r.written.clone())) {
            continue;
        }
        let Target::Def(ns, _) = &r.ns else { continue };
        let (tier, namespace) = (tier_of_winner(checked, r), namespace_word(ns));
        let line = OverrideJson {
            kind: "override",
            name: r.written.clone(),
            at: Location::of(file, r.span),
            tier,
            namespace,
            shadowed: r
                .shadowed
                .iter()
                .map(|(t, n)| ShadowedJson {
                    tier: tier_word(*t),
                    namespace: namespace_word(n),
                })
                .collect(),
        };
        out.push_str(&serde_json::to_string(&line)?);
        out.push('\n');
    }
    Ok(out)
}

/// The tier a reference's namespaced binding came from: the first non-empty
/// tier of its resolution path.
fn tier_of_winner(
    checked: &blue_lang_runtime::pipeline::Checked,
    r: &blue_lang_check::names::Reference,
) -> &'static str {
    let own = blue_lang_runtime::pipeline::namespace_of(&checked.program, r.top_level);
    checked
        .names
        .tiers_of(&r.written, &own, r.top_level)
        .into_iter()
        .find(|(_, nss)| !nss.is_empty())
        .map_or("builtin", |(t, _)| tier_word(t))
}