jethrogb / rust-cexpr

A C expression parser and evaluator
Apache License 2.0
45 stars 19 forks source link

update nom to 5.0.1 #19

Closed Dushistov closed 4 years ago

Dushistov commented 4 years ago

It would be great if cexpr bump nom deps to 5.x, that prevent building two versions of nom during build of my project.

jonhoo commented 4 years ago

Took a quick look at this and it's going to be quite a pain since the crate is pretty much all parsing. The parsing in nom 5 is nicer, but not sure if @jethrogb has the time to spare to do the upgrade? It would be really nice though since bindgen -> cexpr is the only dependency that keeps nom 4 in my dependency tree at the moment :sweat_smile:

For anyone thinking of undertaking this, here's how to fix assert_full_parse btw:

diff --git a/Cargo.toml b/Cargo.toml
index a7c980d..b0a4b8f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,7 +13,7 @@ keywords = ["C","expression","parser"]
 travis-ci = { repository = "jethrogb/rust-cexpr" }

 [dependencies]
-nom = {version = "^4", features = ["verbose-errors"] }
+nom = "5"

 [dev-dependencies]
 clang-sys = ">= 0.13.0, < 0.29.0"
diff --git a/src/lib.rs b/src/lib.rs
index e3115ba..c17d526 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -12,7 +12,7 @@ extern crate nom as nom_crate;

 pub mod nom {
     //! nom's result types, re-exported.
-    pub use crate::nom_crate::{Err, ErrorKind, IResult, Needed};
+    pub use crate::nom_crate::{error::ErrorKind, Err, IResult, Needed};
 }
 pub mod expr;
 pub mod literal;
@@ -52,7 +52,9 @@ macro_rules! identity (

 /// If the input result indicates a succesful parse, but there is data left,
 /// return an `Error::Partial` instead.
-pub fn assert_full_parse<I, O, E>(result: IResult<&[I], O, E>) -> IResult<&[I], O, crate::Error>
+pub fn assert_full_parse<'i, I, O, E>(
+    result: IResult<&'i [I], O, (&'i [I], E)>,
+) -> IResult<&'i [I], O, (&'i [I], crate::Error)>
 where
     Error: From<E>,
 {
@@ -61,12 +63,13 @@ where
             if rem.len() == 0 {
                 Ok((rem, output))
             } else {
-                Err(Err::Error(error_position!(
-                    rem,
-                    ErrorKind::Custom(crate::Error::Partial)
-                )))
+                Err(nom_crate::Err::Error((rem, crate::Error::Partial)))
             }
         }
-        r => r,
+        Err(nom_crate::Err::Incomplete(n)) => Err(nom_crate::Err::Incomplete(n)),
+        Err(nom_crate::Err::Failure((rem, e))) => {
+            Err(nom_crate::Err::Failure((rem, Error::from(e))))
+        }
+        Err(nom_crate::Err::Error((rem, e))) => Err(nom_crate::Err::Error((rem, Error::from(e)))),
     }
 }
jonhoo commented 4 years ago

Found some time. See https://github.com/jethrogb/rust-cexpr/pull/22, which gets us most of the way there.

jonhoo commented 4 years ago

For anyone watching this, #22 is now complete and just awaiting review.