Add strings

This commit is contained in:
dalance 2019-06-28 20:49:42 +09:00
parent ade1ca229a
commit 8ef64f86f8
2 changed files with 55 additions and 0 deletions

View File

@ -3,4 +3,5 @@ pub mod comments;
pub mod identifiers;
pub mod numbers;
pub mod operators;
pub mod strings;
pub mod util;

54
src/strings.rs Normal file
View File

@ -0,0 +1,54 @@
use nom::bytes::complete::*;
use nom::combinator::*;
use nom::multi::*;
use nom::sequence::*;
use nom::IResult;
// -----------------------------------------------------------------------------
#[derive(Debug)]
pub struct StringLiteral<'a> {
pub raw: Vec<&'a str>,
}
// -----------------------------------------------------------------------------
pub fn string_literal(s: &str) -> IResult<&str, StringLiteral> {
let (s, _) = tag("\"")(s)?;
let (s, x) = many1(pair(is_not("\\\""), opt(pair(tag("\\"), take(1usize)))))(s)?;
let (s, _) = tag("\"")(s)?;
let mut raw = Vec::new();
for (x, y) in x {
raw.push(x);
if let Some((y, z)) = y {
raw.push(y);
raw.push(z);
}
}
Ok((s, StringLiteral { raw }))
}
// -----------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
assert_eq!(
format!("{:?}", all_consuming(string_literal)("\"aaa aaaa\"")),
"Ok((\"\", StringLiteral { raw: [\"aaa aaaa\"] }))"
);
assert_eq!(
format!("{:?}", all_consuming(string_literal)(r#""aaa\" aaaa""#)),
"Ok((\"\", StringLiteral { raw: [\"aaa\", \"\\\\\", \"\\\"\", \" aaaa\"] }))"
);
assert_eq!(
format!("{:?}", all_consuming(string_literal)(r#""aaa\"""#)),
"Ok((\"\", StringLiteral { raw: [\"aaa\", \"\\\\\", \"\\\"\"] }))"
);
}
}