mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 13:59:08 +00:00
Add Position::offset, and recompute line/column info based on source
This commit is contained in:
parent
443d738f9b
commit
4b04ec6bbc
7 changed files with 279 additions and 300 deletions
|
@ -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
|
||||
|
|
|
@ -366,7 +366,7 @@ mod test_parse {
|
|||
assert_segments(r#""Hi, \u(123)!""#, |arena| {
|
||||
bumpalo::vec![in arena;
|
||||
Plaintext("Hi, "),
|
||||
Unicode(Loc::new(Position::new(0, 8), Position::new(0, 11), "123")),
|
||||
Unicode(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), "123")),
|
||||
Plaintext("!")
|
||||
]
|
||||
});
|
||||
|
@ -376,7 +376,7 @@ mod test_parse {
|
|||
fn unicode_escape_in_front() {
|
||||
assert_segments(r#""\u(1234) is a unicode char""#, |arena| {
|
||||
bumpalo::vec![in arena;
|
||||
Unicode(Loc::new(Position::new(0, 4), Position::new(0, 8), "1234")),
|
||||
Unicode(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), "1234")),
|
||||
Plaintext(" is a unicode char")
|
||||
]
|
||||
});
|
||||
|
@ -387,7 +387,7 @@ mod test_parse {
|
|||
assert_segments(r#""this is unicode: \u(1)""#, |arena| {
|
||||
bumpalo::vec![in arena;
|
||||
Plaintext("this is unicode: "),
|
||||
Unicode(Loc::new(Position::new(0, 21), Position::new(0, 22), "1"))
|
||||
Unicode(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), "1"))
|
||||
]
|
||||
});
|
||||
}
|
||||
|
@ -396,11 +396,11 @@ mod test_parse {
|
|||
fn unicode_escape_multiple() {
|
||||
assert_segments(r#""\u(a1) this is \u(2Bcd) unicode \u(ef97)""#, |arena| {
|
||||
bumpalo::vec![in arena;
|
||||
Unicode(Loc::new(Position::new(0, 4), Position::new(0, 6), "a1")),
|
||||
Unicode(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), "a1")),
|
||||
Plaintext(" this is "),
|
||||
Unicode(Loc::new(Position::new(0, 19), Position::new(0, 23), "2Bcd")),
|
||||
Unicode(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), "2Bcd")),
|
||||
Plaintext(" unicode "),
|
||||
Unicode(Loc::new(Position::new(0, 36), Position::new(0, 40), "ef97"))
|
||||
Unicode(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), "ef97"))
|
||||
]
|
||||
});
|
||||
}
|
||||
|
@ -417,7 +417,7 @@ mod test_parse {
|
|||
|
||||
bumpalo::vec![in arena;
|
||||
Plaintext("Hi, "),
|
||||
Interpolated(Loc::new(Position::new(0, 7), Position::new(0, 11), expr)),
|
||||
Interpolated(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), expr)),
|
||||
Plaintext("!")
|
||||
]
|
||||
});
|
||||
|
@ -432,7 +432,7 @@ mod test_parse {
|
|||
});
|
||||
|
||||
bumpalo::vec![in arena;
|
||||
Interpolated(Loc::new(Position::new(0, 3), Position::new(0, 7), expr)),
|
||||
Interpolated(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), expr)),
|
||||
Plaintext(", hi!")
|
||||
]
|
||||
});
|
||||
|
@ -448,7 +448,7 @@ mod test_parse {
|
|||
|
||||
bumpalo::vec![in arena;
|
||||
Plaintext("Hello "),
|
||||
Interpolated(Loc::new(Position::new(0, 9), Position::new(0, 13), expr))
|
||||
Interpolated(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), expr))
|
||||
]
|
||||
});
|
||||
}
|
||||
|
@ -468,9 +468,9 @@ mod test_parse {
|
|||
|
||||
bumpalo::vec![in arena;
|
||||
Plaintext("Hi, "),
|
||||
Interpolated(Loc::new(Position::new(0, 7), Position::new(0, 11), expr1)),
|
||||
Interpolated(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), expr1)),
|
||||
Plaintext("! How is "),
|
||||
Interpolated(Loc::new(Position::new(0, 23), Position::new(0, 30), expr2)),
|
||||
Interpolated(Loc::new(Position::new(0, 0, 0), Position::new(0, 0, 0), expr2)),
|
||||
Plaintext(" going?")
|
||||
]
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue