@@ -58,7 +58,7 @@ impl<'s> Parser<'s> {
5858 ) -> ResultExpr {
5959 let mut variants: Vec < Box < Expr > > = vec ! [ ] ;
6060 while let Some ( _) = self . lexer . collect_if ( Token :: Bar ) {
61- let x = self . ident ( ) . xconvert_to_sym_decl ( ) ?;
61+ let x = self . ident ( ) . xconvert_to_decl ( ) ?;
6262 variants. push ( x) ;
6363 }
6464 result_expr ! ( TagDecl , visibility, mutability, identifier, variants, sig)
@@ -71,15 +71,12 @@ impl<'s> Parser<'s> {
7171 identifier : Box < Expr > ,
7272 sig : Option < Box < Expr > > ,
7373 ) -> ResultExpr {
74- let _ = self
75- . lexer
76- . collect_if ( Token :: OBrace )
77- . xexpect_token ( & self , "expected '{'" . to_string ( ) ) ?;
78- let _ = self
79- . lexer
80- . collect_if ( Token :: CBrace )
81- . xexpect_token ( & self , "expected '}'" . to_string ( ) ) ?;
82- result_expr ! ( ErrorDecl , visibility, mutability, identifier, sig)
74+ let mut variants: Vec < Box < Expr > > = vec ! [ ] ;
75+ while let Some ( _) = self . lexer . collect_if ( Token :: Bar ) {
76+ let x = self . ident ( ) . xconvert_to_decl ( ) ?;
77+ variants. push ( x) ;
78+ }
79+ result_expr ! ( ErrorDecl , visibility, mutability, identifier, variants, sig)
8380 }
8481
8582 pub fn _enum (
@@ -99,7 +96,7 @@ impl<'s> Parser<'s> {
9996 . xexpect_token ( & self , "expected ')'" . to_string ( ) ) ?;
10097 }
10198 while let Some ( _) = self . lexer . collect_if ( Token :: Bar ) {
102- let x = self . ident ( ) . xconvert_to_sym_decl ( ) ?;
99+ let x = self . ident ( ) . xconvert_to_decl ( ) ?;
103100 variants. push ( x) ;
104101 }
105102 result_expr ! ( EnumDecl , visibility, mutability, identifier, variants, sig, enum_type)
@@ -131,9 +128,9 @@ impl<'s> Parser<'s> {
131128 . collect_of_if ( & [ Token :: Let , Token :: Const , Token :: Type , Token :: Impl ] )
132129 . xexpect_token ( & self , "expected mutability" . to_string ( ) ) ?;
133130 let identifier = self
134- . ident ( )
135- . xexpect_expr ( & self , "expected identifier" . to_string ( ) )
136- . xconvert_to_sym_decl ( ) ?;
131+ . destructure ( )
132+ . xexpect_expr ( & self , "expected identifier, or destructure " . to_string ( ) )
133+ . xconvert_to_decl ( ) ?;
137134 let sig = self . opt_signature ( ) ?;
138135 let _ = self
139136 . lexer
@@ -266,7 +263,7 @@ impl<'s> Parser<'s> {
266263 let sig = self
267264 . opt_signature ( )
268265 . xexpect_expr ( & self , "expected signature" . to_string ( ) ) ?;
269- return result_expr ! ( Declarator , id. xconvert_to_sym_decl ( ) . unwrap( ) , sig)
266+ return result_expr ! ( Declarator , id. xconvert_to_decl ( ) . unwrap( ) , sig)
270267 . xconvert_to_result_opt ( ) ;
271268 }
272269 pub fn args ( & mut self ) -> Result < Option < Vec < Box < Expr > > > > {
@@ -421,8 +418,9 @@ impl<'s> Parser<'s> {
421418 let mutability = self . lexer . collect_of_if ( & [ Token :: Let , Token :: Const ] ) ;
422419 if let Some ( muta) = mutability {
423420 let identifier = self
424- . ident ( )
425- . xexpect_expr ( & self , "expected an identifier" . to_string ( ) ) ?;
421+ . destructure ( )
422+ . xexpect_expr ( & self , "expected identifier, or destructure" . to_string ( ) )
423+ . xconvert_to_decl ( ) ?;
426424 let sig = self . opt_signature ( ) ?;
427425 let _ = self
428426 . lexer
@@ -449,8 +447,11 @@ impl<'s> Parser<'s> {
449447 . lexer
450448 . collect_if ( Token :: CParen )
451449 . xexpect_token ( & self , "expected ')'" . to_string ( ) ) ?;
450+ if let Some ( _fn) = self . anon_fn ( ) ? {
451+ return bubble_expr ! ( If , x, _fn) ;
452+ }
452453 let blk = self . block ( ) ?;
453- return bubble_expr ! ( For , x, blk) ;
454+ return bubble_expr ! ( If , x, blk) ;
454455 }
455456 pub fn _while ( & mut self ) -> ResultOptExpr {
456457 let f = self . lexer . collect_if ( Token :: While ) ;
@@ -492,6 +493,27 @@ impl<'s> Parser<'s> {
492493 let blk = self . block ( ) ?;
493494 return bubble_expr ! ( For , x, blk) ;
494495 }
496+ pub fn destructure ( & mut self ) -> ResultOptExpr {
497+ let brace = self . lexer . collect_if ( Token :: OBrace ) ;
498+ if brace. is_some ( ) {
499+ let mut idents: Vec < Box < Expr > > = vec ! [ ] ;
500+ loop {
501+ match self . ident ( ) {
502+ Some ( x) => {
503+ idents. push ( Box :: new ( Expr :: SymbolDecl ( x. into_symbol ( ) ) ) ) ;
504+ let _ = self . lexer . collect_if ( Token :: Comma ) ;
505+ }
506+ None => break ,
507+ }
508+ }
509+ let _ = self
510+ . lexer
511+ . collect_if ( Token :: CBrace )
512+ . xexpect_token ( & self , "expected '}' or more identifiers" . to_string ( ) ) ?;
513+ return bubble_expr ! ( Destructure , idents) ;
514+ }
515+ return Ok ( self . ident ( ) ) ;
516+ }
495517 pub fn block ( & mut self ) -> ResultExpr {
496518 self . lexer
497519 . collect_if ( Token :: OBrace )
@@ -996,12 +1018,12 @@ trait ConvertToResult {
9961018 fn xconvert_to_result ( self , parser : & Parser , title : String ) -> ResultExpr ;
9971019}
9981020
999- trait ConvertToSymbolDecl {
1000- fn xconvert_to_sym_decl ( self ) -> ResultExpr ;
1021+ trait ConvertToDecl {
1022+ fn xconvert_to_decl ( self ) -> ResultExpr ;
10011023}
10021024
1003- trait ConvertToSymbolDeclResult {
1004- fn xconvert_to_sym_decl ( self ) -> ResultExpr ;
1025+ trait ConvertToDeclResult {
1026+ fn xconvert_to_decl ( self ) -> ResultExpr ;
10051027}
10061028
10071029trait ConvertToResultOpt {
@@ -1077,22 +1099,25 @@ impl ExpectExpr for OptExpr {
10771099 }
10781100}
10791101
1080- impl ConvertToSymbolDeclResult for ResultExpr {
1081- fn xconvert_to_sym_decl ( self ) -> ResultExpr {
1102+ impl ConvertToDeclResult for ResultExpr {
1103+ fn xconvert_to_decl ( self ) -> ResultExpr {
10821104 match self {
10831105 Err ( x) => Err ( x) ,
10841106 Ok ( val) => match * val {
10851107 Expr :: Symbol ( x) => {
10861108 return Ok ( Box :: new ( Expr :: SymbolDecl ( x) ) ) ;
10871109 }
1110+ Expr :: Destructure ( _) => {
1111+ return Ok ( val) ;
1112+ }
10881113 _ => panic ! ( "type lang issue, expected into symbol" ) ,
10891114 } ,
10901115 }
10911116 }
10921117}
10931118
1094- impl ConvertToSymbolDecl for OptExpr {
1095- fn xconvert_to_sym_decl ( self ) -> ResultExpr {
1119+ impl ConvertToDecl for OptExpr {
1120+ fn xconvert_to_decl ( self ) -> ResultExpr {
10961121 match self {
10971122 None => panic ! ( "type lang issue, expected a symbol" ) ,
10981123 Some ( val) => match * val {
@@ -1310,14 +1335,11 @@ mod tests {
13101335 token: Token :: Let ,
13111336 span: 2 ..5 ,
13121337 } ,
1313- expr!(
1314- Symbol ,
1315- Lexeme {
1316- slice: String :: from( "x" ) ,
1317- token: Token :: Symbol ,
1318- span: 6 ..7
1319- }
1320- ) ,
1338+ Box :: new( Expr :: SymbolDecl ( Symbol :: new( Lexeme {
1339+ slice: String :: from( "x" ) ,
1340+ token: Token :: Symbol ,
1341+ span: 6 ..7
1342+ } ) ) ) ,
13211343 None ,
13221344 expr!(
13231345 Number ,
0 commit comments