From e698bcc9a2a4496da6366835ccffd03e805cb6cd Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Wed, 8 Sep 2021 18:54:32 +0200 Subject: [PATCH] strip C-style comments in the preprocessor Using '#' for comments sometime confused the preprocessor. It also made no sense to have comment support in the parser itself, so instead C-style comments ('//') are being stipped by the preprocessor directly. --- Cargo.lock | 78 +++++++++++++++++++++-------- cli/Cargo.toml | 2 +- emulator/Cargo.toml | 3 +- emulator/src/parser/condition.rs | 1 + emulator/src/parser/line.rs | 72 ++++---------------------- emulator/src/parser/preprocessor.rs | 18 +++++++ emulator/src/parser/value.rs | 2 + samples/fact.S | 16 +++--- samples/swap.S | 8 +-- web/Cargo.toml | 2 +- 10 files changed, 106 insertions(+), 96 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8447e0aa..e85b9d6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,6 +130,22 @@ dependencies = [ "winapi", ] +[[package]] +name = "ctor" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccc0a48a9b826acdf4028595adc9db92caea352f7af011a3034acd172a52a0aa" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "diff" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" + [[package]] name = "dirs-next" version = "2.0.0" @@ -246,9 +262,9 @@ checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] name = "js-sys" -version = "0.3.53" +version = "0.3.54" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4bf49d50e2961077d9c99f4b7997d770a1114f087c3c2e0069b36c13fc2979d" +checksum = "1866b355d9c878e5e607473cbe3f63282c0b7aad2db1dbebf55076c686918254" dependencies = [ "wasm-bindgen", ] @@ -300,9 +316,9 @@ dependencies = [ [[package]] name = "minimal-lexical" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6595bb28ed34f43c3fe088e48f6cfb2e033cab45f25a5384d5fdf564fbc8c4b2" +checksum = "0c835948974f68e0bd58636fc6c5b1fbff7b297e3046f11b3b3c18bbac012c6d" [[package]] name = "nibble_vec" @@ -368,6 +384,15 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6acbef58a60fe69ab50510a55bc8cdd4d6cf2283d27ad338f54cb52747a9cf2d" +[[package]] +name = "output_vt100" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53cdc5b785b7a58c5aad8216b3dfa114df64b0b06ae6e1501cef91df2fbdf8f9" +dependencies = [ + "winapi", +] + [[package]] name = "parse-display" version = "0.5.1" @@ -400,6 +425,18 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d31d11c69a6b52a174b42bdc0c30e5e11670f90788b2c471c31c1d17d449443" +[[package]] +name = "pretty_assertions" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cab0e7c02cf376875e9335e0ba1da535775beb5450d21e1dffca068818ed98b" +dependencies = [ + "ansi_term", + "ctor", + "diff", + "output_vt100", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -644,9 +681,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.75" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7f58f7e8eaa0009c5fec437aabf511bd9933e4b2d7407bd05273c01a8906ea7" +checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84" dependencies = [ "proc-macro2", "quote", @@ -673,18 +710,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.28" +version = "1.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "283d5230e63df9608ac7d9691adc1dfb6e701225436eb64d0b9a7f0a5a04f6ec" +checksum = "602eca064b2d83369e2b2f34b09c70b605402801927c65c11071ac911d299b88" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.28" +version = "1.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa3884228611f5cd3608e2d409bf7dce832e4eb3135e3f11addbd7e41bd68e71" +checksum = "bad553cc2c78e8de258400763a647e80e6d1b31ee237275d756f6836d204494c" dependencies = [ "proc-macro2", "quote", @@ -825,9 +862,9 @@ checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] name = "wasm-bindgen" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce9b1b516211d33767048e5d47fa2a381ed8b76fc48d2ce4aa39877f9f183e0" +checksum = "5e68338db6becec24d3c7977b5bf8a48be992c934b5d07177e3931f5dc9b076c" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -835,9 +872,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfe8dc78e2326ba5f845f4b5bf548401604fa20b1dd1d365fb73b6c1d6364041" +checksum = "f34c405b4f0658583dba0c1c7c9b694f3cac32655db463b56c254a1c75269523" dependencies = [ "bumpalo", "lazy_static", @@ -850,9 +887,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44468aa53335841d9d6b6c023eaab07c0cd4bddbcfdee3e2bb1e8d2cb8069fef" +checksum = "b9d5a6580be83b19dc570a8f9c324251687ab2184e57086f71625feb57ec77c8" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -860,9 +897,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0195807922713af1e67dc66132c7328206ed9766af3858164fb583eedc25fbad" +checksum = "e3775a030dc6f5a0afd8a84981a21cc92a781eb429acef9ecce476d0c9113e92" dependencies = [ "proc-macro2", "quote", @@ -873,9 +910,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acdb075a845574a1fa5f09fd77e43f7747599301ea3417a9fbffdeedfc1f4a29" +checksum = "c279e376c7a8e8752a8f1eaa35b7b0bee6bb9fb0cdacfa97cc3f1f289c87e2b4" [[package]] name = "winapi" @@ -935,6 +972,7 @@ dependencies = [ "indoc", "nom", "parse-display", + "pretty_assertions", "thiserror", "tracing", "tracing-subscriber", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 54e33bf9..7d09f4f8 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -21,4 +21,4 @@ clap_generate = "3.0.0-beta.4" atty = "0.2.14" nom = "7.0.0" anyhow = "1.0.43" -thiserror = "1.0.28" +thiserror = "1.0.29" diff --git a/emulator/Cargo.toml b/emulator/Cargo.toml index d797609c..90dfd8e4 100644 --- a/emulator/Cargo.toml +++ b/emulator/Cargo.toml @@ -10,7 +10,7 @@ license = "MIT" [dependencies] bitflags = "1.3.2" -thiserror = "1.0.28" +thiserror = "1.0.29" nom = "7.0.0" tracing = "0.1.26" tracing-subscriber = "0.2.20" @@ -19,3 +19,4 @@ parse-display = "0.5.1" [dev-dependencies] indoc = "1.0.3" +pretty_assertions = "0.7.2" diff --git a/emulator/src/parser/condition.rs b/emulator/src/parser/condition.rs index 3458e7cf..de2aff67 100644 --- a/emulator/src/parser/condition.rs +++ b/emulator/src/parser/condition.rs @@ -518,6 +518,7 @@ fn parse_parenthesis<'a, Error: ParseError<&'a str>>( #[cfg(test)] mod tests { use nom::Finish; + use pretty_assertions::assert_eq; use super::*; diff --git a/emulator/src/parser/line.rs b/emulator/src/parser/line.rs index 5c3ddf73..06ab103d 100644 --- a/emulator/src/parser/line.rs +++ b/emulator/src/parser/line.rs @@ -10,8 +10,8 @@ use nom::{ branch::alt, - bytes::complete::{escaped, tag}, - character::complete::{char, line_ending, none_of, not_line_ending, one_of, space0, space1}, + bytes::complete::escaped, + character::complete::{char, line_ending, none_of, one_of, space0, space1}, combinator::{all_consuming, cut, eof, map, opt, peek, value}, error::context, multi::separated_list1, @@ -105,7 +105,6 @@ impl std::fmt::Display for LineContent { pub(crate) struct Line { pub symbols: Vec>, pub content: Option, L>>, - comment: Option>, } impl AstNode for Line { @@ -124,12 +123,6 @@ impl AstNode for Line { children.extend(self.content.iter().map(|c| c.to_node())); - children.extend( - self.comment - .iter() - .map(|c| Node::new(NodeKind::Comment, c.location.clone()).content(c.inner.clone())), - ); - children } } @@ -147,15 +140,6 @@ impl std::fmt::Display for Line { write!(f, " ")?; } write!(f, "{}", c.inner)?; - had_something = true; - } - - if let Some(ref c) = self.comment { - if had_something { - write!(f, "\t{}", c.inner)?; - } else { - write!(f, "{}", c.inner)?; - } } Ok(()) @@ -305,15 +289,6 @@ fn parse_line_content<'a, Error: ParseError<&'a str>>( ))(input) } -/// Parses an inline comment -fn parse_comment<'a, Error: ParseError<&'a str>>( - input: &'a str, -) -> IResult<&'a str, String, Error> { - let (input, _) = peek(tag("#"))(input)?; - let (input, comment) = not_line_ending(input)?; - Ok((input, comment.into())) -} - /// Parses symbol definitions fn parse_symbol_definition<'a, Error: ParseError<&'a str>>( input: &'a str, @@ -348,20 +323,8 @@ fn parse_line<'a, Error: ParseError<&'a str>>( let content = content.map(|line| line.with_location((input, start, rest))); // Save location information let (rest, _) = space0(rest)?; - // Extract the comment - let start = rest; - let (rest, comment) = opt(parse_comment)(rest)?; - let comment = comment.map(|comment| comment.with_location((input, start, rest))); // Save location information - // Build the line - Ok(( - rest, - Line { - symbols, - content, - comment, - }, - )) + Ok((rest, Line { symbols, content })) } fn split_lines<'a, Error: ParseError<&'a str>>( @@ -397,6 +360,8 @@ pub(crate) fn parse_program<'a, Error: ParseError<&'a str>>( #[cfg(test)] mod tests { + use pretty_assertions::assert_eq; + use crate::runtime::Reg; use super::super::location::Locatable; @@ -415,18 +380,6 @@ mod tests { assert_eq!(line, Line::default()); } - #[test] - fn parse_comment_line_test() { - let line = fully_parsed(parse_line("# hello")); - assert_eq!( - line, - Line { - comment: Some("# hello".to_string().with_location((0, 7))), - ..Default::default() - } - ); - } - #[test] fn parse_symbol_line_test() { let line = fully_parsed(parse_line("hello:world : duplicate: duplicate: ")); @@ -447,7 +400,7 @@ mod tests { #[test] fn parse_full_line_test() { use super::super::expression::Node; - let line = fully_parsed(parse_line("foo: bar: .space 30 + 5 # comment")); + let line = fully_parsed(parse_line("foo: bar: .space 30 + 5")); assert_eq!( line, Line { @@ -466,7 +419,6 @@ mod tests { } .with_location((10, 13)) ), - comment: Some("# comment".to_string().with_location((24, 9))), } ); } @@ -496,7 +448,7 @@ this has escaped chars: \r \n \t \""#; let input = r#" str: .space "some multiline \ string" -main: # beginning of program +main: add %a, %b reset "#; @@ -519,15 +471,13 @@ main: # beginning of program } .with_location((5, 32)) ), - ..Default::default() } .with_location((1, 37)), Line { symbols: vec!["main".to_string().with_location((0, 5))], - comment: Some("# beginning of program".to_string().with_location((6, 22))), ..Default::default() } - .with_location((39, 28)), + .with_location((39, 5)), Line { content: Some( LineContent::Instruction { @@ -541,7 +491,7 @@ main: # beginning of program ), ..Default::default() } - .with_location((68, 14)), + .with_location((45, 14)), Line { content: Some( LineContent::Instruction { @@ -552,8 +502,8 @@ main: # beginning of program ), ..Default::default() } - .with_location((83, 9)), - Line::default().with_location((93, 8)), + .with_location((60, 9)), + Line::default().with_location((70, 8)), ] } ); diff --git a/emulator/src/parser/preprocessor.rs b/emulator/src/parser/preprocessor.rs index 7e07dc3b..1bbb2efe 100644 --- a/emulator/src/parser/preprocessor.rs +++ b/emulator/src/parser/preprocessor.rs @@ -336,6 +336,12 @@ fn parse_raw<'a, Error: ParseError<&'a str>>( ) -> IResult<&'a str, Node, Error> { let (rest, _) = not(char('#'))(input)?; let (rest, content) = not_line_ending(rest)?; + // Strip the comment from the content + let content = if let Some(i) = content.find("//") { + &content[..i] + } else { + content + }; let content = content.to_string(); Ok((rest, Node::Raw { content })) } @@ -377,6 +383,8 @@ pub(crate) fn parse<'a, Error: ParseError<&'a str>>( #[cfg(test)] mod tests { + use pretty_assertions::assert_eq; + use super::*; #[test] @@ -450,6 +458,16 @@ mod tests { } ); + // It extracts the line and discard the comment + let (rest, body) = parse_raw::<()>("line // comment").unwrap(); + assert_eq!(rest, ""); + assert_eq!( + body, + Node::Raw { + content: "line ".to_string() + } + ); + // Gets only one line let (rest, body) = parse_raw::<()>("line\nline").unwrap(); assert_eq!(rest, "\nline"); diff --git a/emulator/src/parser/value.rs b/emulator/src/parser/value.rs index fe5ed8bb..8a83f018 100644 --- a/emulator/src/parser/value.rs +++ b/emulator/src/parser/value.rs @@ -413,6 +413,8 @@ fn parse_indirect<'a, Error: ParseError<&'a str>>( #[cfg(test)] mod tests { + use pretty_assertions::assert_eq; + use super::*; #[test] diff --git a/samples/fact.S b/samples/fact.S index 042ea68d..4d6004d5 100644 --- a/samples/fact.S +++ b/samples/fact.S @@ -10,15 +10,15 @@ factorielle: cmp 1,%a jge casparticulier - # cas général - sub 1,%a # a ← n-1 + // cas général + sub 1,%a // a ← n-1 push %a - call factorielle # a ← (n-1) ! - add 1,%sp # dépile l’argument n-1 - push %b # sauvegarder b - ld [%sp+2],%b # b ← n (argument original) - mul %b,%a # a ← n * (n-1)! - pop %b # restaurer b + call factorielle // a ← (n-1) ! + add 1,%sp // dépile l’argument n-1 + push %b // sauvegarder b + ld [%sp+2],%b // b ← n (argument original) + mul %b,%a // a ← n * (n-1)! + pop %b // restaurer b rtn .addr 300 diff --git a/samples/swap.S b/samples/swap.S index e9577a89..f53b2084 100644 --- a/samples/swap.S +++ b/samples/swap.S @@ -2,9 +2,9 @@ val: .word 42 main: ld 1, %a - swap [val], %a # swap between an address and a register + swap [val], %a // swap between an address and a register ld [val], %b - debugreg # %a should be 42 and %b should be 1 - swap %a, %b # swap between two registers - debugreg # %b should be 42 and %a should be 1 + debugreg // %a should be 42 and %b should be 1 + swap %a, %b // swap between two registers + debugreg // %b should be 42 and %a should be 1 reset diff --git a/web/Cargo.toml b/web/Cargo.toml index b765d800..a0163556 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -11,6 +11,6 @@ crate-type = ["cdylib", "rlib"] [dependencies] z33-emulator = { path = "../emulator" } -wasm-bindgen = "0.2.76" +wasm-bindgen = "0.2.77" serde = { version = "1.0.130", features = ["derive"] } serde-wasm-bindgen = "0.3.1"