use crate::refs_impl::error::OFCode::*;
use crate::refs_impl::error::ParseOFError;
use crate::refs_impl::{span_union, ParseResult, Span};
use nom::combinator::opt;
use nom::error::ErrorKind::*;
use nom::InputTake;
pub mod nomtokens;
use nomtokens::{
add_op_nom, brackets_close_nom, brackets_open_nom, col_nom, colon_nom, comparison_op_nom,
dollar_dollar_nom, dollar_nom, dot_nom, double_quote_nom, double_string_nom, fn_name_nom,
hashtag_nom, identifier_nom, mul_op_nom, number_nom, parentheses_close_nom,
parentheses_open_nom, postfix_op_nom, pow_op_nom, prefix_op_nom, ref_concat_op_nom,
ref_intersection_op_nom, reference_op_nom, row_nom, semikolon_nom, sheet_name_nom,
single_quote_nom, single_string_nom, string_op_nom,
};
pub use nomtokens::{
eat_space, lah_dollar_dollar, lah_dot, lah_fn_name, lah_identifier, lah_iri, lah_number,
lah_parentheses_open, lah_prefix_op, lah_sheet_name, lah_string,
};
pub fn empty<'a>(i: Span<'a>) -> Span<'a> {
i.take(0)
}
pub fn number<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match number_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Char || e.code == OneOf => {
Err(ParseOFError::number(rest))
}
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn string<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
const QUOTE: char = '\"';
let (rest, first_quote) = match double_quote_nom(rest) {
Ok((rest, quote)) => (rest, quote),
Err(nom::Err::Error(e)) if e.code == Char => return Err(ParseOFError::start_quote(rest)),
Err(e) => return Err(ParseOFError::nom(e)),
};
let (rest, _string) = match double_string_nom(rest) {
Ok((rest, tok)) => (rest, tok),
Err(nom::Err::Error(e)) if e.code == TakeWhile1 || e.code == Char => {
return Err(ParseOFError::string(rest));
}
Err(e) => {
return Err(ParseOFError::nom(e));
}
};
let (rest, last_quote) = match double_quote_nom(rest) {
Ok((rest, quote)) => (rest, quote),
Err(nom::Err::Error(e)) if e.code == Char => return Err(ParseOFError::end_quote(rest)),
Err(e) => return Err(ParseOFError::nom(e)),
};
let token = unsafe { span_union(first_quote, last_quote) };
Ok((rest, token))
}
pub fn fn_name<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match fn_name_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == TakeWhile1 || e.code == TakeWhileMN => {
Err(ParseOFError::fn_name(rest))
}
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn comparison_op<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match comparison_op_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::comp_op(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn string_op<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match string_op_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::string_op(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn reference_op<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match reference_op_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::ref_op(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn ref_intersection_op<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match ref_intersection_op_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::ref_intersect_op(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn ref_concat_op<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match ref_concat_op_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::ref_concat_op(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn dollar_dollar<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match dollar_dollar_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::dollardollar(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn dollar<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match dollar_dollar_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::dollar(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn hashtag<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match hashtag_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::hashtag(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn semikolon<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match semikolon_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::semikolon(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn dot<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match dot_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::dot(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn colon<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match colon_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::colon(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn parentheses_open<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match parentheses_open_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::parens_open(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn parentheses_close<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match parentheses_close_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::parens_close(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn brackets_open<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match brackets_open_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::brackets_open(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn brackets_close<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match brackets_close_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::brackets_close(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn add_op<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match add_op_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::add_op(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn mul_op<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match mul_op_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::mul_op(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn pow_op<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match pow_op_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::pow_op(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn prefix_op<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match prefix_op_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::prefix_op(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn postfix_op<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match postfix_op_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::postfix_op(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn identifier<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
match identifier_nom(rest) {
Ok((rest, tok)) => Ok((rest, tok)),
Err(nom::Err::Error(e)) if e.code == TakeWhile1 || e.code == TakeWhileMN => {
Err(ParseOFError::identifier(rest))
}
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn sheet_name(rest: Span<'_>) -> ParseResult<'_, (Option<Span<'_>>, Span<'_>)> {
let (rest, abs) = match opt(dollar_nom)(rest) {
Ok((rest, abs)) => (rest, abs),
Err(nom::Err::Error(e)) if e.code == Tag => {
return Err(ParseOFError::dollar(rest));
}
Err(e) => return Err(ParseOFError::nom(e)),
};
let (rest, name) = match single_quoted(rest) {
Ok((rest, name)) => (rest, Some(name)),
Err(e) if e.code == OFCSingleQuoteStart => (rest, None),
Err(e) if e.code == OFCString || e.code == OFCSingleQuoteEnd => return Err(e),
Err(e) => return Err(ParseOFError::unexpected(e)),
};
let (rest, name) = if let Some(name) = name {
(rest, name)
} else {
match sheet_name_nom(rest) {
Ok((rest, tok)) => (rest, tok),
Err(nom::Err::Error(e)) if e.code == NoneOf => {
return Err(ParseOFError::sheet_name(rest));
}
Err(e) => return Err(ParseOFError::nom(e)),
}
};
Ok((rest, (abs, name)))
}
pub fn quoted_sheet_name(rest: Span<'_>) -> ParseResult<'_, (Option<Span<'_>>, Span<'_>)> {
let (rest, abs) = match opt(dollar_nom)(rest) {
Ok((rest, tok)) => (rest, tok),
Err(e) => return Err(ParseOFError::nom(e)),
};
let rest = eat_space(rest);
let (rest, name) = match single_quoted(rest) {
Ok((rest, tok)) => (rest, tok),
Err(e)
if e.code == OFCSingleQuoteStart
|| e.code == OFCString
|| e.code == OFCSingleQuoteEnd =>
{
return Err(e)
}
Err(e) => return Err(ParseOFError::unexpected(e)),
};
Ok((rest, (abs, name)))
}
pub fn iri(rest: Span<'_>) -> ParseResult<'_, Span<'_>> {
let (rest, iri) = match single_quoted(rest) {
Ok((rest, tok)) => (rest, tok),
Err(e)
if e.code == OFCSingleQuoteStart
|| e.code == OFCString
|| e.code == OFCSingleQuoteEnd =>
{
return Err(e)
}
Err(e) => return Err(ParseOFError::unexpected(e)),
};
let rest = eat_space(rest);
let (rest,) = match hashtag_nom(rest) {
Ok((rest, _hash)) => (rest,),
Err(nom::Err::Error(e)) if e.code == Tag => {
return Err(ParseOFError::hashtag(rest));
}
Err(e) => return Err(ParseOFError::nom(e)),
};
Ok((rest, iri))
}
pub fn row(rest: Span<'_>) -> ParseResult<'_, (Option<Span<'_>>, Span<'_>)> {
match row_nom(rest) {
Ok((rest, (abs, row))) => Ok((rest, (abs, row))),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::dollar(rest)),
Err(nom::Err::Error(e)) if e.code == OneOf => Err(ParseOFError::digit(rest)),
Err(nom::Err::Error(e)) if e.code == Many1 => Err(ParseOFError::digit(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn col(rest: Span<'_>) -> ParseResult<'_, (Option<Span<'_>>, Span<'_>)> {
match col_nom(rest) {
Ok((rest, (abs, col))) => Ok((rest, (abs, col))),
Err(nom::Err::Error(e)) if e.code == Tag => Err(ParseOFError::dollar(rest)),
Err(nom::Err::Error(e)) if e.code == Alpha => Err(ParseOFError::alpha(rest)),
Err(e) => Err(ParseOFError::nom(e)),
}
}
pub fn single_quoted<'a>(rest: Span<'a>) -> ParseResult<'a, Span<'a>> {
const QUOTE: char = '\'';
let (rest, first_quote) = match single_quote_nom(rest) {
Ok((rest, quote)) => (rest, quote),
Err(nom::Err::Error(e)) if e.code == Char => {
return Err(ParseOFError::start_single_quote(rest))
}
Err(e) => return Err(ParseOFError::nom(e)),
};
let (rest, _string) = match single_string_nom(rest) {
Ok((rest, tok)) => (rest, tok),
Err(nom::Err::Error(e)) if e.code == TakeWhile1 => return Err(ParseOFError::string(rest)),
Err(nom::Err::Error(e)) if e.code == Char => return Err(ParseOFError::string(rest)),
Err(e) => return Err(ParseOFError::nom(e)),
};
let (rest, last_quote) = match single_quote_nom(rest) {
Ok((rest, quote)) => (rest, quote),
Err(nom::Err::Error(e)) if e.code == Char => {
return Err(ParseOFError::end_single_quote(rest))
}
Err(e) => return Err(ParseOFError::nom(e)),
};
let token = unsafe { span_union(first_quote, last_quote) };
Ok((rest, token))
}