Parse escape characters properly
This commit is contained in:
parent
8edf1b9595
commit
9f55bef238
32
src/basic.rs
32
src/basic.rs
@ -1,25 +1,33 @@
|
||||
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("\"")
|
||||
escaped_transform(
|
||||
none_of("\\\""),
|
||||
'\\',
|
||||
alt((value("\\", tag("\\")), value("\"", tag("\"")))),
|
||||
),
|
||||
tag("\""),
|
||||
)(i)
|
||||
}
|
||||
|
||||
@ -45,7 +53,7 @@ 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);
|
||||
@ -55,13 +63,13 @@ mod tests {
|
||||
fn it_reads_a_string() {
|
||||
let input = r#""Hello, \"World\"""#;
|
||||
let (_, output) = super::read_string(input).unwrap();
|
||||
assert_eq!(r#"Hello, \"World\""#, output);
|
||||
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\""#));
|
||||
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