Add expression parsing
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
use std::fmt::{Display, Formatter, self};
|
||||
|
||||
#[derive(Debug, Clone, Copy, Eq)]
|
||||
#[cfg_attr(not(test), derive(PartialEq))]
|
||||
pub struct Pos {
|
||||
pub source: usize,
|
||||
pub line: usize,
|
||||
@@ -7,6 +10,12 @@ pub struct Pos {
|
||||
pub len: usize,
|
||||
}
|
||||
|
||||
impl Display for Pos {
|
||||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
||||
write!(fmt, "line {} at {}", self.line + 1, self.col + 1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Pos {
|
||||
fn default() -> Self {
|
||||
Pos {
|
||||
@@ -19,6 +28,13 @@ impl Default for Pos {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl PartialEq for Pos {
|
||||
fn eq(&self, _other: &Pos) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl Pos {
|
||||
pub fn from_char(c: char, source: usize, line: usize, col: usize, byte: usize) -> Self {
|
||||
Pos {
|
||||
@@ -77,6 +93,18 @@ impl Span {
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Span {
|
||||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
||||
if self.start == self.end {
|
||||
Display::fmt(&self.start, fmt)
|
||||
} else if self.start.line == self.end.line {
|
||||
write!(fmt, "line {} at {}-{}", self.start.line + 1, self.start.col + 1, self.end.col + 1)
|
||||
} else {
|
||||
write!(fmt, "lines {} to {}", self.start.line + 1, self.end.line + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Spanned {
|
||||
fn span(&self) -> Span;
|
||||
|
||||
@@ -92,6 +120,23 @@ impl Spanned for Span {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Sourced<'t, T: Spanned> {
|
||||
text: &'t str,
|
||||
inner: T,
|
||||
}
|
||||
|
||||
impl<'t, T: Spanned> Sourced<'t, T> {
|
||||
fn text(&self) -> &'t str {
|
||||
self.text_at(self.text)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Spanned> Spanned for Sourced<'_, T> {
|
||||
fn span(&self) -> Span {
|
||||
self.inner.span()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user