@@ -10,6 +10,7 @@ pub mod ident;
10
10
pub mod if_else;
11
11
pub mod let_stmt;
12
12
pub mod parse_loop;
13
+ pub mod parse_type_ann;
13
14
pub mod seq;
14
15
pub mod structs;
15
16
@@ -135,7 +136,7 @@ impl<'inp> Parser<'inp> {
135
136
fn expect_token_for_type_ann ( token : Option < & Result < Token , ( ) > > ) -> Result < ( ) , ParseError > {
136
137
if let Some ( Ok ( tok) ) = token {
137
138
match tok {
138
- Token :: Ident ( _) | Token :: OpenParen => Ok ( ( ) ) ,
139
+ Token :: Ident ( _) | Token :: OpenParen | Token :: Fn => Ok ( ( ) ) ,
139
140
_ => {
140
141
let e = format ! (
141
142
"Expected identifier or '(' for type annotation, got '{}'" ,
@@ -150,39 +151,6 @@ impl<'inp> Parser<'inp> {
150
151
) )
151
152
}
152
153
}
153
-
154
- /// Parse and return type annotation. Expect lexer.peek() to be at Colon before call
155
- // Should only consume tokens belonging to the annotation, starting peek at first token and ending
156
- // peek at the last token of the annotation
157
- fn parse_type_annotation ( & mut self ) -> Result < Type , ParseError > {
158
- // self.consume_token_type(Token::Colon, "Expected a colon")?;
159
- // expect_token_body!(self.lexer.peek(), Ident, "identifier")?;
160
- Parser :: expect_token_for_type_ann ( self . lexer . peek ( ) ) ?;
161
-
162
- // if ident, get the string and try to convert type. else, handle specially
163
- let peek = self
164
- . lexer
165
- . peek ( )
166
- . unwrap ( )
167
- . to_owned ( )
168
- . expect ( "Lexer should not fail" ) ; // would have erred earlier
169
-
170
- let type_ann = match peek {
171
- Token :: Ident ( id) => Type :: from_string ( & id) ,
172
- Token :: OpenParen => {
173
- self . advance ( ) ;
174
- if let Some ( Ok ( Token :: CloseParen ) ) = self . lexer . peek ( ) {
175
- Ok ( Type :: Unit )
176
- } else {
177
- Err ( ParseError :: new ( "Expected '()' for unit type annotation" ) )
178
- }
179
- }
180
- _ => unreachable ! ( ) ,
181
- } ?;
182
-
183
- Ok ( type_ann)
184
- }
185
-
186
154
/* Precedence */
187
155
188
156
// Return (left bp, right bp)
@@ -384,39 +352,6 @@ mod tests {
384
352
) ;
385
353
}
386
354
387
- #[ test]
388
- fn test_parse_type_annotations ( ) {
389
- test_parse ( "let x : int = 2;" , "let x : int = 2;" ) ;
390
- test_parse ( "let x : bool = true;" , "let x : bool = true;" ) ;
391
- test_parse ( "let x : float = true;" , "let x : float = true;" ) ;
392
- test_parse ( "let x : () = true;" , "let x : () = true;" ) ;
393
- }
394
-
395
- #[ test]
396
- fn test_parse_type_annotations_errs ( ) {
397
- // test_parse("let x : int = 2;", "");
398
- test_parse_err (
399
- "let x : let " ,
400
- "Expected identifier or '(' for type annotation, got 'let'" ,
401
- true ,
402
- ) ;
403
- test_parse_err (
404
- "let x : 2 " ,
405
- "Expected identifier or '(' for type annotation, got '2'" ,
406
- true ,
407
- ) ;
408
- test_parse_err (
409
- "let x : " ,
410
- "Expected identifier or '(' for type annotation, got end of input" ,
411
- true ,
412
- ) ;
413
- test_parse_err (
414
- "let x : (2 " ,
415
- "Expected '()' for unit type annotation" ,
416
- true ,
417
- ) ;
418
- }
419
-
420
355
#[ test]
421
356
fn test_parse_concurrency ( ) {
422
357
let t = r"
0 commit comments