diff --git a/Cargo.lock b/Cargo.lock index 0323c91..2c3ef2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,6 +16,14 @@ dependencies = [ "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "arrayvec" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "atty" version = "0.2.11" @@ -150,6 +158,18 @@ name = "lazy_static" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lexical-core" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "libc" version = "0.2.55" @@ -168,10 +188,25 @@ name = "mem_arena" version = "0.1.0" [[package]] -name = "nom" +name = "memchr" version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "nodrop" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "nom" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lexical-core 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "num-traits" version = "0.2.8" @@ -265,7 +300,7 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "math3d 0.1.0", "mem_arena 0.1.0", - "nom 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "nom 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "oct32norm 0.1.0", "openexr 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -433,6 +468,14 @@ name = "rustc-serialize" version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rusty-fork" version = "0.2.2" @@ -444,6 +487,11 @@ dependencies = [ "wait-timeout 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ryu" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "safemem" version = "0.3.0" @@ -454,6 +502,19 @@ name = "scoped_threadpool" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "sobol" version = "0.1.0" @@ -465,6 +526,11 @@ dependencies = [ "glam 0.7.1 (git+https://github.com/bitshifter/glam-rs.git?rev=0f314f99)", ] +[[package]] +name = "static_assertions" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "strsim" version = "0.8.0" @@ -535,6 +601,11 @@ name = "vec_map" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "version_check" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "wait-timeout" version = "0.2.0" @@ -565,6 +636,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum approx 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0e60b75072ecd4168020818c0107f2857bb6c4e64252d8d3983f6263b40a5c3" +"checksum arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf" "checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" @@ -584,8 +656,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum glam 0.7.1 (git+https://github.com/bitshifter/glam-rs.git?rev=0f314f99)" = "" "checksum half 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9353c2a89d550b58fa0061d8ed8d002a7d8cdf2494eb0e432859bd3a9e543836" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" +"checksum lexical-core 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2304bccb228c4b020f3a4835d247df0a02a7c4686098d4167762cfbbe4c5cb14" "checksum libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "42914d39aad277d9e176efbdad68acb1d5443ab65afe0e0e4f0d49352a950880" -"checksum nom 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf51a729ecf40266a2368ad335a5fdde43471f545a967109cd62146ecf8b66ff" +"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" +"checksum nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" +"checksum nom 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c618b63422da4401283884e6668d39f819a106ef51f5f59b81add00075da35ca" "checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32" "checksum num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a23f0ed30a54abaa0c7e83b1d2d87ada7c3c23078d1d87815af3e3b6385fbba" "checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" @@ -612,9 +687,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum regex-syntax 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dcfd8681eebe297b81d98498869d4aae052137651ad7b96822f09ceb690d0a96" "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" +"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum rusty-fork 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3dd93264e10c577503e926bd1430193eeb5d21b059148910082245309b424fae" +"checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" "checksum safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dca453248a96cb0749e36ccdfe2b0b4e54a61bfef89fb97ec621eb8e0a93dd9" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" +"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +"checksum static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum tempfile 3.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7dc4738f2e68ed2855de5ac9cdbe05c9216773ecde4739b2f095002ab03a13ef" "checksum termion 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dde0593aeb8d47accea5392b39350015b5eccb12c0d98044d856983d89548dea" @@ -623,6 +703,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" +"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum wait-timeout 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" "checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" diff --git a/Cargo.toml b/Cargo.toml index f2fe96e..9699d8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ copy_in_place = "0.2.0" crossbeam = "0.3" half = "1.0" lazy_static = "1.0" -nom = "2.2" +nom = "5" num_cpus = "1.8" openexr = "0.6.0" png_encode_mini = "0.1.2" diff --git a/src/main.rs b/src/main.rs index 1d77988..c6a1d38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,7 @@ mod transform_stack; use std::{fs::File, io, io::Read, mem, path::Path, str::FromStr}; use clap::{App, Arg}; -use nom::{error_position, take_until}; +use nom::bytes::complete::take_until; use mem_arena::MemArena; @@ -211,7 +211,8 @@ fn main() { let mut done = false; let mut trunc_len = 0; - if let nom::IResult::Done(remaining, _) = take_until!(&input[start..end], "__PSY_EOF__") + if let nom::IResult::Ok((remaining, _)) = + take_until::<&str, &[u8], ()>("__PSY_EOF__")(&input[start..end]) { done = true; trunc_len = input.len() - remaining.len(); diff --git a/src/parse/basics.rs b/src/parse/basics.rs index 2bd610f..fceaa4d 100644 --- a/src/parse/basics.rs +++ b/src/parse/basics.rs @@ -1,352 +1,97 @@ //! Some basic nom parsers #![allow(dead_code)] -use std::str; +use std::str::{self, FromStr}; use nom::{ - call, complete, delimited, digit, do_parse, error_position, multispace, named, one_of, opt, - tag, tuple_parser, - IResult::{self, *}, - Needed, + character::complete::{digit1, multispace0}, + combinator::map_res, + number::complete::float, + sequence::delimited, + IResult, }; -// Consumes any whitespace, including zero whitespace -named!(any_space>, opt!(complete!(multispace))); - -// Parsers for numbers surrounded by whitespace -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, - do_parse!( - bytes: digit >> - (str::from_utf8(bytes).unwrap().parse::().unwrap()) - ) -); - -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, - do_parse!( - bytes: digit >> - (str::from_utf8(bytes).unwrap().parse::().unwrap()) - ) -); - -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, - do_parse!( - bytes: digit >> - (str::from_utf8(bytes).unwrap().parse::().unwrap()) - ) -); - -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, - do_parse!( - 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], ()>, - do_parse!( - opt!(one_of!("-+")) >> - digit >> - () - ) - ); - - match rr(i) { - Done(remaining, _) => { - let m = i.len() - remaining.len(); - if m == 0 { - Incomplete(Needed::Unknown) - } else { - Done(&i[m..], &i[0..m]) - } - } - - Error(e) => Error(e), - - Incomplete(e) => Incomplete(e), - } +pub fn ws_f32(input: &str) -> IResult<&str, f32, ()> { + delimited(multispace0, float, multispace0)(input) } -fn take_decimal_real(i: &[u8]) -> IResult<&[u8], &[u8]> { - named!(rr<&[u8], ()>, - do_parse!( - opt!(one_of!("-+")) >> - digit >> - opt!(complete!(do_parse!( - tag!(".") >> - digit >> - () - ))) >> - () - ) - ); - - match rr(i) { - Done(remaining, _) => { - let m = i.len() - remaining.len(); - if m == 0 { - Incomplete(Needed::Unknown) - } else { - Done(&i[m..], &i[0..m]) - } - } - - Error(e) => Error(e), - - Incomplete(e) => Incomplete(e), - } +pub fn ws_u32(input: &str) -> IResult<&str, u32, ()> { + map_res(delimited(multispace0, digit1, multispace0), u32::from_str)(input) } +pub fn ws_usize(input: &str) -> IResult<&str, usize, ()> { + map_res(delimited(multispace0, digit1, multispace0), usize::from_str)(input) +} + +// pub fn ws_i32(input: &str) -> IResult<&str, u32, ()> { +// map_res( +// delimited( +// multispace0, +// digit1, +// multispace0, +// ), +// u32::from_str, +// )(input) +// } + // ======================================================== #[cfg(test)] mod test { - use super::take_decimal_real; use super::*; + use nom::{combinator::all_consuming, sequence::tuple}; #[test] fn ws_u32_1() { - assert_eq!(ws_u32(b"42"), Done(&b""[..], 42)); - assert_eq!(ws_u32(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_u32(b"42 "), Done(&b""[..], 42)); - assert_eq!(ws_u32(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_u32(b" 42 53"), Done(&b"53"[..], 42)); - } - - #[test] - fn ws_i32_1() { - assert_eq!(ws_i32(b"42"), Done(&b""[..], 42)); - assert_eq!(ws_i32(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_i32(b"42 "), Done(&b""[..], 42)); - assert_eq!(ws_i32(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_i32(b" 42 53"), Done(&b"53"[..], 42)); - } - - #[test] - fn ws_i32_2() { - assert_eq!(ws_i32(b"-42"), Done(&b""[..], -42)); - assert_eq!(ws_i32(b" -42"), Done(&b""[..], -42)); - assert_eq!(ws_i32(b"-42 "), Done(&b""[..], -42)); - assert_eq!(ws_i32(b" -42 "), Done(&b""[..], -42)); - assert_eq!(ws_i32(b" -42 53"), Done(&b"53"[..], -42)); - } - - #[test] - fn ws_u64_1() { - assert_eq!(ws_u64(b"42"), Done(&b""[..], 42)); - assert_eq!(ws_u64(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_u64(b"42 "), Done(&b""[..], 42)); - assert_eq!(ws_u64(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_u64(b" 42 53"), Done(&b"53"[..], 42)); - } - - #[test] - fn ws_i64_1() { - assert_eq!(ws_i64(b"42"), Done(&b""[..], 42)); - assert_eq!(ws_i64(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_i64(b"42 "), Done(&b""[..], 42)); - assert_eq!(ws_i64(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_i64(b" 42 53"), Done(&b"53"[..], 42)); - } - - #[test] - fn ws_i64_2() { - assert_eq!(ws_i64(b"-42"), Done(&b""[..], -42)); - assert_eq!(ws_i64(b" -42"), Done(&b""[..], -42)); - assert_eq!(ws_i64(b"-42 "), Done(&b""[..], -42)); - assert_eq!(ws_i64(b" -42 "), Done(&b""[..], -42)); - assert_eq!(ws_i64(b" -42 53"), Done(&b"53"[..], -42)); + assert_eq!(ws_u32("42"), Ok((&""[..], 42))); + assert_eq!(ws_u32(" 42"), Ok((&""[..], 42))); + assert_eq!(ws_u32("42 "), Ok((&""[..], 42))); + assert_eq!(ws_u32(" 42"), Ok((&""[..], 42))); + assert_eq!(ws_u32(" 42 53"), Ok((&"53"[..], 42))); } #[test] fn ws_usize_1() { - assert_eq!(ws_usize(b"42"), Done(&b""[..], 42)); - assert_eq!(ws_usize(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_usize(b"42 "), Done(&b""[..], 42)); - assert_eq!(ws_usize(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_usize(b" 42 53"), Done(&b"53"[..], 42)); - } - - #[test] - fn ws_isize_1() { - assert_eq!(ws_isize(b"42"), Done(&b""[..], 42)); - assert_eq!(ws_isize(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_isize(b"42 "), Done(&b""[..], 42)); - assert_eq!(ws_isize(b" 42"), Done(&b""[..], 42)); - assert_eq!(ws_isize(b" 42 53"), Done(&b"53"[..], 42)); - } - - #[test] - fn ws_isize_2() { - assert_eq!(ws_isize(b"-42"), Done(&b""[..], -42)); - assert_eq!(ws_isize(b" -42"), Done(&b""[..], -42)); - assert_eq!(ws_isize(b"-42 "), Done(&b""[..], -42)); - assert_eq!(ws_isize(b" -42 "), Done(&b""[..], -42)); - assert_eq!(ws_isize(b" -42 53"), Done(&b"53"[..], -42)); + assert_eq!(ws_usize("42"), Ok((&""[..], 42))); + assert_eq!(ws_usize(" 42"), Ok((&""[..], 42))); + assert_eq!(ws_usize("42 "), Ok((&""[..], 42))); + assert_eq!(ws_usize(" 42"), Ok((&""[..], 42))); + assert_eq!(ws_usize(" 42 53"), Ok((&"53"[..], 42))); } #[test] fn ws_f32_1() { - assert_eq!(ws_f32(b"42"), Done(&b""[..], 42.0)); - assert_eq!(ws_f32(b" 42"), Done(&b""[..], 42.0)); - assert_eq!(ws_f32(b"42 "), Done(&b""[..], 42.0)); - assert_eq!(ws_f32(b" 42"), Done(&b""[..], 42.0)); - assert_eq!(ws_f32(b" 42 53"), Done(&b"53"[..], 42.0)); + assert_eq!(ws_f32("42"), Ok((&""[..], 42.0))); + assert_eq!(ws_f32(" 42"), Ok((&""[..], 42.0))); + assert_eq!(ws_f32("42 "), Ok((&""[..], 42.0))); + assert_eq!(ws_f32(" 42"), Ok((&""[..], 42.0))); + assert_eq!(ws_f32(" 42 53"), Ok((&"53"[..], 42.0))); } #[test] fn ws_f32_2() { - assert_eq!(ws_f32(b"42.5"), Done(&b""[..], 42.5)); - assert_eq!(ws_f32(b" 42.5"), Done(&b""[..], 42.5)); - assert_eq!(ws_f32(b"42.5 "), Done(&b""[..], 42.5)); - assert_eq!(ws_f32(b" 42.5"), Done(&b""[..], 42.5)); - assert_eq!(ws_f32(b" 42.5 53"), Done(&b"53"[..], 42.5)); + assert_eq!(ws_f32("42.5"), Ok((&""[..], 42.5))); + assert_eq!(ws_f32(" 42.5"), Ok((&""[..], 42.5))); + assert_eq!(ws_f32("42.5 "), Ok((&""[..], 42.5))); + assert_eq!(ws_f32(" 42.5"), Ok((&""[..], 42.5))); + assert_eq!(ws_f32(" 42.5 53"), Ok((&"53"[..], 42.5))); } #[test] fn ws_f32_3() { - assert_eq!(ws_f32(b"-42.5"), Done(&b""[..], -42.5)); - assert_eq!(ws_f32(b" -42.5"), Done(&b""[..], -42.5)); - assert_eq!(ws_f32(b"-42.5 "), Done(&b""[..], -42.5)); - assert_eq!(ws_f32(b" -42.5"), Done(&b""[..], -42.5)); - assert_eq!(ws_f32(b" -42.5 53"), Done(&b"53"[..], -42.5)); + assert_eq!(ws_f32("-42.5"), Ok((&""[..], -42.5))); + assert_eq!(ws_f32(" -42.5"), Ok((&""[..], -42.5))); + assert_eq!(ws_f32("-42.5 "), Ok((&""[..], -42.5))); + assert_eq!(ws_f32(" -42.5"), Ok((&""[..], -42.5))); + assert_eq!(ws_f32(" -42.5 53"), Ok((&"53"[..], -42.5))); } #[test] - fn ws_f64_1() { - assert_eq!(ws_f64(b"42"), Done(&b""[..], 42.0)); - assert_eq!(ws_f64(b" 42"), Done(&b""[..], 42.0)); - assert_eq!(ws_f64(b"42 "), Done(&b""[..], 42.0)); - assert_eq!(ws_f64(b" 42"), Done(&b""[..], 42.0)); - assert_eq!(ws_f64(b" 42 53"), Done(&b"53"[..], 42.0)); - } - - #[test] - fn ws_f64_2() { - assert_eq!(ws_f64(b"42.5"), Done(&b""[..], 42.5)); - assert_eq!(ws_f64(b" 42.5"), Done(&b""[..], 42.5)); - assert_eq!(ws_f64(b"42.5 "), Done(&b""[..], 42.5)); - assert_eq!(ws_f64(b" 42.5"), Done(&b""[..], 42.5)); - assert_eq!(ws_f64(b" 42.5 53"), Done(&b"53"[..], 42.5)); - } - - #[test] - fn ws_f64_3() { - assert_eq!(ws_f64(b"-42.5"), Done(&b""[..], -42.5)); - assert_eq!(ws_f64(b" -42.5"), Done(&b""[..], -42.5)); - assert_eq!(ws_f64(b"-42.5 "), Done(&b""[..], -42.5)); - assert_eq!(ws_f64(b" -42.5"), Done(&b""[..], -42.5)); - assert_eq!(ws_f64(b" -42.5 53"), Done(&b"53"[..], -42.5)); - } - - #[test] - fn take_decimal_real_1() { - assert_eq!(take_decimal_real(b"-42.3"), Done(&b""[..], &b"-42.3"[..])); - assert_eq!(take_decimal_real(b"42.3"), Done(&b""[..], &b"42.3"[..])); - assert_eq!(take_decimal_real(b"-42"), Done(&b""[..], &b"-42"[..])); - assert_eq!(take_decimal_real(b"+42.3"), Done(&b""[..], &b"+42.3"[..])); - } - - #[test] - fn u32_utf8_1() { - assert_eq!(u32_utf8(b"42"), Done(&b""[..], 42)); - assert_eq!(u32_utf8(b"-42").is_err(), true); - } - - #[test] - fn i32_utf8_1() { - assert_eq!(i32_utf8(b"42"), Done(&b""[..], 42)); - assert_eq!(i32_utf8(b"-42"), Done(&b""[..], -42)); - assert_eq!(i32_utf8(b"+42"), Done(&b""[..], 42)); - assert_eq!(i32_utf8(b"--42").is_err(), true); - assert_eq!(i32_utf8(b"+-42").is_err(), true); - } - - #[test] - fn u64_utf8_1() { - assert_eq!(u64_utf8(b"42"), Done(&b""[..], 42)); - assert_eq!(u64_utf8(b"-42").is_err(), true); - } - - #[test] - fn i64_utf8_1() { - assert_eq!(i64_utf8(b"42"), Done(&b""[..], 42)); - assert_eq!(i64_utf8(b"-42"), Done(&b""[..], -42)); - assert_eq!(i64_utf8(b"+42"), Done(&b""[..], 42)); - assert_eq!(i64_utf8(b"--42").is_err(), true); - assert_eq!(i64_utf8(b"+-42").is_err(), true); - } - - #[test] - fn f32_utf8_1() { - assert_eq!(f32_utf8(b"-42.3"), Done(&b""[..], -42.3)); - assert_eq!(f32_utf8(b"+42.3"), Done(&b""[..], 42.3)); - assert_eq!(f32_utf8(b"42.3"), Done(&b""[..], 42.3)); - assert_eq!(f32_utf8(b"42"), Done(&b""[..], 42.0)); - } - - #[test] - fn f64_utf8_1() { - assert_eq!(f64_utf8(b"-42.3"), Done(&b""[..], -42.3)); - assert_eq!(f64_utf8(b"+42.3"), Done(&b""[..], 42.3)); - assert_eq!(f64_utf8(b"42.3"), Done(&b""[..], 42.3)); - assert_eq!(f64_utf8(b"42"), Done(&b""[..], 42.0)); + fn ws_f32_4() { + assert_eq!(ws_f32("a1.0").is_err(), true); + assert_eq!(all_consuming(ws_f32)("0abc").is_err(), true); + assert_eq!(tuple((ws_f32, ws_f32))("0.abc 1.2").is_err(), true); } } diff --git a/src/parse/psy.rs b/src/parse/psy.rs index 7dded61..ef302ea 100644 --- a/src/parse/psy.rs +++ b/src/parse/psy.rs @@ -2,7 +2,7 @@ use std::{f32, result::Result}; -use nom::{call, closure, eof, error_position, terminated, tuple, tuple_parser, IResult}; +use nom::{combinator::all_consuming, sequence::tuple, IResult}; use mem_arena::MemArena; @@ -273,8 +273,8 @@ fn parse_render_settings(tree: &DataTree) -> Result<((u32, u32), u32, u32), PsyP contents, byte_offset, } if type_name == "Resolution" => { - if let IResult::Done(_, (w, h)) = - closure!(terminated!(tuple!(ws_u32, ws_u32), eof!()))(contents.as_bytes()) + if let IResult::Ok((_, (w, h))) = + all_consuming(tuple((ws_u32, ws_u32)))(contents) { found_res = true; res = (w, h); @@ -294,7 +294,7 @@ fn parse_render_settings(tree: &DataTree) -> Result<((u32, u32), u32, u32), PsyP contents, byte_offset, } if type_name == "SamplesPerPixel" => { - if let IResult::Done(_, n) = ws_u32(contents.as_bytes()) { + if let IResult::Ok((_, n)) = all_consuming(ws_u32)(contents) { found_spp = true; spp = n; } else { @@ -314,7 +314,7 @@ fn parse_render_settings(tree: &DataTree) -> Result<((u32, u32), u32, u32), PsyP contents, byte_offset, } if type_name == "Seed" => { - if let IResult::Done(_, n) = ws_u32(contents.as_bytes()) { + if let IResult::Ok((_, n)) = all_consuming(ws_u32)(contents) { seed = n; } else { // Found Seed, but its contents is not in the right format @@ -366,7 +366,7 @@ fn parse_camera<'a>(arena: &'a MemArena, tree: &'a DataTree) -> Result { - if let IResult::Done(_, fov) = ws_f32(contents.as_bytes()) { + if let IResult::Ok((_, fov)) = all_consuming(ws_f32)(contents) { fovs.push(fov * (f32::consts::PI / 180.0)); } else { // Found Fov, but its contents is not in the right format @@ -385,7 +385,7 @@ fn parse_camera<'a>(arena: &'a MemArena, tree: &'a DataTree) -> Result { - if let IResult::Done(_, fd) = ws_f32(contents.as_bytes()) { + if let IResult::Ok((_, fd)) = all_consuming(ws_f32)(contents) { focus_distances.push(fd); } else { // Found FocalDistance, but its contents is not in the right format @@ -404,7 +404,7 @@ fn parse_camera<'a>(arena: &'a MemArena, tree: &'a DataTree) -> Result { - if let IResult::Done(_, ar) = ws_f32(contents.as_bytes()) { + if let IResult::Ok((_, ar)) = all_consuming(ws_f32)(contents) { aperture_radii.push(ar); } else { // Found ApertureRadius, but its contents is not in the right format @@ -554,21 +554,20 @@ fn parse_world<'a>(arena: &'a MemArena, tree: &'a DataTree) -> Result, } pub fn parse_matrix(contents: &str) -> Result { - if let IResult::Done(_, ns) = closure!(terminated!( - tuple!( - ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, - ws_f32, ws_f32, ws_f32, ws_f32, ws_f32 - ), - eof!() - ))(contents.as_bytes()) + if let IResult::Ok((leftover, ns)) = all_consuming(tuple(( + ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, + ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, + )))(contents) { - return Ok(Matrix4x4::new_from_values( - ns.0, ns.4, ns.8, ns.12, ns.1, ns.5, ns.9, ns.13, ns.2, ns.6, ns.10, ns.14, ns.3, ns.7, - ns.11, ns.15, - )); - } else { - return Err(PsyParseError::UnknownError(0)); + if leftover.is_empty() { + return Ok(Matrix4x4::new_from_values( + ns.0, ns.4, ns.8, ns.12, ns.1, ns.5, ns.9, ns.13, ns.2, ns.6, ns.10, ns.14, ns.3, + ns.7, ns.11, ns.15, + )); + } } + + return Err(PsyParseError::UnknownError(0)); } pub fn make_transform_format_error(byte_offset: usize) -> PsyParseError { @@ -587,9 +586,7 @@ pub fn parse_color(contents: &str) -> Result { match items[0] { "rec709" => { - if let IResult::Done(_, color) = - closure!(tuple!(ws_f32, ws_f32, ws_f32))(items[1].as_bytes()) - { + if let IResult::Ok((_, color)) = tuple((ws_f32, ws_f32, ws_f32))(items[1]) { return Ok(Color::new_xyz(rec709_e_to_xyz(color))); } else { return Err(PsyParseError::UnknownError(0)); @@ -597,9 +594,7 @@ pub fn parse_color(contents: &str) -> Result { } "blackbody" => { - if let IResult::Done(_, (temperature, factor)) = - closure!(tuple!(ws_f32, ws_f32))(items[1].as_bytes()) - { + if let IResult::Ok((_, (temperature, factor))) = tuple((ws_f32, ws_f32))(items[1]) { return Ok(Color::new_blackbody(temperature, factor)); } else { return Err(PsyParseError::UnknownError(0)); @@ -607,9 +602,7 @@ pub fn parse_color(contents: &str) -> Result { } "color_temperature" => { - if let IResult::Done(_, (temperature, factor)) = - closure!(tuple!(ws_f32, ws_f32))(items[1].as_bytes()) - { + if let IResult::Ok((_, (temperature, factor))) = tuple((ws_f32, ws_f32))(items[1]) { return Ok(Color::new_temperature(temperature, factor)); } else { return Err(PsyParseError::UnknownError(0)); diff --git a/src/parse/psy_light.rs b/src/parse/psy_light.rs index 22c0386..18fe878 100644 --- a/src/parse/psy_light.rs +++ b/src/parse/psy_light.rs @@ -2,7 +2,7 @@ use std::result::Result; -use nom::{call, closure, tuple, tuple_parser, IResult}; +use nom::{combinator::all_consuming, sequence::tuple, IResult}; use mem_arena::MemArena; @@ -35,7 +35,7 @@ pub fn parse_distant_disk_light<'a>( contents, byte_offset, } if type_name == "Radius" => { - if let IResult::Done(_, radius) = ws_f32(contents.as_bytes()) { + if let IResult::Ok((_, radius)) = all_consuming(ws_f32)(contents) { radii.push(radius); } else { // Found radius, but its contents is not in the right format @@ -49,8 +49,8 @@ pub fn parse_distant_disk_light<'a>( contents, byte_offset, } if type_name == "Direction" => { - if let IResult::Done(_, direction) = - closure!(tuple!(ws_f32, ws_f32, ws_f32))(contents.as_bytes()) + if let IResult::Ok((_, direction)) = + all_consuming(tuple((ws_f32, ws_f32, ws_f32)))(contents) { directions.push(Vector::new(direction.0, direction.1, direction.2)); } else { @@ -100,7 +100,7 @@ pub fn parse_sphere_light<'a>( contents, byte_offset, } if type_name == "Radius" => { - if let IResult::Done(_, radius) = ws_f32(contents.as_bytes()) { + if let IResult::Ok((_, radius)) = all_consuming(ws_f32)(contents) { radii.push(radius); } else { // Found radius, but its contents is not in the right format @@ -149,8 +149,8 @@ pub fn parse_rectangle_light<'a>( contents, byte_offset, } if type_name == "Dimensions" => { - if let IResult::Done(_, radius) = - closure!(tuple!(ws_f32, ws_f32))(contents.as_bytes()) + if let IResult::Ok((_, radius)) = + all_consuming(tuple((ws_f32, ws_f32)))(contents) { dimensions.push(radius); } else { diff --git a/src/parse/psy_mesh_surface.rs b/src/parse/psy_mesh_surface.rs index d1388bc..ccc87e8 100644 --- a/src/parse/psy_mesh_surface.rs +++ b/src/parse/psy_mesh_surface.rs @@ -2,7 +2,7 @@ use std::result::Result; -use nom::{call, closure, tuple, tuple_parser, IResult}; +use nom::{sequence::tuple, IResult}; use mem_arena::MemArena; @@ -37,15 +37,11 @@ pub fn parse_mesh_surface<'a>( // and other validation. // Get verts - for (_, text, _) in tree.iter_leaf_children_with_type("Vertices") { - let mut raw_text = text.trim().as_bytes(); - + for (_, mut text, _) in tree.iter_leaf_children_with_type("Vertices") { // Collect verts for this time sample let mut tverts = Vec::new(); - while let IResult::Done(remaining, vert) = - closure!(tuple!(ws_f32, ws_f32, ws_f32))(raw_text) - { - raw_text = remaining; + while let IResult::Ok((remaining, vert)) = tuple((ws_f32, ws_f32, ws_f32))(text) { + text = remaining; tverts.push(Point::new(vert.0, vert.1, vert.2)); } @@ -59,14 +55,11 @@ pub fn parse_mesh_surface<'a>( } // Get normals, if they exist - for (_, text, _) in tree.iter_leaf_children_with_type("Normals") { - let mut raw_text = text.trim().as_bytes(); - - // Collect verts for this time sample + for (_, mut text, _) in tree.iter_leaf_children_with_type("Normals") { + // Collect normals for this time sample let mut tnormals = Vec::new(); - while let IResult::Done(remaining, nor) = closure!(tuple!(ws_f32, ws_f32, ws_f32))(raw_text) - { - raw_text = remaining; + while let IResult::Ok((remaining, nor)) = tuple((ws_f32, ws_f32, ws_f32))(text) { + text = remaining; tnormals.push(Normal::new(nor.0, nor.1, nor.2).normalized()); } @@ -82,22 +75,18 @@ pub fn parse_mesh_surface<'a>( } // Get face vert counts - if let Some((_, text, _)) = tree.iter_leaf_children_with_type("FaceVertCounts").nth(0) { - let mut raw_text = text.trim().as_bytes(); - - while let IResult::Done(remaining, count) = ws_usize(raw_text) { - raw_text = remaining; + if let Some((_, mut text, _)) = tree.iter_leaf_children_with_type("FaceVertCounts").nth(0) { + while let IResult::Ok((remaining, count)) = ws_usize(text) { + text = remaining; face_vert_counts.push(count); } } // Get face vert indices - if let Some((_, text, _)) = tree.iter_leaf_children_with_type("FaceVertIndices").nth(0) { - let mut raw_text = text.trim().as_bytes(); - - while let IResult::Done(remaining, index) = ws_usize(raw_text) { - raw_text = remaining; + if let Some((_, mut text, _)) = tree.iter_leaf_children_with_type("FaceVertIndices").nth(0) { + while let IResult::Ok((remaining, index)) = ws_usize(text) { + text = remaining; face_vert_indices.push(index); } diff --git a/src/parse/psy_surface_shader.rs b/src/parse/psy_surface_shader.rs index c86d370..7f596ba 100644 --- a/src/parse/psy_surface_shader.rs +++ b/src/parse/psy_surface_shader.rs @@ -2,7 +2,7 @@ use std::result::Result; -use nom::IResult; +use nom::{combinator::all_consuming, IResult}; use mem_arena::MemArena; @@ -77,7 +77,7 @@ pub fn parse_surface_shader<'a>( let roughness = if let Some((_, contents, byte_offset)) = tree.iter_leaf_children_with_type("Roughness").nth(0) { - if let IResult::Done(_, roughness) = ws_f32(contents.as_bytes()) { + if let IResult::Ok((_, roughness)) = all_consuming(ws_f32)(contents) { roughness } else { return Err(PsyParseError::UnknownError(byte_offset)); @@ -93,7 +93,7 @@ pub fn parse_surface_shader<'a>( let fresnel = if let Some((_, contents, byte_offset)) = tree.iter_leaf_children_with_type("Fresnel").nth(0) { - if let IResult::Done(_, fresnel) = ws_f32(contents.as_bytes()) { + if let IResult::Ok((_, fresnel)) = all_consuming(ws_f32)(contents) { fresnel } else { return Err(PsyParseError::UnknownError(byte_offset));