lexer_rs/posn_and_span/
stream_char_pos.rs1use crate::{PosnInCharStream, UserPosn};
3
4#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
15pub struct StreamCharPos<P>
16where
17 P: UserPosn,
18{
19 byte_ofs: usize,
20 pos: P,
21}
22
23impl<P> StreamCharPos<P>
25where
26 P: UserPosn,
27{
28 pub fn pos(&self) -> P {
32 self.pos
33 }
34}
35
36impl<P> UserPosn for StreamCharPos<P>
38where
39 P: UserPosn,
40{
41 fn advance_cols(mut self, num_bytes: usize, num_chars: usize) -> Self {
42 self.byte_ofs += num_bytes;
43 self.pos = self.pos.advance_cols(num_bytes, num_chars);
44 self
45 }
46 fn advance_line(mut self, num_bytes: usize) -> Self {
47 self.byte_ofs += num_bytes;
48 self.pos = self.pos.advance_line(num_bytes);
49 self
50 }
51 fn line(&self) -> usize {
52 self.pos.line()
53 }
54
55 fn column(&self) -> usize {
56 self.pos.column()
57 }
58 fn error_fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
59 self.pos.error_fmt(fmt)
60 }
61}
62
63impl<P> PosnInCharStream for StreamCharPos<P>
65where
66 P: UserPosn,
67{
68 fn byte_ofs(&self) -> usize {
69 self.byte_ofs
70 }
71}
72
73impl<P> std::fmt::Display for StreamCharPos<P>
75where
76 P: UserPosn,
77{
78 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
79 self.pos.error_fmt(fmt)
80 }
81}