surrealpatch/lib/src/sql/common.rs

138 lines
3.1 KiB
Rust
Raw Normal View History

use crate::sql::comment::mightbespace;
use crate::sql::comment::shouldbespace;
use crate::sql::error::Error::Parser;
2022-01-16 20:31:50 +00:00
use crate::sql::error::IResult;
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;
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, ()> {
let (i, _) = mightbespace(i)?;
let (i, _) = many1(char(';'))(i)?;
let (i, _) = mightbespace(i)?;
2020-06-29 15:36:01 +00:00
Ok((i, ()))
}
pub fn commas(i: &str) -> IResult<&str, ()> {
let (i, _) = mightbespace(i)?;
let (i, _) = char(',')(i)?;
let (i, _) = mightbespace(i)?;
2020-06-29 15:36:01 +00:00
Ok((i, ()))
}
pub fn verbar(i: &str) -> IResult<&str, ()> {
let (i, _) = mightbespace(i)?;
let (i, _) = char('|')(i)?;
let (i, _) = mightbespace(i)?;
Ok((i, ()))
}
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, ()))
}
#[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 {
chr.is_ascii_digit()
2020-06-29 15:36:01 +00:00
}
#[inline]
pub fn val_u8(chr: u8) -> bool {
is_alphanumeric(chr) || chr == b'_'
}
#[inline]
pub fn val_char(chr: char) -> bool {
chr.is_ascii_alphanumeric() || chr == '_'
2020-06-29 15:36:01 +00:00
}
pub fn take_u64(i: &str) -> IResult<&str, u64> {
let (i, v) = take_while(is_digit)(i)?;
match v.parse::<u64>() {
Ok(v) => Ok((i, v)),
_ => Err(Error(Parser(i))),
}
2020-06-29 15:36:01 +00:00
}
pub fn take_u32_len(i: &str) -> IResult<&str, (u32, usize)> {
let (i, v) = take_while(is_digit)(i)?;
match v.parse::<u32>() {
Ok(n) => Ok((i, (n, v.len()))),
_ => Err(Error(Parser(i))),
}
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> {
let (i, v) = take_while_m_n(n, n, is_digit)(i)?;
2022-03-16 21:04:22 +00:00
match v.parse::<u32>() {
Ok(v) => Ok((i, v)),
_ => Err(Error(Parser(i))),
}
2020-06-29 15:36:01 +00:00
}
pub fn take_digits_range(i: &str, n: usize, range: impl RangeBounds<u32>) -> IResult<&str, u32> {
let (i, v) = take_while_m_n(n, n, is_digit)(i)?;
2022-03-16 21:04:22 +00:00
match v.parse::<u32>() {
Ok(v) if range.contains(&v) => Ok((i, v)),
_ => Err(Error(Parser(i))),
2020-06-29 15:36:01 +00:00
}
}