Skip to content

Commit 2ad844b

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

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

crates/vim9-lexer/src/lib.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,45 @@ 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!(
43+
f,
44+
"{},{}",
45+
self.start_row, self.start_col
46+
)
47+
}
48+
}
49+
1150
#[derive(Clone, PartialEq)]
1251
pub struct Span {
1352
pub start_row: usize,
@@ -395,6 +434,23 @@ impl Lexer {
395434
})
396435
}
397436

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

@@ -765,7 +821,7 @@ impl Lexer {
765821
Ok(Token {
766822
kind: Illegal,
767823
// text: TokenText::Ch(ch),
768-
text: todo!(),
824+
text: todo_pos!(self, "{:?}", ch),
769825
span: self.make_span(self.position(), self.position())?,
770826
})
771827
}
@@ -923,7 +979,7 @@ impl Lexer {
923979
_ => Token {
924980
kind: TokenKind::Illegal,
925981
// text: TokenText::Ch(self.ch.unwrap()),
926-
text: todo!(),
982+
text: todo_pos!(self, "{:?}", self.peek_char().unwrap()),
927983
span: self.make_span(self.position(), self.position())?,
928984
},
929985
})

0 commit comments

Comments
 (0)