oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/sym.rs
6.5 KiB, 1 run
created by r1870400018:63490, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // U3 owns this file. The `sym` and `emoji` modules as static tables, `symbol(..)`, and modifier resolution |
| 2 | // (`arrow.r.long`). The tables in `sym_data.rs` are generated from the `typst` 0.15.1 oracle rather than |
| 3 | // migrated from `lang/mathparse.rs`, which knew a subset: every name, submodule (`sym.control`, |
| 4 | // `sym.gender`) and variant Typst has is here, in Typst's order, so variant choice and `repr` agree. |
| 5 | // |
| 6 | // Resolution follows codex: the applied modifiers must all be present in a variant, and among those the |
| 7 | // variant with the fewest modifiers wins, the first in table order on a tie. |
| 8 | |
| 9 | use crate::diag::DiagnosticKind; |
| 10 | use crate::eval::args::Args; |
| 11 | use crate::eval::lib::foundations::{ |
| 12 | finish, |
| 13 | mismatch, |
| 14 | pretty_array_like, |
| 15 | repr_str, |
| 16 | }; |
| 17 | use crate::eval::scope::Scope; |
| 18 | use crate::eval::value::{ |
| 19 | Module, |
| 20 | Symbol, |
| 21 | SymVariants, |
| 22 | Value, |
| 23 | }; |
| 24 | use crate::eval::Engine; |
| 25 | |
| 26 | use oxedyne_fe2o3_core::prelude::*; |
| 27 | |
| 28 | use std::sync::Arc; |
| 29 | |
| 30 | native_fns! { |
| 31 | pub enum SymFn { |
| 32 | Symbol => "symbol", |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /// One entry of a generated table: a symbol with one text, a symbol with variants (the unmodified one, |
| 37 | /// when it exists, has empty modifiers), or a submodule. |
| 38 | #[derive(Clone, Copy, Debug)] |
| 39 | pub enum SymDef { |
| 40 | One(&'static str), |
| 41 | Many(&'static [(&'static str, &'static str)]), |
| 42 | Module(&'static [(&'static str, SymDef)]), |
| 43 | } |
| 44 | |
| 45 | include!("sym_data.rs"); |
| 46 | |
| 47 | pub fn define(scope: &mut Scope) { |
| 48 | scope.define("sym", Value::Module(Arc::new(module("sym", SYM)))); |
| 49 | scope.define("emoji", Value::Module(Arc::new(module("emoji", EMOJI)))); |
| 50 | } |
| 51 | |
| 52 | fn module(name: &str, table: &'static [(&'static str, SymDef)]) -> Module { |
| 53 | let mut s = Scope::new(); |
| 54 | for (n, (k, def)) in table.iter().enumerate() { |
| 55 | crate::eval::lib::foundations::define_nth(&mut s, n, k, def_value(k, *def)); |
| 56 | } |
| 57 | Module::new(name, s) |
| 58 | } |
| 59 | |
| 60 | fn def_value(name: &str, def: SymDef) -> Value { |
| 61 | match def { |
| 62 | SymDef::One(t) => Value::Symbol(Symbol { variants: SymVariants::Single(t), modifiers: Arc::new(String::new()) }), |
| 63 | SymDef::Many(v) => Value::Symbol(Symbol { variants: SymVariants::Static(v), modifiers: Arc::new(String::new()) }), |
| 64 | SymDef::Module(m) => Value::Module(Arc::new(module(name, m))), |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// A symbol by its dotted path in `sym` (`"arrow.r.long"`), for maths mode and shorthands. |
| 69 | pub fn lookup(path: &str) -> Option<Value> { |
| 70 | lookup_in(SYM, path) |
| 71 | } |
| 72 | |
| 73 | /// As [`lookup`], in `emoji`. |
| 74 | pub fn lookup_emoji(path: &str) -> Option<Value> { |
| 75 | lookup_in(EMOJI, path) |
| 76 | } |
| 77 | |
| 78 | fn lookup_in(table: &'static [(&'static str, SymDef)], path: &str) -> Option<Value> { |
| 79 | let mut parts = path.split('.'); |
| 80 | let head = parts.next().unwrap_or(""); |
| 81 | let def = match table.iter().find(|(k, _)| *k == head) { |
| 82 | Some((_, d)) => *d, |
| 83 | None => return None, |
| 84 | }; |
| 85 | match def { |
| 86 | SymDef::Module(m) => { |
| 87 | let rest: Vec<&str> = parts.collect(); |
| 88 | if rest.is_empty() { |
| 89 | return Some(def_value(head, def)); |
| 90 | } |
| 91 | lookup_in(m, &rest.join(".")) |
| 92 | } |
| 93 | _ => { |
| 94 | let mut v = def_value(head, def); |
| 95 | for p in parts { |
| 96 | v = match &v { |
| 97 | Value::Symbol(s) => match modify(s, p) { |
| 98 | Some(m) => Value::Symbol(m), |
| 99 | None => return None, |
| 100 | }, |
| 101 | _ => return None, |
| 102 | }; |
| 103 | } |
| 104 | Some(v) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | pub fn method(_name: &str) -> Option<SymFn> { None } |
| 110 | |
| 111 | // Each variant as (modifiers, text), whatever the storage. |
| 112 | fn variants(s: &Symbol) -> Vec<(&str, &str)> { |
| 113 | match &s.variants { |
| 114 | SymVariants::Single(t) => vec![("", *t)], |
| 115 | SymVariants::Static(v) => v.iter().map(|(m, t)| (*m, *t)).collect(), |
| 116 | SymVariants::Runtime(v) => v.iter().map(|(m, t)| (m.as_str(), t.as_str())).collect(), |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fn mod_set(s: &str) -> Vec<&str> { s.split('.').filter(|m| !m.is_empty()).collect() } |
| 121 | |
| 122 | // The best variant for a set of applied modifiers, per codex's `best_match_in`. |
| 123 | fn best<'a>(vars: &[(&'a str, &'a str)], applied: &[&str]) -> Option<&'a str> { |
| 124 | let mut best: Option<(&str, usize)> = None; |
| 125 | for (m, t) in vars { |
| 126 | let set = mod_set(m); |
| 127 | if !applied.iter().all(|a| set.contains(a)) { |
| 128 | continue; |
| 129 | } |
| 130 | let total = set.len(); |
| 131 | if best.map(|(_, b)| total < b).unwrap_or(true) { |
| 132 | best = Some((t, total)); |
| 133 | } |
| 134 | } |
| 135 | best.map(|(t, _)| t) |
| 136 | } |
| 137 | |
| 138 | /// `symbol.modifier`: the symbol with one more modifier applied, or `None` when no variant has them all |
| 139 | /// ("unknown symbol modifier"). |
| 140 | pub fn modify(s: &Symbol, modifier: &str) -> Option<Symbol> { |
| 141 | let mut applied = mod_set(&s.modifiers); |
| 142 | applied.push(modifier); |
| 143 | let vars = variants(s); |
| 144 | best(&vars, &applied).map(|_| Symbol { variants: s.variants.clone(), modifiers: Arc::new(applied.join(".")) }) |
| 145 | } |
| 146 | |
| 147 | /// The text a symbol stands for with its modifiers applied. |
| 148 | pub fn text(s: &Symbol) -> &str { |
| 149 | let applied = mod_set(&s.modifiers); |
| 150 | let vars = variants(s); |
| 151 | best(&vars, &applied).unwrap_or("") |
| 152 | } |
| 153 | |
| 154 | pub fn repr_symbol(s: &Symbol) -> String { |
| 155 | if let SymVariants::Single(t) = &s.variants { |
| 156 | return fmt!("symbol({})", repr_str(t)); |
| 157 | } |
| 158 | let applied = mod_set(&s.modifiers); |
| 159 | let vars = variants(s); |
| 160 | if vars.len() == 1 && vars[0].0.is_empty() && applied.is_empty() { |
| 161 | return fmt!("symbol({})", repr_str(vars[0].1)); |
| 162 | } |
| 163 | let parts: Vec<String> = vars.iter() |
| 164 | .filter(|(m, _)| { |
| 165 | let set = mod_set(m); |
| 166 | applied.iter().all(|a| set.contains(a)) |
| 167 | }) |
| 168 | .map(|(m, t)| { |
| 169 | let rest: Vec<&str> = mod_set(m).into_iter().filter(|x| !applied.contains(x)).collect(); |
| 170 | if rest.is_empty() { |
| 171 | repr_str(t) |
| 172 | } else { |
| 173 | fmt!("({}, {})", repr_str(&rest.join(".")), repr_str(t)) |
| 174 | } |
| 175 | }) |
| 176 | .collect(); |
| 177 | fmt!("symbol{}", pretty_array_like(&parts, false)) |
| 178 | } |
| 179 | |
| 180 | pub fn call(_f: SymFn, engine: &mut Engine, mut args: Args) -> Outcome<Value> { |
| 181 | let span = args.span; |
| 182 | let items = res!(args.all::<Value>()); |
| 183 | res!(finish(engine, args)); |
| 184 | let mut vars: Vec<(String, String)> = Vec::with_capacity(items.len()); |
| 185 | for v in items { |
| 186 | match v { |
| 187 | Value::Str(s) => vars.push((String::new(), (*s).clone())), |
| 188 | Value::Array(a) if a.len() == 2 => match (&a[0], &a[1]) { |
| 189 | (Value::Str(m), Value::Str(t)) => vars.push(((**m).clone(), (**t).clone())), |
| 190 | _ => return Err(engine.error(DiagnosticKind::Type, span, "variant must be a pair of strings")), |
| 191 | }, |
| 192 | other => return Err(mismatch(engine, span, "string or array", &other)), |
| 193 | } |
| 194 | } |
| 195 | if vars.is_empty() { |
| 196 | return Err(engine.error(DiagnosticKind::Type, span, "expected at least one variant")); |
| 197 | } |
| 198 | for (i, (m, _)) in vars.iter().enumerate() { |
| 199 | if vars[..i].iter().any(|(n, _)| { |
| 200 | let (mut a, mut b) = (mod_set(n), mod_set(m)); |
| 201 | a.sort_unstable(); |
| 202 | b.sort_unstable(); |
| 203 | a == b |
| 204 | }) { |
| 205 | return Err(engine.error(DiagnosticKind::Type, span, if m.is_empty() { |
| 206 | "duplicate default variant".to_string() |
| 207 | } else { |
| 208 | fmt!("duplicate variant: {}", repr_str(m)) |
| 209 | })); |
| 210 | } |
| 211 | } |
| 212 | Ok(Value::Symbol(Symbol { variants: SymVariants::Runtime(Arc::new(vars)), modifiers: Arc::new(String::new()) })) |
| 213 | } |