all errors now contain source location

This commit is contained in:
2020-10-06 22:54:34 +02:00
parent d4534c9c66
commit 91573140a4
32 changed files with 400 additions and 322 deletions
+4 -4
View File
@@ -9,7 +9,7 @@ pub struct Error {
}
/// Position in the input string
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Default)]
pub struct SourcePosition {
/// The line number on which the error occurred.
pub line: usize,
@@ -76,10 +76,10 @@ pub(crate) fn err<T>(message: &'static str, s: &str, pos: &usize) -> ERes<T> {
}
/// Build a span
pub(crate) fn spos(s: &str, pos: &usize) -> Option<Box<SourcePosition>> {
pub(crate) fn spos(s: &str, pos: &usize) -> SourcePosition {
if *pos >= s.len() {
None
Default::default()
} else {
Some(Box::new(get_line_and_column(s, *pos)))
get_line_and_column(s, *pos)
}
}
+30 -7
View File
@@ -2,7 +2,6 @@
//! Use `parse` to get an s-expression from its string representation, and the
//! `Display` trait to serialize it, potentially by doing `sexp.to_string()`.
#![deny(missing_docs)]
#![deny(unsafe_code)]
#[macro_use]
@@ -38,9 +37,33 @@ pub enum Atom {
#[derive(Clone)]
pub enum Sexp {
/// Atom
Atom(Atom, Option<Box<SourcePosition>>),
Atom(Atom, SourcePosition),
/// List of expressions
List(Vec<Sexp>, Option<Box<SourcePosition>>),
List(Vec<Sexp>, SourcePosition),
}
impl Sexp {
pub fn pos(&self) -> &SourcePosition {
match self {
Sexp::List(_, pos) | Sexp::Atom(_, pos) => pos
}
}
/// Check fi thsi Sexp is an atom
pub fn is_atom(&self) -> bool {
match self {
Sexp::Atom(_, _) => true,
_ => false,
}
}
/// Check fi thsi Sexp is a list
pub fn is_list(&self) -> bool {
match self {
Sexp::List(_, _) => true,
_ => false,
}
}
}
impl PartialEq for Sexp {
@@ -207,22 +230,22 @@ fn parse_sexp(s: &str, pos: &mut usize) -> ERes<Sexp> {
/// Constructs an atomic s-expression from a string.
pub fn atom_s(s: &str) -> Sexp {
Sexp::Atom(Atom::S(s.to_owned()), None)
Sexp::Atom(Atom::S(s.to_owned()), Default::default())
}
/// Constructs an atomic s-expression from an int.
pub fn atom_i(i: i64) -> Sexp {
Sexp::Atom(Atom::I(i), None)
Sexp::Atom(Atom::I(i), Default::default())
}
/// Constructs an atomic s-expression from a float.
pub fn atom_f(f: f64) -> Sexp {
Sexp::Atom(Atom::F(f), None)
Sexp::Atom(Atom::F(f), Default::default())
}
/// Constructs a list s-expression given a slice of s-expressions.
pub fn list(xs: &[Sexp]) -> Sexp {
Sexp::List(xs.to_owned(), None)
Sexp::List(xs.to_owned(), Default::default())
}
/// Reads an s-expression out of a `&str`.