Parse escape characters properly
This commit is contained in:
parent
8edf1b9595
commit
9f55bef238
56
src/basic.rs
56
src/basic.rs
@ -1,26 +1,34 @@
|
||||
use nom::{
|
||||
bytes::complete::{tag, take_until, escaped},
|
||||
character::{complete::{u32 as ccu32, one_of}, streaming::none_of},
|
||||
combinator::map,
|
||||
branch::alt,
|
||||
bytes::complete::{escaped_transform, tag, take_until},
|
||||
character::{
|
||||
complete::{u32 as ccu32},
|
||||
streaming::none_of,
|
||||
},
|
||||
combinator::{map, value},
|
||||
sequence::{delimited, terminated, tuple},
|
||||
IResult,
|
||||
};
|
||||
|
||||
pub type Line<'a> = (u32, Command<'a>);
|
||||
pub type Line = (u32, Command);
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum Command<'a> {
|
||||
Print(&'a str),
|
||||
pub enum Command {
|
||||
Print(String),
|
||||
None,
|
||||
}
|
||||
|
||||
fn read_string(i: &str) -> IResult<&str, &str> {
|
||||
fn read_string(i: &str) -> IResult<&str, String> {
|
||||
// take_until("\"")(i)
|
||||
delimited(
|
||||
tag("\""),
|
||||
escaped(none_of("\\\""), '\\', one_of("\"\\")),
|
||||
tag("\"")
|
||||
)(i)
|
||||
delimited(
|
||||
tag("\""),
|
||||
escaped_transform(
|
||||
none_of("\\\""),
|
||||
'\\',
|
||||
alt((value("\\", tag("\\")), value("\"", tag("\"")))),
|
||||
),
|
||||
tag("\""),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn parse_command(i: &str) -> IResult<&str, Command> {
|
||||
@ -45,25 +53,25 @@ mod tests {
|
||||
#[test]
|
||||
fn it_parses_a_print_command() {
|
||||
let input = "10 PRINT \"Hello, world\"";
|
||||
let expected = (10, super::Command::Print("Hello, world"));
|
||||
let expected = (10, super::Command::Print(String::from("Hello, world")));
|
||||
|
||||
let (_, result) = super::parse_line(input).unwrap();
|
||||
assert_eq!(expected, result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_reads_a_string() {
|
||||
let input = r#""Hello, \"World\"""#;
|
||||
let (_, output) = super::read_string(input).unwrap();
|
||||
assert_eq!(r#"Hello, \"World\""#, output);
|
||||
}
|
||||
#[test]
|
||||
fn it_reads_a_string() {
|
||||
let input = r#""Hello, \"World\"""#;
|
||||
let (_, output) = super::read_string(input).unwrap();
|
||||
assert_eq!(r#"Hello, "World""#, output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_a_print_command_with_escaped_quotes() {
|
||||
let input = r#"10 PRINT "Hello, \"world\"""#;
|
||||
let expected = (10, super::Command::Print(r#"Hello, \"world\""#));
|
||||
#[test]
|
||||
fn it_parses_a_print_command_with_escaped_quotes() {
|
||||
let input = r#"10 PRINT "Hello, \"world\"""#;
|
||||
let expected = (10, super::Command::Print(String::from(r#"Hello, "world""#)));
|
||||
|
||||
let (_, result) = super::parse_line(input).unwrap();
|
||||
assert_eq!(expected, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ fn main() {
|
||||
let (_, (_, command)) = basic::parse_line(lines).unwrap();
|
||||
match command {
|
||||
basic::Command::Print(input) => {
|
||||
println!("{}", input.replace("\\", ""));
|
||||
println!("{}", input);
|
||||
}
|
||||
_ => {
|
||||
panic!("Command not recognised");
|
||||
|
Loading…
Reference in New Issue
Block a user