use std::rc::Rc;
use std::sync::{Arc, OnceLock};
use indexmap::IndexMap;
use tabnas::{ActionError, Context, LexMatcher, Lexer, Rule, Tabnas, Value};
use crate::converter::AbnfParseError;
use crate::parser_abnf::{
build_parser, open_token, push_node, set_node, token_string, DEPTH_CODE, DEPTH_MESSAGE,
MAX_GROUP_DEPTH,
};
const MAX_COUNT: u64 = 9_007_199_254_740_991;
const TREE_ERRORS: [(&str, &str, &str); 6] = [
(
"abnf_code_point",
"the numeric value {src} names no Unicode code point (the largest is %x10FFFF)",
"A numeric value names a code point from %x0 to %x10FFFF.",
),
(
"abnf_reversed_range",
"the numeric range {src} ends below where it starts",
"Write the range from its lower end to its higher, as %x61-7A.",
),
(
"abnf_reversed_repetition",
"the repetition allows fewer than it requires: its most is below its least",
"In m*n the n is the most and the m the least, so n is not below m.",
),
(
"abnf_count",
"the repetition count {src} is past 9007199254740991, the largest whole number the tree holds",
"A repetition count is a whole number no larger than 9007199254740991.",
),
(
"abnf_unclosed",
"a group or an option is not closed",
"Close the group with ) and the option with ].",
),
(
"abnf_surrogate",
"the numeric value {src} holds half of a surrogate pair standing alone, which names no \
character",
"A concatenation holds a surrogate as one half of a pair, as %xD83D.DE00; one value \
alone, as %xD800, is a code point of its own.",
),
];
fn object(fields: Vec<(&str, Value)>) -> Value {
let mut map = IndexMap::new();
for (key, value) in fields {
map.insert(key.to_string(), value);
}
Value::object(map)
}
fn refuse(code: &str) -> ActionError {
let message = TREE_ERRORS
.iter()
.find(|(name, _, _)| *name == code)
.map_or(code, |(_, message, _)| message);
ActionError::new(code, message.to_string())
}
fn leading_int(text: &str, radix: u32, limit: u64) -> Option<u64> {
let mut value: u64 = 0;
let mut digits = 0;
for character in text.chars() {
let Some(digit) = character.to_digit(radix) else {
break;
};
digits += 1;
value = value
.checked_mul(u64::from(radix))
.and_then(|n| n.checked_add(u64::from(digit)))
.filter(|n| *n <= limit)?;
}
(0 < digits).then_some(value)
}
pub(crate) fn numeric_element(src: &str) -> Result<Value, &'static str> {
let radix = match src.as_bytes().get(1).map(u8::to_ascii_lowercase) {
Some(b'd') => 10,
Some(b'b') => 2,
_ => 16,
};
let body = src.get(2..).unwrap_or_default();
let point = |text: &str| leading_int(text, radix, 0x10FFFF);
if body.contains('-') {
let mut parts = body.split('-');
let from = point(parts.next().unwrap_or_default());
let to = point(parts.next().unwrap_or_default());
let (Some(from), Some(to)) = (from, to) else {
return Err("abnf_code_point");
};
if to < from {
return Err("abnf_reversed_range");
}
return Ok(object(vec![
("type", Value::String("range".into())),
("from", Value::Number(from as f64)),
("to", Value::Number(to as f64)),
]));
}
let mut points = Vec::new();
for part in body.split('.') {
points.push(point(part).ok_or("abnf_code_point")?);
}
if 1 == points.len() {
return Ok(object(vec![
("type", Value::String("char".into())),
("value", Value::Number(points[0] as f64)),
]));
}
let mut units: Vec<u16> = Vec::new();
for n in points {
match char::from_u32(n as u32) {
Some(character) => {
let mut buffer = [0u16; 2];
units.extend_from_slice(character.encode_utf16(&mut buffer));
}
None => units.push(n as u16),
}
}
let text = String::from_utf16(&units).map_err(|_| "abnf_surrogate")?;
let mut chars = text.chars();
if let (Some(only), None) = (chars.next(), chars.next()) {
return Ok(object(vec![
("type", Value::String("char".into())),
("value", Value::Number(f64::from(u32::from(only)))),
]));
}
Ok(object(vec![
("type", Value::String("literal".into())),
("text", Value::String(text)),
("caseSensitive", Value::Bool(true)),
]))
}
fn count(rule: &Rule, index: usize) -> Result<f64, ActionError> {
let token = open_token(rule, index).ok_or_else(|| refuse("abnf_count"))?;
leading_int(token.src.as_str(), 10, MAX_COUNT)
.map(|n| n as f64)
.ok_or_else(|| refuse("abnf_count"))
}
fn repetition(min: f64, max: Option<f64>) -> Value {
let mut fields = vec![("min", Value::Number(min))];
if let Some(max) = max {
fields.push(("max", Value::Number(max)));
}
object(fields)
}
fn set_repeat(rule: &mut Rule, rep: Value) -> Result<(), ActionError> {
rule.u_mut().insert("repeat".into(), rep);
Ok(())
}
fn literal(text: String, sensitive: bool) -> Value {
object(vec![
("type", Value::String("literal".into())),
("text", Value::String(text)),
("caseSensitive", Value::Bool(sensitive)),
])
}
fn open_group(rule: &mut Rule, kind: &str) -> Result<(), ActionError> {
if MAX_GROUP_DEPTH < rule.d {
return Err(ActionError::new(DEPTH_CODE, DEPTH_MESSAGE));
}
rule.u_mut()
.insert("groupKind".into(), Value::String(kind.into()));
Ok(())
}
fn register_tree_refs(parser: &mut Tabnas) {
parser.state_action_ref("@abnf-bo", |rule, _context| {
set_node(
rule,
object(vec![
("type", Value::String("grammar".into())),
("rules", Value::array(Vec::new())),
]),
);
Ok(())
});
parser.action_with_context("@prod-name", |rule, context| {
let Some(token) = open_token(rule, 0) else {
return Ok(());
};
let name = token_string(&token, rule, context);
rule.u_mut().insert("name".into(), Value::String(name));
Ok(())
});
parser.action_with_context("@prod-name-prose", |rule, _context| {
let Some(token) = open_token(rule, 0) else {
return Ok(());
};
rule.u_mut()
.insert("name".into(), Value::String(token.src.as_str().to_string()));
Ok(())
});
parser.action_with_context("@prod-name-inc", |rule, context| {
let Some(token) = open_token(rule, 0) else {
return Ok(());
};
let name = token_string(&token, rule, context);
let bag = rule.u_mut();
bag.insert("name".into(), Value::String(name));
bag.insert("incremental".into(), Value::Bool(true));
Ok(())
});
parser.state_action_ref("@prod-bc", |rule, _context| {
if rule.child_node.is_undefined() {
return Ok(());
}
let name = match rule.u.get("name") {
Some(Value::String(name)) => name.clone(),
_ => String::new(),
};
let mut fields = vec![
("type", Value::String("rule".into())),
("name", Value::String(name)),
];
if matches!(rule.u.get("incremental"), Some(Value::Bool(true))) {
fields.push(("incremental", Value::Bool(true)));
}
fields.push(("alternatives", rule.child_node.clone()));
let entry = object(fields);
if let Some(grammar) = rule.node.borrow_mut().as_object_mut() {
if let Some(rules) = grammar.get_mut("rules").and_then(Value::as_array_mut) {
rules.push(entry);
}
}
Ok(())
});
parser.state_action_ref("@alts-bo", |rule, _context| {
set_node(rule, Value::array(Vec::new()));
Ok(())
});
parser.state_action_ref("@alts-bc", |rule, _context| {
if rule.child_node.is_undefined() {
return Ok(());
}
let sequence = rule.child_node.clone();
push_node(rule, sequence);
Ok(())
});
parser.state_action_ref("@seq-bo", |rule, _context| {
set_node(rule, Value::array(Vec::new()));
Ok(())
});
parser.state_action_ref("@elem-bo", |rule, _context| {
rule.u_mut().remove("repeat");
Ok(())
});
parser.action_with_context("@elem-rep-bounded", |rule, _context| {
let min = count(rule, 0)?;
let max = count(rule, 2)?;
if max < min {
return Err(refuse("abnf_reversed_repetition"));
}
set_repeat(rule, repetition(min, Some(max)))
});
parser.action_with_context("@elem-rep-atleast", |rule, _context| {
let min = count(rule, 0)?;
set_repeat(rule, repetition(min, None))
});
parser.action_with_context("@elem-rep-atmost", |rule, _context| {
let max = count(rule, 1)?;
set_repeat(rule, repetition(0.0, Some(max)))
});
parser.action_with_context("@elem-rep-star", |rule, _context| {
set_repeat(rule, repetition(0.0, None))
});
parser.action_with_context("@elem-rep-exact", |rule, _context| {
let n = count(rule, 0)?;
set_repeat(rule, repetition(n, Some(n)))
});
parser.action_with_context("@elem-close", |rule, _context| {
let mut element = rule.child_node.clone();
if element.is_undefined() {
return Err(refuse("abnf_unclosed"));
}
if let Some(rep) = rule.u.get("repeat").cloned() {
if let Some(members) = element.as_object_mut() {
match members.get_mut("repeat").and_then(Value::as_array_mut) {
Some(reps) => reps.push(rep),
None => {
members.insert("repeat".into(), Value::array(vec![rep]));
}
}
}
}
push_node(rule, element);
Ok(())
});
parser.state_action_ref("@atom-bo", |rule, _context| {
set_node(rule, Value::Undefined);
Ok(())
});
parser.action_with_context("@atom-ss", |rule, context| {
let Some(text) = open_token(rule, 1) else {
return Ok(());
};
let text = token_string(&text, rule, context);
set_node(rule, literal(text, true));
Ok(())
});
parser.action_with_context("@atom-si", |rule, context| {
let Some(text) = open_token(rule, 1) else {
return Ok(());
};
let text = token_string(&text, rule, context);
set_node(rule, literal(text, false));
Ok(())
});
parser.action_with_context("@atom-st", |rule, context| {
let Some(text) = open_token(rule, 0) else {
return Ok(());
};
let text = token_string(&text, rule, context);
set_node(rule, literal(text, false));
Ok(())
});
parser.action_with_context("@atom-nv", |rule, _context| {
let Some(token) = open_token(rule, 0) else {
return Ok(());
};
let element = numeric_element(token.src.as_str()).map_err(refuse)?;
if let Some(first) = Rc::make_mut(&mut rule.o).first_mut() {
first.val = element.clone();
}
set_node(rule, element);
Ok(())
});
parser.action_with_context("@atom-pv", |rule, _context| {
let Some(token) = open_token(rule, 0) else {
return Ok(());
};
let source = token.src.as_str();
let text = source
.strip_prefix('<')
.and_then(|rest| rest.strip_suffix('>'))
.unwrap_or(source)
.to_string();
set_node(
rule,
object(vec![
("type", Value::String("prose".into())),
("text", Value::String(text)),
]),
);
Ok(())
});
parser.action_with_context("@atom-tx", |rule, context| {
let Some(token) = open_token(rule, 0) else {
return Ok(());
};
let name = token_string(&token, rule, context);
set_node(
rule,
object(vec![
("type", Value::String("reference".into())),
("name", Value::String(name)),
]),
);
Ok(())
});
parser.action_with_context("@atom-lp", |rule, _context| open_group(rule, "group"));
parser.action_with_context("@atom-ob", |rule, _context| open_group(rule, "opt"));
parser.alt_condition("@atom-group-c", |rule, _context| {
matches!(rule.u.get("groupKind"), Some(Value::String(kind)) if "group" == kind)
});
parser.action_with_context("@atom-group-close", |rule, _context| {
let alternatives = rule.child_node.clone();
set_node(
rule,
object(vec![
("type", Value::String("group".into())),
("alternatives", alternatives),
]),
);
Ok(())
});
parser.alt_condition("@atom-opt-c", |rule, _context| {
matches!(rule.u.get("groupKind"), Some(Value::String(kind)) if "opt" == kind)
});
parser.action_with_context("@atom-opt-close", |rule, _context| {
let alternatives = rule.child_node.clone();
set_node(
rule,
object(vec![
("type", Value::String("group".into())),
("alternatives", alternatives),
("repeat", Value::array(vec![repetition(0.0, Some(1.0))])),
]),
);
Ok(())
});
}
#[must_use]
pub fn make() -> Tabnas {
let mut parser = build_parser(register_tree_refs, &TREE_ERRORS)
.expect("abnf: the tree parser installs its own meta-grammar");
parser
.set_options(|options| {
options.lex.window = true;
options.lex.matchers.insert(
"abnf-reach".to_string(),
reach_matcher("abnf-reach", abnf_reach),
);
})
.expect("abnf: the tree parser takes the window and its reach");
parser
}
fn abnf_reach(window: &str) -> Result<(), usize> {
let mut chars = window.chars().peekable();
match chars.next() {
None => Err(0),
Some('%') => {
let mut read = 1;
if chars.peek().is_some_and(char::is_ascii_alphabetic) {
chars.next();
read += 1;
}
run(chars, read, |c| {
c.is_ascii_hexdigit() || c == '.' || c == '-'
})
}
Some('<') => run(chars, 1, |c| {
('\x20'..='\x3D').contains(&c) || ('\x3F'..='\x7E').contains(&c)
}),
Some(c) if c.is_ascii_digit() => run(chars, 1, |c| c.is_ascii_digit()),
Some(_) => Ok(()),
}
}
fn run(
chars: impl Iterator<Item = char>,
mut read: usize,
within: impl Fn(char) -> bool,
) -> Result<(), usize> {
for c in chars {
if !within(c) {
return Ok(());
}
read += 1;
}
Err(read)
}
fn reach_matcher(name: &str, reach: fn(&str) -> Result<(), usize>) -> LexMatcher {
LexMatcher {
name: name.to_string(),
order: 900_000.0,
matcher: None,
imperative: Some(Arc::new(
move |lexer: &mut Lexer<'_>, _rule: &mut Rule, _context: &mut Context| {
if !lexer.windowed() {
return None;
}
let mut ahead = 1;
loop {
let held = lexer.fill(ahead);
match reach(lexer.window()) {
Ok(()) => return None,
Err(_) if !held => return None,
Err(read) => ahead = (read + 1).max(2 * ahead),
}
}
},
)),
factory: None,
}
}
pub fn parse_abnf_tree(src: &str) -> Result<Value, AbnfParseError> {
static PARSER: OnceLock<Tabnas> = OnceLock::new();
let parser = PARSER.get_or_init(make);
match parser.parse(src) {
Ok(Value::Undefined) | Ok(Value::Null) => Ok(object(vec![
("type", Value::String("grammar".into())),
("rules", Value::array(Vec::new())),
])),
Ok(tree) => Ok(tree),
Err(error) => Err(AbnfParseError::from_engine(&error)),
}
}