File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 11edition = " 2024"
2+ max_width = 88
Original file line number Diff line number Diff line change 1+ use chumsky:: prelude:: * ;
2+ use std:: io:: { self , Read } ;
3+
4+ #[ derive( Clone , Debug ) ]
5+ pub enum Stmt {
6+ Expr ,
7+ Loop ( Vec < Stmt > ) ,
8+ }
9+
10+ fn parser < ' a > ( ) -> impl Parser < ' a , & ' a str , Vec < Stmt > > {
11+ let expr = just ( "expr" ) ; // TODO
12+
13+ let block = recursive ( |block| {
14+ let indent = just ( ' ' )
15+ . repeated ( )
16+ . configure ( |cfg, parent_indent| cfg. exactly ( * parent_indent) ) ;
17+
18+ let expr_stmt = expr. then_ignore ( text:: newline ( ) ) . to ( Stmt :: Expr ) ;
19+ let control_flow = just ( "loop:" )
20+ . then ( text:: newline ( ) )
21+ . ignore_then ( block)
22+ . map ( Stmt :: Loop ) ;
23+ let stmt = expr_stmt. or ( control_flow) ;
24+
25+ text:: whitespace ( )
26+ . count ( )
27+ . ignore_with_ctx ( stmt. separated_by ( indent) . collect ( ) )
28+ } ) ;
29+
30+ block. with_ctx ( 0 )
31+ }
32+
133fn main ( ) {
2- println ! ( "Hello, world!" ) ;
34+ let mut src = String :: new ( ) ;
35+ io:: stdin ( )
36+ . read_to_string ( & mut src)
37+ . expect ( "Failed to read stdin" ) ;
38+ match parser ( ) . parse ( src. trim ( ) ) . into_result ( ) {
39+ Ok ( ast) => println ! ( "{:#?}" , ast) ,
40+ Err ( errs) => errs. into_iter ( ) . for_each ( |e| println ! ( "{e:?}" ) ) ,
41+ } ;
342}
You can’t perform that action at this time.
0 commit comments