@@ -107,14 +107,22 @@ impl<T> Parse<T> {
107107}
108108
109109impl < T : AstNode > Parse < T > {
110+ /// Converts this parse result into a parse result for an untyped syntax tree.
110111 pub fn to_syntax ( self ) -> Parse < SyntaxNode > {
111112 Parse { green : self . green , errors : self . errors , _ty : PhantomData }
112113 }
113114
115+ /// Gets the parsed syntax tree as a typed ast node.
116+ ///
117+ /// # Panics
118+ ///
119+ /// Panics if the root node cannot be casted into the typed ast node
120+ /// (e.g. if it's an `ERROR` node).
114121 pub fn tree ( & self ) -> T {
115122 T :: cast ( self . syntax_node ( ) ) . unwrap ( )
116123 }
117124
125+ /// Converts from `Parse<T>` to [`Result<T, Vec<SyntaxError>>`].
118126 pub fn ok ( self ) -> Result < T , Vec < SyntaxError > > {
119127 match self . errors ( ) {
120128 errors if !errors. is_empty ( ) => Err ( errors) ,
@@ -177,11 +185,7 @@ impl SourceFile {
177185 let root = SyntaxNode :: new_root ( green. clone ( ) ) ;
178186
179187 assert_eq ! ( root. kind( ) , SyntaxKind :: SOURCE_FILE ) ;
180- Parse {
181- green,
182- errors : if errors. is_empty ( ) { None } else { Some ( errors. into ( ) ) } ,
183- _ty : PhantomData ,
184- }
188+ Parse :: new ( green, errors)
185189 }
186190}
187191
@@ -290,12 +294,7 @@ impl ast::TokenTree {
290294 }
291295
292296 let ( green, errors) = builder. finish_raw ( ) ;
293-
294- Parse {
295- green,
296- errors : if errors. is_empty ( ) { None } else { Some ( errors. into ( ) ) } ,
297- _ty : PhantomData ,
298- }
297+ Parse :: new ( green, errors)
299298 }
300299}
301300
0 commit comments