@@ -8,6 +8,41 @@ use std::{
8
8
9
9
use anyhow:: { Context , Result } ;
10
10
11
+ macro_rules! todo_pos {
12
+ ( $self: ident) => {
13
+ todo!( "{:?}" , $self. make_pos( $self. position( ) ) )
14
+ } ;
15
+
16
+ ( $self: ident, $fmt: literal) => {
17
+ todo!( concat!( "{:?}: " , $fmt) , $self. make_pos( $self. position( ) ) )
18
+ } ;
19
+
20
+ ( $self: ident, $fmt: literal, $( $arg: expr) ,+) => {
21
+ todo!( concat!( "{:?}: " , $fmt) , $self. make_pos( $self. position( ) ) , $( $arg) ,+)
22
+ } ;
23
+ }
24
+
25
+ //#[derive(Clone, PartialEq)]
26
+ pub struct Pos {
27
+ pub start_row : usize ,
28
+ pub start_col : usize ,
29
+ }
30
+
31
+ impl Pos {
32
+ pub fn empty ( ) -> Self {
33
+ Self {
34
+ start_row : 0 ,
35
+ start_col : 0 ,
36
+ }
37
+ }
38
+ }
39
+
40
+ impl Debug for Pos {
41
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
42
+ write ! ( f, "{},{}" , self . start_row, self . start_col)
43
+ }
44
+ }
45
+
11
46
#[ derive( Clone , PartialEq ) ]
12
47
pub struct Span {
13
48
pub start_row : usize ,
@@ -395,6 +430,22 @@ impl Lexer {
395
430
} )
396
431
}
397
432
433
+ fn make_pos ( & self , start : usize ) -> Result < Pos > {
434
+ let start_row = self
435
+ . lines
436
+ . iter ( )
437
+ . enumerate ( )
438
+ . find_map ( |( line, & ch) | ( ch > start) . then_some ( line) )
439
+ . ok_or_else ( || anyhow:: anyhow!( "input: {}" , start) )
440
+ . context ( "start_row" ) ?
441
+ - 1 ;
442
+
443
+ Ok ( Pos {
444
+ start_row,
445
+ start_col : start - self . lines [ start_row] ,
446
+ } )
447
+ }
448
+
398
449
pub fn read_char ( & self ) {
399
450
// println!("read_char: {:?}", self);
400
451
@@ -765,7 +816,7 @@ impl Lexer {
765
816
Ok ( Token {
766
817
kind : Illegal ,
767
818
// text: TokenText::Ch(ch),
768
- text : todo ! ( ) ,
819
+ text : todo_pos ! ( self , "{:?}" , ch ) ,
769
820
span : self . make_span ( self . position ( ) , self . position ( ) ) ?,
770
821
} )
771
822
}
@@ -923,7 +974,7 @@ impl Lexer {
923
974
_ => Token {
924
975
kind : TokenKind :: Illegal ,
925
976
// text: TokenText::Ch(self.ch.unwrap()),
926
- text : todo ! ( ) ,
977
+ text : todo_pos ! ( self , "{:?}" , self . peek_char ( ) . unwrap ( ) ) ,
927
978
span : self . make_span ( self . position ( ) , self . position ( ) ) ?,
928
979
} ,
929
980
} )
0 commit comments