From c4573b26f68c093ceb05ee521d4803623e15bd29 Mon Sep 17 00:00:00 2001 From: DropDemBits Date: Fri, 8 Mar 2024 21:45:01 -0500 Subject: [PATCH] minor: tidy up `Parse` a little bit - Add doc comments to some `Parse` methods - Uses `Parse::new` more --- crates/syntax/src/lib.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 3a9ebafe87..b8a8103cc6 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -107,14 +107,22 @@ impl Parse { } impl Parse { + /// Converts this parse result into a parse result for an untyped syntax tree. pub fn to_syntax(self) -> Parse { Parse { green: self.green, errors: self.errors, _ty: PhantomData } } + /// Gets the parsed syntax tree as a typed ast node. + /// + /// # Panics + /// + /// Panics if the root node cannot be casted into the typed ast node + /// (e.g. if it's an `ERROR` node). pub fn tree(&self) -> T { T::cast(self.syntax_node()).unwrap() } + /// Converts from `Parse` to [`Result>`]. pub fn ok(self) -> Result> { match self.errors() { errors if !errors.is_empty() => Err(errors), @@ -177,11 +185,7 @@ impl SourceFile { let root = SyntaxNode::new_root(green.clone()); assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); - Parse { - green, - errors: if errors.is_empty() { None } else { Some(errors.into()) }, - _ty: PhantomData, - } + Parse::new(green, errors) } } @@ -290,12 +294,7 @@ impl ast::TokenTree { } let (green, errors) = builder.finish_raw(); - - Parse { - green, - errors: if errors.is_empty() { None } else { Some(errors.into()) }, - _ty: PhantomData, - } + Parse::new(green, errors) } }