@@ -8,6 +8,45 @@ 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 ! (
43
+ f,
44
+ "{},{}" ,
45
+ self . start_row, self . start_col
46
+ )
47
+ }
48
+ }
49
+
11
50
#[ derive( Clone , PartialEq ) ]
12
51
pub struct Span {
13
52
pub start_row : usize ,
@@ -395,6 +434,23 @@ impl Lexer {
395
434
} )
396
435
}
397
436
437
+ fn make_pos ( & self , start : usize ) -> Result < Pos > {
438
+
439
+ let start_row = self
440
+ . lines
441
+ . iter ( )
442
+ . enumerate ( )
443
+ . find_map ( |( line, & ch) | ( ch > start) . then_some ( line) )
444
+ . ok_or_else ( || anyhow:: anyhow!( "input: {}" , start) )
445
+ . context ( "start_row" ) ?
446
+ - 1 ;
447
+
448
+ Ok ( Pos {
449
+ start_row,
450
+ start_col : start - self . lines [ start_row] ,
451
+ } )
452
+ }
453
+
398
454
pub fn read_char ( & self ) {
399
455
// println!("read_char: {:?}", self);
400
456
@@ -765,7 +821,7 @@ impl Lexer {
765
821
Ok ( Token {
766
822
kind : Illegal ,
767
823
// text: TokenText::Ch(ch),
768
- text : todo ! ( ) ,
824
+ text : todo_pos ! ( self , "{:?}" , ch ) ,
769
825
span : self . make_span ( self . position ( ) , self . position ( ) ) ?,
770
826
} )
771
827
}
@@ -923,7 +979,7 @@ impl Lexer {
923
979
_ => Token {
924
980
kind : TokenKind :: Illegal ,
925
981
// text: TokenText::Ch(self.ch.unwrap()),
926
- text : todo ! ( ) ,
982
+ text : todo_pos ! ( self , "{:?}" , self . peek_char ( ) . unwrap ( ) ) ,
927
983
span : self . make_span ( self . position ( ) , self . position ( ) ) ?,
928
984
} ,
929
985
} )
0 commit comments