@@ -50,8 +50,6 @@ impl fmt::Display for Token<'_> {
5050 }
5151}
5252
53- pub type Spanned < T > = ( T , SimpleSpan ) ;
54-
5553fn lexer < ' src > (
5654) -> impl Parser < ' src , & ' src str , Vec < Spanned < Token < ' src > > > , extra:: Err < Rich < ' src , char > > > {
5755 recursive ( |token| {
@@ -82,7 +80,7 @@ fn lexer<'src>(
8280 . as_context ( )
8381 . map ( Token :: Parens ) ,
8482 ) )
85- . map_with ( |t , e| ( t , e . span ( ) ) )
83+ . spanned ( )
8684 . padded ( )
8785 } )
8886 . repeated ( )
@@ -128,7 +126,7 @@ fn parser<'tokens, 'src: 'tokens>() -> impl Parser<
128126 ident. map ( Expr :: Var ) ,
129127 // let x = y in z
130128 just ( Token :: Let )
131- . ignore_then ( ident. map_with ( |x , e| ( x , e . span ( ) ) ) )
129+ . ignore_then ( ident. spanned ( ) )
132130 . then_ignore ( just ( Token :: Eq ) )
133131 . then ( expr. clone ( ) )
134132 . then_ignore ( just ( Token :: In ) )
@@ -141,45 +139,39 @@ fn parser<'tokens, 'src: 'tokens>() -> impl Parser<
141139 ) ) ;
142140
143141 choice ( (
144- atom. map_with ( |expr , e| ( expr , e . span ( ) ) ) ,
142+ atom. spanned ( ) ,
145143 // fn x y = z
146- just ( Token :: Fn ) . ignore_then (
147- ident. map_with ( |x, e| ( x, e. span ( ) ) ) . repeated ( ) . foldr_with (
148- just ( Token :: Eq ) . ignore_then ( expr. clone ( ) ) ,
149- |arg, body, e| {
150- (
151- Expr :: Func {
152- arg : Box :: new ( arg) ,
153- body : Box :: new ( body) ,
154- } ,
155- e. span ( ) ,
156- )
157- } ,
158- ) ,
159- ) ,
144+ just ( Token :: Fn ) . ignore_then ( ident. spanned ( ) . repeated ( ) . foldr_with (
145+ just ( Token :: Eq ) . ignore_then ( expr. clone ( ) ) ,
146+ |arg, body, e| {
147+ Expr :: Func {
148+ arg : Box :: new ( arg) ,
149+ body : Box :: new ( body) ,
150+ }
151+ . with_span ( e. span ( ) )
152+ } ,
153+ ) ) ,
160154 // ( x )
161- expr. nested_in ( select_ref ! { Token :: Parens ( ts) = e => ts. split_token_span ( e. span( ) ) } ) ,
155+ expr. nested_in ( select_ref ! { Token :: Parens ( ts) = e => ts. split_spanned ( e. span( ) ) } ) ,
162156 ) )
163157 . pratt ( vec ! [
164158 // Multiply
165159 infix( left( 10 ) , just( Token :: Asterisk ) , |x, _, y, e| {
166- ( Expr :: Mul ( Box :: new( x) , Box :: new( y) ) , e. span( ) )
160+ Expr :: Mul ( Box :: new( x) , Box :: new( y) ) . with_span ( e. span( ) )
167161 } )
168162 . boxed( ) ,
169163 // Add
170164 infix( left( 9 ) , just( Token :: Plus ) , |x, _, y, e| {
171- ( Expr :: Add ( Box :: new( x) , Box :: new( y) ) , e. span( ) )
165+ Expr :: Add ( Box :: new( x) , Box :: new( y) ) . with_span ( e. span( ) )
172166 } )
173167 . boxed( ) ,
174168 // Calls
175169 infix( left( 1 ) , empty( ) , |x, _, y, e| {
176- (
177- Expr :: Apply {
178- func: Box :: new( x) ,
179- arg: Box :: new( y) ,
180- } ,
181- e. span( ) ,
182- )
170+ Expr :: Apply {
171+ func: Box :: new( x) ,
172+ arg: Box :: new( y) ,
173+ }
174+ . with_span( e. span( ) )
183175 } )
184176 . boxed( ) ,
185177 ] )
@@ -270,17 +262,17 @@ impl Solver<'_> {
270262 expr : & Spanned < Expr < ' src > > ,
271263 env : & mut Vec < ( & ' src str , TyVar ) > ,
272264 ) -> TyVar {
273- match & expr. 0 {
274- Expr :: Num ( _) => self . create_ty ( TyInfo :: Num , expr. 1 ) ,
275- Expr :: Bool ( _) => self . create_ty ( TyInfo :: Bool , expr. 1 ) ,
265+ match & * * expr {
266+ Expr :: Num ( _) => self . create_ty ( TyInfo :: Num , expr. span ) ,
267+ Expr :: Bool ( _) => self . create_ty ( TyInfo :: Bool , expr. span ) ,
276268 Expr :: Var ( name) => {
277269 env. iter ( )
278270 . rev ( )
279271 . find ( |( n, _) | n == name)
280272 . unwrap_or_else ( || {
281273 failure (
282274 format ! ( "No such local '{name}'" ) ,
283- ( "not found in scope" . to_string ( ) , expr. 1 ) ,
275+ ( "not found in scope" . to_string ( ) , expr. span ) ,
284276 None ,
285277 self . src ,
286278 )
@@ -289,32 +281,32 @@ impl Solver<'_> {
289281 }
290282 Expr :: Let { lhs, rhs, then } => {
291283 let rhs_ty = self . check ( rhs, env) ;
292- env. push ( ( lhs. 0 , rhs_ty) ) ;
284+ env. push ( ( * * lhs, rhs_ty) ) ;
293285 let out_ty = self . check ( then, env) ;
294286 env. pop ( ) ;
295287 out_ty
296288 }
297289 Expr :: Func { arg, body } => {
298- let arg_ty = self . create_ty ( TyInfo :: Unknown , arg. 1 ) ;
299- env. push ( ( arg. 0 , arg_ty) ) ;
290+ let arg_ty = self . create_ty ( TyInfo :: Unknown , arg. span ) ;
291+ env. push ( ( & * * arg, arg_ty) ) ;
300292 let body_ty = self . check ( body, env) ;
301293 env. pop ( ) ;
302- self . create_ty ( TyInfo :: Func ( arg_ty, body_ty) , expr. 1 )
294+ self . create_ty ( TyInfo :: Func ( arg_ty, body_ty) , expr. span )
303295 }
304296 Expr :: Apply { func, arg } => {
305297 let func_ty = self . check ( func, env) ;
306298 let arg_ty = self . check ( arg, env) ;
307- let out_ty = self . create_ty ( TyInfo :: Unknown , expr. 1 ) ;
308- let func_req_ty = self . create_ty ( TyInfo :: Func ( arg_ty, out_ty) , func. 1 ) ;
309- self . unify ( func_req_ty, func_ty, expr. 1 ) ;
299+ let out_ty = self . create_ty ( TyInfo :: Unknown , expr. span ) ;
300+ let func_req_ty = self . create_ty ( TyInfo :: Func ( arg_ty, out_ty) , func. span ) ;
301+ self . unify ( func_req_ty, func_ty, expr. span ) ;
310302 out_ty
311303 }
312304 Expr :: Add ( l, r) | Expr :: Mul ( l, r) => {
313- let out_ty = self . create_ty ( TyInfo :: Num , expr. 1 ) ;
305+ let out_ty = self . create_ty ( TyInfo :: Num , expr. span ) ;
314306 let l_ty = self . check ( l, env) ;
315- self . unify ( out_ty, l_ty, expr. 1 ) ;
307+ self . unify ( out_ty, l_ty, expr. span ) ;
316308 let r_ty = self . check ( r, env) ;
317- self . unify ( out_ty, r_ty, expr. 1 ) ;
309+ self . unify ( out_ty, r_ty, expr. span ) ;
318310 out_ty
319311 }
320312 }
@@ -363,14 +355,14 @@ pub struct Vm<'src> {
363355
364356impl < ' src > Vm < ' src > {
365357 pub fn eval ( & mut self , expr : & ' src Spanned < Expr < ' src > > ) -> Value < ' src > {
366- match & expr. 0 {
358+ match & * * expr {
367359 Expr :: Num ( x) => Value :: Num ( * x) ,
368360 Expr :: Bool ( x) => Value :: Bool ( * x) ,
369361 Expr :: Var ( var) => self
370362 . stack
371363 . iter ( )
372364 . rev ( )
373- . find ( |( v, _) | v . 0 == * var)
365+ . find ( |( v, _) | * * v == * var)
374366 . unwrap ( )
375367 . 1
376368 . clone ( ) ,
@@ -456,7 +448,7 @@ fn main() {
456448 . unwrap_or_else ( |errs| parse_failure ( & errs[ 0 ] , src) ) ;
457449
458450 let expr = parser ( )
459- . parse ( tokens[ ..] . split_token_span ( ( 0 ..src. len ( ) ) . into ( ) ) )
451+ . parse ( tokens[ ..] . split_spanned ( ( 0 ..src. len ( ) ) . into ( ) ) )
460452 . into_result ( )
461453 . unwrap_or_else ( |errs| parse_failure ( & errs[ 0 ] , src) ) ;
462454
0 commit comments