Skip to main content

yog_script_analyze/
lib.rs

1//! Yog Script (`.ys`) and Yog Nanite Script (`.yns`): the language a Yog
2//! mod is written in (`docs/design/ys.md`, `docs/design/ys-grammar.md`).
3//! This crate reads a source into its syntax tree, resolves its names and
4//! says what's wrong with it, as Diagnostics placed in the source.
5
6mod source;
7
8pub use source::token;
9
10pub mod ast;
11pub mod index;
12mod lexer;
13pub mod parser;
14pub mod resolve;
15pub mod words;
16
17use std::path::Path;
18
19use yog_data::Diagnostic;
20use yog_ids::Format;
21
22/// A source read: its tree (what could be read of it) and its problems.
23#[derive(Debug)]
24pub struct Parsed {
25    pub file: ast::File,
26    pub problems: Vec<Diagnostic>,
27}
28
29/// A source checked: its tree, what its names stand for, its problems.
30#[derive(Debug)]
31pub struct Checked {
32    pub file: ast::File,
33    pub symbols: resolve::Symbols,
34    pub problems: Vec<Diagnostic>,
35}
36
37/// The source `src` of the file `name` read; patches are allowed when it's
38/// a `.yns` file.
39#[must_use]
40pub fn parse(name: &str, src: &str) -> Parsed {
41    let mut report = yog_analysis::Report::new(name, src);
42    let file = read(name, src, &mut report);
43    Parsed {
44        file,
45        problems: report.out,
46    }
47}
48
49/// The source `src` of the file `name` read and its names resolved: what
50/// `yog check` and the editors report.
51#[must_use]
52pub fn check(name: &str, src: &str) -> Checked {
53    let mut report = yog_analysis::Report::new(name, src);
54    let file = read(name, src, &mut report);
55    let symbols = resolve::resolve(&file, &mut report);
56    Checked {
57        file,
58        symbols,
59        problems: report.out,
60    }
61}
62
63fn read(name: &str, src: &str, report: &mut yog_analysis::Report<'_>) -> ast::File {
64    let nanites = Format::Nanites.is(Path::new(name));
65    let tokens = lexer::lex(src, report);
66    parser::Parser::new(&tokens, report, nanites).file()
67}