use std::cell::RefCell;
use std::rc::Rc;
use tabnas::{ActionError, Context, Rule, RuleSnapshot, Tabnas, Value};
use crate::node::{
existing_at, merge_into, new_map, path_of, redefined, reset_paths, set_path, set_root,
snapshot_path, value_at, write_at, Cell, Cursor, Path, Reach,
};
use crate::strmatcher::make_toml_string_matcher;
use crate::values::{isodate_val, localtime_val};
fn key_of(rule: &mut Rule, context: &mut Context) -> String {
match rule.resolve_open_value(0, context) {
Value::String(text) => text,
Value::Text(text) => text.string,
_ => rule
.o0()
.map(|token| token.src.to_string())
.unwrap_or_default(),
}
}
fn cell_of(rule: &Rule) -> Cell {
Rc::clone(&rule.node)
}
fn parent_path(rule: &Rule) -> Path {
snapshot_path(rule.parent_rule.as_ref())
}
fn prev_path(rule: &Rule) -> Path {
snapshot_path(rule.prev_rule.as_ref())
}
fn table_array(rule: &Rule) -> bool {
0 < rule.n.get("table_array").copied().unwrap_or(0)
}
pub(crate) fn continues_key(previous: Option<&Rc<RuleSnapshot>>) -> bool {
previous.is_some_and(|previous| {
"dive" == previous.name.as_ref() && !truthy(previous.u.get("dive_end"))
})
}
fn conclude(
rule: &mut Rule,
context: &mut Context,
cell: &Cell,
cursor: Cursor,
outcome: Result<(), ActionError>,
done: bool,
) -> Result<(), ActionError> {
if outcome.is_ok() {
set_path(rule, cursor.path());
}
if done || outcome.is_err() {
cursor.finish(context, cell);
} else {
cursor.park(context, cell);
}
outcome
}
fn body_table(rule: &Rule) -> Option<Rc<RuleSnapshot>> {
let mut map = rule.parent_rule.clone()?;
if "pair" == map.name.as_ref() {
map = map.parent_rule.clone()?;
}
if "map" != map.name.as_ref() {
return None;
}
map.parent_rule
.clone()
.filter(|table| "table" == table.name.as_ref())
}
fn body_conflict(rule: &Rule, context: &mut Context, key: &str) -> Result<(), ActionError> {
let Some(table) = body_table(rule) else {
return Ok(());
};
let path = snapshot_path(Some(&table));
match existing_at(context, &table.node, path, key) {
Some(_) => Err(redefined(key)),
None => Ok(()),
}
}
fn truthy(value: Option<&Value>) -> bool {
match value {
None | Some(Value::Undefined | Value::Null | Value::Bool(false)) => false,
Some(Value::Number(number)) => 0.0 != *number,
Some(Value::String(text)) => !text.is_empty(),
Some(_) => true,
}
}
pub(crate) fn register(parser: &mut Tabnas) {
register_matchers(parser);
register_state_actions(parser);
register_alt_actions(parser);
register_conditions(parser);
register_routes(parser);
}
fn register_matchers(parser: &mut Tabnas) {
parser.lex_match_factory_ref("@make-toml-string-matcher", make_toml_string_matcher);
parser.value_transform_ref("@isodate-val", isodate_val);
parser.value_transform_ref("@localtime-val", localtime_val);
}
fn register_state_actions(parser: &mut Tabnas) {
parser.state_action_ref("@toml-bo", |rule, context| {
rule.node = Rc::new(RefCell::new(new_map()));
set_root(context, &rule.node);
set_path(rule, Path::ROOT);
Ok(())
});
parser.state_action_ref("@map-bo/append", |rule, _context| {
rule.node = Rc::new(RefCell::new(new_map()));
set_path(rule, Path::ROOT);
Ok(())
});
parser.state_action_ref("@table-bo", |rule, _context| {
if let Some(parent) = rule.parent_rule.clone() {
rule.node = Rc::clone(&parent.node);
}
let path = parent_path(rule);
set_path(rule, path);
Ok(())
});
parser.state_action_ref("@dive-bo", |rule, _context| {
let path = if continues_key(rule.prev_rule.as_ref()) {
prev_path(rule)
} else {
parent_path(rule)
};
set_path(rule, path);
Ok(())
});
parser.state_action_ref("@table-bc", |rule, context| {
if !truthy(rule.u.get("top_dive")) {
if let Some(child) = rule.child_rule.clone() {
let source = child.node.borrow().clone();
merge_into(context, &cell_of(rule), path_of(rule), &source);
}
if table_array(rule) {
if let Some(table) = value_at(context, &cell_of(rule), path_of(rule)) {
rule.node = Rc::new(RefCell::new(table));
}
}
}
let counters = rule.n_mut();
counters.insert("table_dive".to_string(), 0);
counters.insert("table_array".to_string(), 0);
Ok(())
});
parser.state_action_ref("@dive-bc", |rule, context| {
if !truthy(rule.u.get("dive_end")) {
return Ok(());
}
let Some(child) = rule.child_rule.clone() else {
return Ok(());
};
let value = child.node.borrow().clone();
let key = key_of(rule, context);
if existing_at(context, &cell_of(rule), path_of(rule), &key).is_some() {
return Err(redefined(&key));
}
write_at(context, &cell_of(rule), path_of(rule), key, value);
Ok(())
});
parser.state_action_ref("@pair-bc/prepend", |rule, context| {
if !truthy(rule.u.get("pair")) {
return Ok(());
}
let key = key_of(rule, context);
if existing_at(context, &cell_of(rule), Path::ROOT, &key).is_some() {
return Err(redefined(&key));
}
body_conflict(rule, context, &key)
});
}
fn register_alt_actions(parser: &mut Tabnas) {
parser.action_with_context("@table-dive-start", |rule, context| {
let key = key_of(rule, context);
let cell = cell_of(rule);
reset_paths(context);
let mut cursor = Cursor::resume(context, &cell, parent_path(rule));
let outcome = if table_array(rule) && cursor.has_list(&key) {
cursor.enter(context, &key);
cursor.last_of_list(context);
Ok(())
} else {
cursor.table_at(context, &key, Reach::Descend)
};
conclude(rule, context, &cell, cursor, outcome, false)
});
parser.action_with_context("@table-dive-mid", |rule, context| {
let key = key_of(rule, context);
let cell = cell_of(rule);
let mut cursor = Cursor::resume(context, &cell, prev_path(rule));
if cursor.is_list() {
cursor.last_of_list(context);
}
let outcome = cursor.table_at(context, &key, Reach::Descend);
conclude(rule, context, &cell, cursor, outcome, false)
});
parser.action_with_context("@table-key-cs-head", |rule, context| {
let key = key_of(rule, context);
let cell = cell_of(rule);
reset_paths(context);
let array = table_array(rule);
let mut cursor = Cursor::resume(context, &cell, parent_path(rule));
let outcome = if array {
cursor.array_at(context, &key)
} else {
cursor.table_at(context, &key, Reach::Define)
};
conclude(rule, context, &cell, cursor, outcome, !array)
});
parser.action_with_context("@table-key-cs-tail", |rule, context| {
let key = key_of(rule, context);
let cell = cell_of(rule);
let array = table_array(rule);
let mut cursor = Cursor::resume(context, &cell, prev_path(rule));
if cursor.is_list() {
cursor.last_of_list(context);
}
let outcome = if array {
cursor.array_at(context, &key)
} else {
cursor.table_at(context, &key, Reach::Define)
};
conclude(rule, context, &cell, cursor, outcome, !array)
});
parser.action_with_context("@table-cs-push", |rule, context| {
let cell = cell_of(rule);
let mut cursor = Cursor::resume(context, &cell, prev_path(rule));
if !cursor.is_list() {
let key = key_of(rule, context);
cursor.finish(context, &cell);
return Err(redefined(&key));
}
cursor.push_table(context);
set_path(rule, cursor.path());
cursor.finish(context, &cell);
Ok(())
});
parser.action_with_context("@pair-key-set", |rule, context| {
let key = rule.resolve_open_value(0, context);
rule.u_mut().insert("key".to_string(), key);
Ok(())
});
parser.action_with_context("@dive-key-dot", |rule, context| {
let key = key_of(rule, context);
if !continues_key(rule.prev_rule.as_ref()) {
body_conflict(rule, context, &key)?;
}
let cell = cell_of(rule);
let mut cursor = Cursor::resume(context, &cell, path_of(rule));
let outcome = cursor.table_at(context, &key, Reach::Dive);
conclude(rule, context, &cell, cursor, outcome, false)
});
}
fn register_conditions(parser: &mut Tabnas) {
parser.alt_condition("@table-top-dive-cond", |rule, _context| {
let previous = rule
.prev_rule
.as_ref()
.map_or("", |previous| previous.name.as_ref());
1 == rule.d && "table" != previous
});
parser.alt_condition("@lte-table-dive", |rule, _context| {
rule.lte("table_dive", 0)
});
parser.alt_condition("@lte-table-array-1", |rule, _context| {
rule.lte("table_array", 1)
});
parser.alt_condition("@lte-pk", |rule, _context| rule.lte("pk", 0));
parser.alt_condition("@map-is-table-parent", |rule, _context| {
rule.parent_rule
.as_ref()
.is_some_and(|parent| "table" == parent.name.as_ref())
});
}
fn register_routes(parser: &mut Tabnas) {
parser.alt_push("@table-end-p", |rule, _context| {
(0 == rule.n.get("table_array").copied().unwrap_or(0)).then(|| "map".to_string())
});
parser.alt_replace("@table-end-r", |rule, _context| {
(0 < rule.n.get("table_array").copied().unwrap_or(0)).then(|| "table".to_string())
});
}