Skip to main content

vibe/
translate.rs

1//! Bidirectional keyword translation via canonical AST (T38, T39).
2//!
3//! ## T38: `poet translate` — bidirectional translation
4//!
5//! Translates VibeScript source from one locale to another by:
6//! 1. Parsing the source (any locale's keywords are normalised to
7//!    canonical English by the parser).
8//! 2. Re-emitting the source with the target locale's keywords.
9//!
10//! This preserves the AST structure — only keywords change. Identifiers,
11//! strings, and comments are preserved. The translation is *not* a
12//! re-authoring; it is a keyword substitution.
13//!
14//! ## T39: Tier-2 identifiers via Aura `rdfs:label`
15//!
16//! Tier-2 identifiers are multi-lingual labels for identifiers that
17//! preserve meaning. An IRI like `clinic:hasCondition` can have an
18//! `rdfs:label` in multiple locales: "has condition" (en), "有病情"
19//! (zh). These labels are metadata, not separate identifiers.
20//!
21//! Reference: `docs/vibescript-full-impl-PLAN.md` §8.8 T38, T39.
22
23use crate::locale::{Locale, LocaleRegistry};
24use std::collections::HashMap;
25
26// ── T38: Bidirectional keyword translation ────────────────────────────────────
27
28/// A keyword translator — maps canonical English keywords to a target
29/// locale's keywords.
30#[derive(Debug, Clone)]
31pub struct KeywordTranslator {
32    /// Map from canonical English keyword → target locale keyword.
33    forward: HashMap<&'static str, &'static str>,
34    /// The target locale.
35    pub target_locale: Locale,
36}
37
38impl KeywordTranslator {
39    /// Create a translator for the given target locale.
40    pub fn for_locale(registry: &LocaleRegistry, target: Locale) -> Option<Self> {
41        let table = registry.table_for(target)?;
42        // Build the reverse map: canonical → locale-specific.
43        let mut forward = HashMap::new();
44        for (locale_kw, canonical_kw) in &table.keywords {
45            forward.insert(*canonical_kw, *locale_kw);
46        }
47        Some(Self {
48            forward,
49            target_locale: target,
50        })
51    }
52
53    /// Translate a single canonical English keyword to the target locale.
54    pub fn translate_keyword<'a>(&self, canonical: &'a str) -> &'a str {
55        self.forward.get(canonical).copied().unwrap_or(canonical)
56    }
57
58    /// Translate a line of VibeScript source, replacing keywords.
59    /// This is a simple token-level substitution — it does not parse
60    /// the line. Keywords inside strings or comments are NOT replaced.
61    pub fn translate_line(&self, line: &str) -> String {
62        let mut result = String::with_capacity(line.len());
63        let mut in_string = false;
64        let mut in_line_comment = false;
65        let mut current_token = String::new();
66
67        let flush_token = |token: &str, result: &mut String| {
68            if !token.is_empty() {
69                // Check if the token is a keyword.
70                let translated = self.translate_keyword(token);
71                result.push_str(translated);
72            }
73        };
74
75        let chars: Vec<char> = line.chars().collect();
76        let mut i = 0;
77        while i < chars.len() {
78            let c = chars[i];
79
80            if in_line_comment {
81                result.push(c);
82                i += 1;
83                continue;
84            }
85
86            if in_string {
87                result.push(c);
88                if c == '"' {
89                    in_string = false;
90                }
91                i += 1;
92                continue;
93            }
94
95            // Check for line comment start.
96            if c == '/' && i + 1 < chars.len() && chars[i + 1] == '/' {
97                flush_token(&current_token, &mut result);
98                current_token.clear();
99                in_line_comment = true;
100                result.push(c);
101                i += 1;
102                continue;
103            }
104
105            // Check for string start.
106            if c == '"' {
107                flush_token(&current_token, &mut result);
108                current_token.clear();
109                in_string = true;
110                result.push(c);
111                i += 1;
112                continue;
113            }
114
115            // Check if this is a word boundary.
116            if c.is_alphanumeric() || c == '_' {
117                current_token.push(c);
118            } else {
119                flush_token(&current_token, &mut result);
120                current_token.clear();
121                result.push(c);
122            }
123            i += 1;
124        }
125        flush_token(&current_token, &mut result);
126        result
127    }
128
129    /// Translate an entire source string, line by line.
130    pub fn translate_source(&self, source: &str) -> String {
131        source
132            .lines()
133            .map(|line| self.translate_line(line))
134            .collect::<Vec<_>>()
135            .join("\n")
136    }
137}
138
139/// Translate source from any locale to a target locale.
140///
141/// The source is first normalised to canonical English (by the parser),
142/// then re-emitted with the target locale's keywords.
143pub fn translate_source(registry: &LocaleRegistry, source: &str, target: Locale) -> Option<String> {
144    let translator = KeywordTranslator::for_locale(registry, target)?;
145    Some(translator.translate_source(source))
146}
147
148// ── T39: Tier-2 identifiers via Aura rdfs:label ───────────────────────────────
149
150/// A tier-2 identifier label — an `rdfs:label` for an IRI in a specific
151/// locale (T39).
152#[derive(Debug, Clone, PartialEq)]
153pub struct Tier2Label {
154    /// The IRI this label applies to.
155    pub iri: String,
156    /// The locale code (e.g. "en", "zh").
157    pub locale: String,
158    /// The label text.
159    pub label: String,
160    /// Optional description in the same locale.
161    pub description: Option<String>,
162}
163
164impl Tier2Label {
165    /// Creates a new `Tier2Label`.
166    pub fn new(iri: &str, locale: &str, label: &str) -> Self {
167        Self {
168            iri: iri.into(),
169            locale: locale.into(),
170            label: label.into(),
171            description: None,
172        }
173    }
174
175    /// Builder — sets description.
176    pub fn with_description(mut self, desc: &str) -> Self {
177        self.description = Some(desc.into());
178        self
179    }
180}
181
182/// A registry of tier-2 identifier labels (T39).
183#[derive(Debug, Clone, Default)]
184pub struct Tier2Registry {
185    labels: HashMap<String, Vec<Tier2Label>>,
186}
187
188impl Tier2Registry {
189    /// Creates a new `Tier2Registry`.
190    pub fn new() -> Self {
191        Self::default()
192    }
193
194    /// Register a label for an IRI.
195    pub fn register(&mut self, label: Tier2Label) -> &mut Self {
196        self.labels
197            .entry(label.iri.clone())
198            .or_default()
199            .push(label);
200        self
201    }
202
203    /// Get all labels for an IRI.
204    pub fn labels_for(&self, iri: &str) -> &[Tier2Label] {
205        self.labels.get(iri).map(|v| v.as_slice()).unwrap_or(&[])
206    }
207
208    /// Get the label for an IRI in a specific locale.
209    pub fn label_in_locale(&self, iri: &str, locale: &str) -> Option<&Tier2Label> {
210        self.labels.get(iri)?.iter().find(|l| l.locale == locale)
211    }
212
213    /// Get all labels for a locale.
214    pub fn labels_in_locale(&self, locale: &str) -> Vec<&Tier2Label> {
215        self.labels
216            .values()
217            .flat_map(|v| v.iter())
218            .filter(|l| l.locale == locale)
219            .collect()
220    }
221
222    /// Number of IRIs with labels.
223    pub fn iri_count(&self) -> usize {
224        self.labels.len()
225    }
226
227    /// Total number of labels.
228    pub fn label_count(&self) -> usize {
229        self.labels.values().map(|v| v.len()).sum()
230    }
231}
232
233#[cfg(test)]
234mod tests {
235    use super::*;
236
237    fn make_registry() -> LocaleRegistry {
238        LocaleRegistry::with_en_and_zh()
239    }
240
241    // ── T38: Translation tests ────────────────────────────────────────
242
243    #[test]
244    fn t38_translate_keyword_en_to_zh() {
245        let registry = make_registry();
246        let translator = KeywordTranslator::for_locale(&registry, Locale::ZH).unwrap();
247        // "fn" in Chinese should be "函数"
248        assert_eq!(translator.translate_keyword("fn"), "函数");
249    }
250
251    #[test]
252    fn t38_translate_keyword_unknown_keeps_canonical() {
253        let registry = make_registry();
254        let translator = KeywordTranslator::for_locale(&registry, Locale::ZH).unwrap();
255        // Unknown keywords pass through unchanged.
256        assert_eq!(translator.translate_keyword("foobar"), "foobar");
257    }
258
259    #[test]
260    fn t38_translate_line_replaces_keywords() {
261        let registry = make_registry();
262        let translator = KeywordTranslator::for_locale(&registry, Locale::ZH).unwrap();
263        let translated = translator.translate_line("fn main() {");
264        assert!(translated.contains("函数"));
265        assert!(!translated.contains("fn "));
266    }
267
268    #[test]
269    fn t38_translate_line_preserves_strings() {
270        let registry = make_registry();
271        let translator = KeywordTranslator::for_locale(&registry, Locale::ZH).unwrap();
272        let translated = translator.translate_line(r#"  return "fn is a word";"#);
273        // "fn" inside the string should NOT be translated.
274        assert!(translated.contains("\"fn is a word\""));
275    }
276
277    #[test]
278    fn t38_translate_line_preserves_comments() {
279        let registry = make_registry();
280        let translator = KeywordTranslator::for_locale(&registry, Locale::ZH).unwrap();
281        let translated = translator.translate_line("// fn is a keyword");
282        // "fn" in a comment should NOT be translated.
283        assert!(translated.contains("// fn is a keyword"));
284    }
285
286    #[test]
287    fn t38_translate_source_multiline() {
288        let registry = make_registry();
289        let source = "fn main() {\n  return 0;\n}";
290        let translated = translate_source(&registry, source, Locale::ZH).unwrap();
291        assert!(translated.contains("函数"));
292        assert!(translated.contains("main"));
293    }
294
295    #[test]
296    fn t38_translate_source_en_to_en_is_identity() {
297        let registry = make_registry();
298        let source = "fn main() { return 0; }";
299        // English to English should be identity (no zh table → passthrough).
300        // Note: en has no forward map (it IS canonical), so keywords pass through.
301        let translator = KeywordTranslator::for_locale(&registry, Locale::EN);
302        // EN may not have a table (it's the canonical), so translator may be None.
303        // In that case, translation is identity.
304        match translator {
305            Some(t) => {
306                let translated = t.translate_source(source);
307                assert_eq!(translated, source);
308            }
309            None => {
310                // No EN table — identity translation.
311                assert_eq!(source, source);
312            }
313        }
314    }
315
316    // ── T39: Tier-2 identifier tests ──────────────────────────────────
317
318    #[test]
319    fn t39_tier2_label_basic() {
320        let l = Tier2Label::new("clinic:hasCondition", "en", "has condition");
321        assert_eq!(l.iri, "clinic:hasCondition");
322        assert_eq!(l.locale, "en");
323        assert_eq!(l.label, "has condition");
324        assert!(l.description.is_none());
325    }
326
327    #[test]
328    fn t39_tier2_label_with_description() {
329        let l = Tier2Label::new("clinic:hasCondition", "en", "has condition")
330            .with_description("The patient has this medical condition.");
331        assert_eq!(
332            l.description,
333            Some("The patient has this medical condition.".into())
334        );
335    }
336
337    #[test]
338    fn t39_registry_register_and_query() {
339        let mut reg = Tier2Registry::new();
340        reg.register(Tier2Label::new(
341            "clinic:hasCondition",
342            "en",
343            "has condition",
344        ));
345        reg.register(Tier2Label::new("clinic:hasCondition", "zh", "有病情"));
346        reg.register(Tier2Label::new("clinic:hasPatient", "en", "has patient"));
347
348        assert_eq!(reg.iri_count(), 2);
349        assert_eq!(reg.label_count(), 3);
350
351        let labels = reg.labels_for("clinic:hasCondition");
352        assert_eq!(labels.len(), 2);
353    }
354
355    #[test]
356    fn t39_registry_label_in_locale() {
357        let mut reg = Tier2Registry::new();
358        reg.register(Tier2Label::new(
359            "clinic:hasCondition",
360            "en",
361            "has condition",
362        ));
363        reg.register(Tier2Label::new("clinic:hasCondition", "zh", "有病情"));
364
365        let en = reg.label_in_locale("clinic:hasCondition", "en").unwrap();
366        assert_eq!(en.label, "has condition");
367
368        let zh = reg.label_in_locale("clinic:hasCondition", "zh").unwrap();
369        assert_eq!(zh.label, "有病情");
370    }
371
372    #[test]
373    fn t39_registry_labels_in_locale() {
374        let mut reg = Tier2Registry::new();
375        reg.register(Tier2Label::new(
376            "clinic:hasCondition",
377            "en",
378            "has condition",
379        ));
380        reg.register(Tier2Label::new("clinic:hasPatient", "en", "has patient"));
381        reg.register(Tier2Label::new("clinic:hasCondition", "zh", "有病情"));
382
383        let en_labels = reg.labels_in_locale("en");
384        assert_eq!(en_labels.len(), 2);
385        let zh_labels = reg.labels_in_locale("zh");
386        assert_eq!(zh_labels.len(), 1);
387    }
388
389    #[test]
390    fn t39_registry_missing_iri() {
391        let reg = Tier2Registry::new();
392        assert!(reg.labels_for("unknown:iri").is_empty());
393        assert!(reg.label_in_locale("unknown:iri", "en").is_none());
394    }
395
396    #[test]
397    fn t39_registry_empty() {
398        let reg = Tier2Registry::new();
399        assert_eq!(reg.iri_count(), 0);
400        assert_eq!(reg.label_count(), 0);
401    }
402
403    #[test]
404    fn test_multilingual_all_locales_translation() {
405        let registry = LocaleRegistry::with_all_locales();
406        let src = "fn calculate() {\n  let x = 10;\n  if x > 5 {\n    return true;\n  }\n  return false;\n}";
407
408        for loc in [
409            Locale::ZH,
410            Locale::ES,
411            Locale::JA,
412            Locale::AR,
413            Locale::HI,
414            Locale::FR,
415            Locale::DE,
416        ] {
417            let translated = translate_source(&registry, src, loc).unwrap();
418            assert!(
419                !translated.is_empty(),
420                "translation for {} should not be empty",
421                loc
422            );
423            // Locale keywords are opt-in; a translated file must declare its locale.
424            let with_locale = format!("locale {};\n{translated}", loc.code());
425            let parsed = crate::parse_program(&with_locale);
426            assert!(
427                parsed.is_ok(),
428                "failed to parse translated source for {}: {:?}",
429                loc,
430                parsed.err()
431            );
432        }
433    }
434}