Skip to content

Commit cabe26f

Browse files
committed
Add macro todo_pos! used in lexer to print input file line number.
1 parent f91894b commit cabe26f

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

crates/vim9-lexer/src/lib.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,41 @@ use std::{
88

99
use anyhow::{Context, Result};
1010

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+
1146
#[derive(Clone, PartialEq)]
1247
pub struct Span {
1348
pub start_row: usize,
@@ -395,6 +430,22 @@ impl Lexer {
395430
})
396431
}
397432

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+
398449
pub fn read_char(&self) {
399450
// println!("read_char: {:?}", self);
400451

@@ -765,7 +816,7 @@ impl Lexer {
765816
Ok(Token {
766817
kind: Illegal,
767818
// text: TokenText::Ch(ch),
768-
text: todo!(),
819+
text: todo_pos!(self, "{:?}", ch),
769820
span: self.make_span(self.position(), self.position())?,
770821
})
771822
}
@@ -923,7 +974,7 @@ impl Lexer {
923974
_ => Token {
924975
kind: TokenKind::Illegal,
925976
// text: TokenText::Ch(self.ch.unwrap()),
926-
text: todo!(),
977+
text: todo_pos!(self, "{:?}", self.peek_char().unwrap()),
927978
span: self.make_span(self.position(), self.position())?,
928979
},
929980
})

0 commit comments

Comments
 (0)