@@ -288,6 +288,13 @@ impl Expression {
288288 span,
289289 }
290290 }
291+
292+ pub fn error ( span : Span ) -> Self {
293+ Self {
294+ inner : ExpressionInner :: Error ,
295+ span,
296+ }
297+ }
291298}
292299
293300impl_eq_hash ! ( Expression ; inner) ;
@@ -301,6 +308,10 @@ pub enum ExpressionInner {
301308 /// Then, the block returns the value of its final expression.
302309 /// The block returns nothing (unit) if there is no final expression.
303310 Block ( Arc < [ Statement ] > , Option < Arc < Expression > > ) ,
311+ /// An error expression state, which indicates that parser cannot recognize this expression.
312+ /// Also tells an analyzer that we should skip analyzing this expression to not emmit more
313+ /// errors.
314+ Error ,
304315}
305316
306317/// A single expression directly returns a value.
@@ -399,7 +410,7 @@ impl Match {
399410 }
400411 ( MatchPattern :: None , MatchPattern :: Some ( _, ty_r) ) => AliasedType :: option ( ty_r. clone ( ) ) ,
401412 ( MatchPattern :: False , MatchPattern :: True ) => AliasedType :: boolean ( ) ,
402- _ => unreachable ! ( "Match expressions have valid left and right arms" ) ,
413+ _ => AliasedType :: error ( ) . with_span ( * self . as_ref ( ) ) ,
403414 }
404415 }
405416}
@@ -614,6 +625,7 @@ impl TreeLike for ExprTree<'_> {
614625 Tree :: Unary ( Self :: Block ( statements, maybe_expr) )
615626 }
616627 ExpressionInner :: Single ( single) => Tree :: Unary ( Self :: Single ( single) ) ,
628+ ExpressionInner :: Error => Tree :: Nullary ,
617629 } ,
618630 Self :: Block ( statements, maybe_expr) => Tree :: Nary (
619631 statements
@@ -936,13 +948,7 @@ impl<A: ChumskyParse + std::fmt::Debug> ParseFromStrWithErrors for A {
936948
937949 handler. update ( parse_errs) ;
938950
939- // TODO: We should return parsed result if we found errors, but because analyzing in `ast` module
940- // is not handling poisoned tree right now, we don't return parsed result
941- if handler. get ( ) . is_empty ( ) {
942- ast
943- } else {
944- None
945- }
951+ ast
946952 }
947953}
948954
@@ -1043,12 +1049,7 @@ impl ChumskyParse for AliasedType {
10431049 . then ( ty. clone ( ) ) ,
10441050 Token :: LAngle ,
10451051 Token :: RAngle ,
1046- |_| {
1047- (
1048- AliasedType :: alias ( AliasName :: from_str_unchecked ( "error" ) ) ,
1049- AliasedType :: alias ( AliasName :: from_str_unchecked ( "error" ) ) ,
1050- )
1051- } ,
1052+ |_| ( AliasedType :: error ( ) , AliasedType :: error ( ) ) ,
10521053 ) ;
10531054
10541055 let sum_type = just ( Token :: Ident ( "Either" ) )
@@ -1074,7 +1075,7 @@ impl ChumskyParse for AliasedType {
10741075 . map ( |s : Vec < AliasedType > | AliasedType :: tuple ( s) ) ,
10751076 Token :: LParen ,
10761077 Token :: RParen ,
1077- |_| AliasedType :: tuple ( Vec :: new ( ) ) ,
1078+ |_| AliasedType :: error ( ) ,
10781079 )
10791080 . labelled ( "tuple" ) ;
10801081
@@ -1087,12 +1088,7 @@ impl ChumskyParse for AliasedType {
10871088 } ) ,
10881089 Token :: LBracket ,
10891090 Token :: RBracket ,
1090- |_| {
1091- AliasedType :: array (
1092- AliasedType :: alias ( AliasName :: from_str_unchecked ( "error" ) ) ,
1093- 0 ,
1094- )
1095- } ,
1091+ |_| AliasedType :: error ( ) ,
10961092 )
10971093 . labelled ( "array" ) ;
10981094
@@ -1114,18 +1110,13 @@ impl ChumskyParse for AliasedType {
11141110 } ) ) ,
11151111 Token :: LAngle ,
11161112 Token :: RAngle ,
1117- |_| {
1118- (
1119- AliasedType :: alias ( AliasName :: from_str_unchecked ( "error" ) ) ,
1120- NonZeroPow2Usize :: TWO ,
1121- )
1122- } ,
1113+ |_| ( AliasedType :: error ( ) , NonZeroPow2Usize :: TWO ) ,
11231114 ) )
11241115 . map ( |( ty, size) | AliasedType :: list ( ty, size) )
11251116 . labelled ( "List" ) ;
11261117
11271118 choice ( ( sum_type, option_type, tuple, array, list, atom) )
1128- . map_with ( |inner, _ | inner)
1119+ . map_with ( |inner, e | inner. with_span ( e . span ( ) ) )
11291120 . labelled ( "type" )
11301121 } )
11311122 }
@@ -1165,7 +1156,7 @@ impl ChumskyParse for Item {
11651156 let type_parser = TypeAlias :: parser ( ) . map ( Item :: TypeAlias ) ;
11661157 let mod_parser = Module :: parser ( ) . map ( |_| Item :: Module ) ;
11671158
1168- choice ( ( func_parser, type_parser, mod_parser) )
1159+ choice ( ( func_parser, type_parser, mod_parser) ) . labelled ( "item" )
11691160 }
11701161}
11711162
@@ -1201,7 +1192,7 @@ impl ChumskyParse for Function {
12011192 ( Token :: LParen , Token :: RParen ) ,
12021193 ( Token :: LBracket , Token :: RBracket ) ,
12031194 ] ,
1204- Expression :: empty ,
1195+ Expression :: error ,
12051196 ) ) )
12061197 . labelled ( "function body" ) ;
12071198
@@ -1495,7 +1486,7 @@ impl ChumskyParse for Expression {
14951486 ( Token :: RAngle , Token :: RAngle ) ,
14961487 ( Token :: LBracket , Token :: RBracket ) ,
14971488 ] ,
1498- |span| Expression :: empty ( span) . inner ( ) . clone ( ) ,
1489+ |span| Expression :: error ( span) . inner ( ) . clone ( ) ,
14991490 ) ;
15001491
15011492 let statements = statement
@@ -1762,7 +1753,7 @@ impl Match {
17621753 }
17631754 _ => {
17641755 let match_arm_fallback = MatchArm {
1765- expression : Arc :: new ( Expression :: empty ( Span :: new ( 0 , 0 ) ) ) ,
1756+ expression : Arc :: new ( Expression :: error ( Span :: new ( 0 , 0 ) ) ) ,
17661757 pattern : MatchPattern :: False ,
17671758 } ;
17681759
0 commit comments