#![allow(dead_code)]
#![allow(clippy::result_large_err)]
use std::path::{Path, PathBuf};
use indexmap::IndexMap;
use tabnas::{Tabnas, TabnasError, Value};
use tabnas_expr::{ExprOptions, Op, OpDef, PrevalDef, Seg, Site};
use tabnas_support::Failure;
pub fn repo_root() -> &'static Path {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("rs/ has a parent")
}
pub fn spec_dir() -> PathBuf {
repo_root().join("test").join("spec")
}
pub fn parser_for(options: serde_json::Value) -> Tabnas {
let mut parser = tabnas_jsonic::make();
parser
.use_plugin(tabnas_expr::plugin(), Some(Value::from_json(&options)))
.unwrap_or_else(|error| panic!("expr plugin: {error}"));
parser
}
pub fn parser_default() -> Tabnas {
parser_for(serde_json::json!({}))
}
pub fn parser_with(options: ExprOptions) -> Tabnas {
tabnas_expr::make_with(options)
}
pub fn to_failure(error: tabnas::TabnasError) -> Failure {
Failure::new(error.code.clone())
.at(error.row, error.col)
.with_message(error.to_string())
}
pub fn parse_simplified(
parser: &Tabnas,
src: &str,
) -> Result<serde_json::Value, tabnas::TabnasError> {
tabnas_expr::parse_simplified(parser, src).map(|value| value.to_json())
}
pub fn norm(value: serde_json::Value) -> serde_json::Value {
match value {
serde_json::Value::Number(number) => {
serde_json::Number::from_f64(number.as_f64().expect("a JSON number is representable"))
.map_or(serde_json::Value::Null, serde_json::Value::Number)
}
serde_json::Value::Array(entries) => {
serde_json::Value::Array(entries.into_iter().map(norm).collect())
}
serde_json::Value::Object(entries) => serde_json::Value::Object(
entries
.into_iter()
.map(|(key, entry)| (key, norm(entry)))
.collect(),
),
other => other,
}
}
pub fn preval(active: bool, required: bool, allow: Option<Vec<String>>) -> PrevalDef {
PrevalDef {
active,
required,
allow,
}
}
pub fn preval_paren(osrc: &str, csrc: &str) -> OpDef {
OpDef::paren(osrc, csrc).with_preval(preval(true, false, None))
}
pub fn no_defaults() -> ExprOptions {
[
"positive",
"negative",
"addition",
"subtraction",
"multiplication",
"division",
"remainder",
"plain",
]
.into_iter()
.fold(ExprOptions::new(), |options, name| options.without_op(name))
}
pub fn table(ops: &[(&str, OpDef)]) -> IndexMap<String, Option<OpDef>> {
ops.iter()
.map(|(name, def)| ((*name).to_string(), Some(def.clone())))
.collect()
}
pub fn key_parser(ops: &[(&str, OpDef)]) -> Tabnas {
let options = ops.iter().fold(no_defaults(), |options, (name, def)| {
options.with_op(*name, def.clone())
});
tabnas_expr::make_with(options)
}
pub fn text_of(value: &Value) -> Option<&str> {
match value {
Value::String(text) => Some(text),
Value::Text(text) => Some(&text.string),
_ => None,
}
}
pub fn pointer(fragment: &str) -> Vec<Seg> {
let mut rest = fragment.chars();
rest.next();
if rest.as_str().is_empty() {
return Vec::new();
}
rest.next();
rest.as_str()
.split('/')
.map(|part| Seg::Name(part.replace("~1", "/").replace("~0", "~")))
.collect()
}
pub fn spread(target: &Value, siblings: &Value) -> Value {
let mut out: IndexMap<String, Value> = IndexMap::new();
for value in [target, siblings] {
let members = match value {
Value::Object(members) => (**members).clone(),
Value::MapRef(map) => map.value.clone(),
_ => IndexMap::new(),
};
for (name, member) in members {
out.insert(name, member);
}
}
Value::object(out)
}
pub fn resolving(site: &Site<'_>, op: &Op, terms: &[Value]) -> Result<Value, TabnasError> {
resolving_by(false, site, op, terms)
}
pub fn writing(site: &Site<'_>, op: &Op, terms: &[Value]) -> Result<Value, TabnasError> {
resolving_by(true, site, op, terms)
}
fn resolving_by(
written: bool,
site: &Site<'_>,
op: &Op,
terms: &[Value],
) -> Result<Value, TabnasError> {
if !op.key {
return Ok(Value::Number(f64::NAN));
}
let Some(reference) = terms.first().and_then(text_of) else {
return Ok(site.node().clone());
};
let path = pointer(reference);
let hit = if written {
site.written(&path, None)?
} else {
site.at(&path, None)?
};
let value = match hit.value {
Some(value) if hit.found && !hit.cycle => value,
_ => return Ok(site.node().clone()),
};
Ok(match (&value, terms.get(1)) {
(Value::Object(_) | Value::MapRef(_), Some(siblings)) => spread(&value, siblings),
_ => value,
})
}
pub fn canonical(value: &Value) -> String {
let mut out = String::new();
write_canonical(&mut out, value);
out
}
fn write_canonical(out: &mut String, value: &Value) {
let string = |text: &str| serde_json::to_string(text).expect("a string is JSON");
match value {
Value::Undefined | Value::Null => out.push_str("null"),
Value::Bool(flag) => out.push_str(if *flag { "true" } else { "false" }),
Value::Number(number) => out.push_str(&js_number(*number)),
Value::String(text) => out.push_str(&string(text)),
Value::Text(text) => out.push_str(&string(&text.string)),
Value::Array(items) => write_items(out, items),
Value::ListRef(list) => write_items(out, &list.value),
Value::Object(members) => write_members(out, members.iter()),
Value::MapRef(map) => write_members(out, map.value.iter()),
}
}
fn write_items(out: &mut String, items: &[Value]) {
out.push('[');
for (at, item) in items.iter().enumerate() {
if 0 < at {
out.push(',');
}
write_canonical(out, item);
}
out.push(']');
}
fn write_members<'a>(out: &mut String, members: impl Iterator<Item = (&'a String, &'a Value)>) {
out.push('{');
let mut first = true;
for (name, member) in members {
if member.is_undefined() {
continue;
}
if !first {
out.push(',');
}
first = false;
out.push_str(&serde_json::to_string(name).expect("a key is JSON"));
out.push(':');
write_canonical(out, member);
}
out.push('}');
}
pub fn js_number(number: f64) -> String {
if !number.is_finite() {
return "null".to_string();
}
if 0.0 == number {
return "0".to_string();
}
let magnitude = number.abs();
let (exact, n) = exact_decimal(magnitude);
let reads_back = |digits: &[u8], n: i32| {
let text: String = digits
.iter()
.map(|digit| char::from(b'0' + digit))
.collect();
format!("0.{text}e{n}").parse::<f64>() == Ok(magnitude)
};
let mut body = None;
for k in 1..=exact.len() {
let low = &exact[..k];
let rest = &exact[k..];
if rest.iter().all(|digit| 0 == *digit) {
body = Some(js_layout(low, n));
break;
}
let mut high = low.to_vec();
let mut high_n = n;
match high.iter().rposition(|digit| 9 != *digit) {
Some(at) => {
high[at] += 1;
high[at + 1..].fill(0);
}
None => {
high.fill(0);
high[0] = 1;
high_n += 1;
}
}
let take_high = match (reads_back(low, n), reads_back(&high, high_n)) {
(false, false) => continue,
(true, false) => false,
(false, true) => true,
(true, true) => match rest[0].cmp(&5) {
std::cmp::Ordering::Less => false,
std::cmp::Ordering::Greater => true,
std::cmp::Ordering::Equal => {
rest[1..].iter().any(|digit| 0 != *digit) || 1 == low[k - 1] % 2
}
},
};
body = Some(if take_high {
js_layout(&high, high_n)
} else {
js_layout(low, n)
});
break;
}
let body = body.expect("the exact digits read back");
if number < 0.0 {
format!("-{body}")
} else {
body
}
}
fn exact_decimal(magnitude: f64) -> (Vec<u8>, i32) {
const BASE: u64 = 1_000_000_000;
let bits = magnitude.to_bits();
let fraction = bits & ((1 << 52) - 1);
let biased = ((bits >> 52) & 0x7ff) as i32;
let (mantissa, exponent) = if 0 == biased {
(fraction, -1074)
} else {
(fraction | (1 << 52), biased - 1075)
};
let mut limbs = vec![
mantissa % BASE,
mantissa / BASE % BASE,
mantissa / BASE / BASE,
];
let mut multiply = |factor: u64| {
let mut carry = 0;
for limb in limbs.iter_mut() {
let product = *limb * factor + carry;
*limb = product % BASE;
carry = product / BASE;
}
while 0 < carry {
limbs.push(carry % BASE);
carry /= BASE;
}
};
let (base, step) = if 0 <= exponent {
(2u64, 30)
} else {
(5u64, 13)
};
let mut count = exponent.unsigned_abs();
while 0 < count {
let now = count.min(step);
multiply(base.pow(now));
count -= now;
}
let mut text = String::new();
for limb in limbs.iter().rev() {
text.push_str(&format!("{limb:09}"));
}
let digits: Vec<u8> = text
.trim_start_matches('0')
.bytes()
.map(|byte| byte - b'0')
.collect();
let n = digits.len() as i32 + exponent.min(0);
(digits, n)
}
fn js_layout(digits: &[u8], n: i32) -> String {
let text: String = digits
.iter()
.map(|digit| char::from(b'0' + digit))
.collect();
let digits = text.trim_end_matches('0');
let k = digits.len() as i32;
if k <= n && n <= 21 {
format!("{digits}{}", "0".repeat((n - k) as usize))
} else if 0 < n && n <= 21 {
format!("{}.{}", &digits[..n as usize], &digits[n as usize..])
} else if -6 < n && n <= 0 {
format!("0.{}{digits}", "0".repeat(n.unsigned_abs() as usize))
} else {
let sign = if n < 1 { '-' } else { '+' };
let power = (n - 1).abs();
if 1 == k {
format!("{digits}e{sign}{power}")
} else {
format!("{}.{}e{sign}{power}", &digits[..1], &digits[1..])
}
}
}