use std::borrow::Cow;
use std::cell::{Cell, OnceCell, RefCell};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::rc::Rc;
use std::sync::Arc;
use indexmap::IndexMap;
use tabnas::{TabnasError, Token, Value};
use crate::{
new_node, node_id, released_handle, with_node, Op, OpDef, MAX_SAFE_INTEGER, MIN_SAFE_INTEGER,
};
pub const FOLD_CYCLE: &str = "fold_cycle";
pub const FOLD_DEPTH: &str = "fold_depth";
pub const FOLD_INHERIT: &str = "fold_inherit";
pub const FOLD_KEY: &str = "fold_key";
pub const FOLD_OPTION: &str = "fold_option";
#[derive(Debug, Clone, PartialEq)]
pub struct FoldError(Box<FoldErrorData>);
#[derive(Debug, Clone, PartialEq)]
struct FoldErrorData {
code: String,
message: String,
doc: Option<String>,
at: Option<String>,
target_doc: Option<String>,
target: Option<String>,
cause: Option<TabnasError>,
}
impl FoldError {
fn new(code: &str, message: impl fmt::Display) -> Self {
FoldError(Box::new(FoldErrorData {
code: code.to_string(),
message: format!("expr fold: {message}"),
doc: None,
at: None,
target_doc: None,
target: None,
cause: None,
}))
}
fn callback(cause: TabnasError) -> Self {
FoldError(Box::new(FoldErrorData {
code: cause.code.clone(),
message: cause.detail.clone(),
doc: None,
at: None,
target_doc: None,
target: None,
cause: Some(cause),
}))
}
fn located(mut self, doc: &str, at: Option<&str>) -> Self {
self.0.doc = Some(doc.to_string());
self.0.at = at.map(str::to_string);
self
}
fn targeted(mut self, target_doc: &str, target: &str) -> Self {
self.0.target_doc = Some(target_doc.to_string());
self.0.target = Some(target.to_string());
self
}
pub fn code(&self) -> &str {
&self.0.code
}
pub fn message(&self) -> &str {
&self.0.message
}
pub fn doc(&self) -> Option<&str> {
self.0.doc.as_deref()
}
pub fn at(&self) -> Option<&str> {
self.0.at.as_deref()
}
pub fn target_doc(&self) -> Option<&str> {
self.0.target_doc.as_deref()
}
pub fn target(&self) -> Option<&str> {
self.0.target.as_deref()
}
pub fn cause(&self) -> Option<&TabnasError> {
self.0.cause.as_ref()
}
}
impl fmt::Display for FoldError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.0.message)
}
}
impl std::error::Error for FoldError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.0
.cause
.as_ref()
.map(|cause| cause as &(dyn std::error::Error + 'static))
}
}
impl From<FoldError> for TabnasError {
fn from(error: FoldError) -> Self {
let data = *error.0;
if let Some(cause) = data.cause {
return cause;
}
let mut out = TabnasError::new(data.code, "", "", 0, 1, 1);
out.detail = data.message;
out.hint = String::new();
out
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Seg {
Name(String),
Index(usize),
}
impl Seg {
fn text(&self) -> Cow<'_, str> {
match self {
Seg::Name(name) => Cow::Borrowed(name),
Seg::Index(index) => Cow::Owned(index.to_string()),
}
}
}
impl From<&str> for Seg {
fn from(name: &str) -> Self {
Seg::Name(name.to_string())
}
}
impl From<String> for Seg {
fn from(name: String) -> Self {
Seg::Name(name)
}
}
impl From<usize> for Seg {
fn from(index: usize) -> Self {
Seg::Index(index)
}
}
impl fmt::Display for Seg {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.text())
}
}
pub type Path = Vec<Seg>;
#[derive(Debug, Clone, PartialEq)]
pub struct FoldHit {
pub found: bool,
pub cycle: bool,
pub value: Option<Value>,
}
impl FoldHit {
fn missing() -> Self {
FoldHit {
found: false,
cycle: false,
value: None,
}
}
fn cycle() -> Self {
FoldHit {
found: true,
cycle: true,
value: None,
}
}
fn found(value: Value) -> Self {
FoldHit {
found: true,
cycle: false,
value: Some(value),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FoldEntry {
pub doc: String,
pub at: String,
pub target_doc: String,
pub target: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FoldDynamic {
pub doc: String,
pub at: String,
pub src: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FoldReport {
pub cycles: Vec<FoldEntry>,
pub unresolved: Vec<FoldEntry>,
pub dynamic: Vec<FoldDynamic>,
}
pub type FoldEvaluate<'a> =
Box<dyn Fn(&Site<'_>, &Op, &[Value]) -> Result<Value, TabnasError> + 'a>;
pub type Inherit<'a> = Box<dyn Fn(&Site<'_>, &Value, &Value) -> Result<Value, TabnasError> + 'a>;
pub type Recognise<'a> = Box<dyn Fn(&Site<'_>, &Op) -> Result<bool, TabnasError> + 'a>;
pub type Load<'a> = Box<dyn Fn(&str, &Site<'_>) -> Result<Option<Value>, TabnasError> + 'a>;
pub type Positions<'a> = Box<dyn Fn(&Value) -> Option<Token> + 'a>;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum FoldCycle {
#[default]
Verbatim,
Error,
}
pub struct FoldOptions<'a> {
pub op: IndexMap<String, Option<OpDef>>,
pub evaluate: FoldEvaluate<'a>,
pub inherit: Option<Inherit<'a>>,
pub recognise: Option<Recognise<'a>>,
pub k: Value,
pub doc: String,
pub load: Option<Load<'a>>,
pub cycle: FoldCycle,
pub tokens: bool,
pub positions: Option<Positions<'a>>,
pub max_depth: usize,
pub max_stack: usize,
pub report: Option<Box<dyn Fn(FoldReport) + 'a>>,
}
impl<'a> FoldOptions<'a> {
pub fn new(
evaluate: impl Fn(&Site<'_>, &Op, &[Value]) -> Result<Value, TabnasError> + 'a,
) -> Self {
FoldOptions {
op: IndexMap::new(),
evaluate: Box::new(evaluate),
inherit: None,
recognise: None,
k: Value::object(IndexMap::new()),
doc: String::new(),
load: None,
cycle: FoldCycle::Verbatim,
tokens: true,
positions: None,
max_depth: 1000,
max_stack: FOLD_MAX_STACK,
report: None,
}
}
pub fn with_op(mut self, name: impl Into<String>, def: OpDef) -> Self {
self.op.insert(name.into(), Some(def));
self
}
pub fn with_ops(mut self, op: IndexMap<String, Option<OpDef>>) -> Self {
self.op = op;
self
}
pub fn with_inherit(
mut self,
inherit: impl Fn(&Site<'_>, &Value, &Value) -> Result<Value, TabnasError> + 'a,
) -> Self {
self.inherit = Some(Box::new(inherit));
self
}
pub fn with_recognise(
mut self,
recognise: impl Fn(&Site<'_>, &Op) -> Result<bool, TabnasError> + 'a,
) -> Self {
self.recognise = Some(Box::new(recognise));
self
}
pub fn with_k(mut self, k: Value) -> Self {
self.k = k;
self
}
pub fn with_doc(mut self, doc: impl Into<String>) -> Self {
self.doc = doc.into();
self
}
pub fn with_load(
mut self,
load: impl Fn(&str, &Site<'_>) -> Result<Option<Value>, TabnasError> + 'a,
) -> Self {
self.load = Some(Box::new(load));
self
}
pub fn with_cycle(mut self, cycle: FoldCycle) -> Self {
self.cycle = cycle;
self
}
pub fn with_tokens(mut self, tokens: bool) -> Self {
self.tokens = tokens;
self
}
pub fn with_positions(mut self, positions: impl Fn(&Value) -> Option<Token> + 'a) -> Self {
self.positions = Some(Box::new(positions));
self
}
pub fn with_max_depth(mut self, max_depth: usize) -> Self {
self.max_depth = max_depth;
self
}
pub fn with_max_stack(mut self, max_stack: usize) -> Self {
self.max_stack = max_stack;
self
}
pub fn with_report(mut self, report: impl Fn(FoldReport) + 'a) -> Self {
self.report = Some(Box::new(report));
self
}
}
impl fmt::Debug for FoldOptions<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let function = |present: bool| if present { "<function>" } else { "none" };
formatter
.debug_struct("FoldOptions")
.field("op", &self.op)
.field("evaluate", &"<function>")
.field("inherit", &function(self.inherit.is_some()))
.field("recognise", &function(self.recognise.is_some()))
.field("k", &self.k)
.field("doc", &self.doc)
.field("load", &function(self.load.is_some()))
.field("cycle", &self.cycle)
.field("tokens", &self.tokens)
.field("positions", &function(self.positions.is_some()))
.field("max_depth", &self.max_depth)
.field("max_stack", &self.max_stack)
.field("report", &function(self.report.is_some()))
.finish()
}
}
#[derive(Debug, Clone, Default)]
pub struct KeyMap {
ops: Vec<Arc<Op>>,
}
impl KeyMap {
pub fn iter(&self) -> std::slice::Iter<'_, Arc<Op>> {
self.ops.iter()
}
pub fn get(&self, src: &str) -> Option<&Arc<Op>> {
self.ops.iter().find(|op| op.src == src)
}
pub fn len(&self) -> usize {
self.ops.len()
}
pub fn is_empty(&self) -> bool {
self.ops.is_empty()
}
}
pub fn make_key_map(ops: &IndexMap<String, Option<OpDef>>) -> Result<KeyMap, FoldError> {
let mut found: Vec<Op> = Vec::new();
let mut owner: HashMap<&str, &str> = HashMap::new();
for (name, def) in ops {
let Some(def) = def else { continue };
if !def.key {
continue;
}
let src = def.src.first().map(String::as_str).unwrap_or_default();
if src.is_empty() {
return Err(FoldError::new(
FOLD_KEY,
format_args!("key operator {} names no member", shown(name)),
));
}
if let Some(prior) = owner.get(src) {
return Err(FoldError::new(
FOLD_KEY,
format_args!(
"key operators {} and {} both name the member {}",
shown(prior),
shown(name),
shown(src)
),
));
}
owner.insert(src, name);
found.push(Op {
name: if name.ends_with("-key") {
name.clone()
} else {
format!("{name}-key")
},
src: src.to_string(),
left: MIN_SAFE_INTEGER,
right: MAX_SAFE_INTEGER,
use_data: def.use_data.clone(),
terms: 2,
key: true,
dynamic: def.dynamic,
..Default::default()
});
}
found.sort_by(|a, b| a.dynamic.cmp(&b.dynamic).then_with(|| a.src.cmp(&b.src)));
Ok(KeyMap {
ops: found.into_iter().map(Arc::new).collect(),
})
}
pub fn key_op_of(keys: &KeyMap, node: &Value) -> Option<Arc<Op>> {
let members = map_entries(node)?;
keys.ops
.iter()
.find(|op| members.contains_key(op.src.as_str()))
.cloned()
}
pub struct Site<'f> {
fold: &'f Folder<'f>,
pn: usize,
parent_k: Option<Value>,
doc: Rc<str>,
root: Value,
node: Value,
path: OnceCell<Path>,
}
impl Site<'_> {
pub fn root(&self) -> &Value {
&self.root
}
pub fn doc(&self) -> &str {
&self.doc
}
pub fn path(&self) -> &[Seg] {
self.path.get_or_init(|| self.fold.path_of(self.pn))
}
pub fn node(&self) -> &Value {
&self.node
}
pub fn k(&self) -> Result<Value, FoldError> {
match &self.parent_k {
Some(k) => Ok(k.clone()),
None => self.fold.k_of(self.pn),
}
}
pub fn token(&self) -> Option<Token> {
if node_id(&self.node).is_some() {
return with_node(&self.node, |entry| entry.token.clone()).flatten();
}
self.fold
.options
.positions
.as_ref()
.and_then(|lookup| lookup(&self.node))
}
pub fn at(&self, path: &[Seg], doc: Option<&str>) -> Result<FoldHit, FoldError> {
self.fold.at(self.pn, path, doc)
}
pub fn locate(&self, path: &[Seg], doc: Option<&str>) -> Result<Option<Value>, FoldError> {
self.fold.locate(self.pn, path, doc)
}
pub fn plain(&self) -> Result<Value, FoldError> {
self.fold.plain_of(self.pn)
}
}
impl fmt::Debug for Site<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Site")
.field("doc", &self.doc)
.field("path", &self.path())
.field("node", &self.node)
.finish()
}
}
pub fn fold(root: &Value, options: &FoldOptions<'_>) -> Result<Value, FoldError> {
let keys = make_key_map(&options.op)?;
let base = StackBase::enter();
let folder = Folder {
options,
keys,
stack_base: base.at,
main: Rc::from(options.doc.as_str()),
state: RefCell::new(State::default()),
};
let doc = folder.make_doc(&options.doc, root.clone());
let top = folder.state.borrow().docs[doc].top;
let out = folder.walk(top, FROM_PARENT)?;
if let Some(report) = &options.report {
let rows = std::mem::take(&mut folder.state.borrow_mut().report);
report(rows);
}
Ok(out)
}
const FREE: u8 = 0;
const ACTIVE: u8 = 1;
const DONE: u8 = 2;
const FROM_PARENT: u8 = 0;
const FROM_REFERENCE: u8 = 1;
const CYCLES: u8 = 0;
const UNRESOLVED: u8 = 1;
const DYNAMIC: u8 = 2;
pub const FOLD_MAX_STACK: usize = 1 << 20;
struct Folder<'f> {
options: &'f FoldOptions<'f>,
keys: KeyMap,
stack_base: usize,
main: Rc<str>,
state: RefCell<State>,
}
#[derive(Default)]
struct State {
nodes: Vec<PNode>,
docs: Vec<Doc>,
doc_keys: HashMap<String, Option<usize>>,
stack: Vec<usize>,
report: FoldReport,
noted: HashSet<(u8, String, String, String, String)>,
failure: Option<FoldError>,
}
struct Doc {
key: Rc<str>,
root: Value,
top: usize,
}
struct PNode {
doc: usize,
parent: Option<usize>,
seg: Seg,
raw: Value,
kids: Option<Box<Kids>>,
state: u8,
via: u8,
value: Value,
k: Option<Value>,
k_busy: bool,
cut: bool,
looped: bool,
site: Option<Option<Arc<Op>>>,
parts: Option<Box<Parts>>,
plain: Option<Value>,
ptr: Option<Rc<str>>,
}
impl PNode {
fn new(doc: usize, parent: Option<usize>, seg: Seg, raw: Value) -> Self {
PNode {
doc,
parent,
seg,
raw,
kids: None,
state: FREE,
via: FROM_PARENT,
value: Value::Undefined,
k: None,
k_busy: false,
cut: false,
looped: false,
site: None,
parts: None,
plain: None,
ptr: None,
}
}
}
enum Kids {
Names(HashMap<String, usize>),
Indices(HashMap<usize, usize>),
}
struct Parts {
src: String,
value: Value,
siblings: Value,
}
struct StackBase {
at: usize,
outermost: bool,
}
thread_local! {
static STACK_BASE: Cell<Option<usize>> = const { Cell::new(None) };
}
impl StackBase {
fn enter() -> Self {
let here = stack_here();
STACK_BASE.with(|base| match base.get() {
Some(at) => StackBase {
at,
outermost: false,
},
None => {
base.set(Some(here));
StackBase {
at: here,
outermost: true,
}
}
})
}
}
impl Drop for StackBase {
fn drop(&mut self) {
if self.outermost {
STACK_BASE.with(|base| base.set(None));
}
}
}
#[inline(never)]
fn stack_here() -> usize {
let marker = 0u8;
std::hint::black_box(&marker) as *const u8 as usize
}
struct Frame {
pn: usize,
raw: Value,
next: usize,
kind: Kind,
}
enum Kind {
List { out: Vec<Value> },
Node {
op: Option<Arc<Op>>,
terms: Vec<Value>,
out: Vec<Value>,
},
Plain { out: IndexMap<String, Value> },
Site {
op: Arc<Op>,
value: Value,
siblings: IndexMap<String, Value>,
},
}
enum Member {
Index(usize),
Name(usize),
}
enum Found {
Hit(FoldHit),
Through(usize, usize),
End(usize),
}
impl Frame {
fn take(&mut self) -> Option<(Member, Value)> {
let at = self.next;
let taken = match &self.kind {
Kind::List { .. } => list_items(&self.raw)
.get(at)
.map(|item| (Member::Index(at), item.clone())),
Kind::Node { op, terms, .. } => terms
.get(at)
.map(|term| (Member::Index(at + usize::from(op.is_some())), term.clone())),
Kind::Plain { .. } | Kind::Site { .. } => map_entries(&self.raw)
.and_then(|members| members.get_index(at))
.map(|(_, member)| (Member::Name(at), member.clone())),
};
if taken.is_some() {
self.next += 1;
}
taken
}
fn accept(&mut self, value: Value) {
let at = self.next - 1;
let Frame { raw, kind, .. } = self;
match kind {
Kind::List { out } | Kind::Node { out, .. } => out.push(value),
Kind::Plain { out } => {
out.insert(member_name(raw, at).to_string(), value);
}
Kind::Site {
op,
value: slot,
siblings,
} => {
let name = member_name(raw, at);
if name == op.src {
*slot = value;
} else {
siblings.insert(name.to_string(), value);
}
}
}
}
}
impl Folder<'_> {
fn site(&self, pn: usize, parent_k: Option<Value>) -> Site<'_> {
let state = self.state.borrow();
let node = &state.nodes[pn];
let doc = &state.docs[node.doc];
Site {
fold: self,
pn,
parent_k,
doc: doc.key.clone(),
root: doc.root.clone(),
node: node.raw.clone(),
path: OnceCell::new(),
}
}
fn make_doc(&self, key: &str, raw: Value) -> usize {
let mut state = self.state.borrow_mut();
let doc = state.docs.len();
let top = state.nodes.len();
state
.nodes
.push(PNode::new(doc, None, Seg::Name(String::new()), raw.clone()));
state.docs.push(Doc {
key: Rc::from(key),
root: raw,
top,
});
state.doc_keys.insert(key.to_string(), Some(doc));
doc
}
fn doc_key(&self, pn: usize) -> Rc<str> {
let state = self.state.borrow();
state.docs[state.nodes[pn].doc].key.clone()
}
fn doc_of(&self, from: usize, key: Option<&str>) -> Result<Option<usize>, FoldError> {
let own = self.state.borrow().nodes[from].doc;
let Some(key) = key else {
return Ok(Some(own));
};
if *self.doc_key(from) == *key {
return Ok(Some(own));
}
let known = self.state.borrow().doc_keys.get(key).copied();
if let Some(known) = known {
return Ok(known);
}
let raw = match &self.options.load {
Some(load) => {
self.guard()?;
load(key, &self.site(from, None)).map_err(|error| self.callback_error(&error))?
}
None => None,
};
let doc = match raw {
Some(raw) if !raw.is_undefined() => Some(self.make_doc(key, raw)),
_ => None,
};
self.state
.borrow_mut()
.doc_keys
.insert(key.to_string(), doc);
Ok(doc)
}
fn kid_named(&self, pn: usize, name: &str, raw: Value) -> usize {
let mut state = self.state.borrow_mut();
let found = match state.nodes[pn].kids.as_deref() {
Some(Kids::Names(names)) => names.get(name).copied(),
_ => None,
};
if let Some(found) = found {
return found;
}
let id = state.nodes.len();
let doc = state.nodes[pn].doc;
state
.nodes
.push(PNode::new(doc, Some(pn), Seg::Name(name.to_string()), raw));
let kids = state.nodes[pn]
.kids
.get_or_insert_with(|| Box::new(Kids::Names(HashMap::new())));
if let Kids::Names(names) = &mut **kids {
names.insert(name.to_string(), id);
}
id
}
fn kid_indexed(&self, pn: usize, index: usize, raw: Value) -> usize {
let mut state = self.state.borrow_mut();
let found = match state.nodes[pn].kids.as_deref() {
Some(Kids::Indices(indices)) => indices.get(&index).copied(),
_ => None,
};
if let Some(found) = found {
return found;
}
let id = state.nodes.len();
let doc = state.nodes[pn].doc;
state
.nodes
.push(PNode::new(doc, Some(pn), Seg::Index(index), raw));
let kids = state.nodes[pn]
.kids
.get_or_insert_with(|| Box::new(Kids::Indices(HashMap::new())));
if let Kids::Indices(indices) = &mut **kids {
indices.insert(index, id);
}
id
}
fn kid_of(&self, pn: usize, raw: &Value, member: Member, child: Value) -> usize {
match member {
Member::Index(index) => self.kid_indexed(pn, index, child),
Member::Name(at) => self.kid_named(pn, member_name(raw, at), child),
}
}
fn ptr_of(&self, pn: usize) -> Rc<str> {
let mut state = self.state.borrow_mut();
let state = &mut *state;
if let Some(done) = &state.nodes[pn].ptr {
return done.clone();
}
let mut chain = Vec::new();
let mut cursor = Some(pn);
while let Some(at) = cursor {
if state.nodes[at].ptr.is_some() {
break;
}
chain.push(at);
cursor = state.nodes[at].parent;
}
let mut text = cursor
.and_then(|at| state.nodes[at].ptr.as_deref().map(str::to_string))
.unwrap_or_default();
for &at in chain.iter().rev() {
let node = &mut state.nodes[at];
if node.parent.is_none() {
text.clear();
} else {
text.push('/');
escape_segment(&mut text, &node.seg.text());
}
node.ptr = Some(Rc::from(text.as_str()));
}
state.nodes[pn].ptr.clone().unwrap_or_else(|| Rc::from(""))
}
fn path_of(&self, pn: usize) -> Path {
let state = self.state.borrow();
let mut path = Vec::new();
let mut cursor = Some(pn);
while let Some(at) = cursor {
let node = &state.nodes[at];
if node.parent.is_none() {
break;
}
path.push(node.seg.clone());
cursor = node.parent;
}
path.reverse();
path
}
fn note(&self, list: u8, row: FoldEntry) {
let mut state = self.state.borrow_mut();
let key = (
list,
row.doc.clone(),
row.at.clone(),
row.target_doc.clone(),
row.target.clone(),
);
if state.noted.insert(key) {
if CYCLES == list {
state.report.cycles.push(row);
} else {
state.report.unresolved.push(row);
}
}
}
fn note_dynamic(&self, pn: usize, src: &str) {
let row = FoldDynamic {
doc: self.doc_key(pn).to_string(),
at: self.ptr_of(pn).to_string(),
src: src.to_string(),
};
let mut state = self.state.borrow_mut();
let key = (
DYNAMIC,
row.doc.clone(),
row.at.clone(),
row.src.clone(),
String::new(),
);
if state.noted.insert(key) {
state.report.dynamic.push(row);
}
}
fn raise(&self, error: FoldError) -> FoldError {
self.state.borrow_mut().failure = Some(error.clone());
error
}
fn callback_error(&self, error: &TabnasError) -> FoldError {
let own = self.state.borrow().failure.clone();
match own {
Some(own) if own.code() == error.code && own.message() == error.detail => own,
_ => FoldError::callback(error.clone()),
}
}
fn guard(&self) -> Result<(), FoldError> {
if stack_here().abs_diff(self.stack_base) <= self.options.max_stack {
return Ok(());
}
let error = FoldError::new(
FOLD_DEPTH,
format_args!(
"the stack ran out before max_depth ({}) was reached; lower max_depth",
self.options.max_depth
),
)
.located(&self.main, None);
Err(self.raise(error))
}
fn k_of(&self, pn: usize) -> Result<Value, FoldError> {
let Some(inherit) = &self.options.inherit else {
return Ok(self.options.k.clone());
};
self.guard()?;
let (chain, mut bag) = {
let state = self.state.borrow();
if let Some(bag) = &state.nodes[pn].k {
return Ok(bag.clone());
}
let mut chain = Vec::new();
let mut cursor = Some(pn);
let mut busy = None;
while let Some(at) = cursor {
let node = &state.nodes[at];
if node.k.is_some() {
break;
}
if node.k_busy {
busy = Some(at);
break;
}
chain.push(at);
cursor = node.parent;
}
match busy {
Some(at) => (Err(at), Value::Undefined),
None => (
Ok(chain),
cursor
.and_then(|at| state.nodes[at].k.clone())
.unwrap_or_else(|| self.options.k.clone()),
),
}
};
let chain = match chain {
Ok(chain) => chain,
Err(at) => return Err(self.inherit_error("inherit folded its own subtree at", at)),
};
for &at in chain.iter().rev() {
let raw = self.state.borrow().nodes[at].raw.clone();
if is_record(&raw) {
self.state.borrow_mut().nodes[at].k_busy = true;
let next = inherit(&self.site(at, Some(bag.clone())), &raw, &bag);
self.state.borrow_mut().nodes[at].k_busy = false;
let next = next.map_err(|error| self.callback_error(&error))?;
if !is_container(&next) {
return Err(self.inherit_error("inherit returned no bag at", at));
}
bag = next;
}
self.state.borrow_mut().nodes[at].k = Some(bag.clone());
}
Ok(bag)
}
fn inherit_error(&self, what: &str, at: usize) -> FoldError {
let pointer = self.ptr_of(at);
let doc = self.doc_key(at);
let error = FoldError::new(FOLD_INHERIT, format_args!("{what} {}", shown(&pointer)))
.located(&doc, Some(&pointer));
self.raise(error)
}
fn site_op_of(&self, pn: usize) -> Result<Option<Arc<Op>>, FoldError> {
let raw = {
let state = self.state.borrow();
let node = &state.nodes[pn];
if let Some(decided) = &node.site {
return Ok(decided.clone());
}
node.raw.clone()
};
let op = key_op_of(&self.keys, &raw);
self.state.borrow_mut().nodes[pn].site = Some(op.clone());
if let (Some(found), Some(recognise)) = (&op, &self.options.recognise) {
let yes = recognise(&self.site(pn, None), found)
.map_err(|error| self.callback_error(&error))?;
if !yes {
self.state.borrow_mut().nodes[pn].site = Some(None);
return Ok(None);
}
}
Ok(op)
}
fn miss(&self, from: usize, key: Option<&str>, target: &[Seg]) -> FoldHit {
let doc = self.doc_key(from);
let row = FoldEntry {
doc: doc.to_string(),
at: self.ptr_of(from).to_string(),
target_doc: key.unwrap_or(&doc).to_string(),
target: pointer(target),
};
self.note(UNRESOLVED, row);
FoldHit::missing()
}
fn cycle_to(&self, from: usize, target: usize) -> Result<FoldHit, FoldError> {
let row = FoldEntry {
doc: self.doc_key(from).to_string(),
at: self.ptr_of(from).to_string(),
target_doc: self.doc_key(target).to_string(),
target: self.ptr_of(target).to_string(),
};
if FoldCycle::Error == self.options.cycle {
let error = FoldError::new(
FOLD_CYCLE,
format_args!("cycle at {} to {}", shown(&row.at), shown(&row.target)),
)
.located(&row.doc, Some(&row.at))
.targeted(&row.target_doc, &row.target);
return Err(self.raise(error));
}
self.note(CYCLES, row);
let mut state = self.state.borrow_mut();
let state = &mut *state;
state.nodes[from].cut = true;
if ACTIVE == state.nodes[target].state {
if let Some(start) = state.stack.iter().rposition(|&entry| entry == target) {
let refs = state.stack[start + 1..]
.iter()
.all(|&entry| FROM_REFERENCE == state.nodes[entry].via);
if refs {
for &entry in &state.stack[start..] {
state.nodes[entry].looped = true;
}
state.nodes[from].looped = true;
}
}
} else if state.nodes[target].looped {
state.nodes[from].looped = true;
}
Ok(FoldHit::cycle())
}
fn state_of(&self, pn: usize) -> (u8, bool, bool) {
let state = self.state.borrow();
let node = &state.nodes[pn];
(node.state, node.looped, node.cut)
}
fn at(&self, from: usize, target: &[Seg], key: Option<&str>) -> Result<FoldHit, FoldError> {
self.guard()?;
match self.find(from, target, key)? {
Found::Hit(hit) => Ok(hit),
Found::Through(pn, next) => self.through(from, pn, target, next, key),
Found::End(pn) => self.reach(from, pn),
}
}
fn through(
&self,
from: usize,
pn: usize,
target: &[Seg],
next: usize,
key: Option<&str>,
) -> Result<FoldHit, FoldError> {
let (state, looped, _) = self.state_of(pn);
if ACTIVE == state || looped {
return self.cycle_to(from, pn);
}
match self.walk(pn, FROM_REFERENCE) {
Ok(folded) => self.read_on(from, pn, folded, target, next, key),
Err(error) => Err(error),
}
}
fn read_on(
&self,
from: usize,
pn: usize,
folded: Value,
target: &[Seg],
next: usize,
key: Option<&str>,
) -> Result<FoldHit, FoldError> {
let (_, looped, cut) = self.state_of(pn);
if looped || cut {
return self.cycle_to(from, pn);
}
Ok(match read_raw(folded, &target[next..]) {
Some(value) => FoldHit::found(value),
None => self.miss(from, key, target),
})
}
fn reach(&self, from: usize, pn: usize) -> Result<FoldHit, FoldError> {
let (state, _, _) = self.state_of(pn);
if ACTIVE == state {
return self.cycle_to(from, pn);
}
match self.walk(pn, FROM_REFERENCE) {
Ok(value) => self.reached(from, pn, value),
Err(error) => Err(error),
}
}
fn reached(&self, from: usize, pn: usize, value: Value) -> Result<FoldHit, FoldError> {
let (_, looped, _) = self.state_of(pn);
if looped {
return self.cycle_to(from, pn);
}
Ok(FoldHit::found(value))
}
fn find(&self, from: usize, target: &[Seg], key: Option<&str>) -> Result<Found, FoldError> {
let Some(doc) = self.doc_of(from, key)? else {
return Ok(Found::Hit(self.miss(from, key, target)));
};
let (mut pn, mut node) = {
let state = self.state.borrow();
let doc = &state.docs[doc];
(doc.top, doc.root.clone())
};
for (i, seg) in target.iter().enumerate() {
let last = i + 1 == target.len();
let Some(stepped) = step(&node, seg) else {
return Ok(Found::Hit(self.miss(from, key, target)));
};
let (index, value) = match stepped {
Step::Head(head) => {
return Ok(Found::Hit(match read_raw(head, &target[i + 1..]) {
Some(value) => FoldHit::found(value),
None => self.miss(from, key, target),
}));
}
Step::Member(value) => (None, value),
Step::Item(index, value) => (Some(index), value),
};
if last && !is_container(&value) {
return Ok(Found::Hit(FoldHit::found(value)));
}
pn = match index {
Some(index) => self.kid_indexed(pn, index, value.clone()),
None => self.kid_named(pn, &seg.text(), value.clone()),
};
node = value;
if !last && is_record(&node) {
if let Some(op) = self.site_op_of(pn)? {
if !op.dynamic {
return Ok(Found::Through(pn, i + 1));
}
}
}
}
Ok(Found::End(pn))
}
fn locate(
&self,
from: usize,
target: &[Seg],
key: Option<&str>,
) -> Result<Option<Value>, FoldError> {
self.guard()?;
let Some(doc) = self.doc_of(from, key)? else {
return Ok(None);
};
let root = self.state.borrow().docs[doc].root.clone();
Ok(read_raw(root, target))
}
fn member(&self, pn: usize, name: &str, child: &Value) -> Result<Value, FoldError> {
if is_container(child) {
let kid = self.kid_named(pn, name, child.clone());
self.walk(kid, FROM_PARENT)
} else {
Ok(child.clone())
}
}
fn plain_of(&self, pn: usize) -> Result<Value, FoldError> {
self.guard()?;
let (raw, parts) = {
let state = self.state.borrow();
let node = &state.nodes[pn];
if let Some(plain) = &node.plain {
return Ok(plain.clone());
}
let parts = node.parts.as_deref().map(|parts| {
(
parts.src.clone(),
parts.value.clone(),
parts.siblings.clone(),
)
});
(node.raw.clone(), parts)
};
let Some(members) = map_entries(&raw) else {
return self.walk(pn, FROM_PARENT);
};
let mut out = IndexMap::with_capacity(members.len());
for (name, member) in members {
let value = match &parts {
None => self.member(pn, name, member)?,
Some((src, value, _)) if src == name => value.clone(),
Some((_, _, siblings)) => map_entries(siblings)
.and_then(|siblings| siblings.get(name))
.cloned()
.unwrap_or(Value::Undefined),
};
out.insert(name.clone(), value);
}
let out = rebuild_map(&raw, out);
self.state.borrow_mut().nodes[pn].plain = Some(out.clone());
Ok(out)
}
fn depth_error(&self, pn: usize) -> FoldError {
let pointer = self.ptr_of(pn);
let doc = self.doc_key(pn);
let error = FoldError::new(
FOLD_DEPTH,
format_args!(
"nesting and references deeper than {} at {}",
self.options.max_depth,
shown(&pointer)
),
)
.located(&doc, Some(&pointer));
self.raise(error)
}
fn walk(&self, pn: usize, via: u8) -> Result<Value, FoldError> {
self.guard()?;
let mut frames = Vec::new();
if let Some(value) = self.enter(pn, via, &mut frames)? {
return Ok(value);
}
loop {
if let Err(error) = self.descend(&mut frames) {
self.unwind(&frames);
return Err(error);
}
let finished = match frames.last_mut() {
Some(top) => self.finish(top),
None => return Ok(Value::Undefined),
};
frames.truncate(frames.len() - 1);
match finished {
Ok(value) => match frames.last_mut() {
Some(parent) => parent.accept(value),
None => return Ok(value),
},
Err(error) => {
self.unwind(&frames);
return Err(error);
}
}
}
}
fn descend(&self, frames: &mut Vec<Frame>) -> Result<(), FoldError> {
loop {
let Some(top) = frames.last_mut() else {
return Ok(());
};
let Some((member, child)) = top.take() else {
return Ok(());
};
if !is_container(&child) {
top.accept(child);
continue;
}
let kid = self.kid_of(top.pn, &top.raw, member, child);
if let Some(value) = self.enter(kid, FROM_PARENT, frames)? {
if let Some(top) = frames.last_mut() {
top.accept(value);
}
}
}
}
fn enter(
&self,
pn: usize,
via: u8,
frames: &mut Vec<Frame>,
) -> Result<Option<Value>, FoldError> {
let raw = {
let state = self.state.borrow();
let node = &state.nodes[pn];
if DONE == node.state {
return Ok(Some(node.value.clone()));
}
if ACTIVE == node.state || !is_container(&node.raw) {
return Ok(Some(node.raw.clone()));
}
node.raw.clone()
};
{
let mut state = self.state.borrow_mut();
if self.options.max_depth <= state.stack.len() {
drop(state);
return Err(self.depth_error(pn));
}
state.nodes[pn].state = ACTIVE;
state.nodes[pn].via = via;
state.stack.push(pn);
}
let kind = if is_record(&raw) {
let op = match self.site_op_of(pn) {
Ok(op) => op,
Err(error) => {
self.leave(pn, None);
return Err(error);
}
};
match op {
Some(op) if !op.dynamic => Kind::Site {
op,
value: Value::Undefined,
siblings: IndexMap::new(),
},
op => {
if let Some(op) = op {
self.note_dynamic(pn, &op.src);
}
let plain = self.state.borrow().nodes[pn].plain.clone();
if let Some(plain) = plain {
self.leave(pn, Some(plain.clone()));
return Ok(Some(plain));
}
Kind::Plain {
out: IndexMap::with_capacity(map_entries(&raw).map_or(0, IndexMap::len)),
}
}
}
} else if let Some(id) = node_id(&raw) {
let (op, terms) = with_node(&raw, |entry| (entry.op.clone(), entry.terms.clone()))
.unwrap_or_else(|| released_handle(id));
Kind::Node {
op,
out: Vec::with_capacity(terms.len()),
terms,
}
} else {
Kind::List {
out: Vec::with_capacity(list_items(&raw).len()),
}
};
frames.push(Frame {
pn,
raw,
next: 0,
kind,
});
Ok(None)
}
fn finish(&self, frame: &mut Frame) -> Result<Value, FoldError> {
let pn = frame.pn;
let out = match frame.kind {
Kind::Node { op: Some(_), .. } => self.reduce_node(frame),
Kind::Site { .. } => self.reduce_site(frame),
_ => Ok(self.rebuild(frame)),
};
self.leave(pn, out.as_ref().ok().cloned());
out
}
fn rebuild(&self, frame: &mut Frame) -> Value {
let Frame { pn, raw, kind, .. } = frame;
match kind {
Kind::List { out } => rebuild_list(raw, std::mem::take(out)),
Kind::Node { out, .. } => Value::array(std::mem::take(out)),
Kind::Plain { out } => {
let out = rebuild_map(raw, std::mem::take(out));
self.state.borrow_mut().nodes[*pn].plain = Some(out.clone());
out
}
Kind::Site { .. } => Value::Undefined,
}
}
fn reduce_node(&self, frame: &mut Frame) -> Result<Value, FoldError> {
let Frame { pn, raw, kind, .. } = frame;
let Kind::Node {
op: Some(op), out, ..
} = kind
else {
return Ok(Value::Undefined);
};
let op = op.clone();
let terms = std::mem::take(out);
if self.options.tokens {
self.call_evaluate(*pn, &op, &terms)
} else {
Ok(operator_node(raw, op, terms))
}
}
fn reduce_site(&self, frame: &mut Frame) -> Result<Value, FoldError> {
let Frame { pn, raw, kind, .. } = frame;
let Kind::Site {
op,
value,
siblings,
} = kind
else {
return Ok(Value::Undefined);
};
let op = op.clone();
let terms = [
std::mem::replace(value, Value::Undefined),
rebuild_map(raw, std::mem::take(siblings)),
];
self.keep_parts(*pn, &op, &terms);
self.call_evaluate(*pn, &op, &terms)
}
fn keep_parts(&self, pn: usize, op: &Op, terms: &[Value; 2]) {
self.state.borrow_mut().nodes[pn].parts = Some(Box::new(Parts {
src: op.src.clone(),
value: terms[0].clone(),
siblings: terms[1].clone(),
}));
}
fn call_evaluate(&self, pn: usize, op: &Op, terms: &[Value]) -> Result<Value, FoldError> {
let site = self.site(pn, None);
let result = (self.options.evaluate)(&site, op, terms);
match &result {
Ok(value) => Ok(value.clone()),
Err(error) => Err(self.callback_error(error)),
}
}
fn leave(&self, pn: usize, out: Option<Value>) {
let mut state = self.state.borrow_mut();
let top = state.stack.pop();
debug_assert_eq!(Some(pn), top, "the active stack unwinds in order");
let node = &mut state.nodes[pn];
match out {
Some(value) => {
node.state = DONE;
node.value = value;
}
None => node.state = FREE,
}
}
fn unwind(&self, frames: &[Frame]) {
for frame in frames.iter().rev() {
self.leave(frame.pn, None);
}
}
}
fn is_record(value: &Value) -> bool {
matches!(value, Value::Object(_) | Value::MapRef(_))
}
fn is_container(value: &Value) -> bool {
matches!(
value,
Value::Object(_) | Value::MapRef(_) | Value::Array(_) | Value::ListRef(_)
)
}
fn map_entries(value: &Value) -> Option<&IndexMap<String, Value>> {
match value {
Value::Object(members) => Some(members),
Value::MapRef(map) => Some(&map.value),
_ => None,
}
}
fn list_items(value: &Value) -> &[Value] {
match value {
Value::Array(items) => items,
Value::ListRef(list) => &list.value,
_ => &[],
}
}
fn member_name(raw: &Value, at: usize) -> &str {
map_entries(raw)
.and_then(|members| members.get_index(at))
.map_or("", |(name, _)| name.as_str())
}
fn rebuild_map(like: &Value, members: IndexMap<String, Value>) -> Value {
match like {
Value::MapRef(map) => {
let mut out = (**map).clone();
out.value = members;
Value::MapRef(Arc::new(out))
}
_ => Value::object(members),
}
}
fn rebuild_list(like: &Value, items: Vec<Value>) -> Value {
match like {
Value::ListRef(list) => {
let mut out = (**list).clone();
out.value = items;
Value::ListRef(Arc::new(out))
}
_ => Value::array(items),
}
}
fn operator_node(raw: &Value, op: Arc<Op>, terms: Vec<Value>) -> Value {
let token = with_node(raw, |entry| entry.token.clone()).flatten();
new_node(Some(op), token, terms)
}
enum Step {
Member(Value),
Item(usize, Value),
Head(Value),
}
fn step(node: &Value, seg: &Seg) -> Option<Step> {
match node {
Value::Object(members) => members.get(seg.text().as_ref()).cloned().map(Step::Member),
Value::MapRef(map) => map
.value
.get(seg.text().as_ref())
.cloned()
.map(Step::Member),
Value::Array(items) => {
array_index(seg, items.len()).map(|index| Step::Item(index, items[index].clone()))
}
Value::ListRef(list) => match node_id(node) {
None => array_index(seg, list.value.len())
.map(|index| Step::Item(index, list.value[index].clone())),
Some(id) => with_node(node, |entry| {
let head = usize::from(entry.op.is_some());
let index = array_index(seg, entry.terms.len() + head)?;
match (index, &entry.op) {
(0, Some(op)) => Some(Step::Head(op.to_value(entry.token.as_ref()))),
_ => Some(Step::Item(index, entry.terms[index - head].clone())),
}
})
.unwrap_or_else(|| released_handle(id)),
},
_ => None,
}
}
fn read_raw(mut node: Value, path: &[Seg]) -> Option<Value> {
for seg in path {
node = match step(&node, seg)? {
Step::Member(value) | Step::Item(_, value) | Step::Head(value) => value,
};
}
Some(node)
}
fn array_index(seg: &Seg, length: usize) -> Option<usize> {
let index = match seg {
Seg::Index(index) => *index,
Seg::Name(text) => {
let canonical = "0" == text
|| (!text.is_empty()
&& !text.starts_with('0')
&& text.bytes().all(|byte| byte.is_ascii_digit()));
if !canonical {
return None;
}
text.parse::<usize>().ok()?
}
};
(index < length).then_some(index)
}
fn pointer(path: &[Seg]) -> String {
let mut out = String::new();
for seg in path {
out.push('/');
escape_segment(&mut out, &seg.text());
}
out
}
fn escape_segment(out: &mut String, text: &str) {
for c in text.chars() {
match c {
'~' => out.push_str("~0"),
'/' => out.push_str("~1"),
c => out.push(c),
}
}
}
fn shown(text: &str) -> String {
let mut quoted = String::with_capacity(text.len() + 2);
quoted.push('"');
for c in text.chars() {
match c {
'"' => quoted.push_str("\\\""),
'\\' => quoted.push_str("\\\\"),
'\n' => quoted.push_str("\\n"),
'\r' => quoted.push_str("\\r"),
'\t' => quoted.push_str("\\t"),
'\u{8}' => quoted.push_str("\\b"),
'\u{c}' => quoted.push_str("\\f"),
c if (c as u32) < 0x20 => quoted.push_str(&format!("\\u{:04x}", c as u32)),
c => quoted.push(c),
}
}
quoted.push('"');
if quoted.encode_utf16().count() <= 80 {
return quoted;
}
let mut out = String::new();
let mut units = 0;
for c in quoted.chars() {
units += c.len_utf16();
if 76 < units {
break;
}
out.push(c);
}
out.push_str("...\"");
out
}