From 1ec5a1d6bc490c88c5f76e50b29dd47efcec3665 Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Sat, 3 Jul 2021 16:36:48 +0200 Subject: [PATCH] some more cleanup --- src/config.rs | 2 +- src/lib.rs | 46 ++++++++++++++++ src/main.rs | 54 ------------------- .../eww_config__config__test__test-2.snap | 2 +- .../eww_config__config__test__test.snap | 8 +-- src/snapshots/eww_config__test-15.snap | 8 +-- 6 files changed, 56 insertions(+), 64 deletions(-) create mode 100644 src/lib.rs delete mode 100644 src/main.rs diff --git a/src/config.rs b/src/config.rs index 3b9b104..28c5a66 100644 --- a/src/config.rs +++ b/src/config.rs @@ -82,7 +82,7 @@ impl FromExpr for Element { let mut iter = ExprIterator::new(list.into_iter()); let (_, name) = iter.next_symbol()?; let attrs = iter.key_values()?; - let children = iter.map(|x| C::from_expr(x)).collect::>>()?; + let children = iter.map(C::from_expr).collect::>>()?; Element { span, name, attrs, children } }) } diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..d4a6f66 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,46 @@ +#![allow(unused_imports)] +#![allow(unused)] +#![feature(try_blocks)] + +mod config; +mod error; +mod expr; +use error::AstError; + +use std::{fmt::Display, ops::Deref}; + +use itertools::Itertools; + +use lalrpop_util::lalrpop_mod; + +lalrpop_mod!(pub parser); + +macro_rules! test_parser { + ($p:expr, $($text:literal),*) => {{ + $(insta::assert_debug_snapshot!($p.parse($text));)* + }} +} + +#[test] +fn test() { + let p = parser::ExprParser::new(); + test_parser!( + p, + "1", + "(12)", + "(1 2)", + "(1 :foo 1)", + "(:foo 1)", + "(:foo->: 1)", + "(foo 1)", + "(lol😄 1)", + r#"(test "hi")"#, + r#"(test "h\"i")"#, + r#"(test " hi ")"#, + "(+ (1 2 (* 2 5)))", + r#"{:key value 12 "hi" (test) (1 2 3)}"#, + r#"; test"#, + r#"(f arg ; test + arg2)"# + ); +} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 1462fac..0000000 --- a/src/main.rs +++ /dev/null @@ -1,54 +0,0 @@ -#![allow(unused_imports)] -#![allow(unused)] -#![feature(try_blocks)] - -mod config; -mod error; -mod expr; -use error::AstError; - -use std::{fmt::Display, ops::Deref}; - -use itertools::Itertools; - -use lalrpop_util::lalrpop_mod; - -// mod lexer; - -lalrpop_mod!(pub parser); - -fn main() {} - -#[allow(unused_macros)] -macro_rules! test_p { - ($e:expr) => { - let p = parser::ExprParser::new(); - insta::assert_debug_snapshot!(p.parse($e)) - }; -} - -#[test] -fn test() { - test_p!("1"); - test_p!("(12)"); - test_p!("(1 2)"); - test_p!("(1 :foo 1)"); - test_p!("(:foo 1)"); - test_p!("(:foo->: 1)"); - test_p!("(foo 1)"); - test_p!("(lol😄 1)"); - - test_p!(r#"(test "hi")"#); - test_p!(r#"(test "h\"i")"#); - test_p!(r#"(test " hi ")"#); - - test_p!("(+ (1 2 (* 2 5)))"); - - test_p!(r#"{:key value 12 "hi" (test) (1 2 3)}"#); - - test_p!(r#"; test"#); - test_p!( - r#"(f arg ; test - arg2)"# - ); -} diff --git a/src/snapshots/eww_config__config__test__test-2.snap b/src/snapshots/eww_config__config__test__test-2.snap index b983ed4..39b709c 100644 --- a/src/snapshots/eww_config__config__test__test-2.snap +++ b/src/snapshots/eww_config__config__test__test-2.snap @@ -27,5 +27,5 @@ Definitional { ], ), ], - span: 0..32, + span: 1..10, } diff --git a/src/snapshots/eww_config__config__test__test.snap b/src/snapshots/eww_config__config__test__test.snap index cb3999e..b58093b 100644 --- a/src/snapshots/eww_config__config__test__test.snap +++ b/src/snapshots/eww_config__config__test__test.snap @@ -6,14 +6,14 @@ expression: "Element::::from_expr(parser.parse(\"(box :ba Element { name: "box", attrs: { - ":baz": Str( - 18..22, - "hi", - ), ":bar": Number( 10..12, 12, ), + ":baz": Str( + 18..22, + "hi", + ), }, children: [ Symbol( diff --git a/src/snapshots/eww_config__test-15.snap b/src/snapshots/eww_config__test-15.snap index 9b4f877..b2943ca 100644 --- a/src/snapshots/eww_config__test-15.snap +++ b/src/snapshots/eww_config__test-15.snap @@ -1,11 +1,11 @@ --- -source: src/main.rs -expression: "p.parse(r#\"(f arg ; test\n arg2)\"#)" +source: src/lib.rs +expression: "p.parse(r#\"(f arg ; test\n arg2)\"#)" --- Ok( List( - 0..24, + 0..27, [ Symbol( 1..2, @@ -19,7 +19,7 @@ Ok( 7..13, ), Symbol( - 19..23, + 22..26, "arg2", ), ],