use tabnas::{Context, Rule, Value};
const TAB_WIDTH: usize = 8;
#[derive(Default)]
pub(crate) struct Position {
pub(crate) line: usize,
pub(crate) col: usize,
}
impl Position {
pub(crate) fn step(&mut self, bytes: &[u8], at: usize) -> usize {
match bytes[at] {
b'\n' => {
self.line += 1;
self.col = 0;
}
b'\t' => self.col += TAB_WIDTH - self.col % TAB_WIDTH,
_ => self.col += 1,
}
at + 1
}
}
pub(crate) fn aggregate_text(src: &str, open: usize, close: usize) -> String {
let bytes = src.as_bytes();
let mut pos = Position::default();
let mut at = src[..open].rfind('\n').map_or(0, |newline| newline + 1);
while at <= open {
at = pos.step(bytes, at);
}
interior_text(src, open + 1, close, pos.col)
}
pub(crate) fn interior_text(src: &str, from: usize, close: usize, col: usize) -> String {
let bytes = src.as_bytes();
let mut pos = Position { line: 0, col };
let is_comment_start =
|at: usize| b'/' == bytes[at] && matches!(bytes.get(at + 1), Some(b'/') | Some(b'*'));
let mut out = String::new();
let mut from = from;
let mut at = from;
while at < close {
let byte = bytes[at];
if is_comment_start(at) {
out.push_str(&src[from..at]);
let (gap_line, gap_col) = (pos.line, pos.col);
while at < close && is_comment_start(at) {
if b'/' == bytes[at + 1] {
while at < close && b'\n' != bytes[at] {
at = pos.step(bytes, at);
}
if at < close {
at = pos.step(bytes, at);
}
} else {
at = pos.step(bytes, at);
at = pos.step(bytes, at);
while at < close && !(b'*' == bytes[at] && Some(&b'/') == bytes.get(at + 1)) {
at = pos.step(bytes, at);
}
if at < close {
at = pos.step(bytes, at);
at = pos.step(bytes, at);
}
}
}
from = at;
if close <= at {
break;
}
if gap_line < pos.line {
out.push_str(&"\n".repeat(pos.line - gap_line));
out.push_str(&" ".repeat(pos.col));
} else if gap_col < pos.col {
out.push_str(&" ".repeat(pos.col - gap_col));
}
continue;
}
if b'"' == byte || b'\'' == byte {
at = pos.step(bytes, at);
while at < close && byte != bytes[at] && b'\n' != bytes[at] {
if b'\\' == bytes[at] && at + 1 < close && b'\n' != bytes[at + 1] {
at = pos.step(bytes, at);
}
at = pos.step(bytes, at);
}
if at < close && byte == bytes[at] {
at = pos.step(bytes, at);
}
continue;
}
at = pos.step(bytes, at);
}
if from < close {
out.push_str(&src[from..close]);
}
out
}
pub(crate) const AGGREGATE: &str = "protoAggregate";
pub(crate) const AGGREGATE_OPEN: &str = "protoAggregateOpen";
pub(crate) fn record_aggregate(rule: &mut Rule, ctx: &mut Context) {
let is_aggregate = matches!(
&*rule.node.borrow(),
Value::Object(entries)
if matches!(entries.get("rule"), Some(Value::String(name)) if "constant" == name)
&& matches!(entries.get("src"), Some(Value::String(src)) if src.starts_with('{'))
);
if !is_aggregate {
return;
}
let (Some(open), Some(close)) = (rule.o.first(), ctx.v1()) else {
return;
};
let text = if ctx.source.is_empty() {
let read = close.use_data();
match (read.get(AGGREGATE_OPEN), read.get(AGGREGATE)) {
(Some(Value::Number(at)), Some(Value::String(text))) if *at == open.site.si as f64 => {
text.clone()
}
_ => return,
}
} else {
let (open, close) = (open.site.si, close.site.si);
let src = ctx.source.as_str();
if !(open < close && close < src.len()) {
return;
}
if b'{' != src.as_bytes()[open] || b'}' != src.as_bytes()[close] {
return;
}
aggregate_text(src, open, close)
};
if let Some(node) = rule.node.borrow_mut().as_object_mut() {
node.insert("aggregate".to_string(), Value::String(text));
}
}