2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::comment::mightbespace;
|
2022-09-04 09:51:06 +00:00
|
|
|
use crate::sql::comment::shouldbespace;
|
2023-01-08 17:11:35 +00:00
|
|
|
use crate::sql::error::Error::Parser;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2022-09-04 09:51:06 +00:00
|
|
|
use nom::branch::alt;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::bytes::complete::take_while;
|
|
|
|
use nom::bytes::complete::take_while_m_n;
|
2022-03-16 23:52:25 +00:00
|
|
|
use nom::character::complete::char;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::character::is_alphanumeric;
|
|
|
|
use nom::multi::many1;
|
2022-01-16 20:31:50 +00:00
|
|
|
use nom::Err::Error;
|
2020-06-29 15:36:01 +00:00
|
|
|
use std::ops::RangeBounds;
|
|
|
|
|
|
|
|
pub fn colons(i: &str) -> IResult<&str, ()> {
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = many1(char(';'))(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn commas(i: &str) -> IResult<&str, ()> {
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(',')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
2023-04-25 10:13:04 +00:00
|
|
|
pub fn verbar(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, _) = char('|')(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
2022-09-04 09:51:06 +00:00
|
|
|
pub fn commasorspace(i: &str) -> IResult<&str, ()> {
|
|
|
|
alt((commas, shouldbespace))(i)
|
|
|
|
}
|
|
|
|
|
2023-05-10 02:08:09 +00:00
|
|
|
pub fn openparentheses(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = char('(')(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn closeparentheses(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, _) = char(')')(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn openbraces(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = char('{')(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn closebraces(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, _) = char('}')(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn openbracket(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = char('[')(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn closebracket(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, _) = char(']')(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn openchevron(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = char('<')(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn closechevron(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, _) = char('>')(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
2022-06-25 22:37:45 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn is_hex(chr: char) -> bool {
|
|
|
|
chr.is_ascii_hexdigit()
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn is_digit(chr: char) -> bool {
|
2022-09-27 21:35:30 +00:00
|
|
|
chr.is_ascii_digit()
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-10-16 23:04:07 +00:00
|
|
|
pub fn val_u8(chr: u8) -> bool {
|
|
|
|
is_alphanumeric(chr) || chr == b'_'
|
2022-09-27 21:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-10-16 23:04:07 +00:00
|
|
|
pub fn val_char(chr: char) -> bool {
|
|
|
|
chr.is_ascii_alphanumeric() || chr == '_'
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 01:25:45 +00:00
|
|
|
pub fn take_u64(i: &str) -> IResult<&str, u64> {
|
2022-03-16 15:40:26 +00:00
|
|
|
let (i, v) = take_while(is_digit)(i)?;
|
2022-09-22 01:25:45 +00:00
|
|
|
match v.parse::<u64>() {
|
2022-03-16 15:40:26 +00:00
|
|
|
Ok(v) => Ok((i, v)),
|
2023-01-08 17:11:35 +00:00
|
|
|
_ => Err(Error(Parser(i))),
|
2022-03-16 15:40:26 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 01:25:45 +00:00
|
|
|
pub fn take_u32_len(i: &str) -> IResult<&str, (u32, usize)> {
|
2022-03-16 15:40:26 +00:00
|
|
|
let (i, v) = take_while(is_digit)(i)?;
|
2022-09-22 01:25:45 +00:00
|
|
|
match v.parse::<u32>() {
|
|
|
|
Ok(n) => Ok((i, (n, v.len()))),
|
2023-01-08 17:11:35 +00:00
|
|
|
_ => Err(Error(Parser(i))),
|
2022-03-16 15:40:26 +00:00
|
|
|
}
|
2022-01-20 20:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
pub fn take_digits(i: &str, n: usize) -> IResult<&str, u32> {
|
2022-03-16 15:40:26 +00:00
|
|
|
let (i, v) = take_while_m_n(n, n, is_digit)(i)?;
|
2022-03-16 21:04:22 +00:00
|
|
|
match v.parse::<u32>() {
|
2022-03-16 15:40:26 +00:00
|
|
|
Ok(v) => Ok((i, v)),
|
2023-01-08 17:11:35 +00:00
|
|
|
_ => Err(Error(Parser(i))),
|
2022-03-16 15:40:26 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_digits_range(i: &str, n: usize, range: impl RangeBounds<u32>) -> IResult<&str, u32> {
|
2022-03-16 15:40:26 +00:00
|
|
|
let (i, v) = take_while_m_n(n, n, is_digit)(i)?;
|
2022-03-16 21:04:22 +00:00
|
|
|
match v.parse::<u32>() {
|
2022-03-16 15:40:26 +00:00
|
|
|
Ok(v) if range.contains(&v) => Ok((i, v)),
|
2023-01-08 17:11:35 +00:00
|
|
|
_ => Err(Error(Parser(i))),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|