From 54cc0b8a8d7b05fb10999a51d954321bbce5fc53 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 11 Jul 2024 15:17:58 -0300 Subject: [PATCH] Playing with Logos --- logostest/Cargo.lock | 102 +++++++++++++++++++++++++++++++++++++ logostest/Cargo.toml | 7 +++ logostest/src/example.foam | 88 ++++++++++++++++++++++++++++++++ logostest/src/main.rs | 51 +++++++++++++++++++ 4 files changed, 248 insertions(+) create mode 100644 logostest/Cargo.lock create mode 100644 logostest/Cargo.toml create mode 100644 logostest/src/example.foam create mode 100644 logostest/src/main.rs diff --git a/logostest/Cargo.lock b/logostest/Cargo.lock new file mode 100644 index 0000000..bf9d56b --- /dev/null +++ b/logostest/Cargo.lock @@ -0,0 +1,102 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "beef" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "logos" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "161971eb88a0da7ae0c333e1063467c5b5727e7fb6b710b8db4814eade3a42e8" +dependencies = [ + "logos-derive", +] + +[[package]] +name = "logos-codegen" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e31badd9de5131fdf4921f6473d457e3dd85b11b7f091ceb50e4df7c3eeb12a" +dependencies = [ + "beef", + "fnv", + "lazy_static", + "proc-macro2", + "quote", + "regex-syntax", + "syn", +] + +[[package]] +name = "logos-derive" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c2a69b3eb68d5bd595107c9ee58d7e07fe2bb5e360cc85b0f084dedac80de0a" +dependencies = [ + "logos-codegen", +] + +[[package]] +name = "logostest" +version = "0.1.0" +dependencies = [ + "logos", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "syn" +version = "2.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0209b68b3613b093e0ec905354eccaedcfe83b8cb37cbdeae64026c3064c16" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/logostest/Cargo.toml b/logostest/Cargo.toml new file mode 100644 index 0000000..ca4c509 --- /dev/null +++ b/logostest/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "logostest" +version = "0.1.0" +edition = "2021" + +[dependencies] +logos = "0.14.0" diff --git a/logostest/src/example.foam b/logostest/src/example.foam new file mode 100644 index 0000000..a51a5db --- /dev/null +++ b/logostest/src/example.foam @@ -0,0 +1,88 @@ +// List of variables to be checked; optional, since MD5 validation does not use any variables. +variables ("phi" "meanT"); + +// Execution is run in sequence: If the first run ("quick") fails, then the second run ("short") +// is run, and if that fails, the third is run ("long"), and so on. On the other hand, the +// execution of the runs stops on the first full success. +// This example is a bit of a stretch -- why would be want to run the same case 4 different +// times? -- but it shows that, if that necessity appears in the future, we can support it. +// It also shows that one can have mixed reduced and non-reduce runs (e.g., there could be +// just one run without reduction, or just one reduced run, and so on.) +// We could also add support in Verify to only execute runs with a specific name, allowing +// to run all examples in their non-reduced mode (as long as they all name their non-reduced +// mode with the same name, that is). +// Names are free form, and we could use them to defined the run-dir, e.g., +// "short" validation will be run in ".run-short", "long" in ".run-long" and so on. + +runs +( + // This is a quick test: The example is reduced to just 2 timesteps, and we check the + // resulting file against its MD5 hash. + quick { + steps 2; + generatedFile "postProcessing/2/blah"; + // because the validation is in MD5 format, there is no need to have a referenceFile + failIf { + md5Differs "123123"; + } + } + + // In case the MD5 fails, the case is run again, but this time reduced to 10 timesteps. + // This run also includes continuations, by changing the listed files to make one follow + // the timesteps of the previous. The run will fail if relative or absolute differences are + // above 0. + short { + steps 10; + generatedFile "postProcessing/10/blah"; + referenceFile "verification/10/blah"; + // No tolerances mean "it will warn if the values are not the same" + + // This makes the continuations explicit, by specificing files that form a single run. + continuations ( + "system/caseSetupDict.initial" + "system/caseSetupDict.continuation1" + "system/caseSetupDict.continuation2" + ); + } + + // If in 10 timestemps the values do not match the reference file, a 100 timesteps run is + // done. There is no continuation, and for this run to fail, both absolute and relative + // tolerances must be above the designed threshold. + long { + steps 100; + generatedFile "postProcessing/100/blah"; + referenceFile "verification/100/blah"; + failIf { + absolute 10; + relative 10; + // will fail is the absolute difference is above 10 AND the relative difference + // is above 10; + operator and; + } + } + + // If the 100 timesteps fails, then we run the example a 4th time, this time without any + // reductions (the "steps" property is not set in this run). There are no continations + // either. + "run till completion" { + // With no steps, there is no reduction + // (maybe we need something to tell that there is no reduction explicitly?) + generatedFile "postProcessing/20000/blah;" + referenceFile "verification/20000/blah"; + failIf { + absolute 20; + relative 20; + // no operator means OR, so example will fail if the absolute different is + // above 20 OR the relative difference is above 20 + } + } +); + +// This is used for filtering. +tags ("GIB" "AES" "compressible"); + +// Only present if the example can't be run in some platform. +unstable { + operatingSystem windows; // valid values: "windows", "linux", "all" + reason "Allrun uses Python, and Python isn't usually available on Windows"; +} diff --git a/logostest/src/main.rs b/logostest/src/main.rs new file mode 100644 index 0000000..10af640 --- /dev/null +++ b/logostest/src/main.rs @@ -0,0 +1,51 @@ +use logos::Logos; + +#[derive(Logos, Debug)] +#[logos(skip r"[ \t\n\r]")] +enum Token<'a> { + #[regex(r#""[^"]+""#, |lex| lex.slice().trim_start_matches('"').trim_end_matches('"'))] + #[regex("[a-zA-Z0-9]+", |lex| lex.slice())] + Keyword(&'a str), + + #[regex(r#"//[^\n]*"#, |lex| lex.slice())] + Comment(&'a str), + + #[token(";")] + End, + + #[token("{")] + DictStart, + + #[token("}")] + DictEnd, + + #[token("(")] + ListStart, + + #[token(")")] + ListEnd, + +} + +fn main() { + let lex = Token::lexer("variable \"value is weird(but cool)[not so much]\" ;"); + let content = lex.collect::>(); + println!("{content:#?}"); + + let lex = Token::lexer("var1 2;\nvar2 2;"); + let content = lex.collect::>(); + println!("{content:#?}"); + + let lex = Token::lexer(r#"var 1 2 3;\nvar2 (1 2 3);"#); + let content = lex.collect::>(); + println!("{content:#?}"); + + let lex = Token::lexer("variables (\"phi\" \"meanT\");\nruns\n( 1 2 3 );"); + let content = lex.collect::>(); + println!("{content:#?}"); + + let source = std::fs::read("src/example.foam").unwrap(); + let lex = Token::lexer(&std::str::from_utf8(&source).unwrap()); + let content = lex.collect::>(); + println!("{content:#?}"); +}