From 569136d8843d23a9f7077e84398e1ad925828570 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 13 Feb 2023 15:56:42 -0300 Subject: [PATCH] Starting to learn Nom using Foamfiles --- nomfoamtest/Cargo.lock | 32 ++++++++++++++++++ nomfoamtest/Cargo.toml | 9 +++++ nomfoamtest/src/foam.rs | 73 +++++++++++++++++++++++++++++++++++++++++ nomfoamtest/src/main.rs | 5 +++ 4 files changed, 119 insertions(+) create mode 100644 nomfoamtest/Cargo.lock create mode 100644 nomfoamtest/Cargo.toml create mode 100644 nomfoamtest/src/foam.rs create mode 100644 nomfoamtest/src/main.rs diff --git a/nomfoamtest/Cargo.lock b/nomfoamtest/Cargo.lock new file mode 100644 index 0000000..b96200f --- /dev/null +++ b/nomfoamtest/Cargo.lock @@ -0,0 +1,32 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "nomfoamtest" +version = "0.1.0" +dependencies = [ + "nom", +] diff --git a/nomfoamtest/Cargo.toml b/nomfoamtest/Cargo.toml new file mode 100644 index 0000000..349ab67 --- /dev/null +++ b/nomfoamtest/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "nomfoamtest" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom = "7.1.3" diff --git a/nomfoamtest/src/foam.rs b/nomfoamtest/src/foam.rs new file mode 100644 index 0000000..ea93f61 --- /dev/null +++ b/nomfoamtest/src/foam.rs @@ -0,0 +1,73 @@ +//! Foam format parser. + +use std::str::FromStr; + +use nom::bytes::complete::is_not; +use nom::bytes::complete::tag; +use nom::bytes::complete::take_until; +use nom::IResult; + +#[derive(Debug, Clone, PartialEq)] +struct Comment { + comment: String, +} + +impl FromStr for Comment { + type Err = (); + + fn from_str(s: &str) -> Result { + Ok(Self { + comment: s.trim().to_string(), + }) + } +} + +fn multiline_comment(input: &str) -> IResult<&str, Comment> { + let (input, _) = tag("/*")(input)?; + let (input, content) = take_until("*/")(input)?; + let (input, _) = tag("*/")(input)?; + + Ok((input, Comment::from_str(content).unwrap())) +} + +fn singleline_comment(input: &str) -> IResult<&str, Comment> { + let (input, _) = tag("//")(input)?; + let (input, content) = is_not("\n\r")(input)?; + + Ok((input, Comment::from_str(content).unwrap())) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_multiline_comment() { + let text = "/* this is comment */"; + let result = multiline_comment(text); + assert_eq!( + result, + Ok(( + "", + Comment { + comment: "this is comment".to_string() + } + )) + ) + } + + #[test] + fn test_singleline_comment() { + let text = "// this is comment"; + let result = singleline_comment(text); + assert_eq!( + result, + Ok(( + "", + Comment { + comment: "this is comment".to_string() + } + )) + ) + } +} diff --git a/nomfoamtest/src/main.rs b/nomfoamtest/src/main.rs new file mode 100644 index 0000000..d13e062 --- /dev/null +++ b/nomfoamtest/src/main.rs @@ -0,0 +1,5 @@ +mod foam; + +fn main() { + println!("Hello, world!"); +}