Add Position::offset, and recompute line/column info based on source

This commit is contained in:
Joshua Warner 2021-12-23 21:06:08 -08:00
parent 443d738f9b
commit 4b04ec6bbc
7 changed files with 279 additions and 300 deletions

View file

@ -8,8 +8,12 @@ use std::fmt;
#[derive(Clone)]
pub struct State<'a> {
/// The raw input bytes from the file.
/// Beware: bytes[0] always points the the current byte the parser is examining.
bytes: &'a [u8],
/// Length of the original input in bytes
input_len: usize,
/// Current position within the input (line/column)
pub xyzlcol: LineColumn,
@ -22,6 +26,7 @@ impl<'a> State<'a> {
pub fn new(bytes: &'a [u8]) -> State<'a> {
State {
bytes,
input_len: bytes.len(),
xyzlcol: LineColumn::default(),
indent_column: 0,
}
@ -40,7 +45,10 @@ impl<'a> State<'a> {
/// Returns the current position
pub const fn pos(&self) -> Position {
Position::new(self.xyzlcol.line, self.xyzlcol.column)
Position::new(
(self.input_len - self.bytes.len()) as u32,
self.xyzlcol.line,
self.xyzlcol.column)
}
/// Returns whether the parser has reached the end of the input
@ -95,6 +103,7 @@ impl<'a> State<'a> {
Region::new(
self.pos(),
Position::new(
self.pos().bump_column(length).offset,
self.xyzlcol.line,
self
.xyzlcol