@@ -158,13 +158,23 @@ impl Parser {
158158
159159impl Parser {
160160 fn parse_item ( & mut self ) -> PResult < NodeId > {
161+ let public = match self . peek ( ) {
162+ TokenKind :: Pub => {
163+ self . next ( ) ;
164+ true
165+ } ,
166+ _ => false
167+ } ;
168+
161169 match self . peek_with_span ( ) {
162- ( TokenKind :: Fn , _) => self . parse_function ( ) ,
170+ ( TokenKind :: Mod , _) => self . parse_module ( public) ,
171+ ( TokenKind :: Use , _) => self . parse_use ( public) ,
172+ ( TokenKind :: Fn , _) => self . parse_function ( public) ,
163173 ( other, span) => Err ( ParseError :: expected ( span, vec ! [ TokenKind :: Fn ] , other. clone ( ) ) ) ,
164174 }
165175 }
166176
167- fn parse_function ( & mut self ) -> PResult < NodeId > {
177+ fn parse_function ( & mut self , public : bool ) -> PResult < NodeId > {
168178 self . start_span ( ) ;
169179 self . expect ( TokenKind :: Fn ) ?;
170180
@@ -182,7 +192,7 @@ impl Parser {
182192
183193 let body = self . parse_block_expression ( ) ?;
184194
185- self . push_node ( NodeKind :: Function { name, params, return_type, body } )
195+ self . push_node ( NodeKind :: Function { public , name, params, return_type, body } )
186196 }
187197
188198 fn parse_parameters ( & mut self ) -> PResult < Vec < ( String , TypeId ) > > {
@@ -213,6 +223,44 @@ impl Parser {
213223 self . expect ( TokenKind :: RParen ) ?;
214224 Ok ( params)
215225 }
226+
227+ fn parse_module ( & mut self , public : bool ) -> PResult < NodeId > {
228+ self . start_span ( ) ;
229+ self . expect ( TokenKind :: Mod ) ?;
230+
231+ let name = match self . next_with_span ( ) {
232+ ( TokenKind :: Identifier ( name) , _) => name. clone ( ) ,
233+ ( other, span) => return Err ( ParseError :: new ( span, format ! ( "expected module name, found {:?}" , other) ) ) ,
234+ } ;
235+
236+ let mut items = Vec :: new ( ) ;
237+
238+ match self . next_with_span ( ) {
239+ ( TokenKind :: Semi , _) => { } ,
240+ ( TokenKind :: LBrace , _) => {
241+ while !matches ! ( self . peek( ) , TokenKind :: EOF ) {
242+ items. push ( self . parse_item ( ) ?)
243+ }
244+ self . expect ( TokenKind :: RBrace ) ?;
245+ }
246+
247+ ( other, span) => return Err ( ParseError :: expected ( span, vec ! [ TokenKind :: Semi , TokenKind :: LBrace ] , other. clone ( ) ) ) ,
248+ }
249+
250+ self . push_node ( NodeKind :: Module { public, name, items } )
251+ }
252+
253+ fn parse_use ( & mut self , public : bool ) -> PResult < NodeId > {
254+ self . start_span ( ) ;
255+ self . expect ( TokenKind :: Use ) ?;
256+
257+ // Use tree
258+ todo ! ( ) ;
259+
260+ self . expect ( TokenKind :: Semi ) ?;
261+
262+ self . push_node ( NodeKind :: UseDecl { public } )
263+ }
216264}
217265
218266// --- Statements ---
@@ -221,11 +269,7 @@ impl Parser {
221269 fn parse_statement ( & mut self ) -> PResult < NodeId > {
222270 match self . peek ( ) {
223271 TokenKind :: Let => self . parse_let_statement ( ) ,
224- TokenKind :: Fn => {
225- self . start_span ( ) ;
226- let item = self . parse_function ( ) ?;
227- self . push_node ( NodeKind :: ItemStmt { item } )
228- }
272+ TokenKind :: Fn | TokenKind :: Use | TokenKind :: Mod | TokenKind :: Pub => self . parse_item ( ) ,
229273 _ => {
230274 self . start_span ( ) ;
231275 let expr = self . parse_expression ( 0 ) ?;
@@ -284,7 +328,7 @@ impl Parser {
284328 loop {
285329 let op = self . peek ( ) . clone ( ) ;
286330
287- if let Some ( expr) = self . try_parse_postfix ( & op, lhs) ? {
331+ if let Some ( expr) = self . try_parse_postfix ( & op, lhs, start ) ? {
288332 lhs = expr;
289333 continue ;
290334 }
@@ -327,8 +371,14 @@ impl Parser {
327371 self . next ( ) ;
328372 self . push_node ( kind)
329373 }
330- ( TokenKind :: Bool ( b) , _) => {
331- let kind = NodeKind :: Literal ( Literal :: Bool ( * b) ) ;
374+ ( TokenKind :: True , _) => {
375+ let kind = NodeKind :: Literal ( Literal :: Bool ( true ) ) ;
376+ self . start_span ( ) ;
377+ self . next ( ) ;
378+ self . push_node ( kind)
379+ }
380+ ( TokenKind :: False , _) => {
381+ let kind = NodeKind :: Literal ( Literal :: Bool ( false ) ) ;
332382 self . start_span ( ) ;
333383 self . next ( ) ;
334384 self . push_node ( kind)
@@ -339,6 +389,11 @@ impl Parser {
339389 self . next ( ) ;
340390 self . push_node ( kind)
341391 }
392+ ( TokenKind :: Underscore , _) => {
393+ self . start_span ( ) ;
394+ self . next ( ) ;
395+ self . push_node ( NodeKind :: UnderscoreExpr )
396+ }
342397
343398 // Unary ops
344399 ( TokenKind :: Minus | TokenKind :: Not , _) => {
@@ -390,9 +445,24 @@ impl Parser {
390445 // if expression
391446 ( TokenKind :: If , _) => self . parse_if_expression ( ) ,
392447
448+ // Loop expression
449+ ( TokenKind :: Loop , _) => self . parse_loop_expression ( ) ,
450+
393451 // Block expression
394452 ( TokenKind :: LBrace , _) => self . parse_block_expression ( ) ,
395453
454+ // break expression
455+ ( TokenKind :: Break , _) => {
456+ self . start_span ( ) ;
457+ self . next ( ) ;
458+ if matches ! ( self . peek( ) , TokenKind :: Semi | TokenKind :: RBrace | TokenKind :: EOF ) {
459+ self . push_node ( NodeKind :: Break { expr : None } )
460+ } else {
461+ let expr = self . parse_expression ( 0 ) ?;
462+ self . push_node ( NodeKind :: Break { expr : Some ( expr) } )
463+ }
464+ }
465+
396466 // return expression
397467 ( TokenKind :: Return , _) => {
398468 self . start_span ( ) ;
@@ -409,11 +479,11 @@ impl Parser {
409479 }
410480 }
411481
412- fn try_parse_postfix ( & mut self , op : & TokenKind , lhs : NodeId ) -> PResult < Option < NodeId > > {
482+ fn try_parse_postfix ( & mut self , op : & TokenKind , lhs : NodeId , start : Pos ) -> PResult < Option < NodeId > > {
413483 // Function call
414484 if matches ! ( op, TokenKind :: LParen ) {
415485 self . next ( ) ;
416-
486+
417487 let mut args = Vec :: new ( ) ;
418488 if !matches ! ( self . peek( ) , TokenKind :: RParen ) {
419489 loop {
@@ -430,9 +500,35 @@ impl Parser {
430500 }
431501 }
432502 self . expect ( TokenKind :: RParen ) ?;
433-
503+
504+ self . pos_stack . push ( start) ;
434505 return Ok ( Some ( self . push_node ( NodeKind :: Call { callee : lhs, args } ) ?) ) ;
435506 }
507+ // array indexing
508+ if matches ! ( op, TokenKind :: LBracket ) {
509+ self . next ( ) ;
510+
511+ let index = self . parse_expression ( 0 ) ?;
512+
513+ self . expect ( TokenKind :: RBracket ) ?;
514+
515+ self . pos_stack . push ( start) ;
516+ return Ok ( Some ( self . push_node ( NodeKind :: IndexExpression { array : lhs, index } ) ?) ) ;
517+ }
518+ // tuple indexing / field access
519+ if matches ! ( op, TokenKind :: Dot ) {
520+ self . next ( ) ;
521+
522+ match self . next_with_span ( ) {
523+ ( TokenKind :: Identifier ( _field) , _) => todo ! ( "Field Access is not yet implemented" ) ,
524+ ( TokenKind :: Int ( i) , _) => {
525+ let index = * i;
526+ self . pos_stack . push ( start) ;
527+ return Ok ( Some ( self . push_node ( NodeKind :: TupleIndexExpression { tuple : lhs, index } ) ?) ) ;
528+ }
529+ ( other, span) => return Err ( ParseError :: new ( span, format ! ( "expected field name or tuple index, found {:?}" , other) ) ) ,
530+ }
531+ }
436532
437533 Ok ( None )
438534 }
@@ -456,6 +552,15 @@ impl Parser {
456552 self . push_node ( NodeKind :: If { cond, then_block, else_block } )
457553 }
458554
555+ fn parse_loop_expression ( & mut self ) -> PResult < NodeId > {
556+ self . start_span ( ) ;
557+ self . expect ( TokenKind :: Loop ) ?;
558+
559+ let block = self . parse_block_expression ( ) ?;
560+
561+ self . push_node ( NodeKind :: Loop { block } )
562+ }
563+
459564 fn parse_block_expression ( & mut self ) -> PResult < NodeId > {
460565 self . start_span ( ) ;
461566 self . expect ( TokenKind :: LBrace ) ?;
@@ -774,7 +879,7 @@ mod tests {
774879 let src = r#"
775880 fn main() {
776881 fn local(x: Int) -> Int { x * 2 }
777- let x = 5 ;
882+ let mut x: [(List<Int>, Int); 3] = [(List(_), 923); 3] ;
778883 x + 5 = x - 1;
779884 }
780885 "# ;
0 commit comments