From b2576c20e7d21bec00084366c7f6f84e98ea22f3 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sun, 4 Mar 2018 22:06:20 -0800 Subject: [PATCH] Upgraded to Nom 2.x --- Cargo.lock | 22 +++---- Cargo.toml | 2 +- src/main.rs | 1 - src/parse/basics.rs | 145 +++++++++++++++++++++++++------------------- src/parse/psy.rs | 5 +- 5 files changed, 98 insertions(+), 77 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 471815e..a8f644f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,7 +8,7 @@ name = "atty" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.38 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -87,7 +87,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libc" -version = "0.2.37" +version = "0.2.38" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -103,7 +103,7 @@ version = "0.1.0" [[package]] name = "nom" -version = "1.2.4" +version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -111,7 +111,7 @@ name = "num_cpus" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.38 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -120,7 +120,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "half 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.38 (registry+https://github.com/rust-lang/crates.io-index)", "openexr-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -130,7 +130,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.38 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -159,7 +159,7 @@ dependencies = [ "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "math3d 0.1.0", "mem_arena 0.1.0", - "nom 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "nom 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "openexr 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "png_encode_mini 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -221,7 +221,7 @@ name = "termion" version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.38 (registry+https://github.com/rust-lang/crates.io-index)", "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -239,7 +239,7 @@ name = "time" version = "0.1.39" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.38 (registry+https://github.com/rust-lang/crates.io-index)", "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -284,8 +284,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" "checksum half 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1aad9c31c71e74a1476316df059e235b67a3b625dd9e404bf94b1c19789f880b" "checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" -"checksum libc 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "56aebce561378d99a0bb578f8cb15b6114d2a1814a6c7949bbe646d968bb4fa9" -"checksum nom 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce" +"checksum libc 0.2.38 (registry+https://github.com/rust-lang/crates.io-index)" = "84a7beecb6b131a81c7d6c7b90cdaa1155b8531b4808bd3bc23bf4b3c33f4d9e" +"checksum nom 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf51a729ecf40266a2368ad335a5fdde43471f545a967109cd62146ecf8b66ff" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" "checksum openexr 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "849a5af32d6b0716cf44a8b18c1b611b8448aec44a9b1c59b6199bf9d22aba94" "checksum openexr-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d597730c7049d9098ce64bb97c6eb4e6f96fa55d5ced7eba74a01c224fe0bcc4" diff --git a/Cargo.toml b/Cargo.toml index 1e2d2a7..d17c8b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ clap = "2.30" crossbeam = "0.3" half = "1.0" lazy_static = "1.0" -nom = "1.2" +nom = "2.2" num_cpus = "1.8" openexr = "0.5.0" png_encode_mini = "0.1.2" diff --git a/src/main.rs b/src/main.rs index 303dfc2..26c879c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ #![cfg_attr(feature = "cargo-clippy", allow(needless_return))] #![cfg_attr(feature = "cargo-clippy", allow(or_fun_call))] #![cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))] - extern crate bvh_order; extern crate color as color_util; extern crate float4; diff --git a/src/parse/basics.rs b/src/parse/basics.rs index 00a13b9..2e9d472 100644 --- a/src/parse/basics.rs +++ b/src/parse/basics.rs @@ -6,86 +6,103 @@ use std::str; use nom::{digit, multispace, IResult, Needed}; use nom::IResult::*; +// Consumes any whitespace, including zero whitespace +named!(any_space>, opt!(complete!(multispace))); + // Parsers for numbers surrounded by whitespace -named!(pub ws_u32, delimited!(opt!(multispace), u32_utf8, opt!(multispace))); -named!(pub ws_u64, delimited!(opt!(multispace), u64_utf8, opt!(multispace))); -named!(pub ws_usize, delimited!(opt!(multispace), usize_utf8, opt!(multispace))); -named!(pub ws_i32, delimited!(opt!(multispace), i32_utf8, opt!(multispace))); -named!(pub ws_i64, delimited!(opt!(multispace), i64_utf8, opt!(multispace))); -named!(pub ws_isize, delimited!(opt!(multispace), isize_utf8, opt!(multispace))); -named!(pub ws_f32, delimited!(opt!(multispace), f32_utf8, opt!(multispace))); -named!(pub ws_f64, delimited!(opt!(multispace), f64_utf8, opt!(multispace))); +named!(pub ws_u32, delimited!(any_space, u32_utf8, any_space)); +named!(pub ws_u64, delimited!(any_space, u64_utf8, any_space)); +named!(pub ws_usize, delimited!(any_space, usize_utf8, any_space)); +named!(pub ws_i32, delimited!(any_space, i32_utf8, any_space)); +named!(pub ws_i64, delimited!(any_space, i64_utf8, any_space)); +named!(pub ws_isize, delimited!(any_space, isize_utf8, any_space)); +named!(pub ws_f32, delimited!(any_space, f32_utf8, any_space)); +named!(pub ws_f64, delimited!(any_space, f64_utf8, any_space)); // ======================================================== -named!(pub u32_utf8, chain!( - bytes: digit, - || { str::from_utf8(bytes).unwrap().parse::().unwrap() } -)); +named!(pub u32_utf8, + do_parse!( + bytes: digit >> + (str::from_utf8(bytes).unwrap().parse::().unwrap()) + ) +); -named!(pub i32_utf8, chain!( - sign: one_of!("-+")? ~ - bytes: digit, - || { +named!(pub i32_utf8, + do_parse!( + sign: opt!(one_of!("-+")) >> + bytes: digit >> + ({ match sign { Some(s) if s == '-' => -str::from_utf8(bytes).unwrap().parse::().unwrap(), _ => str::from_utf8(bytes).unwrap().parse::().unwrap(), } - } -)); + }) + ) +); -named!(pub u64_utf8, chain!( - bytes: digit, - || { str::from_utf8(bytes).unwrap().parse::().unwrap() } -)); +named!(pub u64_utf8, + do_parse!( + bytes: digit >> + (str::from_utf8(bytes).unwrap().parse::().unwrap()) + ) +); -named!(pub i64_utf8, chain!( - sign: one_of!("-+")? ~ - bytes: digit, - || { +named!(pub i64_utf8, + do_parse!( + sign: opt!(one_of!("-+")) >> + bytes: digit >> + ({ match sign { Some(s) if s == '-' => -str::from_utf8(bytes).unwrap().parse::().unwrap(), _ => str::from_utf8(bytes).unwrap().parse::().unwrap(), } - } -)); + }) + ) +); -named!(pub usize_utf8, chain!( - bytes: digit, - || { str::from_utf8(bytes).unwrap().parse::().unwrap() } -)); +named!(pub usize_utf8, + do_parse!( + bytes: digit >> + (str::from_utf8(bytes).unwrap().parse::().unwrap()) + ) +); -named!(pub isize_utf8, chain!( - sign: one_of!("-+")? ~ - bytes: digit, - || { +named!(pub isize_utf8, + do_parse!( + sign: opt!(one_of!("-+")) >> + bytes: digit >> + ({ match sign { Some(s) if s == '-' => -str::from_utf8(bytes).unwrap().parse::().unwrap(), _ => str::from_utf8(bytes).unwrap().parse::().unwrap(), } - } -)); + }) + ) +); -named!(pub f32_utf8, chain!( - bytes: take_decimal_real, - || { - str::from_utf8(bytes).unwrap().parse::().unwrap() - } -)); +named!(pub f32_utf8, + do_parse!( + bytes: take_decimal_real >> + (str::from_utf8(bytes).unwrap().parse::().unwrap()) + ) +); -named!(pub f64_utf8, chain!( - bytes: take_decimal_real, - || { - str::from_utf8(bytes).unwrap().parse::().unwrap() - } -)); +named!(pub f64_utf8, + do_parse!( + bytes: take_decimal_real >> + (str::from_utf8(bytes).unwrap().parse::().unwrap()) + ) +); fn take_decimal_integer(i: &[u8]) -> IResult<&[u8], &[u8]> { - named!(rr<&[u8], ()>, chain!( - one_of!("-+")? ~ - digit, - ||{()} - )); + named!(rr<&[u8], ()>, + do_parse!( + opt!(one_of!("-+")) >> + digit >> + () + ) + ); match rr(i) { Done(remaining, _) => { @@ -104,12 +121,18 @@ fn take_decimal_integer(i: &[u8]) -> IResult<&[u8], &[u8]> { } fn take_decimal_real(i: &[u8]) -> IResult<&[u8], &[u8]> { - named!(rr<&[u8], ()>, chain!( - one_of!("-+")? ~ - digit ~ - complete!(chain!(tag!(".") ~ digit, ||{()}))?, - ||{()} - )); + named!(rr<&[u8], ()>, + do_parse!( + opt!(one_of!("-+")) >> + digit >> + opt!(complete!(do_parse!( + tag!(".") >> + digit >> + () + ))) >> + () + ) + ); match rr(i) { Done(remaining, _) => { diff --git a/src/parse/psy.rs b/src/parse/psy.rs index 663198e..cd03b55 100644 --- a/src/parse/psy.rs +++ b/src/parse/psy.rs @@ -3,7 +3,6 @@ use std::result::Result; use std::f32; -use nom; use nom::IResult; use mem_arena::MemArena; @@ -274,7 +273,7 @@ fn parse_render_settings(tree: &DataTree) -> Result<((u32, u32), u32, u32), PsyP } if type_name == "Resolution" => { if let IResult::Done(_, (w, h)) = - closure!(terminated!(tuple!(ws_u32, ws_u32), nom::eof))(contents.as_bytes()) + closure!(terminated!(tuple!(ws_u32, ws_u32), eof!()))(contents.as_bytes()) { found_res = true; res = (w, h); @@ -583,7 +582,7 @@ pub fn parse_matrix(contents: &str) -> Result { ws_f32, ws_f32 ), - nom::eof + eof!() ))(contents.as_bytes()) { return Ok(Matrix4x4::new_from_values(