Skip to main content

workshop_rs/output/
emitter.rs

1//! Deterministic localized Workshop emitter.
2//!
3//! Serializes validated Workshop IR into localized Workshop text with a
4//! selectable output locale. Canonical catalog identities resolve to
5//! locale-specific spellings; missing target-locale mappings fail explicitly
6//! with a [`WorkshopError::MissingMapping`] diagnostic — never a guess, never
7//! a silent passthrough of another locale's spelling. Fallback to another
8//! declared locale is opt-in ([`EmitOptions`]) and every fell-back identity
9//! is recorded in [`EmitOutput::fallback_ids`].
10
11pub(crate) use std::fmt::Write;
12
13pub(crate) use crate::catalog::{Catalog, Kind, Locale};
14pub(crate) use crate::core::error::{Result, WorkshopError};
15pub(crate) use crate::core::format::format_number;
16pub(crate) use crate::settings::table::{self, KeyKind, PathPart};
17pub(crate) use crate::settings::{Settings as SettingsTree, SettingsNode};
18pub(crate) use crate::wir;
19
20/// Emission options: opt-in fallback for missing target-locale mappings.
21#[derive(Debug, Clone, Default, PartialEq, Eq)]
22pub struct EmitOptions {
23    /// When a canonical identity has no spelling for the target locale, its
24    /// spelling in this declared locale is used instead. `None` (the default)
25    /// keeps missing mappings failing explicitly. The fallback choice is
26    /// visible in [`EmitOutput::fallback_ids`].
27    pub fallback_locale: Option<Locale>,
28}
29
30/// The result of a localized emission.
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct EmitOutput {
33    /// The emitted localized Workshop text.
34    pub text: String,
35    /// Canonical identities (and the `settings` marker) whose spelling came
36    /// from the opt-in fallback locale instead of the target locale. Empty
37    /// when no fallback occurred.
38    pub fallback_ids: Vec<String>,
39}
40
41/// Emit a Workshop IR program as localized Workshop text, failing explicitly
42/// on any missing target-locale mapping (no fallback).
43pub fn emit(program: &wir::Program, catalog: &Catalog, locale: &Locale) -> Result<String> {
44    emit_with_options(program, catalog, locale, &EmitOptions::default()).map(|out| out.text)
45}
46
47/// Emit a Workshop IR program as localized Workshop text with emission
48/// options (opt-in fallback locale).
49pub fn emit_with_options(
50    program: &wir::Program,
51    catalog: &Catalog,
52    locale: &Locale,
53    options: &EmitOptions,
54) -> Result<EmitOutput> {
55    emit_with_options_inner(program, catalog, locale, options, false)
56}
57
58pub(crate) fn emit_with_options_for_conversion(
59    program: &wir::Program,
60    catalog: &Catalog,
61    locale: &Locale,
62    options: &EmitOptions,
63) -> Result<EmitOutput> {
64    emit_with_options_inner(program, catalog, locale, options, true)
65}
66
67fn emit_with_options_inner(
68    program: &wir::Program,
69    catalog: &Catalog,
70    locale: &Locale,
71    options: &EmitOptions,
72    force_hero_constructors: bool,
73) -> Result<EmitOutput> {
74    let mut emitter = EmitContext {
75        program,
76        catalog,
77        locale: locale.clone(),
78        fallback: options.fallback_locale.clone(),
79        force_hero_constructors,
80        fallback_ids: Vec::new(),
81        out: String::new(),
82        line_count: 0,
83    };
84    emitter.run()?;
85    Ok(EmitOutput {
86        text: emitter.out,
87        fallback_ids: emitter.fallback_ids,
88    })
89}
90
91pub(crate) struct EmitContext<'a> {
92    pub(crate) program: &'a wir::Program,
93    pub(crate) catalog: &'a Catalog,
94    pub(crate) locale: Locale,
95    /// The opt-in fallback locale for missing target-locale mappings.
96    pub(crate) fallback: Option<Locale>,
97    /// Canonical ids emitted with a fallback-locale spelling.
98    pub(crate) fallback_ids: Vec<String>,
99    pub(crate) force_hero_constructors: bool,
100    pub(crate) out: String,
101    pub(crate) line_count: usize,
102}
103
104impl EmitContext<'_> {
105    pub(crate) fn run(&mut self) -> Result<()> {
106        // Section order: settings, variables, subroutines, rules.
107        if let Some(settings) = &self.program.settings {
108            self.emit_settings(settings)?;
109            self.out.push('\n');
110        }
111        if !self.program.global_variables.is_empty() || !self.program.player_variables.is_empty() {
112            let variables = self.structural("variables")?;
113            self.line(0, &format!("{variables} {{"))?;
114            if !self.program.global_variables.is_empty() {
115                let global = self.structural("global")?;
116                self.line(1, &format!("{global}:"))?;
117                for variable in self.program.global_variables.iter() {
118                    self.line(2, &format!("{}: {}", variable.index, variable.name))?;
119                }
120            }
121            if !self.program.player_variables.is_empty() {
122                let player = self.structural("player")?;
123                self.line(1, &format!("{player}:"))?;
124                for variable in self.program.player_variables.iter() {
125                    self.line(2, &format!("{}: {}", variable.index, variable.name))?;
126                }
127            }
128            self.line(0, "}")?;
129            self.out.push('\n');
130        }
131        if !self.program.subroutines.is_empty() {
132            let subroutines = self.structural("subroutines")?;
133            self.line(0, &format!("{subroutines} {{"))?;
134            for subroutine in self.program.subroutines.iter() {
135                self.line(1, &format!("{}: {}", subroutine.index, subroutine.name))?;
136            }
137            self.line(0, "}")?;
138            self.out.push('\n');
139        }
140        for (emitted_rules, rule) in self.program.rules.iter().enumerate() {
141            if emitted_rules > 0 {
142                self.out.push('\n');
143            }
144            self.rule(rule)?;
145        }
146        // The oracle's raw artifact ends with a trailing blank line (the
147        // committed snapshots strip it via the acquisition normalizer; the
148        // pinned oracle's own output keeps it).
149        if !self.out.is_empty() && !self.out.ends_with("\n\n") {
150            self.out.push('\n');
151        }
152        Ok(())
153    }
154
155    pub(crate) fn malformed(&self, message: impl Into<String>) -> WorkshopError {
156        WorkshopError::Malformed {
157            message: message.into(),
158            span: None,
159        }
160    }
161
162    pub(crate) fn line(&mut self, level: usize, text: &str) -> Result<()> {
163        for _ in 0..level {
164            self.out.push_str("    ");
165        }
166        self.out.push_str(text);
167        self.out.push('\n');
168        self.line_count += 1;
169        Ok(())
170    }
171}
172
173/// Format a float like the reference frontend: integers print without a
174/// decimal point, and non-integers print the shortest round-trip
175/// representation truncated to 16 significant digits (OverPy behavior;
176/// evidence: the pinned oracle snapshots).
177pub(crate) fn is_comparison_operator(name: &str) -> bool {
178    matches!(name, "==" | "!=" | "<" | "<=" | ">" | ">=")
179}
180
181pub(crate) fn escape_string(value: &str) -> String {
182    value.replace('"', "\\\"")
183}
184
185/// Re-escape a decoded value string the way the pinned oracle does (#87):
186/// `\`, `"`, newline, and carriage return re-escape; tabs pass through raw
187/// (byte-measured oracle behavior: `a\tb` emits a real tab, `a\nb` emits the
188/// literal two-character `\n`).
189pub(crate) fn escape_value_string(value: &str) -> String {
190    let mut out = String::with_capacity(value.len());
191    for ch in value.chars() {
192        match ch {
193            '\\' => out.push_str("\\\\"),
194            '"' => out.push_str("\\\""),
195            '\n' => out.push_str("\\n"),
196            '\r' => out.push_str("\\r"),
197            other => out.push(other),
198        }
199    }
200    out
201}
202
203/// Split a decoded string per the oracle's long-string rule (#87): when the
204/// decoded length exceeds the Workshop 128-char limit, non-final segments
205/// hold exactly 125 decoded chars and are emitted with a `{0}` continuation
206/// placeholder (128 total text chars), chained as nested `Custom String`
207/// arguments; the final segment holds the remainder without a placeholder.
208/// Segment texts are re-escaped. Byte-measured basis: chunk sizes are
209/// counted on the decoded string (70 escaped newlines — 140 escaped chars,
210/// 70 decoded — emit unsplit; 129 decoded newlines split at 125 decoded).
211pub(crate) fn split_string(value: &str) -> Vec<String> {
212    if value.chars().count() <= 128 {
213        return vec![escape_value_string(value)];
214    }
215    let mut segments = Vec::new();
216    let mut rest = value;
217    while rest.chars().count() > 125 {
218        let chunk: String = rest.chars().take(125).collect();
219        let mut text = escape_value_string(&chunk);
220        text.push_str("{0}");
221        segments.push(text);
222        rest = &rest[chunk.len()..];
223    }
224    if !rest.is_empty() {
225        segments.push(escape_value_string(rest));
226    }
227    segments
228}
229
230/// Escape a settings string value the way the pinned oracle does: every
231/// decode the JSONC parser performed is re-escaped, so decoded values
232/// round-trip to the oracle's spelling. Evidence: the inputhud description
233/// (`\n` in the source block) is emitted by the oracle as the literal
234/// two-character sequence `\n` in the Workshop settings section.
235pub(crate) fn escape_settings_string(value: &str) -> String {
236    let mut out = String::with_capacity(value.len());
237    for ch in value.chars() {
238        match ch {
239            '\\' => out.push_str("\\\\"),
240            '"' => out.push_str("\\\""),
241            '\n' => out.push_str("\\n"),
242            '\t' => out.push_str("\\t"),
243            '\r' => out.push_str("\\r"),
244            other => out.push(other),
245        }
246    }
247    out
248}
249
250/// Emit the nested continuation chain
251/// `Custom String(seg0, Custom String(seg1, ...))`; segment texts are
252/// pre-escaped, non-final segments carry the `{0}` placeholder. Iterative:
253/// every segment except the first opens a `Custom String` level, then all
254/// levels close.
255pub(crate) fn emit_string_chain(spelling: &str, segments: &[String], out: &mut String) {
256    let Some((first, rest)) = segments.split_first() else {
257        return;
258    };
259    out.push_str(spelling);
260    out.push('(');
261    write!(out, "\"{first}\"").unwrap();
262    for segment in rest {
263        out.push_str(", ");
264        out.push_str(spelling);
265        out.push('(');
266        write!(out, "\"{segment}\"").unwrap();
267    }
268    for _ in 0..=rest.len() {
269        out.push(')');
270    }
271}
272
273/// Render a constant format argument the way the oracle folds it: integers
274/// without decimals, non-integers with exactly two decimals (JS `toFixed(2)`
275/// rounding: `0.5` -> `0.50`, `0.125` -> `0.13`, #87).
276pub(crate) fn fold_number(value: f64) -> String {
277    if value.fract() == 0.0 && value.abs() < 1e15 {
278        format!("{}", value as i64)
279    } else {
280        let scaled = (value * 100.0).round();
281        let sign = if scaled < 0.0 { "-" } else { "" };
282        let scaled = scaled.abs() as i64;
283        format!("{sign}{}.{:02}", scaled / 100, scaled % 100)
284    }
285}