Skip to main content

yog_script_analyze/words/
patch.rs

1//! `patch` — code reaching into a class the mod doesn't own.
2
3use yog_ids::script_problem;
4
5use crate::ast::{DeclKind, Patch};
6use crate::parser::{PResult, Parser};
7use crate::source::token::{Punct, Tok};
8use crate::words::{Role, Word};
9
10pub static WORD: Word = Word {
11    text: "patch",
12    doc: "Reaches into a class the mod doesn't own (`.yns` files only): `patch LivingEntity { before \
13          bool Hurt(DamageSource source, float amount) { … } }`. Methods and points by their meaning, \
14          checked against every targeted version.",
15    reserved: false,
16    roles: &[Role::Decl { starts: Some(starts), parse }],
17};
18
19fn starts(p: &Parser) -> bool {
20    p.at_word(&WORD) && matches!(p.nth(1), Tok::Ident(_)) && !p.nth_is(1, Punct::Eq)
21        && !matches!(p.nth(2), Tok::Punct(Punct::Eq | Punct::Semi | Punct::LParen))
22}
23
24fn parse(p: &mut Parser) -> PResult<DeclKind> {
25    let start = p.bump().span;
26    if !p.nanites {
27        p.error(
28            script_problem::PATCH_OUTSIDE_NANITES,
29            start,
30            "patches go in `.yns` files (Yog Nanite Script)",
31        );
32    }
33    let target = p.ty()?;
34    let items = p.patch_items()?;
35    Ok(DeclKind::Patch(Patch { target, items }))
36}