some more cleanup
This commit is contained in:
parent
86c02e86bb
commit
1ec5a1d6bc
6 changed files with 56 additions and 64 deletions
|
@ -82,7 +82,7 @@ impl<C: FromExpr, A: FromExpr> FromExpr for Element<C, A> {
|
||||||
let mut iter = ExprIterator::new(list.into_iter());
|
let mut iter = ExprIterator::new(list.into_iter());
|
||||||
let (_, name) = iter.next_symbol()?;
|
let (_, name) = iter.next_symbol()?;
|
||||||
let attrs = iter.key_values()?;
|
let attrs = iter.key_values()?;
|
||||||
let children = iter.map(|x| C::from_expr(x)).collect::<AstResult<Vec<_>>>()?;
|
let children = iter.map(C::from_expr).collect::<AstResult<Vec<_>>>()?;
|
||||||
Element { span, name, attrs, children }
|
Element { span, name, attrs, children }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
46
src/lib.rs
Normal file
46
src/lib.rs
Normal file
|
@ -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)"#
|
||||||
|
);
|
||||||
|
}
|
54
src/main.rs
54
src/main.rs
|
@ -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)"#
|
|
||||||
);
|
|
||||||
}
|
|
|
@ -27,5 +27,5 @@ Definitional {
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
span: 0..32,
|
span: 1..10,
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,14 @@ expression: "Element::<Expr,\n Expr>::from_expr(parser.parse(\"(box :ba
|
||||||
Element {
|
Element {
|
||||||
name: "box",
|
name: "box",
|
||||||
attrs: {
|
attrs: {
|
||||||
":baz": Str(
|
|
||||||
18..22,
|
|
||||||
"hi",
|
|
||||||
),
|
|
||||||
":bar": Number(
|
":bar": Number(
|
||||||
10..12,
|
10..12,
|
||||||
12,
|
12,
|
||||||
),
|
),
|
||||||
|
":baz": Str(
|
||||||
|
18..22,
|
||||||
|
"hi",
|
||||||
|
),
|
||||||
},
|
},
|
||||||
children: [
|
children: [
|
||||||
Symbol(
|
Symbol(
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
---
|
---
|
||||||
source: src/main.rs
|
source: src/lib.rs
|
||||||
expression: "p.parse(r#\"(f arg ; test\n arg2)\"#)"
|
expression: "p.parse(r#\"(f arg ; test\n arg2)\"#)"
|
||||||
|
|
||||||
---
|
---
|
||||||
Ok(
|
Ok(
|
||||||
List(
|
List(
|
||||||
0..24,
|
0..27,
|
||||||
[
|
[
|
||||||
Symbol(
|
Symbol(
|
||||||
1..2,
|
1..2,
|
||||||
|
@ -19,7 +19,7 @@ Ok(
|
||||||
7..13,
|
7..13,
|
||||||
),
|
),
|
||||||
Symbol(
|
Symbol(
|
||||||
19..23,
|
22..26,
|
||||||
"arg2",
|
"arg2",
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
Loading…
Add table
Reference in a new issue