Add comment

This commit is contained in:
dalance 2019-06-28 12:29:06 +09:00
parent d6f2fbabc2
commit a3c2e79e7b
2 changed files with 52 additions and 0 deletions

51
src/comment.rs Normal file
View File

@ -0,0 +1,51 @@
use nom::branch::*;
use nom::bytes::complete::*;
use nom::IResult;
// -----------------------------------------------------------------------------
#[derive(Debug)]
pub struct Comment<'a> {
pub raw: Vec<&'a str>,
}
// -----------------------------------------------------------------------------
pub fn comment(s: &str) -> IResult<&str, Comment> {
alt((one_line_comment, block_comment))(s)
}
pub fn one_line_comment(s: &str) -> IResult<&str, Comment> {
let (s, x) = tag("//")(s)?;
let (s, y) = is_not("\n")(s)?;
let raw = vec![x, y];
Ok((s, Comment { raw }))
}
pub fn block_comment(s: &str) -> IResult<&str, Comment> {
let (s, x) = tag("/*")(s)?;
let (s, y) = is_not("*/")(s)?;
let (s, z) = tag("*/")(s)?;
let raw = vec![x, y, z];
Ok((s, Comment { raw }))
}
// -----------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use nom::combinator::*;
#[test]
fn test() {
assert_eq!(
format!("{:?}", all_consuming(comment)("// comment")),
"Ok((\"\", Comment { raw: [\"//\", \" comment\"] }))"
);
assert_eq!(
format!("{:?}", all_consuming(comment)("/* comment\n\n */")),
"Ok((\"\", Comment { raw: [\"/*\", \" comment\\n\\n \", \"*/\"] }))"
);
}
}

View File

@ -1,3 +1,4 @@
pub mod comment;
pub mod identifier; pub mod identifier;
pub mod number; pub mod number;
pub mod util; pub mod util;