surrealpatch/lib/src/sql/comment.rs

77 lines
1.8 KiB
Rust
Raw Normal View History

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;
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, ()))
}
pub fn block(i: &str) -> IResult<&str, ()> {
2020-06-29 15:36:01 +00:00
let (i, _) = multispace0(i)?;
let (i, _) = char('/')(i)?;
let (i, _) = char('*')(i)?;
2020-06-29 15:36:01 +00:00
let (i, _) = take_until("*/")(i)?;
let (i, _) = char('*')(i)?;
let (i, _) = char('/')(i)?;
2020-06-29 15:36:01 +00:00
let (i, _) = multispace0(i)?;
Ok((i, ()))
}
pub fn slash(i: &str) -> IResult<&str, ()> {
2020-06-29 15:36:01 +00:00
let (i, _) = multispace0(i)?;
let (i, _) = char('/')(i)?;
let (i, _) = char('/')(i)?;
2020-06-29 15:36:01 +00:00
let (i, _) = not_line_ending(i)?;
Ok((i, ()))
}
pub fn dash(i: &str) -> IResult<&str, ()> {
2020-06-29 15:36:01 +00:00
let (i, _) = multispace0(i)?;
let (i, _) = char('-')(i)?;
let (i, _) = char('-')(i)?;
2020-06-29 15:36:01 +00:00
let (i, _) = not_line_ending(i)?;
Ok((i, ()))
}
pub fn hash(i: &str) -> IResult<&str, ()> {
2020-06-29 15:36:01 +00:00
let (i, _) = multispace0(i)?;
let (i, _) = char('#')(i)?;
2020-06-29 15:36:01 +00:00
let (i, _) = not_line_ending(i)?;
Ok((i, ()))
}
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());
}
}