frazaro-core 0.9.4

The Frazaro language, host-free: sentences in, spreadsheets out
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! The rule store's registration grammar: VLA_SentenceEngine.bas's
//! `IsSlotTok`, `IsOptTok`, `IsAltCat`, the surface specs (`SurfaceMatch`,
//! `SurfaceForms`, `BareSurfaces`, `BareAltMatch`, `ValidateSurfaceSpec`),
//! `ValidateRuleItems`, `ExpandedSignatures` and `JoinSigs` (PORT.6, slice
//! 6c). Everything here is a pure function of a pattern's items; the store
//! itself, `AddPhraseRule` and `LintRule`, is `grammar.rs`.
//!
//! A pattern is split on spaces, each word folded, noise words dropped and
//! number words rewritten as the tokenizer rewrites them; what remains is
//! the rule's items: literals, slots (`{name}`, `{name:category}`,
//! `{name:category=default}`), optionals (`[word]`, `[a|b]`) and bare
//! alternations (`into|in`, `center/ed`). A category holding `|` or `/` is
//! an alternation of surfaces; a surface is a word or `stem/suffix`, which
//! matches the stem or the stem with its suffix and binds the stem.

use crate::intrinsics::fold;
use crate::messages::{raise, Refusal};

/// A slot's parts, as `IsSlotTok` hands them back.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Slot {
    pub name: String,
    pub cat: String,
    pub has_default: bool,
    pub default: String,
}

/// VBA `IsSlotTok`: `Some` for a well-formed slot, `None` for a literal, a
/// refusal with directions for a malformed one.
pub fn slot_tok(t: &str) -> Result<Option<Slot>, Refusal> {
    if !t.starts_with('{') {
        if let Some(rest) = t.strip_prefix('?') {
            return Err(raise(
                "english-old-slot-spelling",
                &[
                    ("t", t),
                    ("suggested", &format!("{{{rest}}}")),
                    ("example", "{name} (no category)"),
                ],
            ));
        }
        return Ok(None);
    }
    if !t.ends_with('}') || t.chars().count() < 3 {
        return Err(raise(
            "english-slot-not-closed",
            &[("t", t), ("ex1", "{name}"), ("ex2", "{name:category}")],
        ));
    }
    let inner = &t[1..t.len() - 1];
    let (name, mut cat) = match inner.find(':') {
        None => (inner.to_string(), "name".to_string()),
        Some(cp) => (inner[..cp].to_string(), inner[cp + 1..].to_string()),
    };
    if name.is_empty() || (inner.contains(':') && cat.is_empty()) {
        return Err(raise(
            "english-malformed-slot",
            &[("t", t), ("ex1", "{name}"), ("ex2", "{name:category}")],
        ));
    }
    // G7: an optional default, {name:category=text}; the last '=' wins.
    let mut has_default = false;
    let mut default = String::new();
    if let Some(eq) = cat.rfind('=') {
        has_default = true;
        default = cat[eq + 1..].to_string();
        cat = cat[..eq].to_string();
        if cat.is_empty() || default.is_empty() {
            return Err(raise(
                "english-malformed-default",
                &[("t", t), ("ex", "{name:category=text}")],
            ));
        }
    }
    Ok(Some(Slot {
        name,
        cat,
        has_default,
        default,
    }))
}

/// VBA `IsOptTok`: `[word]` or `[a|b]`, the inside folded; `None` for a
/// token that does not begin with `[`.
pub fn opt_tok(t: &str) -> Result<Option<String>, Refusal> {
    if !t.starts_with('[') {
        return Ok(None);
    }
    if !t.ends_with(']') || t.chars().count() < 3 {
        return Err(raise("english-optional-not-closed", &[("token", t)]));
    }
    Ok(Some(fold(&t[1..t.len() - 1])))
}

/// VBA `IsAltCat`: a category that is an alternation of surfaces.
pub fn is_alt_cat(cat: &str) -> bool {
    cat.contains('|') || cat.contains('/')
}

/// VBA `SurfaceMatch`: does a token match a surface spec, and what is its
/// stem? `center/ed` matches `center` and `centered`, binding `center`.
pub fn surface_match(spec: &str, tok: &str) -> (bool, String) {
    match spec.find('/') {
        None => (tok == spec, spec.to_string()),
        Some(sp) => {
            let stem = &spec[..sp];
            let suffix = &spec[sp + 1..];
            (
                tok == stem || tok == format!("{stem}{suffix}"),
                stem.to_string(),
            )
        }
    }
}

/// VBA `SurfaceForms`: the one or two surfaces a spec presents.
pub fn surface_forms(spec: &str) -> Vec<String> {
    match spec.find('/') {
        None => vec![spec.to_string()],
        Some(sp) => vec![
            spec[..sp].to_string(),
            format!("{}{}", &spec[..sp], &spec[sp + 1..]),
        ],
    }
}

/// VBA `BareSurfaces`: every surface of every branch of a bare spec.
pub fn bare_surfaces(spec: &str) -> Vec<String> {
    spec.split('|')
        .flat_map(|branch| surface_forms(&fold(branch)))
        .collect()
}

/// VBA `BareAltMatch`: a token against a bare surface spec, binding nothing.
pub fn bare_alt_match(spec: &str, tok: &str) -> bool {
    spec.split('|')
        .any(|branch| surface_match(&fold(branch), tok).0)
}

/// VBA `ValidateSurfaceSpec`: at most one slash, both sides non-empty.
pub fn validate_surface_spec(pattern: &str, where_: &str, spec: &str) -> Result<(), Refusal> {
    let Some(first) = spec.find('/') else {
        return Ok(());
    };
    if spec[first + 1..].contains('/') {
        return Err(raise(
            "english-surface-spec-multi-slash",
            &[("pattern", pattern), ("where", where_), ("spec", spec)],
        ));
    }
    if first == 0 || first == spec.len() - 1 {
        return Err(raise(
            "english-surface-spec-empty-side",
            &[("pattern", pattern), ("where", where_), ("spec", spec)],
        ));
    }
    Ok(())
}

/// VBA `ValidateRuleItems`: every slot's category is known or a
/// well-formed alternation, every optional and bare alternation has
/// non-empty branches, every surface spec stands.
pub fn validate_rule_items(pattern: &str, items: &[String]) -> Result<(), Refusal> {
    for t in items {
        if let Some(slot) = slot_tok(t)? {
            if is_alt_cat(&slot.cat) {
                for branch in slot.cat.split('|') {
                    if branch.trim_matches(' ').is_empty() {
                        return Err(raise(
                            "english-alternation-empty-branch",
                            &[
                                ("pattern", pattern),
                                ("token", t),
                                ("example", "{name:word|word}"),
                            ],
                        ));
                    }
                    validate_surface_spec(pattern, "branch", &fold(branch))?;
                }
            } else {
                match slot.cat.as_str() {
                    "name" | "var" | "text" | "expr" | "cond" | "conditions" | "role"
                    | "relation" | "clause" | "question" | "range" | "cell" | "column"
                    | "sheet" | "color" | "path" => {}
                    // G6: list-valued slots.
                    "text-list" | "range-list" | "cell-list" | "column-list" | "sheet-list"
                    | "color-list" => {}
                    other => {
                        return Err(raise(
                            "english-unknown-slot-category",
                            &[
                                ("pattern", pattern),
                                ("cat", other),
                                ("example", "{name:word|word}"),
                            ],
                        ))
                    }
                }
            }
        } else if let Some(ow) = opt_tok(t)? {
            for branch in ow.split('|') {
                if branch.trim_matches(' ').is_empty() {
                    return Err(raise(
                        "english-optional-empty-branch",
                        &[("pattern", pattern), ("token", t)],
                    ));
                }
                validate_surface_spec(pattern, "optional literal", &fold(branch))?;
            }
        } else if t.contains('|') || t.contains('/') {
            // G10: a bare surface token validates branch by branch.
            for branch in t.split('|') {
                if branch.trim_matches(' ').is_empty() {
                    return Err(raise(
                        "english-bare-alternation-empty-branch",
                        &[("pattern", pattern), ("token", t)],
                    ));
                }
                validate_surface_spec(pattern, "branch", &fold(branch))?;
            }
        }
    }
    Ok(())
}

/// VBA `ExpandedSignatures`: every shape a rule can present, one per
/// alternation branch and optional include/omit combination, literals as
/// themselves and category slots as `{category}`, pieces joined by `|`.
pub fn expanded_signatures(items: &[String]) -> Result<Vec<String>, Refusal> {
    let mut sigs: Vec<String> = vec![String::new()];
    for t in items {
        let mut pieces: Vec<String> = Vec::new();
        if let Some(slot) = slot_tok(t)? {
            if is_alt_cat(&slot.cat) {
                for branch in slot.cat.split('|') {
                    // G1.1: a stem/suffix branch is two shapes.
                    pieces.extend(surface_forms(&fold(branch)));
                }
            } else {
                pieces.push(format!("{{{}}}", slot.cat));
            }
            // G7: a default makes the slot omissible.
            if slot.has_default {
                pieces.push(String::new());
            }
        } else if let Some(ow) = opt_tok(t)? {
            pieces.push(String::new());
            pieces.extend(bare_surfaces(&ow));
        } else if t.contains('|') || t.contains('/') {
            pieces.extend(bare_surfaces(t));
        } else {
            pieces.push(t.clone());
        }
        let mut grown = Vec::with_capacity(sigs.len() * pieces.len());
        for s in &sigs {
            for pc in &pieces {
                if pc.is_empty() {
                    grown.push(s.clone());
                } else if s.is_empty() {
                    grown.push(pc.clone());
                } else {
                    grown.push(format!("{s}|{pc}"));
                }
            }
        }
        sigs = grown;
    }
    Ok(sigs)
}

/// VBA `JoinSigs`: the signatures as one text, one per line (LF).
pub fn join_sigs(sigs: &[String]) -> String {
    sigs.join("\n")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn slots_parse_as_isslottok_does() {
        let s = slot_tok("{v:var}").unwrap().unwrap();
        assert_eq!(
            (s.name.as_str(), s.cat.as_str(), s.has_default),
            ("v", "var", false)
        );
        let s = slot_tok("{n}").unwrap().unwrap();
        assert_eq!((s.name.as_str(), s.cat.as_str()), ("n", "name"));
        let s = slot_tok("{k:sample|population}").unwrap().unwrap();
        assert!(is_alt_cat(&s.cat));
        let s = slot_tok("{r:text=\"a1\"}").unwrap().unwrap();
        assert_eq!(
            (s.cat.as_str(), s.default.as_str(), s.has_default),
            ("text", "\"a1\"", true)
        );
        assert!(slot_tok("plain").unwrap().is_none());
        assert_eq!(slot_tok("?x").unwrap_err().id, "english-old-slot-spelling");
        assert_eq!(slot_tok("{x").unwrap_err().id, "english-slot-not-closed");
        assert_eq!(slot_tok("{}").unwrap_err().id, "english-slot-not-closed");
        assert_eq!(
            slot_tok("{:expr}").unwrap_err().id,
            "english-malformed-slot"
        );
        assert_eq!(slot_tok("{x:}").unwrap_err().id, "english-malformed-slot");
        assert_eq!(
            slot_tok("{x:text=}").unwrap_err().id,
            "english-malformed-default"
        );
        assert_eq!(
            slot_tok("{x:=5}").unwrap_err().id,
            "english-malformed-default"
        );
        assert_eq!(opt_tok("[key]").unwrap().unwrap(), "key");
        assert_eq!(opt_tok("[A|B]").unwrap().unwrap(), "a|b");
        assert!(opt_tok("key").unwrap().is_none());
        assert_eq!(
            opt_tok("[key").unwrap_err().id,
            "english-optional-not-closed"
        );
    }

    #[test]
    fn surfaces_match_stems_and_suffixes() {
        assert_eq!(
            surface_match("center/ed", "centered"),
            (true, "center".to_string())
        );
        assert_eq!(
            surface_match("center/ed", "center"),
            (true, "center".to_string())
        );
        assert_eq!(
            surface_match("center/ed", "centre"),
            (false, "center".to_string())
        );
        assert_eq!(surface_match("left", "left"), (true, "left".to_string()));
        assert_eq!(surface_forms("sort/ed"), vec!["sort", "sorted"]);
        assert_eq!(
            bare_surfaces("left|center/ed"),
            vec!["left", "center", "centered"]
        );
        assert!(bare_alt_match("into|in", "in") && !bare_alt_match("into|in", "on"));
        assert!(validate_surface_spec("p", "branch", "a/b").is_ok());
        assert_eq!(
            validate_surface_spec("p", "branch", "a/b/c")
                .unwrap_err()
                .id,
            "english-surface-spec-multi-slash"
        );
        assert_eq!(
            validate_surface_spec("p", "branch", "/b").unwrap_err().id,
            "english-surface-spec-empty-side"
        );
        assert_eq!(
            validate_surface_spec("p", "branch", "a/").unwrap_err().id,
            "english-surface-spec-empty-side"
        );
    }

    fn items(s: &str) -> Vec<String> {
        s.split(' ').map(|w| w.to_string()).collect()
    }

    #[test]
    fn items_validate_and_expand_into_signatures() {
        assert!(validate_rule_items("p", &items("warm cell {r:cell}")).is_ok());
        assert_eq!(
            validate_rule_items("p", &items("warm {r:banana}"))
                .unwrap_err()
                .id,
            "english-unknown-slot-category"
        );
        assert_eq!(
            validate_rule_items("p", &items("x {d:left||right}"))
                .unwrap_err()
                .id,
            "english-alternation-empty-branch"
        );
        assert_eq!(
            validate_rule_items("p", &items("x [a|]")).unwrap_err().id,
            "english-optional-empty-branch"
        );
        assert_eq!(
            validate_rule_items("p", &items("x into|")).unwrap_err().id,
            "english-bare-alternation-empty-branch"
        );
        assert_eq!(
            expanded_signatures(&items("fix {w:cell|range} {r:text}")).unwrap(),
            vec!["fix|cell|{text}", "fix|range|{text}"]
        );
        assert_eq!(
            expanded_signatures(&items("warm cell {r:text} [up]")).unwrap(),
            vec!["warm|cell|{text}", "warm|cell|{text}|up"]
        );
        assert_eq!(
            expanded_signatures(&items("align {d:left|center/ed}")).unwrap(),
            vec!["align|left", "align|center", "align|centered"]
        );
        assert_eq!(
            expanded_signatures(&items("store {e:expr} into|in {r:text=\"a1\"}")).unwrap(),
            vec![
                "store|{expr}|into|{text}",
                "store|{expr}|into",
                "store|{expr}|in|{text}",
                "store|{expr}|in"
            ]
        );
        assert_eq!(join_sigs(&["a|b".to_string(), "c".to_string()]), "a|b\nc");
    }
}