| @@ -1,5 +1,8 @@ |
| 1 | | -//! Built-in functions — the single-word, non-temporal slice (DECISIONS.md |
| 2 | | -//! ADR-15). |
| 1 | +//! Built-in functions — the non-temporal slice (DECISIONS.md ADR-15/ADR-17). |
| 2 | +//! |
| 3 | +//! Single-word (`abs`, `count`) and multi-word (`string length`, `list |
| 4 | +//! contains`, `get value`) names alike; the lexer hands a space-normalised name |
| 5 | +//! string here, so dispatch is a plain match either way (ADR-17). |
| 3 | 6 | //! |
| 4 | 7 | //! Dispatch is by name + arity. Error policy follows ADR-14: a type/arity/domain |
| 5 | 8 | //! error (or an unknown function) is an *error-null* (`Value::Error`), while a |
| @@ -18,6 +21,7 @@ |
| 18 | 21 | //! match/replace/split itself live in the provider. |
| 19 | 22 | |
| 20 | 23 | use std::cmp::Ordering; |
| 24 | +use std::collections::BTreeMap; |
| 21 | 25 | |
| 22 | 26 | use crate::eval::{as_number, feel_order}; |
| 23 | 27 | use crate::regex_provider::{match_value, FeelRegex}; |
| @@ -50,6 +54,14 @@ |
| 50 | 54 | "replace" => replace_fn(args, regex), |
| 51 | 55 | "split" => split_fn(args, regex), |
| 52 | 56 | "matches" => matches_fn(args, regex), |
| 57 | + "string length" => string_length_fn(args), |
| 58 | + "upper case" => case_fn(args, true), |
| 59 | + "lower case" => case_fn(args, false), |
| 60 | + "substring before" => substring_before_after_fn(args, true), |
| 61 | + "substring after" => substring_before_after_fn(args, false), |
| 62 | + "starts with" => starts_ends_with_fn(args, true), |
| 63 | + "ends with" => starts_ends_with_fn(args, false), |
| 64 | + "string join" => string_join_fn(args), |
| 53 | 65 | |
| 54 | 66 | // --- list --- |
| 55 | 67 | "count" => count_fn(args), |
| @@ -70,6 +82,15 @@ |
| 70 | 82 | "reverse" => reverse_fn(args), |
| 71 | 83 | "flatten" => flatten_fn(args), |
| 72 | 84 | "sublist" => sublist_fn(args), |
| 85 | + "list contains" => list_contains_fn(args), |
| 86 | + "index of" => index_of_fn(args), |
| 87 | + "distinct values" => distinct_values_fn(args), |
| 88 | + "insert before" => insert_before_fn(args), |
| 89 | + "list replace" => list_replace_fn(args), |
| 90 | + |
| 91 | + // --- context --- |
| 92 | + "get value" => get_value_fn(args), |
| 93 | + "get entries" => get_entries_fn(args), |
| 73 | 94 | |
| 74 | 95 | // --- numeric --- |
| 75 | 96 | "decimal" => decimal_fn(args), |
| @@ -293,6 +314,92 @@ |
| 293 | 314 | } |
| 294 | 315 | } |
| 295 | 316 | |
| 317 | +fn string_length_fn(args: Vec<Value>) -> Value { |
| 318 | + match args.as_slice() { |
| 319 | + // FEEL string length counts code points, not UTF-8 bytes. |
| 320 | + [Value::String(s)] => Value::Number(s.chars().count() as f64), |
| 321 | + [Value::Null] => Value::Null, |
| 322 | + _ => Value::Error, |
| 323 | + } |
| 324 | +} |
| 325 | + |
| 326 | +fn case_fn(args: Vec<Value>, upper: bool) -> Value { |
| 327 | + match args.as_slice() { |
| 328 | + [Value::String(s)] => { |
| 329 | + Value::String(if upper { s.to_uppercase() } else { s.to_lowercase() }) |
| 330 | + } |
| 331 | + [Value::Null] => Value::Null, |
| 332 | + _ => Value::Error, |
| 333 | + } |
| 334 | +} |
| 335 | + |
| 336 | +fn starts_ends_with_fn(args: Vec<Value>, starts: bool) -> Value { |
| 337 | + match args.as_slice() { |
| 338 | + [Value::String(s), Value::String(m)] => Value::Boolean(if starts { |
| 339 | + s.starts_with(m.as_str()) |
| 340 | + } else { |
| 341 | + s.ends_with(m.as_str()) |
| 342 | + }), |
| 343 | + [Value::Null, _] | [_, Value::Null] => Value::Null, |
| 344 | + _ => Value::Error, |
| 345 | + } |
| 346 | +} |
| 347 | + |
| 348 | +/// `substring before`/`substring after`: split `string` at the **first** |
| 349 | +/// occurrence of `match`. Not found (for a non-empty match) is `""`. An empty |
| 350 | +/// match sits at position 0, so `before` is `""` and `after` is the whole string. |
| 351 | +fn substring_before_after_fn(args: Vec<Value>, before: bool) -> Value { |
| 352 | + match args.as_slice() { |
| 353 | + [Value::String(s), Value::String(m)] => { |
| 354 | + // A byte offset from `find` is always a valid char boundary because it |
| 355 | + // is the start of an actual substring match. |
| 356 | + let part = match s.find(m.as_str()) { |
| 357 | + Some(p) if before => &s[..p], |
| 358 | + Some(p) => &s[p + m.len()..], |
| 359 | + None => "", |
| 360 | + }; |
| 361 | + Value::String(part.to_string()) |
| 362 | + } |
| 363 | + [Value::Null, _] | [_, Value::Null] => Value::Null, |
| 364 | + _ => Value::Error, |
| 365 | + } |
| 366 | +} |
| 367 | + |
| 368 | +/// `string join(list[, delimiter])`: concatenate the string items of `list`, |
| 369 | +/// joined by `delimiter` (absent/null → no separator). Null items are skipped; a |
| 370 | +/// non-string, non-null item is a type error. A scalar string argument is treated |
| 371 | +/// as a one-element list. Other arities (0, 3, …) and non-list/-string arguments |
| 372 | +/// are errors (per DMN-TCK 1140). |
| 373 | +fn string_join_fn(args: Vec<Value>) -> Value { |
| 374 | + let (list_v, delim) = match args.as_slice() { |
| 375 | + [l] => (l, String::new()), |
| 376 | + [l, d] => { |
| 377 | + let d = match d { |
| 378 | + Value::String(s) => s.clone(), |
| 379 | + Value::Null => String::new(), |
| 380 | + _ => return Value::Error, |
| 381 | + }; |
| 382 | + (l, d) |
| 383 | + } |
| 384 | + _ => return Value::Error, |
| 385 | + }; |
| 386 | + let items = match list_v { |
| 387 | + Value::List(items) => items.clone(), |
| 388 | + Value::String(s) => vec![Value::String(s.clone())], |
| 389 | + Value::Null => return Value::Null, |
| 390 | + _ => return Value::Error, |
| 391 | + }; |
| 392 | + let mut parts: Vec<String> = Vec::new(); |
| 393 | + for it in &items { |
| 394 | + match it { |
| 395 | + Value::String(s) => parts.push(s.clone()), |
| 396 | + Value::Null => {} // null entries are skipped |
| 397 | + _ => return Value::Error, |
| 398 | + } |
| 399 | + } |
| 400 | + Value::String(parts.join(&delim)) |
| 401 | +} |
| 402 | + |
| 296 | 403 | // --------------------------------------------------------------------------- // |
| 297 | 404 | // String (regex) — dialect-dependent; the actual engine is the host's (ADR-16). |
| 298 | 405 | // These functions only sort out the FEEL argument shapes and hand pattern + |
| @@ -688,6 +795,135 @@ |
| 688 | 795 | Value::List(items[from0 as usize..(from0 + take) as usize].to_vec()) |
| 689 | 796 | } |
| 690 | 797 | |
| 798 | +fn list_contains_fn(args: Vec<Value>) -> Value { |
| 799 | + match args.as_slice() { |
| 800 | + [Value::List(items), e] => Value::Boolean(items.iter().any(|x| x == e)), |
| 801 | + [Value::Null, _] => Value::Null, |
| 802 | + _ => Value::Error, |
| 803 | + } |
| 804 | +} |
| 805 | + |
| 806 | +/// `index of(list, match)`: the 1-based positions where an element equals `match` |
| 807 | +/// (FEEL equality), as a list (empty if none). |
| 808 | +fn index_of_fn(args: Vec<Value>) -> Value { |
| 809 | + match args.as_slice() { |
| 810 | + [Value::List(items), m] => Value::List( |
| 811 | + items |
| 812 | + .iter() |
| 813 | + .enumerate() |
| 814 | + .filter(|(_, x)| *x == m) |
| 815 | + .map(|(i, _)| Value::Number((i + 1) as f64)) |
| 816 | + .collect(), |
| 817 | + ), |
| 818 | + [Value::Null, _] => Value::Null, |
| 819 | + _ => Value::Error, |
| 820 | + } |
| 821 | +} |
| 822 | + |
| 823 | +/// `distinct values(list)`: drop duplicates (FEEL equality), keeping first order. |
| 824 | +fn distinct_values_fn(args: Vec<Value>) -> Value { |
| 825 | + match args.as_slice() { |
| 826 | + [Value::List(items)] => { |
| 827 | + let mut out: Vec<Value> = Vec::new(); |
| 828 | + for it in items { |
| 829 | + if !out.iter().any(|x| x == it) { |
| 830 | + out.push(it.clone()); |
| 831 | + } |
| 832 | + } |
| 833 | + Value::List(out) |
| 834 | + } |
| 835 | + [Value::Null] => Value::Null, |
| 836 | + _ => Value::Error, |
| 837 | + } |
| 838 | +} |
| 839 | + |
| 840 | +/// `insert before(list, position, newItem)`: insert `newItem` so it ends up at |
| 841 | +/// `position` (1-based, negative from the end). The position must address an |
| 842 | +/// existing element; out of range is an error. |
| 843 | +fn insert_before_fn(args: Vec<Value>) -> Value { |
| 844 | + match args.as_slice() { |
| 845 | + [Value::List(items), pos_v, new_item] => { |
| 846 | + let pos = match num_arg(pos_v) { |
| 847 | + NumArg::Num(p) => p.trunc() as i64, |
| 848 | + NumArg::Null => return Value::Null, |
| 849 | + NumArg::Err => return Value::Error, |
| 850 | + }; |
| 851 | + let n = items.len() as i64; |
| 852 | + match resolve_index(pos, n) { |
| 853 | + Some(idx) if idx < n => { |
| 854 | + let mut out = items.clone(); |
| 855 | + out.insert(idx as usize, new_item.clone()); |
| 856 | + Value::List(out) |
| 857 | + } |
| 858 | + _ => Value::Error, |
| 859 | + } |
| 860 | + } |
| 861 | + [Value::Null, _, _] => Value::Null, |
| 862 | + _ => Value::Error, |
| 863 | + } |
| 864 | +} |
| 865 | + |
| 866 | +/// `list replace(list, position, newItem)` — the positional form only; the |
| 867 | +/// matching-function form needs function values and stays deferred (ADR-15). A |
| 868 | +/// scalar `list` is treated as a one-element list (per DMN-TCK 1155). |
| 869 | +fn list_replace_fn(args: Vec<Value>) -> Value { |
| 870 | + let [list_v, pos_v, new_item] = args.as_slice() else { |
| 871 | + return Value::Error; |
| 872 | + }; |
| 873 | + let mut items = match list_v { |
| 874 | + Value::List(items) => items.clone(), |
| 875 | + Value::Null => return Value::Null, |
| 876 | + other => vec![other.clone()], |
| 877 | + }; |
| 878 | + let pos = match num_arg(pos_v) { |
| 879 | + NumArg::Num(p) => p.trunc() as i64, |
| 880 | + NumArg::Null => return Value::Null, |
| 881 | + NumArg::Err => return Value::Error, |
| 882 | + }; |
| 883 | + let n = items.len() as i64; |
| 884 | + match resolve_index(pos, n) { |
| 885 | + Some(idx) if idx < n => { |
| 886 | + items[idx as usize] = new_item.clone(); |
| 887 | + Value::List(items) |
| 888 | + } |
| 889 | + _ => Value::Error, |
| 890 | + } |
| 891 | +} |
| 892 | + |
| 893 | +// --------------------------------------------------------------------------- // |
| 894 | +// Context |
| 895 | +// --------------------------------------------------------------------------- // |
| 896 | + |
| 897 | +/// `get value(context, key)`: the value bound to `key`, or `null` if absent |
| 898 | +/// (including a present `null` value). A non-context or non-string key is an |
| 899 | +/// error (per DMN-TCK 0080). |
| 900 | +fn get_value_fn(args: Vec<Value>) -> Value { |
| 901 | + match args.as_slice() { |
| 902 | + [Value::Context(map), Value::String(key)] => map.get(key).cloned().unwrap_or(Value::Null), |
| 903 | + [Value::Null, _] | [Value::Context(_), Value::Null] => Value::Null, |
| 904 | + _ => Value::Error, |
| 905 | + } |
| 906 | +} |
| 907 | + |
| 908 | +/// `get entries(context)`: a list of `{key, value}` contexts, one per entry, in |
| 909 | +/// key order (per DMN-TCK 0081). |
| 910 | +fn get_entries_fn(args: Vec<Value>) -> Value { |
| 911 | + match args.as_slice() { |
| 912 | + [Value::Context(map)] => Value::List( |
| 913 | + map.iter() |
| 914 | + .map(|(k, v)| { |
| 915 | + let mut entry = BTreeMap::new(); |
| 916 | + entry.insert("key".to_string(), Value::String(k.clone())); |
| 917 | + entry.insert("value".to_string(), v.clone()); |
| 918 | + Value::Context(entry) |
| 919 | + }) |
| 920 | + .collect(), |
| 921 | + ), |
| 922 | + [Value::Null] => Value::Null, |
| 923 | + _ => Value::Error, |
| 924 | + } |
| 925 | +} |
| 926 | + |
| 691 | 927 | // --------------------------------------------------------------------------- // |
| 692 | 928 | // Numeric |
| 693 | 929 | // --------------------------------------------------------------------------- // |