2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::take_until;
|
2022-03-16 23:52:25 +00:00
|
|
|
use nom::character::complete::char;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::character::complete::multispace0;
|
|
|
|
use nom::character::complete::multispace1;
|
|
|
|
use nom::character::complete::not_line_ending;
|
2023-08-21 15:29:50 +00:00
|
|
|
use nom::multi::many0;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::multi::many1;
|
|
|
|
|
|
|
|
pub fn mightbespace(i: &str) -> IResult<&str, ()> {
|
2023-08-21 15:29:50 +00:00
|
|
|
let (i, _) = many0(alt((comment, space)))(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shouldbespace(i: &str) -> IResult<&str, ()> {
|
2023-08-21 15:29:50 +00:00
|
|
|
let (i, _) = many1(alt((comment, space)))(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn comment(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = multispace0(i)?;
|
|
|
|
let (i, _) = many1(alt((block, slash, dash, hash)))(i)?;
|
|
|
|
let (i, _) = multispace0(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:52:16 +00:00
|
|
|
pub fn block(i: &str) -> IResult<&str, ()> {
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = multispace0(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('/')(i)?;
|
|
|
|
let (i, _) = char('*')(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = take_until("*/")(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('*')(i)?;
|
|
|
|
let (i, _) = char('/')(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = multispace0(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:52:16 +00:00
|
|
|
pub fn slash(i: &str) -> IResult<&str, ()> {
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = multispace0(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('/')(i)?;
|
|
|
|
let (i, _) = char('/')(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = not_line_ending(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:52:16 +00:00
|
|
|
pub fn dash(i: &str) -> IResult<&str, ()> {
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = multispace0(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('-')(i)?;
|
|
|
|
let (i, _) = char('-')(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = not_line_ending(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:52:16 +00:00
|
|
|
pub fn hash(i: &str) -> IResult<&str, ()> {
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = multispace0(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('#')(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = not_line_ending(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
2023-03-18 14:52:16 +00:00
|
|
|
|
|
|
|
fn space(i: &str) -> IResult<&str, ()> {
|
|
|
|
let (i, _) = multispace1(i)?;
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
2023-08-21 15:29:50 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::sql::parse;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn any_whitespace() {
|
|
|
|
let sql = "USE /* white space and comment between */ NS test;";
|
|
|
|
assert!(parse(sql).is_ok());
|
|
|
|
}
|
|
|
|
}
|