use std::cell::RefCell;
#[allow(unused_imports)]
use std::ops::DerefMut;
use nom8::bytes::take;
use nom8::combinator::cut;
use nom8::combinator::peek;
use nom8::sequence::delimited;
use crate::parser::key::key;
use crate::parser::prelude::*;
use crate::parser::state::ParseState;
use crate::parser::trivia::line_trailing;
pub(crate) const STD_TABLE_OPEN: u8 = b'[';
const STD_TABLE_CLOSE: u8 = b']';
const ARRAY_TABLE_OPEN: &[u8] = b"[[";
const ARRAY_TABLE_CLOSE: &[u8] = b"]]";
pub(crate) fn std_table<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl FnMut(Input<'i>) -> IResult<Input<'i>, (), ParserError<'i>> + 's {
move |i| {
(
delimited(
STD_TABLE_OPEN,
cut(key),
cut(STD_TABLE_CLOSE)
.context(Context::Expected(ParserValue::CharLiteral('.')))
.context(Context::Expected(ParserValue::StringLiteral("]"))),
)
.with_span(),
cut(line_trailing)
.context(Context::Expected(ParserValue::CharLiteral('\n')))
.context(Context::Expected(ParserValue::CharLiteral('#'))),
)
.map_res(|((h, span), t)| state.borrow_mut().deref_mut().on_std_header(h, t, span))
.parse(i)
}
}
pub(crate) fn array_table<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl FnMut(Input<'i>) -> IResult<Input<'i>, (), ParserError<'i>> + 's {
move |i| {
(
delimited(
ARRAY_TABLE_OPEN,
cut(key),
cut(ARRAY_TABLE_CLOSE)
.context(Context::Expected(ParserValue::CharLiteral('.')))
.context(Context::Expected(ParserValue::StringLiteral("]]"))),
)
.with_span(),
cut(line_trailing)
.context(Context::Expected(ParserValue::CharLiteral('\n')))
.context(Context::Expected(ParserValue::CharLiteral('#'))),
)
.map_res(|((h, span), t)| state.borrow_mut().deref_mut().on_array_header(h, t, span))
.parse(i)
}
}
pub(crate) fn table<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl FnMut(Input<'i>) -> IResult<Input<'i>, (), ParserError<'i>> + 's {
move |i| {
dispatch!(peek::<_, &[u8],_,_>(take(2usize));
b"[[" => array_table(state),
_ => std_table(state),
)
.context(Context::Expression("table header"))
.parse(i)
}
}