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;
10pub mod index;
11mod jvm;
12mod macros;
13mod versions;
14
15use yog_data::Diagnostic;
16
17pub use jvm::{JvmBuild, build_jar};
18pub use macros::MacroLibrary;
19pub use yog_script_analyze::versions::targeted;
20
21/// What's wrong with the source `text` of the file `name` (`.ys`, `.yns`);
22/// a `.yns` checked on each version it targets, its classes from the cache
23/// (built the first time).
24#[must_use]
25pub fn check_source(name: &str, text: &str) -> Vec<Diagnostic> {
26    check_source_with(name, text, index::load)
27}
28
29/// [`check_source`], each version's classes from `load`.
30pub fn check_source_with(
31    name: &str,
32    text: &str,
33    load: impl Fn(&str) -> Result<index::ClassIndex, String>,
34) -> Vec<Diagnostic> {
35    check_source_using(name, text, load, Project::default())
36}
37
38/// What a source's mod brings to its check: the build's procedural macros,
39/// what its `yog.toml` says it does (sending to the server).
40#[derive(Default, Clone, Copy)]
41pub struct Project<'a> {
42    pub macros: Option<&'a MacroLibrary>,
43    pub to_server: bool,
44}
45
46/// [`check_source_with`] in its mod's `project`.
47pub fn check_source_using(
48    name: &str,
49    text: &str,
50    load: impl Fn(&str) -> Result<index::ClassIndex, String>,
51    project: Project<'_>,
52) -> Vec<Diagnostic> {
53    let env = yog_script_analyze::Env {
54        game: None,
55        macros: project
56            .macros
57            .map(|m| m as &dyn yog_script_analyze::macros::ProcMacros),
58        to_server: project.to_server,
59    };
60    if !yog_script_analyze::is_nanites(name) {
61        return yog_script_analyze::check_in(name, text, env).problems;
62    }
63    versions::check(name, text, load, env)
64}