Browse Source

switch to using anyhow

Getty Ritter 2 years ago
parent
commit
b52f562f00
5 changed files with 18 additions and 34 deletions
  1. 1 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 4 0
      build.rs
  4. 6 34
      src/interp.rs
  5. 6 0
      src/lexer.rs

+ 1 - 0
Cargo.lock

@@ -491,6 +491,7 @@ name = "matzo"
 version = "0.1.0"
 dependencies = [
  "ansi_term",
+ "anyhow",
  "lalrpop",
  "lalrpop-util",
  "logos",

+ 1 - 0
Cargo.toml

@@ -26,6 +26,7 @@ logos = "*"
 rustyline = "*"
 ansi_term = "*"
 string-interner = "*"
+anyhow = "*"
 
 [build-dependencies]
 vergen = "*"

+ 4 - 0
build.rs

@@ -69,6 +69,10 @@ fn test_%PREFIX%() {
 fn main() -> Result<(), Box<dyn std::error::Error>> {
     lalrpop::process_root()?;
 
+    for file in ["build.rs", "src/grammar.lalrpop"] {
+        println!("cargo:rerun-if-changed={}", file);
+    }
+
     vergen::vergen(vergen::Config::default())?;
 
     let out_dir = env::var("OUT_DIR")?;

+ 6 - 34
src/interp.rs

@@ -1,4 +1,6 @@
 use crate::ast::*;
+
+use anyhow::{Error,anyhow,bail};
 use rand::Rng;
 use std::cell::RefCell;
 use std::collections::HashMap;
@@ -6,36 +8,6 @@ use std::fmt;
 use std::io;
 use std::rc::Rc;
 
-macro_rules! bail {
-    ($fmt:expr) => { return Err(Error { message: format!($fmt), }) };
-    ($fmt:expr, $($e:expr),*) => { return Err(Error { message: format!($fmt, $($e),*), }) }
-}
-
-#[derive(Debug)]
-pub struct Error {
-    message: String,
-}
-
-impl From<lalrpop_util::ParseError<usize, crate::lexer::Token<'_>, crate::lexer::LexerError>>
-    for Error
-{
-    fn from(
-        err: lalrpop_util::ParseError<usize, crate::lexer::Token<'_>, crate::lexer::LexerError>,
-    ) -> Error {
-        Error {
-            message: format!("{:?}", err),
-        }
-    }
-}
-
-impl fmt::Display for Error {
-    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
-        write!(fmt, "{}", self.message)
-    }
-}
-
-impl std::error::Error for Error {}
-
 /// A `Value` is a representation of the resut of evaluation. Note
 /// that a `Value` is a representation of something in _weak head
 /// normal form_: i.e. for compound expressions (right now just
@@ -343,7 +315,7 @@ impl State {
     /// results to stdout.
     pub fn run(&self, src: &str) -> Result<(), Error> {
         let lexed = crate::lexer::tokens(src);
-        let stmts = self.parser.parse(&mut self.ast.borrow_mut(), lexed)?;
+        let stmts = self.parser.parse(&mut self.ast.borrow_mut(), lexed).map_err(|err| anyhow!("Got {:?}", err))?;
         let mut stdout = io::stdout();
         for stmt in stmts {
             self.execute(&stmt, &mut stdout)?;
@@ -372,7 +344,7 @@ impl State {
                 if let Ok(stmts) = self.parser.parse(&mut self.ast.borrow_mut(), lexed) {
                     stmts
                 } else {
-                    return Err(err.into());
+                    bail!("{:?}", err);
                 }
             }
         };
@@ -627,7 +599,7 @@ impl State {
     /// patterns. An example where that might work poorly if we're not
     /// careful is here:
     ///
-    /// ```
+    /// ```ignore
     /// {Foo => "1"; Foo => "2"; _ => "..."}.(Foo | Bar)
     /// ```
     ///
@@ -636,7 +608,7 @@ impl State {
     /// the forced argument_. But we also want the following to still
     /// contain non-determinism:
     ///
-    /// ```
+    /// ```ignore
     /// {<Foo, x> => x x "!"; <Bar, x> => x x "?"}.<Foo | Bar, "a" | "b">
     /// ```
     ///

+ 6 - 0
src/lexer.rs

@@ -109,6 +109,12 @@ pub enum Token<'a> {
 #[derive(Debug)]
 pub struct LexerError;
 
+impl std::fmt::Display for LexerError {
+    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+        write!(f, "LexerError")
+    }
+}
+
 pub type Spanned<Tok, Loc, Error> = Result<(Loc, Tok, Loc), Error>;
 
 pub fn tokens(source: &str) -> impl Iterator<Item = Spanned<Token<'_>, usize, LexerError>> {