meta-language 1.0.0

A self-describing links-network core for lossless language representation
Documentation
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! Project-aware semantics: resolves an entry program's module requests to the
//! project's source files, reads the project manifests, and links the entry's
//! imports, references, attributes, macros, notations and tactics to the
//! declarations they name in other files. Every symbol is identified as
//! `<file>#<qualified name>`. The JavaScript runtime implements the same
//! analysis in js/src/program-project.js.

mod javascript;
mod lean;
mod rocq;
mod rust_lang;
mod tree;

use std::collections::BTreeMap;
use std::rc::Rc;

use super::analysis::syntax_facts;
use super::{ProgramBinding, ProgramDiagnostic, ProgramFact, ProgramRange};
use crate::{LinkNetwork, ParseConfiguration};
use tree::{SyntaxTree, Target};

const TYPE_KINDS: [&str; 9] = [
    "class",
    "struct",
    "enum",
    "trait",
    "type",
    "structure",
    "inductive",
    "constructor",
    "universe",
];
const EFFECT_TRAITS: [&str; 4] = ["async", "generator", "io", "proof-state"];
const PROOF_KINDS: [&str; 3] = ["theorem", "lemma", "tactic"];
// Tactic steps, and the checked assertions of languages without proof terms.
const PROOF_ROLES: [&str; 3] = ["tactic", "assertion", "const-assertion"];

/// One project file by project-relative path.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProgramProjectSource {
    path: String,
    source: String,
}

impl ProgramProjectSource {
    /// Creates a project file.
    #[must_use]
    pub fn new(path: impl Into<String>, source: impl Into<String>) -> Self {
        Self {
            path: path.into(),
            source: source.into(),
        }
    }

    /// Project-relative path.
    #[must_use]
    pub fn path(&self) -> &str {
        &self.path
    }

    /// File contents.
    #[must_use]
    pub fn source(&self) -> &str {
        &self.source
    }
}

/// Project files and dependency names available during analysis.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ProgramProjectContext {
    root: String,
    files: Vec<String>,
    dependencies: Vec<String>,
    extensions: Vec<String>,
    entry: Option<String>,
    sources: Vec<ProgramProjectSource>,
}

impl ProgramProjectContext {
    /// Creates a project context.
    #[must_use]
    pub fn new(root: impl Into<String>, files: Vec<String>, dependencies: Vec<String>) -> Self {
        Self {
            root: root.into(),
            files,
            dependencies,
            extensions: Vec::new(),
            entry: None,
            sources: Vec::new(),
        }
    }

    /// Adds project-defined syntax extensions.
    #[must_use]
    pub fn with_extensions(mut self, extensions: Vec<String>) -> Self {
        self.extensions = extensions;
        self
    }

    /// Names the analyzed program's path within the project and the project's
    /// files (manifests and modules), enabling project-aware semantics.
    #[must_use]
    pub fn with_entry(
        mut self,
        entry: impl Into<String>,
        sources: Vec<ProgramProjectSource>,
    ) -> Self {
        self.entry = Some(entry.into());
        self.sources = sources;
        self
    }

    /// Project root.
    #[must_use]
    pub fn root(&self) -> &str {
        &self.root
    }

    /// Project files.
    #[must_use]
    pub fn files(&self) -> &[String] {
        &self.files
    }

    /// Declared dependencies.
    #[must_use]
    pub fn dependencies(&self) -> &[String] {
        &self.dependencies
    }

    /// Project-defined syntax extensions.
    #[must_use]
    pub fn extensions(&self) -> &[String] {
        &self.extensions
    }

    /// The analyzed program's path within the project.
    #[must_use]
    pub fn entry(&self) -> Option<&str> {
        self.entry.as_deref()
    }

    /// The project's files by project-relative path.
    #[must_use]
    pub fn sources(&self) -> &[ProgramProjectSource] {
        &self.sources
    }
}

/// An entry module request resolved to a project file.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProgramProjectModule {
    request: String,
    module: String,
    file: String,
    range: ProgramRange,
}

impl ProgramProjectModule {
    /// The request as written.
    #[must_use]
    pub fn request(&self) -> &str {
        &self.request
    }

    /// The resolved module.
    #[must_use]
    pub fn module(&self) -> &str {
        &self.module
    }

    /// The project file defining the module.
    #[must_use]
    pub fn file(&self) -> &str {
        &self.file
    }

    /// Entry range of the request.
    #[must_use]
    pub const fn range(&self) -> ProgramRange {
        self.range
    }
}

/// An entry range linked to the declaration it names, identified as
/// `<file>#<qualified name>`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProgramProjectReference {
    role: &'static str,
    name: String,
    range: ProgramRange,
    symbol: String,
    target_kind: String,
    traits: Vec<String>,
    file: String,
    declaration: ProgramRange,
}

impl ProgramProjectReference {
    /// How the entry uses the declaration: `import`, `reference`, `attribute`,
    /// `macro`, `notation`, `template-tag`, `tactic`, `assertion` or
    /// `const-assertion`.
    #[must_use]
    pub const fn role(&self) -> &'static str {
        self.role
    }

    /// The name as written.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Entry range of the use.
    #[must_use]
    pub const fn range(&self) -> ProgramRange {
        self.range
    }

    /// Declaration identity.
    #[must_use]
    pub fn symbol(&self) -> &str {
        &self.symbol
    }

    /// Declaration kind.
    #[must_use]
    pub fn target_kind(&self) -> &str {
        &self.target_kind
    }

    /// Declaration traits (`recursive`, `async`, `const`, `io`, ...).
    #[must_use]
    pub fn traits(&self) -> &[String] {
        &self.traits
    }

    /// The project file declaring the target.
    #[must_use]
    pub fn file(&self) -> &str {
        &self.file
    }

    /// Declaration range within its file.
    #[must_use]
    pub const fn declaration(&self) -> ProgramRange {
        self.declaration
    }
}

/// A macro, notation or tagged template use with the source it expands to.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProgramExpansion {
    name: String,
    kind: &'static str,
    range: ProgramRange,
    expansion: String,
    target: String,
}

impl ProgramExpansion {
    /// The macro, notation or tag name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// `macro-rules`, `notation` or `tagged-template`.
    #[must_use]
    pub const fn kind(&self) -> &'static str {
        self.kind
    }

    /// Entry range of the use.
    #[must_use]
    pub const fn range(&self) -> ProgramRange {
        self.range
    }

    /// Expanded source.
    #[must_use]
    pub fn expansion(&self) -> &str {
        &self.expansion
    }

    /// Symbol of the expanded definition.
    #[must_use]
    pub fn target(&self) -> &str {
        &self.target
    }
}

/// Results of the project-aware analysis.
#[derive(Clone, Debug, Default)]
pub(super) struct ProjectResult {
    pub(super) modules: Vec<ProgramProjectModule>,
    pub(super) facts: Vec<ProgramFact>,
    pub(super) references: Vec<ProgramProjectReference>,
    pub(super) expansions: Vec<ProgramExpansion>,
    pub(super) diagnostics: Vec<ProgramDiagnostic>,
    /// Module requests the language-neutral request scan does not see.
    pub(super) requests: Vec<ProgramFact>,
    /// Requests resolved to project files, as (name, start).
    pub(super) resolved_requests: Vec<(String, usize)>,
}

/// Analyzes an entry program within its project. Returns empty results
/// unless the project names an entry file.
pub(super) fn project_semantics(
    language: &'static str,
    source: &str,
    mappings: &[super::ProgramSourceMapping],
    bindings: &[ProgramBinding],
    unresolved: &[ProgramFact],
    project: &ProgramProjectContext,
) -> ProjectResult {
    let Some(entry) = project.entry().filter(|entry| !entry.is_empty()) else {
        return ProjectResult::default();
    };
    let mut context = ProjectContext::new(language, entry, source, mappings, project);
    context.bindings = bindings;
    context.unresolved = unresolved;
    match language {
        "JavaScript" => javascript::analyze(&mut context),
        "Rust" => rust_lang::analyze(&mut context),
        "Lean" => lean::analyze(&mut context),
        "Rocq" => rocq::analyze(&mut context),
        _ => {}
    }
    for file in &project.sources {
        if context
            .parsed
            .get(&file.path)
            .is_some_and(|parsed| !parsed.clean)
        {
            context.result.diagnostics.push(ProgramDiagnostic {
                kind: "project-parse-error",
                term: file.path.clone(),
                range: ProgramRange::default(),
            });
        }
    }
    context.result
}

/// A parsed project file.
#[derive(Debug)]
struct ParsedFile {
    path: String,
    source: String,
    tree: SyntaxTree,
    clean: bool,
}

struct ProjectContext<'a> {
    language: &'static str,
    bindings: &'a [ProgramBinding],
    unresolved: &'a [ProgramFact],
    entry: Rc<ParsedFile>,
    sources: BTreeMap<&'a str, &'a str>,
    parsed: BTreeMap<String, Rc<ParsedFile>>,
    result: ProjectResult,
}

impl<'a> ProjectContext<'a> {
    fn new(
        language: &'static str,
        entry: &str,
        source: &str,
        mappings: &[super::ProgramSourceMapping],
        project: &'a ProgramProjectContext,
    ) -> Self {
        let mut context = Self {
            language,
            bindings: &[],
            unresolved: &[],
            entry: Rc::new(ParsedFile {
                path: entry.to_string(),
                source: source.to_string(),
                tree: SyntaxTree::build(mappings, source),
                clean: true,
            }),
            sources: project
                .sources
                .iter()
                .map(|file| (file.path.as_str(), file.source.as_str()))
                .collect(),
            parsed: BTreeMap::new(),
            result: ProjectResult::default(),
        };
        for file in &project.sources {
            context.fact(
                "project-file",
                &file.path,
                &file.path,
                (0, file.source.len()),
            );
        }
        context
    }

    fn has(&self, path: &str) -> bool {
        path == self.entry.path || self.sources.contains_key(path)
    }

    fn text(&self, path: &str) -> Option<&str> {
        if path == self.entry.path {
            return Some(&self.entry.source);
        }
        self.sources.get(path).copied()
    }

    /// Parses a project file once; `None` when the project has no such file.
    fn load(&mut self, path: &str) -> Option<Rc<ParsedFile>> {
        self.load_as(path, self.language)
    }

    fn load_as(&mut self, path: &str, language: &str) -> Option<Rc<ParsedFile>> {
        if path == self.entry.path {
            return Some(Rc::clone(&self.entry));
        }
        let source = *self.sources.get(path)?;
        let parsed = self.parsed.entry(path.to_string()).or_insert_with(|| {
            let network = LinkNetwork::parse(source, language, ParseConfiguration::default());
            Rc::new(ParsedFile {
                path: path.to_string(),
                source: source.to_string(),
                tree: SyntaxTree::build(&syntax_facts(&network), source),
                clean: network.verify_full_match(None).is_clean(),
            })
        });
        Some(Rc::clone(parsed))
    }

    fn fact(&mut self, kind: &str, name: &str, file: &str, range: (usize, usize)) {
        let mut fact = ProgramFact::new(kind, name, ProgramRange::new(range.0, range.1));
        fact.file = Some(file.to_string());
        self.result.facts.push(fact);
    }

    fn module(&mut self, request: &str, module: &str, file: &str, range: (usize, usize)) {
        self.result.modules.push(ProgramProjectModule {
            request: request.to_string(),
            module: module.to_string(),
            file: file.to_string(),
            range: ProgramRange::new(range.0, range.1),
        });
        self.result
            .resolved_requests
            .push((request.to_string(), range.0));
    }

    /// A module request the language-neutral request scan does not see.
    fn request(&mut self, name: &str, range: (usize, usize)) {
        self.result.requests.push(ProgramFact::new(
            "module-import",
            name,
            ProgramRange::new(range.0, range.1),
        ));
    }

    /// Links an entry range to a declaration.
    fn reference(
        &mut self,
        role: &'static str,
        name: &str,
        range: (usize, usize),
        target: &Target,
    ) {
        self.result.references.push(ProgramProjectReference {
            role,
            name: name.to_string(),
            range: ProgramRange::new(range.0, range.1),
            symbol: target.symbol.clone(),
            target_kind: target.kind.clone(),
            traits: target.traits.clone(),
            file: target.file.clone(),
            declaration: ProgramRange::new(target.start, target.end),
        });
    }

    fn expansion(
        &mut self,
        name: &str,
        kind: &'static str,
        range: (usize, usize),
        expansion: String,
        target: &str,
    ) {
        self.result.expansions.push(ProgramExpansion {
            name: name.to_string(),
            kind,
            range: ProgramRange::new(range.0, range.1),
            expansion,
            target: target.to_string(),
        });
    }

    fn diagnose(&mut self, kind: &'static str, term: String, range: (usize, usize)) {
        self.result.diagnostics.push(ProgramDiagnostic {
            kind,
            term,
            range: ProgramRange::new(range.0, range.1),
        });
    }
}

/// Construct evidence contributed by the project analysis.
pub(super) fn project_evidence(
    project: &ProgramProjectContext,
    result: &ProjectResult,
    construct: &str,
) -> Vec<ProgramFact> {
    let entry = project.entry().unwrap_or_default();
    let located = |kind: String, name: &str, range: ProgramRange, file: &str| {
        let mut fact = ProgramFact::new(kind, name, range);
        fact.file = Some(file.to_string());
        fact
    };
    let references = |predicate: &dyn Fn(&ProgramProjectReference) -> bool| {
        result
            .references
            .iter()
            .filter(|reference| predicate(reference))
            .map(|reference| {
                located(
                    format!(
                        "project-reference:{}:{}",
                        reference.role, reference.target_kind
                    ),
                    &reference.symbol,
                    reference.range,
                    entry,
                )
            })
            .collect::<Vec<_>>()
    };
    let has_trait = |reference: &ProgramProjectReference, traits: &[&str]| {
        reference
            .traits
            .iter()
            .any(|value| traits.contains(&value.as_str()))
    };
    match construct {
        "modules-and-imports" => result
            .modules
            .iter()
            .map(|module| {
                located(
                    "project-module".to_string(),
                    &module.module,
                    module.range,
                    entry,
                )
            })
            .chain(references(&|reference| reference.role == "import"))
            .collect(),
        "scopes-and-bindings" => references(&|reference| reference.role == "reference"),
        "recursive-definitions" => references(&|reference| has_trait(reference, &["recursive"])),
        "types-and-universes" => references(&|reference| {
            TYPE_KINDS.contains(&reference.target_kind.as_str())
                || has_trait(reference, &["universe-polymorphic"])
        }),
        "effects" => references(&|reference| has_trait(reference, &EFFECT_TRAITS)),
        "attributes" => references(&|reference| reference.role == "attribute"),
        "macros-and-notation" => {
            references(&|reference| matches!(reference.role, "macro" | "notation" | "template-tag"))
        }
        "proof-terms-and-tactics" => references(&|reference| {
            PROOF_ROLES.contains(&reference.role)
                || PROOF_KINDS.contains(&reference.target_kind.as_str())
        }),
        "surface-expansion-elaboration-traces" => result
            .expansions
            .iter()
            .map(|expansion| {
                located(
                    format!("expansion:{}", expansion.kind),
                    &expansion.target,
                    expansion.range,
                    entry,
                )
            })
            .collect(),
        "project-context-and-dependencies" => result.facts.clone(),
        _ => Vec::new(),
    }
}