Skip to main content

yog_script_build/
lib.rs

1//! Yog Script's compiler: a source checked (`check_source` — read, its
2//! names resolved, its types checked; with the generator, compiled). A
3//! `.ys` reaches Yog only; a `.yns` reaches the game too, and is checked
4//! on every version it targets against that version's class index
5//! (`index`, built from the game's jars, read with `classfile`). It knows
6//! sources, not projects: the Yog CLI hands it a mod's, the `yog-script`
7//! tool any.
8
9mod classfile;
10mod files;
11pub mod index;
12mod jvm;
13mod macros;
14mod versions;
15
16use yog_data::Diagnostic;
17
18pub use files::FilesBeside;
19pub use jvm::{JvmBuild, build_jar};
20pub use macros::MacroLibrary;
21pub use yog_script_analyze::versions::targeted;
22
23/// What's wrong with the source `text` of the file `name` (`.ys`, `.yns`);
24/// a `.yns` checked on each version it targets, its classes from the cache
25/// (built the first time).
26#[must_use]
27pub fn check_source(name: &str, text: &str) -> Vec<Diagnostic> {
28    check_source_with(name, text, index::load)
29}
30
31/// [`check_source`], each version's classes from `load`.
32pub fn check_source_with(
33    name: &str,
34    text: &str,
35    load: impl Fn(&str) -> Result<index::ClassIndex, String>,
36) -> Vec<Diagnostic> {
37    check_source_using(name, text, load, Project::default())
38}
39
40/// What a source's mod brings to its check: the build's procedural macros,
41/// what its `yog.toml` says it does (sending to the server).
42#[derive(Default, Clone, Copy)]
43pub struct Project<'a> {
44    pub macros: Option<&'a MacroLibrary>,
45    /// The mod's files its sources include.
46    pub files: Option<&'a dyn yog_script_analyze::macros::ModFiles>,
47    pub to_server: bool,
48    /// The mod's content files, each (kind, id): what its content
49    /// declarations bind to.
50    pub content: Option<&'a [(String, String)]>,
51    /// The mod's other sources' declarations: the types it reaches.
52    pub others: &'a [&'a [yog_script_analyze::ast::Decl]],
53}
54
55/// [`check_source_with`] in its mod's `project`.
56pub fn check_source_using(
57    name: &str,
58    text: &str,
59    load: impl Fn(&str) -> Result<index::ClassIndex, String>,
60    project: Project<'_>,
61) -> Vec<Diagnostic> {
62    let env = yog_script_analyze::Env {
63        game: None,
64        macros: project
65            .macros
66            .map(|m| m as &dyn yog_script_analyze::macros::ProcMacros),
67        files: project.files,
68        to_server: project.to_server,
69        content: project.content,
70        others: project.others,
71    };
72    if !yog_script_analyze::is_nanites(name) {
73        return yog_script_analyze::check_in(name, text, env).problems;
74    }
75    versions::check(name, text, load, env)
76}