From 95e6b078fecddeaa3d6f2c920b617201b74bf01e Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 19 Jan 2020 21:50:20 +0100 Subject: =?UTF-8?q?Move=20to=20non-fatal=20errors=20=F0=9F=AA=82=20[WIP]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Dynamic models instead of SyntaxTrees - No more ParseResult/LayoutResult - Errors and Decorations which are propagated to parent contexts - Models are finally clonable --- tests/src/layouter.rs | 49 +++++++++++++++++++------------------------------ tests/src/parser.rs | 38 +++++++++----------------------------- tests/src/spanless.rs | 12 +++++++----- 3 files changed, 35 insertions(+), 64 deletions(-) (limited to 'tests/src') diff --git a/tests/src/layouter.rs b/tests/src/layouter.rs index fa0c631a..eeca1a1b 100644 --- a/tests/src/layouter.rs +++ b/tests/src/layouter.rs @@ -76,10 +76,7 @@ fn test(name: &str, src: &str) -> DynResult<()> { let font_paths = provider.paths(); typesetter.add_font_provider(provider); - let layouts = match compile(&typesetter, src) { - Some(layouts) => layouts, - None => return Ok(()), - }; + let layouts = compile(&typesetter, src); // Compute the font's paths. let mut fonts = HashMap::new(); @@ -122,40 +119,32 @@ fn test(name: &str, src: &str) -> DynResult<()> { } /// Compile the source code with the typesetter. -fn compile(typesetter: &Typesetter, src: &str) -> Option { +fn compile(typesetter: &Typesetter, src: &str) -> MultiLayout { #[cfg(not(debug_assertions))] { use std::time::Instant; // Warmup. let warmup_start = Instant::now(); - let is_ok = block_on(typesetter.typeset(&src)).is_ok(); + block_on(typesetter.typeset(&src)); let warmup_end = Instant::now(); - // Only continue if the typesetting was successful. - if is_ok { - let start = Instant::now(); - let tree = typesetter.parse(&src).unwrap(); - let mid = Instant::now(); - block_on(typesetter.layout(&tree)).unwrap(); - let end = Instant::now(); - - println!(" - cold start: {:?}", warmup_end - warmup_start); - println!(" - warmed up: {:?}", end - start); - println!(" - parsing: {:?}", mid - start); - println!(" - layouting: {:?}", end - mid); - println!(); - } - }; - - match block_on(typesetter.typeset(&src)) { - Ok(layouts) => Some(layouts), - Err(err) => { - println!(" - compilation failed: {}", err); - #[cfg(not(debug_assertions))] - println!(); - None - } + let start = Instant::now(); + let tree = typesetter.parse(&src).output; + let mid = Instant::now(); + let layouts = block_on(typesetter.layout(&tree)).output; + let end = Instant::now(); + + println!(" - cold start: {:?}", warmup_end - warmup_start); + println!(" - warmed up: {:?}", end - start); + println!(" - parsing: {:?}", mid - start); + println!(" - layouting: {:?}", end - mid); + println!(); + + layouts } + + #[cfg(debug_assertions)] + block_on(typesetter.typeset(&src)) } /// Command line options. diff --git a/tests/src/parser.rs b/tests/src/parser.rs index b2aa01da..550090a8 100644 --- a/tests/src/parser.rs +++ b/tests/src/parser.rs @@ -107,23 +107,22 @@ macro_rules! case { (ps $($rest:tt)*) => (case!(@parse PartialEq::eq, $($rest)*)); (@parse $cmp:expr, $src:expr, [$($e:tt)*]) => ({ - let expected = SyntaxTree { nodes: list!(nodes [$($e)*]) }; + let expected = SyntaxModel { nodes: list!(nodes [$($e)*]) }; let found = parse($src, ParseContext { scope: &scope() }).0; ($cmp(&found, &expected), expected, found) }); (c $src:expr, [$($e:tt)*]) => ({ - let expected = Colorization { tokens: list!(colors [$($e)*]) }; + let expected = Colorization { tokens: list!(decorations [$($e)*]) }; let found = parse($src, ParseContext { scope: &scope() }).1; (expected == found, expected, found) }); (e $src:expr, [$($e:tt)*]) => ({ - let errors = list!([$($e)*]).into_iter() + let expected = list!([$($e)*]).into_iter() .map(|s| s.map(|m| m.to_string())) .collect(); - let expected = ErrorMap { errors }; let found = parse($src, ParseContext { scope: &scope() }).2; (expected == found, expected, found) }); @@ -131,7 +130,7 @@ macro_rules! case { /// A scope containing the `DebugFn` as a fallback. fn scope() -> Scope { - Scope::with_debug::() + Scope::with_fallback::() } /// Parses possibly-spanned lists of token or node expressions. @@ -182,7 +181,7 @@ macro_rules! func { $(positional = list!(expr [$($p)*]);)? $(keyword = list!(expr [$($k)*]);)? - Node::Func(FuncCall(Box::new(DebugFn { + Node::Model(Box::new(DebugFn { header: FuncHeader { name: zspan(Ident($name.to_string())), args: FuncArgs { @@ -191,10 +190,10 @@ macro_rules! func { }, }, body: func!(@body $($b)*), - }))) + })) }); - (@body Some($($b:tt)*)) => (Some(SyntaxTree { nodes: list!(nodes $($b)*) })); + (@body Some($($b:tt)*)) => (Some(SyntaxModel{ nodes: list!(nodes $($b)*) })); (@body None) => (None); } @@ -270,27 +269,8 @@ mod cuts { } } - pub mod colors { - pub use typstc::syntax::ColorToken::{ - Comment as C, - Bracket as B, - FuncName as FN, - Colon as CL, - Key as K, - Equals as EQ, - Comma as CM, - Paren as P, - Brace as BR, - ExprIdent as ID, - ExprStr as STR, - ExprNumber as NUM, - ExprSize as SIZE, - ExprBool as BOOL, - Bold as BD, - Italic as IT, - Monospace as MS, - Invalid as INV, - }; + pub mod decorations { + pub use typstc::syntax::Decoration::*; } pub mod expr { diff --git a/tests/src/spanless.rs b/tests/src/spanless.rs index fde5a2ed..87d3f39d 100644 --- a/tests/src/spanless.rs +++ b/tests/src/spanless.rs @@ -13,15 +13,17 @@ impl SpanlessEq>>> for Vec>> { } } -impl SpanlessEq for SyntaxTree { - fn spanless_eq(&self, other: &SyntaxTree) -> bool { - fn downcast(func: &FuncCall) -> &DebugFn { - func.0.downcast::().expect("not a debug fn") +impl SpanlessEq for SyntaxModel { + fn spanless_eq(&self, other: &SyntaxModel) -> bool { + fn downcast(func: &dyn Model) -> &DebugFn { + func.downcast::().expect("not a debug fn") } self.nodes.len() == other.nodes.len() && self.nodes.iter().zip(&other.nodes).all(|(x, y)| match (&x.v, &y.v) { - (Node::Func(a), Node::Func(b)) => downcast(a).spanless_eq(downcast(b)), + (Node::Model(a), Node::Model(b)) => { + downcast(a.as_ref()).spanless_eq(downcast(b.as_ref())) + } (a, b) => a == b, }) } -- cgit v1.2.3