minor: tidy up Parse a little bit

- Add doc comments to some `Parse` methods
- Uses `Parse::new` more
This commit is contained in:
DropDemBits 2024-03-08 21:45:01 -05:00
parent 3b763a847a
commit c4573b26f6
No known key found for this signature in database
GPG key ID: 7FE02A6C1EDFA075

View file

@ -107,14 +107,22 @@ impl<T> Parse<T> {
} }
impl<T: AstNode> Parse<T> { impl<T: AstNode> Parse<T> {
/// Converts this parse result into a parse result for an untyped syntax tree.
pub fn to_syntax(self) -> Parse<SyntaxNode> { pub fn to_syntax(self) -> Parse<SyntaxNode> {
Parse { green: self.green, errors: self.errors, _ty: PhantomData } 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 { pub fn tree(&self) -> T {
T::cast(self.syntax_node()).unwrap() T::cast(self.syntax_node()).unwrap()
} }
/// Converts from `Parse<T>` to [`Result<T, Vec<SyntaxError>>`].
pub fn ok(self) -> Result<T, Vec<SyntaxError>> { pub fn ok(self) -> Result<T, Vec<SyntaxError>> {
match self.errors() { match self.errors() {
errors if !errors.is_empty() => Err(errors), errors if !errors.is_empty() => Err(errors),
@ -177,11 +185,7 @@ impl SourceFile {
let root = SyntaxNode::new_root(green.clone()); let root = SyntaxNode::new_root(green.clone());
assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
Parse { Parse::new(green, errors)
green,
errors: if errors.is_empty() { None } else { Some(errors.into()) },
_ty: PhantomData,
}
} }
} }
@ -290,12 +294,7 @@ impl ast::TokenTree {
} }
let (green, errors) = builder.finish_raw(); let (green, errors) = builder.finish_raw();
Parse::new(green, errors)
Parse {
green,
errors: if errors.is_empty() { None } else { Some(errors.into()) },
_ty: PhantomData,
}
} }
} }