diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/nom | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/nom')
58 files changed, 24273 insertions, 0 deletions
diff --git a/third_party/rust/nom/.cargo-checksum.json b/third_party/rust/nom/.cargo-checksum.json new file mode 100644 index 0000000000..b236aa3bb5 --- /dev/null +++ b/third_party/rust/nom/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{".travis.yml":"124c5613de02fe2088a7410a7057b0e0ed49dcbc8509704b690de98e83b289e0","CHANGELOG.md":"f4015ae48bb8f7c672c4fdf1ca4bdde5910f1b423ddc12e9fe80395911759d3e","Cargo.lock":"d7985e783bf7275009ab524f13aac37adc7291838a1956e1f092413ff3b8ea04","Cargo.toml":"72afa6607971579c7009dff34b6b334d4893b1a9f1983cd80364c5e318e4ec2b","LICENSE":"4dbda04344456f09a7a588140455413a9ac59b6b26a1ef7cdf9c800c012d87f0","build.rs":"fd66799ca3bd6a83b10f18a62e6ffc3b1ac94074fe65de4e4c1c447bf71d6ebb","src/bits/complete.rs":"8a60ae4cd6aaf32cb232b598b1a3dda7858c619944ba12ebdb01a31c75243293","src/bits/macros.rs":"8d9ba23f237b4fc01e3b2106002d0f6d59930a42f34d80662c61e0057dfb4a5b","src/bits/mod.rs":"4ca0148b4ef2de4e88796da7831eaa5c4fcbc5515a101eae1b0bc4853c80b5e7","src/bits/streaming.rs":"7c587808476edee57caeccca7dccd744bdfdbb13ae1156400fb4961980fa325d","src/branch/macros.rs":"8b885185725c16369d90954fed8450070bcd4bf8ae7de1df1bb46bb378450d71","src/branch/mod.rs":"d0c871ad7b74428ddccef445a10254249c3907528005a00275e8eb6516255f2f","src/bytes/complete.rs":"107f776885161e48a596962925d2b1628f20fd4bbe5b3777bb34ec175b97e280","src/bytes/macros.rs":"914a821761bbf49f04425068b7426bdd60394e8cc30e7c2193160b16e06c266f","src/bytes/mod.rs":"577231e6e6bd51726a877a73292d8f1c626f6b32ebe4a57943acaed8a8a2975d","src/bytes/streaming.rs":"4b2e577e6057fda932d1edc2ebe53c5df71a21417a304b35d5230dd7221411f3","src/character/complete.rs":"bb80656f3405eca79ba93c674cae7cd296733d784928e5c45754ee27963b7325","src/character/macros.rs":"f330ab60d469802b1664dcbbccd4bfe3e1ca87b348e92609ef34e25b4d475983","src/character/mod.rs":"9f520d535a88849726eac8648aa5c8b86193ab2f3a984d63c8371b846cc0b72c","src/character/streaming.rs":"9be7051951e8d0a54e294542baaf115aeb6efb818521f045386cd3a1777ca6a0","src/combinator/macros.rs":"df9ba1157bda21313a9c23826baeefd99f54e92c47d60c8dbb25697fe4d52686","src/combinator/mod.rs":"d1b2073683be1c9c4a06d1d3764ac789027ad7401ec726805d1505c1ad8ab1fd","src/error.rs":"ef7feb06b9689aa2f4b11a367b6f7058df8fd151b673c7482edd6600e55e38da","src/internal.rs":"c4029b0e32d41eb6407e517f3102a41a3a80a6a69df4767008ac5656b28d7ab0","src/lib.rs":"f01cdc23cc17201f978796d2d80fb6bba5a9b81ffb4653286e1e53f904079911","src/methods.rs":"56099c30123e92f9f6bacb16017f29fcdbc6546afbf0f80cf4951d2d8093ba83","src/multi/macros.rs":"01d15ae913921bd1ed0ff579a868ea39a314a866e6b5a365ef6b448a75f9b3a8","src/multi/mod.rs":"342c30e0c601558c867df215f7094bc9e43534a7793f2e8b19c114fe07cfea41","src/number/complete.rs":"560dfb2ffbbfe7fe276389b60eec2d717fec20eab68cc72d10d87ff6e2195352","src/number/macros.rs":"e614ee142658c126902a466f29ef250a77016fa126b8dfd59db0c6657a0ef205","src/number/mod.rs":"e432c317ee839a2f928cd0e8e583acadb165ed84fb649e681688a0fcd84b0a75","src/number/streaming.rs":"4c6fbce64e0d535f69d2c928314e1d0a019480db5a09e4f47a654a2e8fd56e8c","src/regexp.rs":"ac6fc61c2e7b629e6786638b44d63d15139c50f9d6a38acd044e4c2b3a699e08","src/sequence/macros.rs":"0e72871cdb2f1bf804f7f637def117c8e531f78cc7da5a6a0e343f0dbfb04271","src/sequence/mod.rs":"ec34b969462703252c4f00c27a03928c9580da612c71b7100e0902190a633ab9","src/str.rs":"fcae4d6f2f7bc921cafe3d0ce682d0566618cbe5f3b3e4b51ca34d11cb0e3e93","src/traits.rs":"2a84c3aa40f1cf78e0149e344da8b85486f7b6c0ddff8f23689bccce0def2260","src/util.rs":"bcedca3c88ac24f11f73e836efd8fe00014562163cc3d43d0cec9d726a4687c3","src/whitespace.rs":"53bddd9d559dc7793effcb828f9c196a7f098338edb0877940d1246834761308","tests/arithmetic.rs":"c57bc547110e498e7bddc973a94202f22356bc525fed55dac4f03daf16eb54f7","tests/arithmetic_ast.rs":"8fbc4c5d8850fa1cf0a16f97e8718d5427b2657f12ca4a0b3b6c1b47fd9e67d4","tests/blockbuf-arithmetic.rs":"099fdf75da97ae032006d8c82ea2207265c5161a08d1370e1ddddb01e73afaf4","tests/css.rs":"b13466eb6a0831f98ede83ffdd752ba821f7d03db724fd92b5bfbc0b9f804a65","tests/custom_errors.rs":"3d2511f8a8d0eb20d9efc19f29ae4ab34389bdd33214a421989d0c47b540b7fd","tests/escaped.rs":"03ecb10828472e4de2ace05a12cb49997b47a113b6a3b0eea3d56bc2bafd8446","tests/float.rs":"92947cc112a6b865f5e19d80edbf300ddc0d0ca4b4e4543eda15337b5c60eedf","tests/inference.rs":"fe476d1367fce9f0baf82295dc037c79321ededf12b9bcc0c5acdc7cefff4720","tests/ini.rs":"04ebf3ead0008974b3bedc387e889bab5942efd0c9c8563fe47e056a9b90bbab","tests/ini_str.rs":"2831a4ee26b37734dba8862cc2972a3a1433abf4fcab253b6cf4fb43f120301d","tests/issues.rs":"142c8d206089b04cf2fd0cbd90f87421ded435ce300516c029294b056354e00f","tests/json.rs":"25476ec2daca19295f5f99b621eecc859a5db5789ac35be381eaf8703a70bce8","tests/mp4.rs":"d0e61bfc93ff40676ca7e9d7813a5ad7c73b1db874599d8f3ea784115bfcab87","tests/multiline.rs":"6a5321cb53c7f88778fa100499533abfa602bada7a6b1d0fbba7ef77b9c110f5","tests/named_args.rs":"bd8095c3abc6fb806c9181c6025c0111d1e7f3b7269ea89ae122bf3bb8ed7e7d","tests/overflow.rs":"d1d6d8ce9b34ed47b42a5f7250ce711805a397691dc6cad3cc8945ec230da161","tests/reborrow_fold.rs":"9328deafc2143c2a2d1a0be86e2448b644cffcb5f0935c8b24eb469f1f9477c0","tests/test1.rs":"06fc9e52638f16bfc3ef69cd26b927e0cf55706d6f132ab7c0f1072208475853"},"package":"0b471253da97532da4b61552249c521e01e736071f71c1a4f7ebbfbf0a06aad6"}
\ No newline at end of file diff --git a/third_party/rust/nom/.travis.yml b/third_party/rust/nom/.travis.yml new file mode 100644 index 0000000000..e18b14079d --- /dev/null +++ b/third_party/rust/nom/.travis.yml @@ -0,0 +1,101 @@ +language: rust +# sudo is required to enable kcov to use the personality syscall +sudo: required +dist: trusty +cache: cargo + +rust: + - nightly + - beta + - stable + - 1.31.0 + +env: + matrix: + - FEATURES='--features "regexp regexp_macros"' + +before_script: + - eval git pull --rebase https://github.com/Geal/nom master + - eval git log --pretty=oneline HEAD~5..HEAD + +matrix: + include: + - rust: nightly + env: FEATURES='--no-default-features' + - rust: nightly + env: FEATURES='--no-default-features --features "alloc"' + - rust: stable + env: FEATURES='' + - rust: nightly + env: DOC_FEATURES='--features "std lexical regexp regexp_macros" --no-default-features' + before_script: + - export PATH=$HOME/.cargo/bin:$PATH + script: + - eval cargo doc --verbose $DOC_FEATURES + - rust: nightly + env: FEATURES='' + before_script: + - export PATH=$HOME/.cargo/bin:$PATH + - cargo install cargo-update || echo "cargo-update already installed" + - cargo install cargo-travis || echo "cargo-travis already installed" + - cargo install-update -a + - mkdir -p target/kcov-master + script: + cargo coveralls --verbose --all-features + allow_failures: + - rust: stable + env: FEATURES='' + before_script: + - export PATH=$HOME/.cargo/bin:$PATH + - rustup component add rustfmt-preview + script: + - eval cargo fmt -- --write-mode=diff + +notifications: + webhooks: + urls: + - https://webhooks.gitter.im/e/9c035a194ac4fd4cc061 + on_success: change + on_failure: always + on_start: false + + +addons: + apt: + packages: + - libcurl4-openssl-dev + - libelf-dev + - libdw-dev + - binutils-dev + - cmake + sources: + - kalakris-cmake + +cache: + directories: + - /home/travis/.cargo + +before_cache: + - rm -rf /home/travis/.cargo/registry + +script: + - eval cargo build --verbose $FEATURES + - eval cargo test --verbose $FEATURES + +after_success: | + case "$TRAVIS_RUST_VERSION" in + nightly) + if [ "${TRAVIS_PULL_REQUEST_BRANCH:-$TRAVIS_BRANCH}" != "master" ]; then + git fetch && + git checkout master && + cargo bench --verbose + fi + + if [ "$FEATURES" == '--features "regexp regexp_macros"' ]; then + cargo bench --verbose + fi + ;; + + *) + ;; + esac diff --git a/third_party/rust/nom/CHANGELOG.md b/third_party/rust/nom/CHANGELOG.md new file mode 100644 index 0000000000..33d2a782d0 --- /dev/null +++ b/third_party/rust/nom/CHANGELOG.md @@ -0,0 +1,1170 @@ +# Change Log + +## [Unreleased][unreleased] + +### Thanks + +### Added + +### Fixed + +## 5.1.1 - 2020-02-24 + +### Thanks + +- @Alexhuszagh for float fixes +- @AlexanderEkdahl, @JoshOrndorff, @akitsu-sanae for docs fixes +- @ignatenkobrain: dependency update +- @derekdreery: `map` implementation for errors +- @Lucretiel for docs fixes and compilation fixes +- adytzu2007: warning fixes +- @lo48576: error management fixes + +### Fixed + +- C symbols compilation errors due to old lexical-core version + +### Added + +- `Err` now has a `map` function + +### Changed + +- Make `error::context()` available without `alloc` feature + +## 5.1.0 - 2020-01-07 + +### Thanks + +- @Hywan, @nickmooney, @jplatte, @ngortheone, @ejmg, @SirWindfield, @demurgos, @spazm, @nyarly, @guedou, @adamnemecek, for docs fixes +- @Alxandr for error management bugfixes +- @Lucretiel for example fixes and optimizations +- @adytzu2007 for optimizations +- @audunhalland for utf8 fixes + +### Fixed + +- panic in `convert_error` +- `compile_error` macro usage + +### Added + +- `std::error::Error`, `std::fmt::Display`, `Eq`, `ToOwned` implementations for errors +- inline attribute for `ToUsize` + +### Changed + +- `convert_error` optimization +- `alt` optimization + +## 5.0.1 - 2020-08-22 + +### Thanks + +- @waywardmonkeys, @phaazon, @dalance for docs fixes +- @kali for `many0_m_n` fixes +- @ia0 for macros fixes + +### Fixed + +- `many0_m_n` now supports the n=1 case +- relaxed trait requirements in `cut` +- `peek!` macro reimplementation +- type inference in `value!` + +## 5.0.0 - 2019-06-24 + +This version comes with a complete rewrite of nom internals to use functions as a base +for parsers, instead of macros. Macros have been updated to use functions under +the hood, so that most existing parsers will work directly or require minimal changes. + +The `CompleteByteSlice` and `CompleteStr` input types were removed. To get different +behaviour related to streaming or complete input, there are different versions of some +parsers in different submodules, like `nom::character::streaming::alpha0` and +`nom::character::complete::alpha0`. + +The `verbose-errors` feature is gone, now the error type is decided through a generic +bound. To get equivalent behaviour to `verbose-errors`, check out `nom::error::VerboseError` + +### Thanks + +- @lowenheim helped in refactoring and error management +- @Keruspe helped in refactoring and fixing tests +- @pingiun, @Songbird0, @jeremystucki, @BeatButton, @NamsooCho, @Waelwindows, @rbtcollins, @MarkMcCaskey for a lot of help in rewriting the documentation and adding code examples +- @GuillaumeGomez for documentation rewriting and checking +- @iosmanthus for bug fixes +- @lo48576 for error management fixes +- @vaffeine for macros visibility fixes +- @webholik and @Havvy for `escaped` and `escaped_transform` fixes +- @proman21 for help on porting bits parsers + +### Added + +- the `VerboseError` type accumulates position info and error codes, and can generate a trace with span information +- the `lexical-core` crate is now used by default (through the `lexical` compilation feature) to parse floats from text +- documentation and code examples for all functions and macros + +### Changed + +- nom now uses functions instead of macros to generate parsers +- macros now use the functions under the hood +- the minimal Rust version is now 1.31 +- the verify combinator's condition function now takes its argument by reference +- `cond` will now return the error of the parser instead of None +- `alpha*`, `digit*`, `hex_digit*`, `alphanumeric*` now recognize only ASCII characters + +### Removed + +- deprecated string parsers (with the `_s` suffix), the normal version can be used instead +- `verbose-errors` is not needed anymore, now the error type can be decided when writing the parsers, and parsers provided by nom are generic over the error type +- `AtEof`, `CompleteByteSlice` and `CompleteStr` are gone, instead some parsers are specialized to work on streaming or complete input, and provided in different modules +- character parsers that were aliases to their `*1` version: eol, alpha, digit, hex_digit, oct_digit, alphanumeric, space, multispace +- `count_fixed` macro +- `whitespace::sp` can be replaced by `character::complete::multispace0` +- method combinators are now in the nom-methods crate +- `take_until_either`, `take_until_either1`, `take_until_either_and_consume` and `take_until_either_and_consume1`: they can be replaced with `is_not` (possibly combined with something else) +- `take_until_and_consume`, `take_until_and_consume1`: they can be replaced with `take_until` combined with `take` +- `sized_buffer` and `length_bytes!`: they can be replaced with the `length_data` function +- `non_empty`, `begin` and `rest_s` function +- `cond_reduce!`, `cond_with_error!`, `closure!`, `apply`, `map_res_err!`, `expr_opt!`, `expr_res!` +- `alt_complete`, `separated_list_complete`, `separated_nonempty_list_complete` + +## 4.2.3 - 2019-03-23 + +### Fixed + +- add missing `build.rs` file to the package +- fix code comparison links in changelog + +## 4.2.2 - 2019-03-04 + +### Fixed + +- regression in do_parse macro import for edition 2018 + +## 4.2.1 - 2019-02-27 + +### Fixed + +- macro expansion error in `do_parse` due to `compile_error` macro usage + +## 4.2.0 - 2019-01-29 + +### Thanks + +- @JoshMcguigan for unit test fixes +- @oza for documentation fixes +- @wackywendell for better error conversion +- @Zebradil for documentation fixes +- @tsraom for new combinators +- @hcpl for minimum Rust version tests +- @KellerFuchs for removing some unsafe uses in float parsing + +### Changed + +- macro import in edition 2018 code should work without importing internal macros now +- the regex parsers do not require the calling code to have imported the regex crate anymore +- error conversions are more ergonomic +- method combinators are now deprecated. They might be moved to a separate crate +- nom now specifies Rust 1.24.1 as minimum version. This was already the case before, now it is made explicit + +### Added + +- `many0_count` and `many1_count` to count applications of a parser instead of +accumulating its results in a `Vec` + +### Fixed + +- overflow in the byte wrapper for bit level parsers +- `f64` parsing does not use `transmute` anymore + +## 4.1.1 - 2018-10-14 + +### Fixed + +- compilation issue in verbose-errors mode for `add_return_error` + +## 4.1.0 - 2018-10-06 + +### Thanks + +- @xfix for fixing warnings, simplifying examples and performance fixes +- @dvberkel for documentation fixes +- @chifflier for fixing warnings +- @myrrlyn for dead code elimination +- @petrochenkov for removing redundant test macros +- @tbelaire for documentation fixes +- @khernyo for fixing warnings +- @linkmauve for documentation fixes +- @ProgVal for documentation fixes, warning fixes and error management +- @Nemo157 for compilation fixes +- @RReverser for documentation fixes +- @xpayn for fixing warnings +- Blas Rodriguez Irizar for documentation fixes +- @badboy for documentation fixes +- @kyrias for compilation fixes +- @kurnevsky for the `rest_len` parser +- @hjr3 for new documentation examples +- @fengalin for error management +- @ithinuel for the pcap example project +- @phaazon for documentation fixes +- @juchiast for documentation fixes +- @jrakow for the `u128` and `i128` parsers +- @smarnach for documentation fixes +- @derekdreery for `pub(crate)` support +- @YaLTeR for `map_res_err!` + +### Added + +- `rest_len` parser, returns the length of the remaining input +- `parse_to` has its own error code now +- `u128` and `i128` parsers in big and little endian modes +- support for `pub(crate)` syntax +- `map_res_err!` combinator that appends the error of its argument function in verbose errors mode + +### Fixed + +- lots of unused imports warnings were removed +- the `bytes` combinator was not compiling in some cases +- the big and little endian combinators now work without external imports +- CI is now faster and uses less cache +- in `add_return_error`, the provided error code is now evaluated only once + +### Changed + +- `fold_many1` will now transmit a `Failure` instead of transforming it to an `Error` +- `float` and `double` now work on all of nom's input types (`&[u8]`, `&str`, `CompleteByteSlice`, `CompleteStr` and any type that implements the required traits). `float_s` and `double_s` got the same modification, but are now deprecated +- `CompleteByteSlice` and `CompleteStr` get a small optimization by inlining some functions + + +## 4.0.0 - 2018-05-14 + +### Thanks + +- @jsgf for the new `AtEof` trait +- @tmccombs for fixes on `escaped*` combinators +- @s3bk for fixes around non Copy input types and documentation help +- @kamarkiewicz for fixes to no_std and CI +- @bheisler for documentation and examples +- @target-san for simplifying the `InputIter` trait for `&[u8]` +- @willmurphyscode for documentation and examples +- @Chaitanya1416 for typo fixes +- @fflorent for `input_len()` usage fixes +- @dbrgn for typo fixes +- @iBelieve for no_std fixes +- @kpp for warning fixes and clippy fixes +- @keruspe for fixes on FindToken +- @dtrebbien for fixes on take_until_and_consume1 +- @Henning-K for typo fixes +- @vthriller for documentation fixes +- @federicomenaquintero and @veprbl for their help fixing the float parsers +- @vmchale for new named_args versions +- @hywan for documentation fixes +- @fbenkstein for typo fixes +- @CAD97 for catching missing trait implementations +- @goldenlentils for &str optimizations +- @passy for typo fixes +- @ayrat555 for typo fixes +- @GuillaumeGomez for documentation fixes +- @jrakow for documentation fixes and fixes for `switch!` +- @phlosioneer for documentation fixes +- @creativcoder for typo fixes +- @derekdreery for typo fixes +- @lucasem for implementing `Deref` on `CompleteStr` and `CompleteByteSlice` +- @lowenheim for `parse_to!` fixes +- @myrrlyn for trait fixes around `CompleteStr` and `CompleteByteSlice` +- @NotBad4U for fixing code coverage analysis +- @murarth for code formatting +- @glandium for fixing build in no_std +- @csharad for regex compatibility with `CompleteStr` +- @FauxFaux for implementing `AsRef<str>` on `CompleteStr` +- @jaje for implementing `std::Error` on `nom:Err` +- @fengalin for warning fixes +- @@khernyo for doc formatting + +Special thanks to @corkami for the logo :) + +### Breaking changes + +- the `IResult` type now becomes a `Result` from the standard library +- `Incomplete` now returns the additional data size needed, not the total data size needed +- verbose-errors is now a superset of basic errors +- all the errors now include the related input slice +- the arguments from `error_position` and other such macros were swapped to be more consistent with the rest of nom +- automatic error conversion: to fix error type inference issues, a custom error type must now implement `std::convert::From<u32>` +- the `not!` combinator returns unit `()` +- FindToken's calling convention was swapped +- the `take_*` combinators are now more coherent and stricter, see commit 484f6724ea3ccb for more information +- `many0` and other related parsers will now return `Incomplete` if the reach the end of input without an error of the child parser. They will also return `Incomplete` on an empty input +- the `sep!` combinator for whitespace only consumes whitespace in the prefix, while the `ws!` combinator takes care of consuming the remaining whitespace + +### Added + +- the `AtEof` trait for input type: indicate if we can get more input data later (related to streaming parsers and `Incomplete` handling) +- the `escaped*` parsers now support the `&str`input type +- the `Failure` error variant represents an unrecoverable error, for which `alt` and other combinators will not try other branches. This error means we got in the right part of the code (like, a prefix was checked correctly), but there was an error in the following parts +- the `CompleteByteSlice` and `CompleteStr` input types consider there will be no more refill of the input. They fixed the `Incomplete` related issues when we have all of the data +- the `exact!()` combinator will fail if we did not consume the whole input +- the `take_while_m_n!` combinator will match a specified number of characters +- `ErrorKind::TakeUntilAndConsume1` +- the `recognize_float` parser will match a float number's characters, but will not transform to a `f32` or `f64` +- `alpha` and other basic parsers are now much stricter about partial inputs. We also introduce the `*0` and `*1` versions of those parsers +- `named_args` can now specify the input type as well +- `HexDisplay` is now implemented for `&str` +- `alloc` feature +- the `InputTakeAtposition` trait allows specialized implementations of parsers like `take_while!` + +### Removed + +- the producers and consumers were removed +- the `error_code` and `error_node` macros are not used anymore + +### Fixed + +- `anychar!` now works correctly with multibyte characters +- `take_until_and_consume1!` no longer results in "no method named \`find_substring\`" and "no method named \`slice\`" compilation errors +- `take_until_and_consume1!` returns the correct Incomplete(Needed) amount +- `no_std` compiles properly, and nom can work with `alloc` too +- `parse_to!` now consumes its input + +### Changed + +- `alt` and other combinators will now clone the input if necessary. If the input is already `Copy` there is no performance impact +- the `rest` parser now works on various input types +- `InputIter::Item` for `&[u8]` is now a `u8` directly, not a reference +- we now use the `compile_error` macro to return a compile time error if there was a syntax issue +- the permutation combinator now supports optional child parsers +- the float numbers parsers have been refactored to use one common implementation that is nearly 2 times faster than the previous one +- the float number parsers now accept more variants + + +## 3.2.1 - 2017-10-27 + +### Thanks + +- @ordian for `alt_complete` fixes +- @friedm for documentation fixes +- @kali for improving error management + +### Fixed + +- there were cases where `alt_complete` could return `Incomplete` + +### Added + +- an `into_error_kind` method can be used to transform any error to a common value. This helps when the library is included multiple times as dependency with different feature sets + + +## 3.2.0 - 2017-07-24 + +### Thanks + +- @jedireza for documentation fixes +- @gmorenz for the `bytes` combinator +- @meh for character combinator fixes for UTF-8 +- @jethrogb for avoiding move issues in `separated_list` + +### Changed + +- new layout for the main page of documentation +- `anychar` can now work on any input type +- `length_bytes` is now an alias for `length_data` + +### Fixed + +- `one_of`, `none_of` and `char` will now index correctly UTF-8 characters +- the `compiler_error` macro is now correctly exported + + +### Added + +- the `bytes` combinator transforms a bit stream back to a byte slice for child parsers + +## 3.1.0 - 2017-06-16 + +### Thanks + +- @sdroege: implementing be_i24 and le_i24 +- @Hywan: integrating faster substring search using memchr +- @nizox: fixing type issues in bit stream parsing +- @grissiom: documentation fixes +- @doomrobo: implementing separated_list_complete and separated_nonempty_list_complete +- @CWood1: fixing memchr integration in no_std +- @lu_zero: integrating the compiler_error crate +- @dtolnay: helping debug a type inference issue in map + +### Changed + +- memchr is used for substring search if possible +- if building on nightly, some common syntax errors will display a specific error message. If building no stable, display the documentation to activate those messages +- `count` no longer preallocates its vector + +### Fixed + +- better type inference in alt_complete +- `alt` should now work with whitespace parsing +- `map` should not make type inference errors anymore + +### Added + +- be_i24 and le_i24, parsing big endian and little endian signed 24 bit integers +- `separated_list_complete` and `separated_nonempty_list_complete` will treat incomplete from sub parsers as error + +## 3.0.0 - 2017-05-12 + +### Thanks + +- Chris Pick for some `Incomplete` related refactors +- @dbrgn for documentation fixes +- @valarauca for adding `be_u24` +- @ithinuel for usability fixes +- @evuez for README readability fixes and improvements to `IResult` +- @s3bk for allowing non-`Copy` types as input +- @keruspe for documentation fixes +- @0xd34d10cc for trait fixes on `InputIter` +- @sdleffler for lifetime shenanigans on `named_args` +- @chengsun for type inference fixes in `alt` +- @iBelieve for adding str to no_std +- @Hywan for simplifying code in input traits +- @azerupi for extensive documentation of `alt` and `alt_complete` + +### Breaking Changes + +- `escaped`, `separated_list` and `separated_nonempty_list` can now return `Incomplete` when necessary +- `InputIter` does not require `AsChar` on its `Item` type anymore +- the `core` feature that was putting nom in `no_std` mode has been removed. There is now a `std` feature, activated by default. If it is not activated, nom is in `no_std` +- in `verbose-errors` mode, the error list is now stored in a `Vec` instead of a box based linked list +- `chain!` has finally been removed + +### Changed + +- `Endianness` now implements `Debug`, `PartialEq`, `Eq`, `Clone` and `Copy` +- custom input types can now be cloned if they're not `Copy` +- the infamous 'Cannot infer type for E' error should happen less often now +- `str` is now available in `no_std` mode + +### Fixed + +- `FileProducer` will be marked as `Eof` on full buffer +- `named_args!` now has lifetimes that cannot conflict with the lifetimes from other arguments + +### Added + +- `be_u24`: big endian 24 bit unsigned integer parsing +- `IResult` now has a `unwrap_or` method + + +## 2.2.1 - 2017-04-03 + +### Thanks + +- @Victor-Savu for formatting fixes in the README +- @chifflier for detecting and fixing integer overflows +- @utkarshkukreti for some performance improvements in benchmarks + +### Changed + +- when calculating how much data is needed in `IResult::Incomplete`, the addition could overflow (it is stored as a usize). This would apparently not result in any security vulnerability on release code + +## 2.2.0 - 2017-03-20 + +### Thanks + +- @seppo0010 for fixing `named_args` +- @keruspe for implementing or() on `IResult`, adding the option of default cases in `switch!`, adding support for `cargo-travis` +- @timlyo for documentation fixes +- @JayKickliter for extending `hex_u32` +- @1011X for fixing regex integration +- @Kerollmops for actually marking `chain!` as deprecated +- @joliss for documentation fixes +- @utkarshkukreti for tests refactoring and performance improvement +- @tmccombs for documentation fixes + +### Added + +- `IResult` gets an `or()` method +- `take_until1`, `take_until_and_consume1`, `take_till1!` and `take_till1_s!` require at least 1 character + +### Changed + +- `hex_u32` accepts uppercase digits as well +- the character based combinators leverage the input traits +- the whitespace parsers now work on &str and other types +- `take_while1` returns `Incomplete` on empty input +- `switch!` can now take a default case + +### Fixed + +- `named_args!` now imports `IResult` directly +- the upgrade to regex 0.2 broke the regex combinators, they work now + +## 2.1.0 - 2017-01-27 + +### Thanks + +- @nickbabcock for documentation fixes +- @derekdreery for documentation fixes +- @DirkyJerky for documentation fixes +- @saschagrunert for documentation fixes +- @lucab for documentation fixes +- @hyone for documentation fixes +- @tstorch for factoring `Slice` +- @shepmaster for adding crate categories +- @antoyo for adding `named_args!` + +### Added + +- `verify!` uses a first parser, then applies a function to check that its result satisfies some conditions +- `named_args!` creates a parser function that can accept other arguments along with the input +- `parse_to!` will use the `parse` method from `FromStr` to parse a value. It will automatically translate the input to a string if necessary +- `float`, `float_s`, `double`, `double_s` can recognize floating point numbers in text + +### Changed + +- `escaped!` will now return `Incomplete` if needed +- `permutation!` supports up to 20 child parsers + +## 2.0.1 - 2016-12-10 + +Bugfix release + +*Warning*: there is a small breaking change, `add_error!` is renamed to `add_return_error!`. This was planned for the 2.0 release but was forgotten. This is a small change in a feature that not many people use, for a release that is not yet widely in use, so there will be no 3.0 release for that change. + +### Thanks + +- @nickbabcock for catching and fixing the `add_error!` mixup +- @lucab for documentation fixes +- @jtdowney for noticing that `tag_no_case!` was not working at all for byte slices + +### Fixed + +- `add_error!` has been renamed to `add_return_error!` +- the `not!` combinator now accepts functions +- `tag_no_case!` is now working as accepted (before, it accepted everything) + + +## 2.0 - 2016-11-25 + +The 2.0 release is one of the biggest yet. It was a good opportunity to clean up some badly named combinators and fix invalid behaviours. + +Since this version introduces a few breaking changes, an [upgrade documentation](https://github.com/Geal/nom/blob/master/doc/upgrading_to_nom_2.md) is available, detailing the steps to fix the most common migration issues. After testing on a set of 30 crates, most of them will build directly, a large part will just need to activate the "verbose-errors" compilation feature. The remaining fixes are documented. + +This version also adds a lot of interesting features, like the permutation combinator or whitespace separated formats support. + +### Thanks + +- @lu-zero for license help +- @adamgreig for type inference fixes +- @keruspe for documentation and example fixes, for the `IResult => Result` conversion work, making `AsChar`'s method more consistent, and adding `many_till!` +- @jdeeny for implementing `Offset` on `&str` +- @vickenty for documentation fixes and his refactoring of `length_value!` and `length_bytes!` +- @overdrivenpotato for refactoring some combinators +- @taralx for documentation fixes +- @keeperofdakeys for fixing eol behaviour, writing documentation and adding `named_attr!` +- @jturner314 for writing documentation +- @bozaro for fixing compilation errors +- @uniphil for adding a `crates.io` badge +- @badboy for documentation fixes +- @jugglerchris for fixing `take_s!` +- @AndyShiue for implementing `Error` and `Display` on `ErrorKind` and detecting incorrect UTF-8 string indexing + +### Added + +- the "simple" error management system does not accumulates errors when backtracking. This is a big perf gain, and is activated by default in nom 2.0 +- nom can now work on any type that implement the traits defined in `src/traits.rs`: `InputLength`, `InputIter`, `InputTake`, `Compare`, `FindToken`, `FindSubstring`, `Slice` +- the documentation from Github's wiki has been moved to the `doc/` directory. They are markdown files that you can build with [cargo-external-doc](https://crates.io/crates/cargo-external-doc) +- whitespace separated format support: with the `ws!` combinator, you can automatically introduce whitespace parsers between all parsers and combinators +- the `permutation!` combinator applies its child parsers in any order, as long as they all succeed once, and return a tuple of the results +- `do_parse!` is a simpler alternative to `chain!`, which is now deprecated +- you can now transform an `IResult` in a `std::result::Result` +- `length_data!` parses a length, and returns a subslice of that length +- `tag_no_case!` provides case independent comparison. It works nicely, without any allocation, for ASCII strings, but for UTF-8 strings, it defaults to an unsatisfying (and incorrect) comparison by lowercasing both strings +- `named_attr!` creates functions like `named!` but can add attributes like documentation +- `many_till!` applies repeatedly its first child parser until the second succeeds + +### Changed + +- the "verbose" error management that was available in previous versions is now activated by the "verbose-errors" compilation feature +- code reorganization: most of the parsers were moved in separate files to make the source easier to navigate +- most of the combinators are now independent from the input type +- the `eof` function was replaced with the `eof!` macro +- `error!` and `add_error!` were replaced with `return_error!` and `add_return_error!` to fix the name conflict with the log crate +- the `offset()` method is now in the `Offset` trait +- `length_value!` has been renamed to `length_count!`. The new `length_value!` selects a slice and applies the second parser once on that slice +- `AsChar::is_0_to_9` is now `AsChar::is_dec_digit` +- the combinators with configurable endianness now take an enum instead of a boolean as parameter + +### Fixed +- the `count!`, `count_fixed!` and `length_*!` combinator calculate incomplete data needs correctly +- `eol`, `line_ending` and `not_line_ending` now have a consistent behaviour that works correctly with incomplete data +- `take_s!` didn't correctly handle the case when the slice is exactly the right length + +## 1.2.4 - 2016-07-20 + +### Thanks +- @Phlosioneer for documentation fixes +- @sourrust for fixing offsets in `take_bits!` +- @ChrisMacNaughton for the XFS crate +- @pwoolcoc for `rest_s` +- @fitzgen for more `IResult` methods +- @gtors for the negative lookahead feature +- @frk1 and @jeandudey for little endian float parsing +- @jethrogb for fixing input usage in `many1` +- @acatton for beating me at nom golf :D + +### Added +- the `rest_s` method on `IResult` returns the remaining `&str` input +- `unwrap_err` and `unwrap_inc` methods on `IResult` +- `not!` will peek at the input and return `Done` if the underlying parser returned `Error` or `Incomplete`, without consuming the input +- `le_f32` and `le_f64` parse little endian floating point numbers (IEEE 754) +- + +### Fixed +- documentation fixes +- `take_bits!` is now more precise +- `many1` inccorectly used the `len` function instead of `input_len` +- the INI parser is simpler +- `recognize!` had an early `return` that is removed now + +## 1.2.3 - 2016-05-10 + +### Thanks +- @lu-zero for the contribution guidelines +- @GuillaumeGomez for fixes on `length_bytes` and some documentation +- @Hywan for documentation and test fixes +- @Xirdus for correct trait import issues +- @mspiegel for the new AST example +- @cholcombe973 for adding the `cond_with_error!` combinator +- @tstorch for refactoring `many0!` +- @panicbit for the folding combinators +- @evestera for `separated_list!` fixes +- @DanielKeep for correcting some enum imports + +### Added +- Regular expression combinators starting with `re_bytes_` work on byte slices +- example parsing arithmetic expressions to an AST +- `cond_with_error!` works like `cond!` but will return `None` if the condition is false, and `Some(value)` if the underlying parser succeeded +- `fold_many0!`, `fold_many1!` and `fold_many_m_n!` will take a parser, an initial value and a combining function, and fold over the successful applications of the parser + +### Fixed +- `length_bytes!` converts the result of its child parser to usize +- `take_till!` now imports `InputLength` instead of assuming it's in scope +- `separated_list!` and `separated_nonempty_list!` will not consume the separator if there's no following successfully parsed value +- no more warnings on build + +### Changed +- simpler implementation of `many0!` + +## 1.2.2 - 2016-03-09 + +### Thanks +- @conradev for fixing `take_until_s!` +- @GuillaumeGomez for some documentation fixes +- @frewsxcv for some documentation fixes +- @tstorch for some test refactorings + +### Added +- `nom::Err` now implements `std::error::Error` + +### Fixed +- `hex_u32` does not parses more than 8 chars now +- `take_while!` and `take_while1!` will not perturb the behaviour of `recognize!` anymore + +## 1.2.1 - 2016-02-23 + +### Thanks +- @sourrust for adding methods to `IResult` +- @tstorch for the test refactoring, and for adding methods to `IResult` and `Needed` +- @joelself for fixing the method system + +### Added + +- mapping methods over `IResult` and `Needed` + +### Changed + +- `apply_rf` is renamed to `apply_m`. This will not warrant a major version, since it is part missing from the methods feture added in the 1.2.0 release +- the `regexp_macros` feature that used `regex!` to precompile regular expressions has been replaced by the normal regex engine combined with `lazy_static` + +### Fixed + +- when a parser or combinator was returning an empty buffer as remaining part, it was generating one from a static empty string. This was messing with buffer offset calculation. Now, that empty slice is taken like this: `&input[input.len()..]`. +- The `regexp_macros` and `no_std` feature build again and are now tested with Travis CI + +## 1.2.0 - 2016-02-08 + +### Thanks +- @zentner-kyle for type inference fixes +- @joelself for his work on `&str` parsing and method parsers +- @GuillaumeGomez for implementing methods on `IResult` +- @dirk for the `alt_complete!` combinator +- @tstorch for a lot of refactoring work and unit tests additions +- @jansegre for the hex digit parsers +- @belgum for some documentation fixes +- @lwandrebeck for some documentation fixes and code fixes in `hex_digit` + +### Added +- `take_until_and_consume_s!` for consumption of string data until a tag +- more function patterns in `named!`. The error type can now be specified +- `alt_complete!` works like the `alt!` combinator, but tries the next branch if the current one returned `Incomplete`, instead of returning directly +- more unit tests for a lot of combinators +- hexadecimal digit parsers +- the `tuple!` combinator takes a list of parsers as argument, and applies them serially on the input. If all of them are successful, it willr eturn a tuple accumulating all the values. This combinator will (hopefully) replace most uses of `chain!` +- parsers can now be implemented as a method for a struct thanks to the `method!`, `call_m!` and `apply_rf!` combinators + +### Fixed +- there were type inference issues in a few combinators. They will now be easier to compile +- `peek!` compilation with bare functions +- `&str` parsers were splitting data at the byte level, not at the char level, which can result in inconsistencies in parsing UTF-8 characters. They now use character indexes +- some method implementations were missing on `IResult<I,O,E>` (with specified error type instead of implicit) + +## 1.1.0 - 2016-01-01 + +This release adds a lot of features related to `&str` parsing. The previous versions +were focused on `&[u8]` and bit streams parsing, but there's a need for more text +parsing with nom. The parsing functions like `alpha`, `digit` and others will now +accept either a `&[u8]` or a `&str`, so there is no breaking change on that part. + +There are also a few performance improvements and documentation fixes. + +### Thanks +- @Binero for pushing the work on `&str` parsing +- @meh for fixing `Option` and `Vec` imports +- @hoodie for a documentation fix +- @joelself for some documentation fixes +- @vberger for his traits magic making nom functions more generic + +### Added + +- string related parsers: `tag_s!`, `take_s!`, `is_a_s!`, `is_not_s!`, `take_while_s!`, `take_while1_s!`, `take_till_s!` +- `value!` is a combinator that always returns the same value. If a child parser is passed as second argument, that value is returned when the child parser succeeds + +### Changed + +- `tag!` will now compare even on partial input. If it expects "abcd" but receives "ef", it will now return an `Error` instead of `Incomplete` +- `many0!` and others will preallocate a larger vector to avoid some copies and reallocations +- `alpha`, `digit`, `alphanumeric`, `space` and `multispace` now accept as input a `&[u8]` or a `&str`. Additionally, they return an error if they receive an empty input +- `take_while!`, `take_while1!`, `take_while_s!`, `take_while1_s!` wilreturn an error on empty input + +### Fixed + +- if the child parser of `many0!` or `many1!` returns `Incomplete`, it will return `Incomplete` too, possibly updating the needed size +- `Option,` `Some`, `None` and `Vec` are now used with full path imports + +## 1.0.1 - 2015-11-22 + +This releases makes the 1.0 version compatible with Rust 1.2 and 1.3 + +### Thanks +- @steveklabnik for fixing lifetime issues in Producers and Consumers + +## 1.0.0 - 2015-11-16 + +Stable release for nom. A lot of new features, a few breaking changes + +### Thanks +- @ahenry for macro fixes +- @bluss for fixing documentation +- @sourrust for cleaning code and debugging the new streaming utilities +- @meh for inline optimizations +- @ccmtaylor for fixing function imports +- @soro for improvements to the streaming utilities +- @breard-r for catching my typos +- @nelsonjchen for catching my typos too +- @divarvel for hex string parsers +- @mrordinaire for the `length_bytes!` combinator + +### Breaking changes +- `IResult::Error` can now use custom error types, and is generic over the input type +- Producers and consumers have been replaced. The new implementation uses less memory and integrates more with parsers +- `nom::ErrorCode` is now `nom::ErrorKind` +- `filter!` has been renamed to `take_while!` +- `chain!` will count how much data is consumed and use that number to calculate how much data is needed if a parser returned `Incomplete` +- `alt!` returns `Incomplete` if a child parser returned `Incomplete`, instead of skipping to the next parser +- `IResult` does not require a lifetime tag anymore, yay! + +### Added + +- `complete!` will return an error if the child parser returned `Incomplete` +- `add_error!` will wrap an error, but allow backtracking +- `hex_u32` parser + +### Fixed +- the behaviour around `Incomplete` is better for most parsers now + +## 0.5.0 - 2015-10-16 + +This release fixes a few issues and stabilizes the code. + +### Thanks +- @nox for documentation fixes +- @daboross for linting fixes +- @ahenry for fixing `tap!` and extending `dbg!` and `dbg_dmp!` +- @bluss for tracking down and fixing issues with unsafe code +- @meh for inlining parser functions +- @ccmtaylor for fixing import of `str::from_utf8` + +### Fixed +- `tap!`, `dbg!` and `dbg_dmp!` now accept function parameters + +### Changed +- the type used in `count_fixed!` must be `Copy` +- `chain!` calculates how much data is needed if one of the parsers returns `Incomplete +- optional parsers in `chain!` can return `Incomplete` + +## 0.4.0 - 2015-09-08 + +Considering the number of changes since the last release, this version can contain breaking changes, so the version number becomes 0.4.0. A lot of new features and performance improvements! + +### Thanks +- @frewsxcv for documentation fixes +- @ngrewe for his work on producers and consumers +- @meh for fixes on `chain!` and for the `rest` parser +- @daboross for refactoring `many0!` and `many1!` +- @aleksander for the `switch!` combinator idea +- @TechnoMancer for his help with bit level parsing +- @sxeraverx for pointing out a bug in `is_a!` + +### Fixed +- `count_fixed!` must take an explicit type as argument to generate the fixed-size array +- optional parsing behaviour in `chain!` +- `count!` can take 0 elements +- `is_a!` and `is_not!` can now consume the whole input + +### Added +- it is now possible to seek to the end of a `MemProducer` +- `opt!` returns `Done(input, None)` if `the child parser returned `Incomplete` +- `rest` will return the remaining input +- consumers can now seek to and from the end of input +- `switch!` applies a first parser then matches on its result to choose the next parser +- bit-level parsers +- character-level parsers +- regular expression parsers +- implementation of `take_till!`, `take_while!` and `take_while1!` + +### Changed +- `alt!` can return `Incomplete` +- the error analysis functions will now take references to functions instead of moving them +- performance improvements on producers +- performance improvement for `filter!` +- performance improvement for `count!`: a `Vec` of the right size is directly allocated + +## 0.3.11 - 2015-08-04 + +### Thanks +- @bluss for remarking that the crate included random junk lying non commited in my local repository + +### Fixed +- cleanup of my local repository will ship less files in the crates, resulting in a smaller download + +## 0.3.10 - 2015-08-03 + +### Added + +- `bits!` for bit level parsing. It indicates that all child parsers will take a `(&[u8], usize)`as input, with the second parameter indicating the bit offset in the first byte. This allows viewing a byte slice as a bit stream. Most combinators can be used directly under `bits!` +- `take_bits!` takes an integer type and a number of bits, consumes that number of bits and updates the offset, possibly by crossing byte boundaries +- bit level parsers are all written in `src/bits.rs` + +### Changed + +- Parsers that specifically handle bytes have been moved to src/bytes.rs`. This applies to `tag!`, `is_not!`, `is_a!`, `filter!`, `take!`, `take_str!`, `take_until_and_consume!`, `take_until!`, `take_until_either_and_consume!`, `take_until_either!` + +## 0.3.9 - 2015-07-20 + +### Thanks +- @badboy for fixing `filter!` +- @idmit for some documentation fixes + +### Added +- `opt_res!` applies a parser and transform its result in a Result. This parser never fails +- `cond_reduce!` takes an expression as parameter, applies the parser if the expression is true, and returns an error if the expression is false +- `tap!` pass the result of a parser to a block to manipulate it, but do not affect the parser's result +- `AccReader` is a Read+BufRead that supports data accumulation and partial consumption. The `consume` method must be called afterwardsto indicate how much was consumed +- Arithmetic expression evaluation and parsing example +- `u16!`, `u32!`, `u64!`, `i16!`, `i32!`, `i64!` take an expression as parameter, if the expression is true, apply the big endian integer parser, if false, the little endian version +- type information for combinators. This will make the documentation a bit easier to navigate + +### Fixed +- `map_opt!` and `map_res!` had issues with argument order due to bad macros +- `delimited!` did not compile for certain combinations of arguments +- `filter!` did not return a byte slice but a fixed array + +## 0.3.8 - 2015-07-03 + +### Added +- code coverage is now calculated automatically on Travis CI +- `Stepper`: wrap a `Producer`, and call the method `step` with a parser. This method will buffer data if there is not enough, apply the parser if there is, and keep the rest of the input in memory for the next call +- `ReadProducer`: takes something implementing `Read`, and makes a `Producer` out of it + +### Fixed +- the combinators `separated_pair!` and `delimited!` did not work because an implementation macro was not exported +- if a `MemProducer` reached its end, it should always return `Eof` +- `map!` had issues with argument matching + +## 0.3.7 - 2015-06-24 + +### Added +- `expr_res!` and `expr_opt!` evaluate an expression returning a Result or Opt and convert it to IResult +- `AsBytes` is implemented for fixed size arrays. This allows `tag!([41u8, 42u8])` + +### Fixed +- `count_fixed!` argument parsing works again + +## 0.3.6 - 2015-06-15 + +### Added +- documentation for a few functions +- the consumer trait now requires the `failed(&self, error_code)` method in case of parsing error +- `named!` now handles thge alternative `named!(pub fun_name<OutputType>, ...)` + +### Fixed +- `filter!` now returns the whole input if the filter function never returned false +- `take!` casts its argument as usize, so it can accepts any integer type now + +## 0.3.5 - 2015-06-10 + +### Thanks +- @cmr for some documentation fixes + +### Added +- `count_fixed!` returns a fixed array + +### Fixed +- `count!` is back to the previous behaviour, returning a `Vec` for sizes known at runtime + +### Changed +- functions and traits exported from `nom::util` are now directly in `nom::` + +## 0.3.4 - 2015-06-09 + +### Thanks +- @andrew-d for fixes on `cond!` +- @keruspe for features in `chain!` + +### Added +- `chain!` can now have mutable fields + +### Fixed +- `cond!` had an infinite macro recursion + +### Changed +- `chain!` generates less code now. No apprent compilation time improvement + +## 0.3.3 - 2015-06-09 + +### Thanks +- @andrew-d for the little endian signed integer parsers +- @keruspe for fixes on `count!` + +### Added +- `le_i8`, `le_i16`, `le_i32`, `le_i64`: little endian signed integer parsers + +### Changed +- the `alt!` parser compiles much faster, even with more than 8 branches +- `count!` can now return a fixed size array instead of a growable vector + +## 0.3.2 - 2015-05-31 + +### Thanks +- @keruspe for the `take_str` parser and the function application combinator + +### Added +- `take_str!`: takes the specified number of bytes and return a UTF-8 string +- `apply!`: do partial application on the parameters of a function + +### Changed +- `Needed::Size` now contains a `usize` instead of a `u32` + +## 0.3.1 - 2015-05-21 + +### Thanks +- @divarvel for the big endian signed integer parsers + +### Added +- `be_i8`, `be_i16`, `be_i32`, `be_i64`: big endian signed integer parsers +- the `core` feature can be passed to cargo to build with `no_std` +- colored hexdump can be generated from error chains + +## 0.3.0 - 2015-05-07 + +### Thanks +- @filipegoncalves for some documentation and the new eof parser +- @CrimsonVoid for putting fully qualified types in the macros +- @lu_zero for some documentation fixes + +### Added +- new error types that can contain an error code, an input slice, and a list of following errors +- `error!` will cut backtracking and return directly from the parser, with a specified error code +- `eof` parser, successful if there is no more input +- specific error codes for the parsers provided by nom + +### Changed +- fully qualified types in macros. A lot of imports are not needed anymore + +### Removed +- `FlatMap`, `FlatpMapOpt` and `Functor` traits (replaced by `map!`, `map_opt!` and `map_res!`) + +## 0.2.2 - 2015-04-12 + +### Thanks +- @filipegoncalves and @thehydroimpulse for debugging an infinite loop in many0 and many1 +- @thehydroimpulse for suggesting public named parsers +- @skade for removing the dependency on the collections gate + +### Added +- `named!` can now declare public functions like this: `named!(pub tst, tag!("abcd"));` +- `pair!(X,Y)` returns a tuple `(x, y)` +- `separated_pair!(X, sep, Y)` returns a tuple `(x, y)` +- `preceded!(opening, X)` returns `x` +- `terminated!(X, closing)` returns `x` +- `delimited(opening, X, closing)` returns `x` +- `separated_list(sep, X)` returns a `Vec<X>` +- `separated_nonempty_list(sep, X)` returns a `Vec<X>` of at list one element + +### Changed +- `many0!` and `many1!` forbid parsers that do not consume input +- `is_a!`, `is_not!`, `alpha`, `digit`, `space`, `multispace` will now return an error if they do not consume at least one byte + +## 0.2.1 - 2015-04-04 + +### Thanks +- @mtsr for catching the remaining debug println! +- @jag426 who killed a lot of warnings +- @skade for removing the dependency on the core feature gate + + +### Added +- little endian unsigned int parsers le_u8, le_u16, le_u32, le_u64 +- `count!` to apply a parser a specified number of times +- `cond!` applies a parser if the condition is met +- more parser development tools in `util::*` + +### Fixed +- in one case, `opt!` would not compile + +### Removed +- most of the feature gates are now removed. The only one still needed is `collections` + +## 0.2.0 - 2015-03-24 +*works with `rustc 1.0.0-dev (81e2396c7 2015-03-19) (built 2015-03-19)`* + +### Thanks +- Ryman for the AsBytes implementation +- jag426 and jaredly for documentation fixes +- eternaleye on #rust IRC for his help on the new macro syntax + +### Changed +- the AsBytes trait improves readability, no more b"...", but "..." instead +- Incomplete will now hold either Needed;;Unknown, or Needed::Size(u32). Matching on Incomplete without caring for the value is done with `Incomplete(_)`, but if more granularity is mandatory, `Needed` can be matched too +- `alt!` can pass the result of the parser to a closure +- the `take_*` macros changed behaviour, the default case is now not to consume the separator. The macros have been renamed as follows: `take_until!` -> `take_until_and_consume!`, `take_until_and_leave!` -> `take_until!`, `take_until_either_and_leave!` -> `take_until_either!`, `take_until_either!` -> `take_until_either_and_consume!` + +### Added +- `peek!` macro: matches the future input but does not consume it +- `length_value!` macro: the first argument is a parser returning a `n` that can cast to usize, then applies the second parser `n` times. The macro has a variant with a third argument indicating the expected input size for the second parser +- benchmarks are available at https://github.com/Geal/nom_benchmarks +- more documentation +- **Unnamed parser syntax**: warning, this is a breaking change. With this new syntax, the macro combinators do not generate functions anymore, they create blocks. That way, they can be nested, for better readability. The `named!` macro is provided to create functions from parsers. Please be aware that nesting parsers comes with a small cost of compilation time, negligible in most cases, but can quickly get to the minutes scale if not careful. If this happens, separate your parsers in multiple subfunctions. +- `named!`, `closure!` and `call!` macros used to support the unnamed syntax +- `map!`, `map_opt!` and `map_res!` to combine a parser with a normal function, transforming the input directly, or returning an `Option` or `Result` + +### Fixed +- `is_a!` is now working properly + +### Removed +- the `o!` macro does less than `chain!`, so it has been removed +- the `fold0!` and `fold1!` macros were too complex and awkward to use, the `many*` combinators will be useful for most uses for now + +## 0.1.6 - 2015-02-24 +### Changed +- consumers must have an end method that will be called after parsing + +### Added +- big endian unsigned int and float parsers: be_u8, be_u16, be_u32, be_u64, be_f32, be_f64 +- producers can seek +- function and macros documentation +- README documentation +### Fixed +- lifetime declarations +- tag! can return Incomplete + +## 0.1.5 - 2015-02-17 +### Changed +- traits were renamed: FlatMapper -> FlatMap, Mapper -> FlatMapOpt, Mapper2 -> Functor + +### Fixed +- woeks with rustc f1bb6c2f4 + +## 0.1.4 - 2015-02-17 +### Changed +- the chaining macro can take optional arguments with '?' + +## 0.1.3 - 2015-02-16 +### Changed +- the chaining macro now takes the closure at the end of the argument list + +## 0.1.2 - 2015-02-16 +### Added +- flat_map implementation for <&[u8], &[u8]> +- chaining macro +- partial MP4 parser example + + +## 0.1.1 - 2015-02-06 +### Fixed +- closure syntax change + +## Compare code + +* [unreleased](https://github.com/Geal/nom/compare/5.1.1...HEAD) +* [5.1.1](https://github.com/Geal/nom/compare/5.1.0...5.1.1) +* [5.1.0](https://github.com/Geal/nom/compare/5.0.1...5.1.0) +* [5.0.1](https://github.com/Geal/nom/compare/5.0.0...5.0.1) +* [5.0.0](https://github.com/Geal/nom/compare/4.2.3...5.0.0) +* [4.2.3](https://github.com/Geal/nom/compare/4.2.2...4.2.3) +* [4.2.2](https://github.com/Geal/nom/compare/4.2.1...4.2.2) +* [4.2.1](https://github.com/Geal/nom/compare/4.2.0...4.2.1) +* [4.2.0](https://github.com/Geal/nom/compare/4.1.1...4.2.0) +* [4.1.1](https://github.com/Geal/nom/compare/4.1.0...4.1.1) +* [4.1.0](https://github.com/Geal/nom/compare/4.0.0...4.1.0) +* [4.0.0](https://github.com/Geal/nom/compare/3.2.1...4.0.0) +* [3.2.1](https://github.com/Geal/nom/compare/3.2.0...3.2.1) +* [3.2.0](https://github.com/Geal/nom/compare/3.1.0...3.2.0) +* [3.1.0](https://github.com/Geal/nom/compare/3.0.0...3.1.0) +* [3.0.0](https://github.com/Geal/nom/compare/2.2.1...3.0.0) +* [2.2.1](https://github.com/Geal/nom/compare/2.2.0...2.2.1) +* [2.2.0](https://github.com/Geal/nom/compare/2.1.0...2.2.0) +* [2.1.0](https://github.com/Geal/nom/compare/2.0.1...2.1.0) +* [2.0.1](https://github.com/Geal/nom/compare/2.0.0...2.0.1) +* [2.0.0](https://github.com/Geal/nom/compare/1.2.4...2.0.0) +* [1.2.4](https://github.com/Geal/nom/compare/1.2.3...1.2.4) +* [1.2.3](https://github.com/Geal/nom/compare/1.2.2...1.2.3) +* [1.2.2](https://github.com/Geal/nom/compare/1.2.1...1.2.2) +* [1.2.1](https://github.com/Geal/nom/compare/1.2.0...1.2.1) +* [1.2.0](https://github.com/Geal/nom/compare/1.1.0...1.2.0) +* [1.1.0](https://github.com/Geal/nom/compare/1.0.1...1.1.0) +* [1.0.1](https://github.com/Geal/nom/compare/1.0.0...1.0.1) +* [1.0.0](https://github.com/Geal/nom/compare/0.5.0...1.0.0) +* [0.5.0](https://github.com/geal/nom/compare/0.4.0...0.5.0) +* [0.4.0](https://github.com/geal/nom/compare/0.3.11...0.4.0) +* [0.3.11](https://github.com/geal/nom/compare/0.3.10...0.3.11) +* [0.3.10](https://github.com/geal/nom/compare/0.3.9...0.3.10) +* [0.3.9](https://github.com/geal/nom/compare/0.3.8...0.3.9) +* [0.3.8](https://github.com/Geal/nom/compare/0.3.7...0.3.8) +* [0.3.7](https://github.com/Geal/nom/compare/0.3.6...0.3.7) +* [0.3.6](https://github.com/Geal/nom/compare/0.3.5...0.3.6) +* [0.3.5](https://github.com/Geal/nom/compare/0.3.4...0.3.5) +* [0.3.4](https://github.com/Geal/nom/compare/0.3.3...0.3.4) +* [0.3.3](https://github.com/Geal/nom/compare/0.3.2...0.3.3) +* [0.3.2](https://github.com/Geal/nom/compare/0.3.1...0.3.2) +* [0.3.1](https://github.com/Geal/nom/compare/0.3.0...0.3.1) +* [0.3.0](https://github.com/Geal/nom/compare/0.2.2...0.3.0) +* [0.2.2](https://github.com/Geal/nom/compare/0.2.1...0.2.2) +* [0.2.1](https://github.com/Geal/nom/compare/0.2.0...0.2.1) +* [0.2.0](https://github.com/Geal/nom/compare/0.1.6...0.2.0) +* [0.1.6](https://github.com/Geal/nom/compare/0.1.5...0.1.6) +* [0.1.5](https://github.com/Geal/nom/compare/0.1.4...0.1.5) +* [0.1.4](https://github.com/Geal/nom/compare/0.1.3...0.1.4) +* [0.1.3](https://github.com/Geal/nom/compare/0.1.2...0.1.3) +* [0.1.2](https://github.com/Geal/nom/compare/0.1.1...0.1.2) +* [0.1.1](https://github.com/Geal/nom/compare/0.1.0...0.1.1) diff --git a/third_party/rust/nom/Cargo.lock b/third_party/rust/nom/Cargo.lock new file mode 100644 index 0000000000..0f59f087d7 --- /dev/null +++ b/third_party/rust/nom/Cargo.lock @@ -0,0 +1,639 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "arrayvec" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "atty" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "autocfg" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bstr" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-automata 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "byteorder" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cast" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cc" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "clap" +version = "2.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "criterion" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "criterion-plot 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "csv 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_xoshiro 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon-core 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", + "tinytemplate 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "criterion-plot" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "crossbeam-deque" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crossbeam-epoch 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "memoffset 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "crossbeam-queue" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "crossbeam-utils" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "csv" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bstr 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "csv-core 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "csv-core" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "doc-comment" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "either" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fs_extra" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "itertools" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "itoa" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "jemalloc-sys" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", + "fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "jemallocator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "jemalloc-sys 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "lexical-core" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.2.1 (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.0 (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.62" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "memchr" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memoffset" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nodrop" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "nom" +version = "5.1.1" +dependencies = [ + "criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "doc-comment 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "jemallocator 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lexical-core 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num_cpus" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand_os" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_xoshiro" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rayon" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crossbeam-deque 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon-core 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rayon-core" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crossbeam-deque 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-queue 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-automata" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.6.11" +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 = "ryu" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "same-file" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "scopeguard" +version = "1.0.0" +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 = "serde" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde_derive" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "static_assertions" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "syn" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tinytemplate" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-width" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "version_check" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "walkdir" +version = "2.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "same-file 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d" +"checksum arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b8d73f9beda665eaa98ab9e4f7442bd4e7de6652587de55b2525e52e29c1b0ba" +"checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" +"checksum autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b671c8fb71b457dd4ae18c4ba1e59aa81793daacc361d82fcd410cef0d491875" +"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +"checksum bstr 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94cdf78eb7e94c566c1f5dbe2abf8fc70a548fc902942a48c4b3a98b48ca9ade" +"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" +"checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" +"checksum cc 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)" = "b548a4ee81fccb95919d4e22cfea83c7693ebfd78f0495493178db20b3139da7" +"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" +"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0363053954f3e679645fc443321ca128b7b950a6fe288cf5f9335cc22ee58394" +"checksum criterion-plot 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76f9212ddf2f4a9eb2d401635190600656a1f88a932ef53d06e7fa4c7e02fb8e" +"checksum crossbeam-deque 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "05e44b8cf3e1a625844d1750e1f7820da46044ff6d28f4d43e455ba3e5bb2c13" +"checksum crossbeam-epoch 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fedcd6772e37f3da2a9af9bf12ebe046c0dfe657992377b4df982a2b54cd37a9" +"checksum crossbeam-queue 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b" +"checksum crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "04973fa96e96579258a5091af6003abde64af786b860f18622b82e026cca60e6" +"checksum csv 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "37519ccdfd73a75821cac9319d4fce15a81b9fcf75f951df5b9988aa3a0af87d" +"checksum csv-core 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9b5cadb6b25c77aeff80ba701712494213f4a8418fcda2ee11b6560c3ad0bf4c" +"checksum doc-comment 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "923dea538cea0aa3025e8685b20d6ee21ef99c4f77e954a30febbaac5ec73a97" +"checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b" +"checksum fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674" +"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +"checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" +"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" +"checksum jemalloc-sys 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "bfc62c8e50e381768ce8ee0428ee53741929f7ebd73e4d83f669bcf7693e00ae" +"checksum jemallocator 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "9f0cd42ac65f758063fea55126b0148b1ce0a6354ff78e07a4d6806bc65c4ab3" +"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" +"checksum lexical-core 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f86d66d380c9c5a685aaac7a11818bdfa1f733198dfd9ec09c70b762cd12ad6f" +"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" +"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" +"checksum memoffset 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce6075db033bbbb7ee5a0bbd3a3186bbae616f57fb001c485c7ff77955f8177f" +"checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" +"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32" +"checksum num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcef43580c035376c0705c42792c294b66974abbfd2789b511784023f71f3273" +"checksum proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c5c2380ae88876faae57698be9e9775e3544decad214599c3a6266cca6ac802" +"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +"checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" +"checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +"checksum rand_xoshiro 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "03b418169fb9c46533f326efd6eed2576699c44ca92d3052a066214a8d828929" +"checksum rayon 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a4b0186e22767d5b9738a05eab7c6ac90b15db17e5b5f9bd87976dd7d89a10a4" +"checksum rayon-core 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ebbe0df8435ac0c397d467b6cad6d25543d06e8a019ef3f6af3c384597515bd2" +"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +"checksum regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88c3d9193984285d544df4a30c23a4e62ead42edf70a4452ceb76dac1ce05c26" +"checksum regex-automata 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "92b73c2a1770c255c240eaa4ee600df1704a38dc3feaa6e949e7fcd4f8dc09f9" +"checksum regex-syntax 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b143cceb2ca5e56d5671988ef8b15615733e7ee16cd348e064333b251b89343f" +"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +"checksum ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997" +"checksum same-file 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "585e8ddcedc187886a30fa705c47985c3fa88d06624095856b36ca0b82ff4421" +"checksum scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d" +"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 serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)" = "fec2851eb56d010dc9a21b89ca53ee75e6528bab60c11e89d38390904982da9f" +"checksum serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)" = "cb4dc18c61206b08dc98216c98faa0232f4337e1e1b8574551d5bad29ea1b425" +"checksum serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)" = "051c49229f282f7c6f3813f8286cc1e3323e8051823fce42c7ea80fe13521704" +"checksum static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3" +"checksum syn 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "158521e6f544e7e3dcfc370ac180794aa38cb34a1b1e07609376d4adcf429b93" +"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" +"checksum tinytemplate 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4574b75faccaacddb9b284faecdf0b544b80b6b294f3d062d325c5726a209c20" +"checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20" +"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +"checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" +"checksum walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "9658c94fa8b940eab2250bd5a457f9c48b748420d71293b165c8cdbe2f55f71e" +"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" +"checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/third_party/rust/nom/Cargo.toml b/third_party/rust/nom/Cargo.toml new file mode 100644 index 0000000000..91894a9746 --- /dev/null +++ b/third_party/rust/nom/Cargo.toml @@ -0,0 +1,181 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +edition = "2018" +name = "nom" +version = "5.1.1" +authors = ["contact@geoffroycouprie.com"] +include = ["CHANGELOG.md", "LICENSE", ".gitignore", ".travis.yml", "Cargo.toml", "src/*.rs", "src/*/*.rs", "tests/*.rs", "build.rs"] +autoexamples = false +description = "A byte-oriented, zero-copy, parser combinators library" +documentation = "https://docs.rs/nom" +readme = "README.md" +keywords = ["parser", "parser-combinators", "parsing", "streaming", "bit"] +categories = ["parsing"] +license = "MIT" +repository = "https://github.com/Geal/nom" +[package.metadata.docs.rs] +all-features = true +features = ["alloc", "std", "regexp", "regexp_macros", "lexical"] +[profile.bench] +lto = true +codegen-units = 1 +debug = true + +[lib] +bench = false + +[[example]] +name = "json" +path = "examples/json.rs" +required-features = ["alloc"] + +[[example]] +name = "s_expression" +path = "examples/s_expression.rs" +required-features = ["alloc"] + +[[example]] +name = "string" +path = "examples/string.rs" +required-features = ["alloc"] + +[[test]] +name = "arithmetic" + +[[test]] +name = "arithmetic_ast" +required-features = ["alloc"] + +[[test]] +name = "blockbuf-arithmetic" + +[[test]] +name = "css" + +[[test]] +name = "custom_errors" + +[[test]] +name = "float" + +[[test]] +name = "inference" + +[[test]] +name = "ini" +required-features = ["alloc"] + +[[test]] +name = "ini_str" +required-features = ["alloc"] + +[[test]] +name = "issues" +required-features = ["alloc", "regexp_macros"] + +[[test]] +name = "json" + +[[test]] +name = "mp4" +required-features = ["alloc"] + +[[test]] +name = "multiline" +required-features = ["alloc"] + +[[test]] +name = "named_args" + +[[test]] +name = "overflow" + +[[test]] +name = "reborrow_fold" + +[[test]] +name = "test1" + +[[bench]] +name = "arithmetic" +path = "benches/arithmetic.rs" +harness = false + +[[bench]] +name = "http" +path = "benches/http.rs" +harness = false + +[[bench]] +name = "ini" +path = "benches/ini.rs" +harness = false + +[[bench]] +name = "ini_complete" +path = "benches/ini_complete.rs" +harness = false + +[[bench]] +name = "ini_str" +path = "benches/ini_str.rs" +harness = false + +[[bench]] +name = "json" +path = "benches/json.rs" +harness = false +[dependencies.lazy_static] +version = "^1.0" +optional = true + +[dependencies.lexical-core] +version = "^0.6.0" +optional = true + +[dependencies.memchr] +version = "^2.0" +default-features = false + +[dependencies.regex] +version = "^1.0" +optional = true +[dev-dependencies.criterion] +version = "0.2" + +[dev-dependencies.doc-comment] +version = "0.3" + +[dev-dependencies.jemallocator] +version = "^0.1" +[build-dependencies.version_check] +version = "0.9" + +[features] +alloc = [] +default = ["std", "lexical"] +lexical = ["lexical-core"] +regexp = ["regex"] +regexp_macros = ["regexp", "lazy_static"] +std = ["alloc", "memchr/use_std"] +[badges.coveralls] +branch = "master" +repository = "Geal/nom" +service = "github" + +[badges.maintenance] +status = "actively-developed" + +[badges.travis-ci] +repository = "Geal/nom" diff --git a/third_party/rust/nom/LICENSE b/third_party/rust/nom/LICENSE new file mode 100644 index 0000000000..88557e44e3 --- /dev/null +++ b/third_party/rust/nom/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2014-2019 Geoffroy Couprie + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/third_party/rust/nom/build.rs b/third_party/rust/nom/build.rs new file mode 100644 index 0000000000..9743c905f7 --- /dev/null +++ b/third_party/rust/nom/build.rs @@ -0,0 +1,7 @@ +extern crate version_check; + +fn main() { + if version_check::is_min_version("1.28.0").unwrap_or(true) { + println!("cargo:rustc-cfg=stable_i128"); + } +} diff --git a/third_party/rust/nom/src/bits/complete.rs b/third_party/rust/nom/src/bits/complete.rs new file mode 100644 index 0000000000..f868ec2643 --- /dev/null +++ b/third_party/rust/nom/src/bits/complete.rs @@ -0,0 +1,75 @@ +//! bit level parsers +//! + +use crate::error::{ErrorKind, ParseError}; +use crate::internal::{Err, IResult}; +use crate::lib::std::ops::{AddAssign, RangeFrom, Shl, Shr, Div}; +use crate::traits::{InputIter, InputLength, Slice, ToUsize}; + +/// generates a parser taking `count` bits +pub fn take<I, O, C, E: ParseError<(I, usize)>>(count: C) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E> +where + I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength, + C: ToUsize, + O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>, +{ + let count = count.to_usize(); + move |(input, bit_offset): (I, usize)| { + if count == 0 { + Ok(((input, bit_offset), 0u8.into())) + } else { + let cnt = (count + bit_offset).div(8); + if input.input_len() * 8 < count + bit_offset { + Err(Err::Error(E::from_error_kind((input, bit_offset), ErrorKind::Eof))) + } else { + let mut acc:O = (0 as u8).into(); + let mut offset: usize = bit_offset; + let mut remaining: usize = count; + let mut end_offset: usize = 0; + + for byte in input.iter_elements().take(cnt + 1) { + if remaining == 0 { + break; + } + let val: O = if offset == 0 { + byte.into() + } else { + ((byte << offset) as u8 >> offset).into() + }; + + if remaining < 8 - offset { + acc += val >> (8 - offset - remaining); + end_offset = remaining + offset; + break; + } else { + acc += val << (remaining - (8 - offset)); + remaining -= 8 - offset; + offset = 0; + } + } + Ok(( (input.slice(cnt..), end_offset) , acc)) + } + } + } +} + +/// generates a parser taking `count` bits and comparing them to `pattern` +pub fn tag<I, O, C, E: ParseError<(I, usize)>>(pattern: O, count: C) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E> +where + I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength + Clone, + C: ToUsize, + O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq, +{ + let count = count.to_usize(); + move |input: (I, usize)| { + let inp = input.clone(); + + take(count)(input).and_then(|(i, o)| { + if pattern == o { + Ok((i, o)) + } else { + Err(Err::Error(error_position!(inp, ErrorKind::TagBits))) + } + }) + } +} diff --git a/third_party/rust/nom/src/bits/macros.rs b/third_party/rust/nom/src/bits/macros.rs new file mode 100644 index 0000000000..b056503fc6 --- /dev/null +++ b/third_party/rust/nom/src/bits/macros.rs @@ -0,0 +1,263 @@ +//! Bit level parsers and combinators +//! +//! Bit parsing is handled by tweaking the input in most macros. +//! In byte level parsing, the input is generally a `&[u8]` passed from combinator +//! to combinator as the slices are manipulated. +//! +//! Bit parsers take a `(&[u8], usize)` as input. The first part of the tuple is a byte slice, +//! the second part is a bit offset in the first byte of the slice. +//! +//! By passing a pair like this, we can leverage most of the existing combinators, and avoid +//! transforming the whole slice to a vector of booleans. This should make it easy +//! to see a byte slice as a bit stream, and parse code points of arbitrary bit length. +//! + +/// Transforms its byte slice input into a bit stream for the underlying parser. This allows the +/// given bit stream parser to work on a byte slice input. +/// +/// Signature: +/// `bits!( parser ) => ( &[u8], (&[u8], usize) -> IResult<(&[u8], usize), T> ) -> IResult<&[u8], T>` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// # fn main() { +/// named!( take_4_bits<u8>, bits!( take_bits!( 4u8 ) ) ); +/// +/// let input = vec![0xAB, 0xCD, 0xEF, 0x12]; +/// let sl = &input[..]; +/// +/// assert_eq!(take_4_bits( sl ), Ok( (&sl[1..], 0xA) )); +/// assert_eq!(take_4_bits( &b""[..] ), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +#[macro_export(local_inner_macros)] +macro_rules! bits ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ({ + $crate::bits::bitsc($i, move |i| { $submac!(i, $($args)*) }) + }); + ($i:expr, $f:expr) => ( + bits!($i, call!($f)) + ); +); + +/// Counterpart to bits, bytes! transforms its bit stream input into a byte slice for the underlying +/// parser, allowing byte-slice parsers to work on bit streams. +/// +/// Signature: +/// `bytes!( parser ) => ( (&[u8], usize), &[u8] -> IResult<&[u8], T> ) -> IResult<(&[u8], usize), T>`, +/// +/// A partial byte remaining in the input will be ignored and the given parser will start parsing +/// at the next full byte. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::combinator::rest; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// +/// named!( parse<(u8, u8, &[u8])>, bits!( tuple!( +/// take_bits!(4u8), +/// take_bits!(8u8), +/// bytes!(rest::<_, (_, ErrorKind)>) +/// ))); +/// +/// let input = &[0xde, 0xad, 0xbe, 0xaf]; +/// +/// assert_eq!(parse( input ), Ok(( &[][..], (0xd, 0xea, &[0xbe, 0xaf][..]) ))); +/// # } +#[macro_export(local_inner_macros)] +macro_rules! bytes ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ({ + $crate::bits::bytesc($i, move |i| { $submac!(i, $($args)*) }) + }); + ($i:expr, $f:expr) => ( + bytes!($i, call!($f)) + ); +); + +/// Consumes the specified number of bits and returns them as the specified type. +/// +/// Signature: +/// `take_bits!(type, count) => ( (&[T], usize), U, usize) -> IResult<(&[T], usize), U>` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(bits_pair<(&[u8], usize), (u8, u8)>, pair!( take_bits!(4u8), take_bits!(4u8) ) ); +/// named!( take_pair<(u8, u8)>, bits!( bits_pair ) ); +/// +/// let input = vec![0xAB, 0xCD, 0xEF]; +/// let sl = &input[..]; +/// +/// assert_eq!(take_pair( sl ), Ok((&sl[1..], (0xA, 0xB))) ); +/// assert_eq!(take_pair( &sl[1..] ), Ok((&sl[2..], (0xC, 0xD))) ); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_bits ( + ($i:expr, $count:expr) => ( + { + let res: $crate::IResult<_, _> = $crate::bits::streaming::take($count)($i); + res + } + ); +); + +/// Matches the given bit pattern. +/// +/// Signature: +/// `tag_bits!(type, count, pattern) => ( (&[T], usize), U, usize, U) -> IResult<(&[T], usize), U>` +/// +/// The caller must specify the number of bits to consume. The matched value is included in the +/// result on success. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!( take_a<u8>, bits!( tag_bits!(4usize, 0xA) ) ); +/// +/// let input = vec![0xAB, 0xCD, 0xEF]; +/// let sl = &input[..]; +/// +/// assert_eq!(take_a( sl ), Ok((&sl[1..], 0xA)) ); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! tag_bits ( + ($i:expr, $count:expr, $p: expr) => ( + { + let res: $crate::IResult<_, _> = $crate::bits::streaming::tag($p, $count)($i); + res + } + ) +); + +#[cfg(test)] +mod tests { + use crate::lib::std::ops::{AddAssign, Shl, Shr}; + use crate::internal::{Err, Needed, IResult}; + use crate::error::ErrorKind; + + #[test] + fn take_bits() { + let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11]; + let sl = &input[..]; + + assert_eq!(take_bits!((sl, 0), 0u8), Ok(((sl, 0), 0))); + assert_eq!(take_bits!((sl, 0), 8u8), Ok(((&sl[1..], 0), 170))); + assert_eq!(take_bits!((sl, 0), 3u8), Ok(((&sl[0..], 3), 5))); + assert_eq!(take_bits!((sl, 0), 6u8), Ok(((&sl[0..], 6), 42))); + assert_eq!(take_bits!((sl, 1), 1u8), Ok(((&sl[0..], 2), 0))); + assert_eq!(take_bits!((sl, 1), 2u8), Ok(((&sl[0..], 3), 1))); + assert_eq!(take_bits!((sl, 1), 3u8), Ok(((&sl[0..], 4), 2))); + assert_eq!(take_bits!((sl, 6), 3u8), Ok(((&sl[1..], 1), 5))); + assert_eq!(take_bits!((sl, 0), 10u8), Ok(((&sl[1..], 2), 683))); + assert_eq!(take_bits!((sl, 0), 8u8), Ok(((&sl[1..], 0), 170))); + assert_eq!(take_bits!((sl, 6), 10u8), Ok(((&sl[2..], 0), 752))); + assert_eq!(take_bits!((sl, 6), 11u8), Ok(((&sl[2..], 1), 1504))); + assert_eq!(take_bits!((sl, 0), 20u8), Ok(((&sl[2..], 4), 700_163))); + assert_eq!(take_bits!((sl, 4), 20u8), Ok(((&sl[3..], 0), 716_851))); + let r: IResult<_,u32> = take_bits!((sl, 4), 22u8); + assert_eq!( + r, + Err(Err::Incomplete(Needed::Size(22))) + ); + } + + #[test] + fn tag_bits() { + let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11]; + let sl = &input[..]; + + assert_eq!(tag_bits!((sl, 0), 3u8, 0b101), Ok(((&sl[0..], 3), 5))); + assert_eq!(tag_bits!((sl, 0), 4u8, 0b1010), Ok(((&sl[0..], 4), 10))); + } + + named!(ch<(&[u8],usize),(u8,u8)>, + do_parse!( + tag_bits!(3u8, 0b101) >> + x: take_bits!(4u8) >> + y: take_bits!(5u8) >> + (x,y) + ) + ); + + #[test] + fn chain_bits() { + let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11]; + let sl = &input[..]; + assert_eq!(ch((&input[..], 0)), Ok(((&sl[1..], 4), (5, 15)))); + assert_eq!(ch((&input[..], 4)), Ok(((&sl[2..], 0), (7, 16)))); + assert_eq!(ch((&input[..1], 0)), Err(Err::Incomplete(Needed::Size(5)))); + } + + named!(ch_bytes<(u8, u8)>, bits!(ch)); + #[test] + fn bits_to_bytes() { + let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11]; + assert_eq!(ch_bytes(&input[..]), Ok((&input[2..], (5, 15)))); + assert_eq!(ch_bytes(&input[..1]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + ch_bytes(&input[1..]), + Err(Err::Error(error_position!(&input[1..], ErrorKind::TagBits))) + ); + } + + named!(bits_bytes_bs, bits!(bytes!(crate::combinator::rest::<_, (&[u8], ErrorKind)>))); + #[test] + fn bits_bytes() { + let input = [0b10_10_10_10]; + assert_eq!(bits_bytes_bs(&input[..]), Ok((&[][..], &[0b10_10_10_10][..]))); + } + + #[derive(PartialEq, Debug)] + struct FakeUint(u32); + + impl AddAssign for FakeUint { + fn add_assign(&mut self, other: FakeUint) { + *self = FakeUint(self.0 + other.0); + } + } + + impl Shr<usize> for FakeUint { + type Output = FakeUint; + + fn shr(self, shift: usize) -> FakeUint { + FakeUint(self.0 >> shift) + } + } + + impl Shl<usize> for FakeUint { + type Output = FakeUint; + + fn shl(self, shift: usize) -> FakeUint { + FakeUint(self.0 << shift) + } + } + + impl From<u8> for FakeUint { + fn from(i: u8) -> FakeUint { + FakeUint(u32::from(i)) + } + } + + #[test] + fn non_privitive_type() { + let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11]; + let sl = &input[..]; + + assert_eq!( + take_bits!((sl, 0), 20u8), + Ok(((&sl[2..], 4), FakeUint(700_163))) + ); + assert_eq!( + take_bits!((sl, 4), 20u8), + Ok(((&sl[3..], 0), FakeUint(716_851))) + ); + let r3: IResult<_, FakeUint> = take_bits!((sl, 4), 22u8); + assert_eq!( + r3, + Err(Err::Incomplete(Needed::Size(22))) + ); + } +} diff --git a/third_party/rust/nom/src/bits/mod.rs b/third_party/rust/nom/src/bits/mod.rs new file mode 100644 index 0000000000..a6b12f1a74 --- /dev/null +++ b/third_party/rust/nom/src/bits/mod.rs @@ -0,0 +1,119 @@ +//! bit level parsers +//! + +#[macro_use] +mod macros; + +pub mod streaming; +pub mod complete; + +use crate::error::{ParseError, ErrorKind}; +use crate::internal::{Err, IResult, Needed}; +use crate::lib::std::ops::RangeFrom; +use crate::traits::{Slice, ErrorConvert}; + + +/// Converts a byte-level input to a bit-level input, for consumption by a parser that uses bits. +/// +/// Afterwards, the input is converted back to a byte-level parser, with any remaining bits thrown +/// away. +/// +/// # Example +/// ```ignore +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// use nom::bits::bits; +/// use nom::bits::complete::take; +/// +/// fn take_4_bits(input: &[u8]) -> IResult<&[u8], u64> { +/// bits(take::<_, _, _, (_, _)>(4usize))(input) +/// } +/// +/// let input = vec![0xAB, 0xCD, 0xEF, 0x12]; +/// let sl = &input[..]; +/// +/// assert_eq!(take_4_bits( sl ), Ok( (&sl[1..], 0xA) )); +/// ``` +pub fn bits<I, O, E1: ParseError<(I, usize)>+ErrorConvert<E2>, E2: ParseError<I>, P>(parser: P) -> impl Fn(I) -> IResult<I, O, E2> +where + I: Slice<RangeFrom<usize>>, + P: Fn((I, usize)) -> IResult<(I, usize), O, E1>, +{ + move |input: I| match parser((input, 0)) { + Ok(((rest, offset), res)) => { + let byte_index = offset / 8 + if offset % 8 == 0 { 0 } else { 1 }; + Ok((rest.slice(byte_index..), res)) + } + Err(Err::Incomplete(n)) => Err(Err::Incomplete(n.map(|u| u / 8 + 1))), + Err(Err::Error(e)) => Err(Err::Error(e.convert())), + Err(Err::Failure(e)) => Err(Err::Failure(e.convert())), + } +} + +#[doc(hidden)] +pub fn bitsc<I, O, E1: ParseError<(I, usize)>+ErrorConvert<E2>, E2: ParseError<I>, P>(input: I, parser: P) -> IResult<I, O, E2> +where + I: Slice<RangeFrom<usize>>, + P: Fn((I, usize)) -> IResult<(I, usize), O, E1>, +{ + bits(parser)(input) +} + +/// Counterpart to bits, bytes transforms its bit stream input into a byte slice for the underlying +/// parser, allowing byte-slice parsers to work on bit streams. +/// +/// A partial byte remaining in the input will be ignored and the given parser will start parsing +/// at the next full byte. +/// +/// ```ignore +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # use nom::combinator::rest; +/// # use nom::sequence::tuple; +/// use nom::bits::{bits, bytes, streaming::take_bits}; +/// +/// fn parse(input: &[u8]) -> IResult<&[u8], (u8, u8, &[u8])> { +/// bits(tuple(( +/// take_bits(4usize), +/// take_bits(8usize), +/// bytes(rest) +/// )))(input) +/// } +/// +/// let input = &[0xde, 0xad, 0xbe, 0xaf]; +/// +/// assert_eq!(parse( input ), Ok(( &[][..], (0xd, 0xea, &[0xbe, 0xaf][..]) ))); +/// ``` +pub fn bytes<I, O, E1: ParseError<I>+ErrorConvert<E2>, E2: ParseError<(I, usize)>, P>(parser: P) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E2> +where + I: Slice<RangeFrom<usize>> + Clone, + P: Fn(I) -> IResult<I, O, E1>, +{ + move |(input, offset): (I, usize)| { + let inner = if offset % 8 != 0 { + input.slice((1 + offset / 8)..) + } else { + input.slice((offset / 8)..) + }; + let i = (input.clone(), offset); + match parser(inner) { + Ok((rest, res)) => Ok(((rest, 0), res)), + Err(Err::Incomplete(Needed::Unknown)) => Err(Err::Incomplete(Needed::Unknown)), + Err(Err::Incomplete(Needed::Size(sz))) => Err(match sz.checked_mul(8) { + Some(v) => Err::Incomplete(Needed::Size(v)), + None => Err::Failure(E2::from_error_kind(i, ErrorKind::TooLarge)), + }), + Err(Err::Error(e)) => Err(Err::Error(e.convert())), + Err(Err::Failure(e)) => Err(Err::Failure(e.convert())), + } + } +} + +#[doc(hidden)] +pub fn bytesc<I, O, E1: ParseError<I>+ErrorConvert<E2>, E2: ParseError<(I, usize)>, P>(input: (I, usize), parser: P) -> IResult<(I, usize), O, E2> +where + I: Slice<RangeFrom<usize>> + Clone, + P: Fn(I) -> IResult<I, O, E1>, +{ + bytes(parser)(input) +} diff --git a/third_party/rust/nom/src/bits/streaming.rs b/third_party/rust/nom/src/bits/streaming.rs new file mode 100644 index 0000000000..5ab75961bb --- /dev/null +++ b/third_party/rust/nom/src/bits/streaming.rs @@ -0,0 +1,75 @@ +//! bit level parsers +//! + +use crate::error::{ErrorKind, ParseError}; +use crate::internal::{Err, IResult, Needed}; +use crate::lib::std::ops::{AddAssign, RangeFrom, Shl, Shr, Div}; +use crate::traits::{InputIter, InputLength, Slice, ToUsize}; + +/// generates a parser taking `count` bits +pub fn take<I, O, C, E: ParseError<(I, usize)>>(count: C) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E> +where + I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength, + C: ToUsize, + O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>, +{ + let count = count.to_usize(); + move |(input, bit_offset): (I, usize)| { + if count == 0 { + Ok(((input, bit_offset), 0u8.into())) + } else { + let cnt = (count + bit_offset).div(8); + if input.input_len() * 8 < count + bit_offset { + Err(Err::Incomplete(Needed::Size(count as usize))) + } else { + let mut acc:O = (0 as u8).into(); + let mut offset: usize = bit_offset; + let mut remaining: usize = count; + let mut end_offset: usize = 0; + + for byte in input.iter_elements().take(cnt + 1) { + if remaining == 0 { + break; + } + let val: O = if offset == 0 { + byte.into() + } else { + ((byte << offset) as u8 >> offset).into() + }; + + if remaining < 8 - offset { + acc += val >> (8 - offset - remaining); + end_offset = remaining + offset; + break; + } else { + acc += val << (remaining - (8 - offset)); + remaining -= 8 - offset; + offset = 0; + } + } + Ok(( (input.slice(cnt..), end_offset) , acc)) + } + } + } +} + +/// generates a parser taking `count` bits and comparing them to `pattern` +pub fn tag<I, O, C, E: ParseError<(I, usize)>>(pattern: O, count: C) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E> +where + I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength + Clone, + C: ToUsize, + O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq, +{ + let count = count.to_usize(); + move |input: (I, usize)| { + let inp = input.clone(); + + take(count)(input).and_then(|(i, o)| { + if pattern == o { + Ok((i, o)) + } else { + Err(Err::Error(error_position!(inp, ErrorKind::TagBits))) + } + }) + } +} diff --git a/third_party/rust/nom/src/branch/macros.rs b/third_party/rust/nom/src/branch/macros.rs new file mode 100644 index 0000000000..4c02461c04 --- /dev/null +++ b/third_party/rust/nom/src/branch/macros.rs @@ -0,0 +1,940 @@ +/// Try a list of parsers and return the result of the first successful one +/// +/// ```rust,ignore +/// alt!(I -> IResult<I,O> | I -> IResult<I,O> | ... | I -> IResult<I,O> ) => I -> IResult<I, O> +/// ``` +/// All the parsers must have the same return type. +/// +/// If one of the parsers returns `Incomplete`, `alt!` will return `Incomplete`, to retry +/// once you get more input. Note that it is better for performance to know the +/// minimum size of data you need before you get into `alt!`. +/// +/// The `alt!` combinator is used in the following way: +/// +/// ```rust,ignore +/// alt!(parser_1 | parser_2 | ... | parser_n) +/// ``` +/// +/// # Basic example +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// // Create a parser that will match either "dragon" or "beast" +/// named!( dragon_or_beast, alt!( tag!( "dragon" ) | tag!( "beast" ) ) ); +/// +/// // Given the input "dragon slayer", the parser will match "dragon" +/// // and the rest will be " slayer" +/// let (rest, result) = dragon_or_beast(b"dragon slayer").unwrap(); +/// assert_eq!(result, b"dragon"); +/// assert_eq!(rest, b" slayer"); +/// +/// // Given the input "beast of Gevaudan", the parser will match "beast" +/// // and the rest will be " of Gevaudan" +/// let (rest, result) = dragon_or_beast(&b"beast of Gevaudan"[..]).unwrap(); +/// assert_eq!(result, b"beast"); +/// assert_eq!(rest, b" of Gevaudan"); +/// # } +/// ``` +/// +/// # Manipulate results +/// +/// There exists another syntax for `alt!` that gives you the ability to +/// manipulate the result from each parser: +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// # +/// // We create an enum to represent our creatures +/// #[derive(Debug,PartialEq,Eq)] +/// enum Creature { +/// Dragon, +/// Beast, +/// Unknown(usize) +/// } +/// +/// // Let's make a helper function that returns true when not a space +/// // we are required to do this because the `take_while!` macro is limited +/// // to idents, so we can't negate `ìs_space` at the call site +/// fn is_not_space(c: u8) -> bool { ! nom::character::is_space(c) } +/// +/// // Our parser will return the `Dragon` variant when matching "dragon", +/// // the `Beast` variant when matching "beast" and otherwise it will consume +/// // the input until a space is found and return an `Unknown` creature with +/// // the size of it's name. +/// named!(creature<Creature>, alt!( +/// tag!("dragon") => { |_| Creature::Dragon } | +/// tag!("beast") => { |_| Creature::Beast } | +/// take_while!(is_not_space) => { |r: &[u8]| Creature::Unknown(r.len()) } +/// // the closure takes the result as argument if the parser is successful +/// )); +/// +/// // Given the input "dragon slayer" the parser will return `Creature::Dragon` +/// // and the rest will be " slayer" +/// let (rest, result) = creature(b"dragon slayer").unwrap(); +/// assert_eq!(result, Creature::Dragon); +/// assert_eq!(rest, b" slayer"); +/// +/// // Given the input "beast of Gevaudan" the parser will return `Creature::Beast` +/// // and the rest will be " of Gevaudan" +/// let (rest, result) = creature(b"beast of Gevaudan").unwrap(); +/// assert_eq!(result, Creature::Beast); +/// assert_eq!(rest, b" of Gevaudan"); +/// +/// // Given the input "demon hunter" the parser will return `Creature::Unknown(5)` +/// // and the rest will be " hunter" +/// let (rest, result) = creature(b"demon hunter").unwrap(); +/// assert_eq!(result, Creature::Unknown(5)); +/// assert_eq!(rest, b" hunter"); +/// # } +/// ``` +/// +/// # Behaviour of `alt!` +/// +/// **BE CAREFUL** there is a case where the behaviour of `alt!` can be confusing: +/// +/// when the alternatives have different lengths, like this case: +/// +/// ```ignore +/// named!( test, alt!( tag!( "abcd" ) | tag!( "ef" ) | tag!( "ghi" ) | tag!( "kl" ) ) ); +/// ``` +/// +/// With this parser, if you pass `"abcd"` as input, the first alternative parses it correctly, +/// but if you pass `"efg"`, the first alternative will return `Incomplete`, since it needs an input +/// of 4 bytes. This behaviour of `alt!` is expected: if you get a partial input that isn't matched +/// by the first alternative, but would match if the input was complete, you want `alt!` to indicate +/// that it cannot decide with limited information. +/// +/// There are two ways to fix this behaviour. The first one consists in ordering the alternatives +/// by size, like this: +/// +/// ```ignore +/// named!( test, alt!( tag!( "ef" ) | tag!( "kl") | tag!( "ghi" ) | tag!( "abcd" ) ) ); +/// ``` +/// +/// With this solution, the largest alternative will be tested last. +/// +/// The other solution uses the `complete!` combinator, which transforms an `Incomplete` in an +/// `Error`. If one of the alternatives returns `Incomplete` but is wrapped by `complete!`, +/// `alt!` will try the next alternative. This is useful when you know that +/// you will not get partial input: +/// +/// ```ignore +/// named!( test, +/// alt!( +/// complete!( tag!( "abcd" ) ) | +/// complete!( tag!( "ef" ) ) | +/// complete!( tag!( "ghi" ) ) | +/// complete!( tag!( "kl" ) ) +/// ) +/// ); +/// ``` +/// +/// This behaviour of `alt!` can get especially confusing if multiple alternatives have different +/// sizes but a common prefix, like this: +/// +/// ```ignore +/// named!( test, alt!( tag!( "abcd" ) | tag!( "ab" ) | tag!( "ef" ) ) ); +/// ``` +/// +/// in that case, if you order by size, passing `"abcd"` as input will always be matched by the +/// smallest parser, so the solution using `complete!` is better suited. +/// +/// You can also nest multiple `alt!`, like this: +/// +/// ```ignore +/// named!( test, +/// alt!( +/// preceded!( +/// tag!("ab"), +/// alt!( +/// tag!( "cd" ) | +/// eof!() +/// ) +/// ) +/// | tag!( "ef" ) +/// ) +/// ); +/// ``` +/// +/// `preceded!` will first parse `"ab"` then, if successful, try the alternatives "cd", +/// or empty input (End Of File). If none of them work, `preceded!` will fail and +/// "ef" will be tested. +/// +#[macro_export(local_inner_macros)] +macro_rules! alt ( + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)* ) => ( + nom_compile_error!("alt uses '|' as separator, not ',': + + alt!( + tag!(\"abcd\") | + tag!(\"efgh\") | + tag!(\"ijkl\") + ) + "); + ); + (__impl $i:expr, $e:path, $($rest:tt)* ) => ( + alt!(__impl $i, call!($e) , $($rest)*); + ); + (__impl $i:expr, $e:path | $($rest:tt)*) => ( + alt!(__impl $i, call!($e) | $($rest)*); + ); + + (__impl $i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let i_ = $i.clone(); + let res = $subrule!(i_, $($args)*); + match res { + Ok(o) => Ok(o), + Err(Err::Error(e)) => { + let out = alt!(__impl $i, $($rest)*); + + // Compile-time hack to ensure that res's E type is not under-specified. + // This all has no effect at runtime. + #[allow(dead_code)] + fn unify_types<T>(_: &T, _: &T) {} + if let Err(Err::Error(ref e2)) = out { + unify_types(&e, e2); + } + + out + }, + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let i_ = $i.clone(); + match $subrule!(i_, $($args)* ) { + Ok((i,o)) => Ok((i,$gen(o))), + Err(Err::Error(e)) => { + let out = alt!(__impl $i, $($rest)*); + + // Compile-time hack to ensure that res's E type is not under-specified. + // This all has no effect at runtime. + fn unify_types<T>(_: &T, _: &T) {} + if let Err(Err::Error(ref e2)) = out { + unify_types(&e, e2); + } + + out + }, + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr, $e:path => { $gen:expr } | $($rest:tt)*) => ( + alt!(__impl $i, call!($e) => { $gen } | $($rest)*); + ); + + (__impl $i:expr, __end) => ( + { + use $crate::{Err,error::ErrorKind}; + let e2 = ErrorKind::Alt; + let err = Err::Error(error_position!($i, e2)); + + Err(err) + } + ); + + ($i:expr, $($rest:tt)*) => ( + { + alt!(__impl $i, $($rest)* | __end) + } + ); +); + +/// `switch!(I -> IResult<I,P>, P => I -> IResult<I,O> | ... | P => I -> IResult<I,O> ) => I -> IResult<I, O>` +/// choose the next parser depending on the result of the first one, if successful, +/// and returns the result of the second parser +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => tag!("XYZ") | +/// b"efgh" => tag!("123") +/// ) +/// ); +/// +/// let a = b"abcdXYZ123"; +/// let b = b"abcdef"; +/// let c = b"efgh123"; +/// let d = b"blah"; +/// +/// assert_eq!(sw(&a[..]), Ok((&b"123"[..], &b"XYZ"[..]))); +/// assert_eq!(sw(&b[..]), Err(Err::Error(error_node_position!(&b"abcdef"[..], ErrorKind::Switch, +/// error_position!(&b"ef"[..], ErrorKind::Tag))))); +/// assert_eq!(sw(&c[..]), Ok((&b""[..], &b"123"[..]))); +/// assert_eq!(sw(&d[..]), Err(Err::Error(error_position!(&b"blah"[..], ErrorKind::Switch)))); +/// # } +/// ``` +/// +/// You can specify a default case like with a normal match, using `_` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => tag!("XYZ") | +/// _ => value!(&b"default"[..]) +/// ) +/// ); +/// +/// let a = b"abcdXYZ123"; +/// let b = b"blah"; +/// +/// assert_eq!(sw(&a[..]), Ok((&b"123"[..], &b"XYZ"[..]))); +/// assert_eq!(sw(&b[..]), Ok((&b""[..], &b"default"[..]))); +/// # } +/// ``` +/// +/// Due to limitations in Rust macros, it is not possible to have simple functions on the right hand +/// side of pattern, like this: +/// +/// ```ignore +/// named!(xyz, tag!("XYZ")); +/// named!(num, tag!("123")); +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => xyz | +/// b"efgh" => 123 +/// ) +/// ); +/// ``` +/// +/// If you want to pass your own functions instead, you can use the `call!` combinator as follows: +/// +/// ```ignore +/// named!(xyz, tag!("XYZ")); +/// named!(num, tag!("123")); +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => call!(xyz) | +/// b"efgh" => call!(num) +/// ) +/// ); +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! switch ( + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $( $($p:pat)|+ => $subrule:ident!( $($args2:tt)* ))|* ) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::{Err,error::ErrorKind}; + + let i_ = $i.clone(); + match map!(i_, $submac!($($args)*), Some) { + Err(Err::Error(err)) => { + fn unify_types<T>(_: &T, _: &T) {} + let e1 = ErrorKind::Switch; + let e2 = error_position!($i, e1.clone()); + unify_types(&err, &e2); + + Err(Err::Error(error_node_position!($i, e1, err))) + }, + Err(e) => Err(e), + Ok((i, o)) => { + + match o { + $($(Some($p) )|+ => match $subrule!(i, $($args2)*) { + Err(Err::Error(err)) => { + fn unify_types<T>(_: &T, _: &T) {} + let e1 = ErrorKind::Switch; + let e2 = error_position!($i, e1.clone()); + unify_types(&err, &e2); + + Err(Err::Error(error_node_position!($i, e1, err))) + }, + Ok(o) => Ok(o), + Err(e) => Err(e), + }),*, + _ => Err(Err::convert(Err::Error(error_position!($i, ErrorKind::Switch)))) + } + } + } + } + ); + ($i:expr, $submac:ident!( $($args:tt)*), $($rest:tt)*) => ( + { + switch!(__impl $i, $submac!($($args)*), $($rest)*) + } + ); + ($i:expr, $e:path, $($rest:tt)*) => ( + { + switch!(__impl $i, call!($e), $($rest)*) + } + ); +); + +/// +/// +/// `permutation!(I -> IResult<I,A>, I -> IResult<I,B>, ... I -> IResult<I,X> ) => I -> IResult<I, (A,B,...X)>` +/// applies its sub parsers in a sequence, but independent from their order +/// this parser will only succeed if all of its sub parsers succeed +/// +/// the tuple of results is in the same order as the parsers are declared +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind,Needed}; +/// # fn main() { +/// named!(perm<(&[u8], &[u8], &[u8])>, +/// permutation!(tag!("abcd"), tag!("efg"), tag!("hi")) +/// ); +/// +/// // whatever the order, if the parser succeeds, each +/// // tag should have matched correctly +/// let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); +/// +/// let a = &b"abcdefghijk"[..]; +/// assert_eq!(perm(a), Ok((&b"jk"[..], expected))); +/// let b = &b"efgabcdhijkl"[..]; +/// assert_eq!(perm(b), Ok((&b"jkl"[..], expected))); +/// let c = &b"hiefgabcdjklm"[..]; +/// assert_eq!(perm(c), Ok((&b"jklm"[..], expected))); +/// +/// let d = &b"efgxyzabcdefghi"[..]; +/// assert_eq!(perm(d), Err(Err::Error(error_node_position!(&b"efgxyzabcdefghi"[..], ErrorKind::Permutation, +/// error_position!(&b"xyzabcdefghi"[..], ErrorKind::Permutation))))); +/// +/// let e = &b"efgabc"[..]; +/// assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4)))); +/// # } +/// ``` +/// +/// If one of the child parsers is followed by a `?`, that parser is now +/// optional: +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind,Needed}; +/// # fn main() { +/// named!(perm<&str, (Option<&str>, &str, &str)>, +/// permutation!(tag!("abcd")?, tag!("efg"), tag!("hi")) +/// ); +/// +/// // whatever the order, if the parser succeeds, each +/// // tag should have matched correctly +/// let expected = (Some("abcd"), "efg", "hi"); +/// +/// let a = "abcdefghijk"; +/// assert_eq!(perm(a), Ok(("jk", expected))); +/// let b = "efgabcdhijkl"; +/// assert_eq!(perm(b), Ok(("jkl", expected))); +/// let c = "hiefgabcdjklm"; +/// assert_eq!(perm(c), Ok(("jklm", expected))); +/// +/// // if `abcd` is missing: +/// let expected = (None, "efg", "hi"); +/// +/// let a = "efghijk"; +/// assert_eq!(perm(a), Ok(("jk", expected))); +/// let b = "efghijkl"; +/// assert_eq!(perm(b), Ok(("jkl", expected))); +/// let c = "hiefgjklm"; +/// assert_eq!(perm(c), Ok(("jklm", expected))); +/// +/// let e = "efgabc"; +/// assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! permutation ( + ($i:expr, $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::{Err,error::ErrorKind}; + + let mut res = permutation_init!((), $($rest)*); + let mut input = $i; + let mut error = None; + let mut needed = None; + + loop { + let mut all_done = true; + permutation_iterator!(0, input, all_done, needed, res, $($rest)*); + + //if we reach that part, it means none of the parsers were able to read anything + if !all_done { + //FIXME: should wrap the error returned by the child parser + error = Some(error_position!(input, ErrorKind::Permutation)); + } + break; + } + + if let Some(need) = needed { + Err(Err::convert(need)) + } else { + if let Some(unwrapped_res) = { permutation_unwrap!(0, (), res, $($rest)*) } { + Ok((input, unwrapped_res)) + } else { + if let Some(e) = error { + Err(Err::Error(error_node_position!($i, ErrorKind::Permutation, e))) + } else { + Err(Err::Error(error_position!($i, ErrorKind::Permutation))) + } + } + } + } + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! permutation_init ( + ((), $e:ident?, $($rest:tt)*) => ( + permutation_init!(($crate::lib::std::option::Option::None), $($rest)*) + ); + ((), $e:ident, $($rest:tt)*) => ( + permutation_init!(($crate::lib::std::option::Option::None), $($rest)*) + ); + + ((), $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ( + permutation_init!(($crate::lib::std::option::Option::None), $($rest)*) + ); + ((), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + permutation_init!(($crate::lib::std::option::Option::None), $($rest)*) + ); + + (($($parsed:expr),*), $e:ident?, $($rest:tt)*) => ( + permutation_init!(($($parsed),* , $crate::lib::std::option::Option::None), $($rest)*); + ); + (($($parsed:expr),*), $e:ident, $($rest:tt)*) => ( + permutation_init!(($($parsed),* , $crate::lib::std::option::Option::None), $($rest)*); + ); + + (($($parsed:expr),*), $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ( + permutation_init!(($($parsed),* , $crate::lib::std::option::Option::None), $($rest)*); + ); + (($($parsed:expr),*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + permutation_init!(($($parsed),* , $crate::lib::std::option::Option::None), $($rest)*); + ); + + (($($parsed:expr),*), $e:ident) => ( + ($($parsed),* , $crate::lib::std::option::Option::None) + ); + (($($parsed:expr),*), $e:ident?) => ( + ($($parsed),* , $crate::lib::std::option::Option::None) + ); + + (($($parsed:expr),*), $submac:ident!( $($args:tt)* )?) => ( + ($($parsed),* , $crate::lib::std::option::Option::None) + ); + (($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( + ($($parsed),* , $crate::lib::std::option::Option::None) + ); + (($($parsed:expr),*),) => ( + ($($parsed),*) + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! succ ( + (0, $submac:ident ! ($($rest:tt)*)) => ($submac!(1, $($rest)*)); + (1, $submac:ident ! ($($rest:tt)*)) => ($submac!(2, $($rest)*)); + (2, $submac:ident ! ($($rest:tt)*)) => ($submac!(3, $($rest)*)); + (3, $submac:ident ! ($($rest:tt)*)) => ($submac!(4, $($rest)*)); + (4, $submac:ident ! ($($rest:tt)*)) => ($submac!(5, $($rest)*)); + (5, $submac:ident ! ($($rest:tt)*)) => ($submac!(6, $($rest)*)); + (6, $submac:ident ! ($($rest:tt)*)) => ($submac!(7, $($rest)*)); + (7, $submac:ident ! ($($rest:tt)*)) => ($submac!(8, $($rest)*)); + (8, $submac:ident ! ($($rest:tt)*)) => ($submac!(9, $($rest)*)); + (9, $submac:ident ! ($($rest:tt)*)) => ($submac!(10, $($rest)*)); + (10, $submac:ident ! ($($rest:tt)*)) => ($submac!(11, $($rest)*)); + (11, $submac:ident ! ($($rest:tt)*)) => ($submac!(12, $($rest)*)); + (12, $submac:ident ! ($($rest:tt)*)) => ($submac!(13, $($rest)*)); + (13, $submac:ident ! ($($rest:tt)*)) => ($submac!(14, $($rest)*)); + (14, $submac:ident ! ($($rest:tt)*)) => ($submac!(15, $($rest)*)); + (15, $submac:ident ! ($($rest:tt)*)) => ($submac!(16, $($rest)*)); + (16, $submac:ident ! ($($rest:tt)*)) => ($submac!(17, $($rest)*)); + (17, $submac:ident ! ($($rest:tt)*)) => ($submac!(18, $($rest)*)); + (18, $submac:ident ! ($($rest:tt)*)) => ($submac!(19, $($rest)*)); + (19, $submac:ident ! ($($rest:tt)*)) => ($submac!(20, $($rest)*)); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! permutation_unwrap ( + ($it:tt, (), $res:ident, $e:ident?, $($rest:tt)*) => ( + succ!($it, permutation_unwrap!(($res.$it), $res, $($rest)*)); + ); + ($it:tt, (), $res:ident, $e:ident, $($rest:tt)*) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_unwrap!((res.unwrap()), $res, $($rest)*)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, (), $res:ident, $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ( + succ!($it, permutation_unwrap!(($res.$it), $res, $($rest)*)); + ); + ($it:tt, (), $res:ident, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_unwrap!((res.unwrap()), $res, $($rest)*)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident, $e:ident?, $($rest:tt)*) => ( + succ!($it, permutation_unwrap!(($($parsed),* , $res.$it), $res, $($rest)*)); + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $e:ident, $($rest:tt)*) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_unwrap!(($($parsed),* , res.unwrap()), $res, $($rest)*)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident, $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ( + succ!($it, permutation_unwrap!(($($parsed),* , $res.$it), $res, $($rest)*)); + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_unwrap!(($($parsed),* , res.unwrap()), $res, $($rest)*)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident?, $e:ident) => ( + $crate::lib::std::option::Option::Some(($($parsed),* , { $res.$it })) + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $e:ident) => ({ + let res = $res.$it; + if res.is_some() { + $crate::lib::std::option::Option::Some(($($parsed),* , res.unwrap() )) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident, $submac:ident!( $($args:tt)* )?) => ( + $crate::lib::std::option::Option::Some(($($parsed),* , { $res.$it })) + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $submac:ident!( $($args:tt)* )) => ({ + let res = $res.$it; + if res.is_some() { + $crate::lib::std::option::Option::Some(($($parsed),* , res.unwrap() )) + } else { + $crate::lib::std::option::Option::None + } + }); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! permutation_iterator ( + ($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident?, $($rest:tt)*) => ( + permutation_iterator!($it, $i, $all_done, $needed, $res, call!($e), $($rest)*); + ); + ($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident, $($rest:tt)*) => ( + permutation_iterator!($it, $i, $all_done, $needed, $res, call!($e), $($rest)*); + ); + + ($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => { + permutation_iterator!($it, $i, $all_done, $needed, $res, $submac!($($args)*) , $($rest)*); + }; + ($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ({ + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::Err; + + if $res.$it.is_none() { + match $submac!($i, $($args)*) { + Ok((i,o)) => { + $i = i; + $res.$it = Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + $needed = Some(e); + break; + } + }; + } + succ!($it, permutation_iterator!($i, $all_done, $needed, $res, $($rest)*)); + }); + + ($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident?) => ( + permutation_iterator!($it, $i, $all_done, $needed, $res, call!($e)); + ); + ($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident) => ( + permutation_iterator!($it, $i, $all_done, $needed, $res, call!($e)); + ); + + ($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )?) => { + permutation_iterator!($it, $i, $all_done, $needed, $res, $submac!($($args)*)); + }; + ($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::Err; + + if $res.$it.is_none() { + match $submac!($i, $($args)*) { + Ok((i,o)) => { + $i = i; + $res.$it = Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + $needed = Some(e); + break; + } + }; + } + }); +); + +#[cfg(test)] +mod tests { + use crate::error::ErrorKind; + #[cfg(feature = "alloc")] + use crate::{ + error::ParseError, + lib::std::{ + fmt::Debug, + string::{String, ToString} + } + }; + use crate::internal::{Err, IResult, Needed}; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes<T: $crate::AsBytes>(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); + ); + + macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + use $crate::lib::std::cmp::min; + + let len = $i.len(); + let blen = $bytes.len(); + let m = min(len, blen); + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: IResult<_,_,_> = if reduced != b { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(error_position!($i, e))) + } else if m < blen { + Err(Err::Incomplete(Needed::Size(blen))) + } else { + Ok((&$i[blen..], reduced)) + }; + res + } + ); + ); + + macro_rules! take( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res:IResult<&[u8],&[u8],_> = if $i.len() < cnt { + Err(Err::Incomplete(Needed::Size(cnt))) + } else { + Ok((&$i[cnt..],&$i[0..cnt])) + }; + res + } + ); + ); + + #[cfg(feature = "alloc")] + #[derive(Debug, Clone, PartialEq)] + pub struct ErrorStr(String); + + #[cfg(feature = "alloc")] + impl From<u32> for ErrorStr { + fn from(i: u32) -> Self { + ErrorStr(format!("custom error code: {}", i)) + } + } + + #[cfg(feature = "alloc")] + impl<'a> From<&'a str> for ErrorStr { + fn from(i: &'a str) -> Self { + ErrorStr(format!("custom error message: {}", i)) + } + } + + #[cfg(feature = "alloc")] + impl<I: Debug> ParseError<I> for ErrorStr { + fn from_error_kind(input: I, kind: ErrorKind) -> Self { + ErrorStr(format!("custom error message: ({:?}, {:?})", input, kind)) + } + + fn append(input: I, kind: ErrorKind, other: Self) -> Self { + ErrorStr(format!("custom error message: ({:?}, {:?}) - {:?}", input, kind, other)) + } + } + + #[cfg(feature = "alloc")] + #[test] + fn alt() { + fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Ok((&b""[..], input)) + } + + #[allow(unused_variables)] + fn dont_work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Err(Err::Error(ErrorStr("abcd".to_string()))) + } + + fn work2(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Ok((input, &b""[..])) + } + + fn alt1(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | dont_work) + } + fn alt2(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | work) + } + fn alt3(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | dont_work | work2 | dont_work) + } + //named!(alt1, alt!(dont_work | dont_work)); + //named!(alt2, alt!(dont_work | work)); + //named!(alt3, alt!(dont_work | dont_work | work2 | dont_work)); + + let a = &b"abcd"[..]; + assert_eq!(alt1(a), Err(Err::Error(error_position!(a, ErrorKind::Alt)))); + assert_eq!(alt2(a), Ok((&b""[..], a))); + assert_eq!(alt3(a), Ok((a, &b""[..]))); + + named!(alt4, alt!(tag!("abcd") | tag!("efgh"))); + let b = &b"efgh"[..]; + assert_eq!(alt4(a), Ok((&b""[..], a))); + assert_eq!(alt4(b), Ok((&b""[..], b))); + + // test the alternative syntax + named!(alt5<bool>, alt!(tag!("abcd") => { |_| false } | tag!("efgh") => { |_| true })); + assert_eq!(alt5(a), Ok((&b""[..], false))); + assert_eq!(alt5(b), Ok((&b""[..], true))); + + // compile-time test guarding against an underspecified E generic type (#474) + named!(alt_eof1, alt!(eof!() | eof!())); + named!(alt_eof2, alt!(eof!() => {|x| x} | eof!() => {|x| x})); + let _ = (alt_eof1, alt_eof2); + } + + #[test] + fn alt_incomplete() { + named!(alt1, alt!(tag!("a") | tag!("bc") | tag!("def"))); + + let a = &b""[..]; + assert_eq!(alt1(a), Err(Err::Incomplete(Needed::Size(1)))); + let a = &b"b"[..]; + assert_eq!(alt1(a), Err(Err::Incomplete(Needed::Size(2)))); + let a = &b"bcd"[..]; + assert_eq!(alt1(a), Ok((&b"d"[..], &b"bc"[..]))); + let a = &b"cde"[..]; + assert_eq!(alt1(a), Err(Err::Error(error_position!(a, ErrorKind::Alt)))); + let a = &b"de"[..]; + assert_eq!(alt1(a), Err(Err::Incomplete(Needed::Size(3)))); + let a = &b"defg"[..]; + assert_eq!(alt1(a), Ok((&b"g"[..], &b"def"[..]))); + } + + #[allow(unused_variables)] + #[test] + fn switch() { + named!( + sw, + switch!(take!(4), + b"abcd" | b"xxxx" => take!(2) | + b"efgh" => take!(4) + ) + ); + + let a = &b"abcdefgh"[..]; + assert_eq!(sw(a), Ok((&b"gh"[..], &b"ef"[..]))); + + let b = &b"efghijkl"[..]; + assert_eq!(sw(b), Ok((&b""[..], &b"ijkl"[..]))); + let c = &b"afghijkl"[..]; + assert_eq!(sw(c), Err(Err::Error(error_position!(&b"afghijkl"[..], ErrorKind::Switch)))); + + let a = &b"xxxxefgh"[..]; + assert_eq!(sw(a), Ok((&b"gh"[..], &b"ef"[..]))); + } + + #[test] + fn permutation() { + named!(perm<(&[u8], &[u8], &[u8])>, permutation!(tag!("abcd"), tag!("efg"), tag!("hi"))); + + let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); + + let a = &b"abcdefghijk"[..]; + assert_eq!(perm(a), Ok((&b"jk"[..], expected))); + let b = &b"efgabcdhijk"[..]; + assert_eq!(perm(b), Ok((&b"jk"[..], expected))); + let c = &b"hiefgabcdjk"[..]; + assert_eq!(perm(c), Ok((&b"jk"[..], expected))); + + let d = &b"efgxyzabcdefghi"[..]; + assert_eq!( + perm(d), + Err(Err::Error(error_node_position!( + &b"efgxyzabcdefghi"[..], + ErrorKind::Permutation, + error_position!(&b"xyzabcdefghi"[..], ErrorKind::Permutation) + ))) + ); + + let e = &b"efgabc"[..]; + assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4)))); + } + + /* + named!(does_not_compile, + alt!(tag!("abcd"), tag!("efgh")) + ); + */ +} diff --git a/third_party/rust/nom/src/branch/mod.rs b/third_party/rust/nom/src/branch/mod.rs new file mode 100644 index 0000000000..55d3633c8f --- /dev/null +++ b/third_party/rust/nom/src/branch/mod.rs @@ -0,0 +1,263 @@ +//! choice combinators + +#[macro_use] +mod macros; + +use crate::error::ErrorKind; +use crate::error::ParseError; +use crate::internal::{Err, IResult}; + +/// helper trait for the [alt()] combinator +/// +/// this trait is implemented for tuples of up to 21 elements +pub trait Alt<I, O, E> { + /// tests each parser in the tuple and returns the result of the first one that succeeds + fn choice(&self, input: I) -> IResult<I, O, E>; +} + +/// tests a list of parsers one by one until one succeeds +/// +/// It takes as argument a tuple of parsers. +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, Needed, IResult}; +/// use nom::character::complete::{alpha1, digit1}; +/// use nom::branch::alt; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// alt((alpha1, digit1))(input) +/// }; +/// +/// // the first parser, alpha1, recognizes the input +/// assert_eq!(parser("abc"), Ok(("", "abc"))); +/// +/// // the first parser returns an error, so alt tries the second one +/// assert_eq!(parser("123456"), Ok(("", "123456"))); +/// +/// // both parsers failed, and with the default error type, alt will return the last error +/// assert_eq!(parser(" "), Err(Err::Error(error_position!(" ", ErrorKind::Digit)))); +/// # } +/// ``` +/// +/// with a custom error type, it is possible to have alt return the error of the parser +/// that went the farthest in the input data +pub fn alt<I: Clone, O, E: ParseError<I>, List: Alt<I, O, E>>(l: List) -> impl Fn(I) -> IResult<I, O, E> { + move |i: I| l.choice(i) +} + +/// helper trait for the [permutation()] combinator +/// +/// this trait is implemented for tuples of up to 21 elements +pub trait Permutation<I, O, E> { + /// tries to apply all parsers in the tuple in various orders until all of them succeed + fn permutation(&self, input: I) -> IResult<I, O, E>; +} + +/// applies a list of parsers in any order +/// +/// permutation will succeed if all of the child parsers succeeded. +/// It takes as argument a tuple of parsers, and returns a +/// tuple of the parser results. +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, Needed, IResult}; +/// use nom::character::complete::{alpha1, digit1}; +/// use nom::branch::permutation; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, (&str, &str)> { +/// permutation((alpha1, digit1))(input) +/// }; +/// +/// // permutation recognizes alphabetic characters then digit +/// assert_eq!(parser("abc123"), Ok(("", ("abc", "123")))); +/// +/// // but also in inverse order +/// assert_eq!(parser("123abc"), Ok(("", ("abc", "123")))); +/// +/// // it will fail if one of the parsers failed +/// assert_eq!(parser("abc;"), Err(Err::Error(error_position!(";", ErrorKind::Permutation)))); +/// # } +/// ``` +pub fn permutation<I: Clone, O, E: ParseError<I>, List: Permutation<I, O, E>>(l: List) -> impl Fn(I) -> IResult<I, O, E> { + move |i: I| l.permutation(i) +} + +macro_rules! alt_trait( + ($first:ident $second:ident $($id: ident)+) => ( + alt_trait!(__impl $first $second; $($id)+); + ); + (__impl $($current:ident)*; $head:ident $($id: ident)+) => ( + alt_trait_impl!($($current)*); + + alt_trait!(__impl $($current)* $head; $($id)+); + ); + (__impl $($current:ident)*; $head:ident) => ( + alt_trait_impl!($($current)*); + alt_trait_impl!($($current)* $head); + ); +); + +macro_rules! alt_trait_impl( + ($($id:ident)+) => ( + impl< + Input: Clone, Output, Error: ParseError<Input>, + $($id: Fn(Input) -> IResult<Input, Output, Error>),+ + > Alt<Input, Output, Error> for ( $($id),+ ) { + + fn choice(&self, input: Input) -> IResult<Input, Output, Error> { + let mut err: Option<Error> = None; + alt_trait_inner!(0, self, input, err, $($id)+); + + Err(Err::Error(Error::append(input, ErrorKind::Alt, err.unwrap()))) + } + } + ); +); + +macro_rules! alt_trait_inner( + ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident $($id:ident)+) => ( + match $self.$it($input.clone()) { + Err(Err::Error(e)) => { + $err = Some(match $err.take() { + None => e, + Some(prev) => prev.or(e), + }); + succ!($it, alt_trait_inner!($self, $input, $err, $($id)+)) + }, + res => return res, + } + ); + ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident) => ( + match $self.$it($input.clone()) { + Err(Err::Error(e)) => { + $err = Some(match $err.take() { + None => e, + Some(prev) => prev.or(e), + }); + }, + res => return res, + } + ); +); + +alt_trait!(A B C D E F G H I J K L M N O P Q R S T U); + +macro_rules! permutation_trait( + ($name1:ident $ty1:ident, $name2:ident $ty2:ident) => ( + permutation_trait_impl!($name1 $ty1, $name2 $ty2); + ); + ($name1:ident $ty1:ident, $name2: ident $ty2:ident, $($name:ident $ty:ident),*) => ( + permutation_trait!(__impl $name1 $ty1, $name2 $ty2; $($name $ty),*); + ); + (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident, $($name2:ident $ty2:ident),*) => ( + permutation_trait_impl!($($name $ty),+); + permutation_trait!(__impl $($name $ty),+ , $name1 $ty1; $($name2 $ty2),*); + ); + (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident) => ( + permutation_trait_impl!($($name $ty),+); + permutation_trait_impl!($($name $ty),+, $name1 $ty1); + ); +); + +macro_rules! permutation_trait_impl( + ($($name:ident $ty: ident),+) => ( + impl< + Input: Clone, $($ty),+ , Error: ParseError<Input>, + $($name: Fn(Input) -> IResult<Input, $ty, Error>),+ + > Permutation<Input, ( $($ty),+ ), Error> for ( $($name),+ ) { + + fn permutation(&self, mut input: Input) -> IResult<Input, ( $($ty),+ ), Error> { + let mut res = permutation_init!((), $($name),+); + + loop { + let mut all_done = true; + permutation_trait_inner!(0, self, input, res, all_done, $($name)+); + + //if we reach that part, it means none of the parsers were able to read anything + if !all_done { + //FIXME: should wrap the error returned by the child parser + return Err(Err::Error(error_position!(input, ErrorKind::Permutation))); + } + break; + } + + if let Some(unwrapped_res) = { permutation_trait_unwrap!(0, (), res, $($name),+) } { + Ok((input, unwrapped_res)) + } else { + Err(Err::Error(error_position!(input, ErrorKind::Permutation))) + } + } + } + ); +); + +macro_rules! permutation_trait_inner( + ($it:tt, $self:expr, $input:ident, $res:expr, $all_done:expr, $head:ident $($id:ident)+) => ({ + if $res.$it.is_none() { + match $self.$it($input.clone()) { + Ok((i,o)) => { + $input = i; + $res.$it = Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + return Err(e); + } + }; + } + succ!($it, permutation_trait_inner!($self, $input, $res, $all_done, $($id)+)); + }); + ($it:tt, $self:expr, $input:ident, $res:expr, $all_done:expr, $head:ident) => ({ + if $res.$it.is_none() { + match $self.$it($input.clone()) { + Ok((i,o)) => { + $input = i; + $res.$it = Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + return Err(e); + } + }; + } + }); +); + +macro_rules! permutation_trait_unwrap ( + ($it:tt, (), $res:ident, $e:ident, $($name:ident),+) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_trait_unwrap!((res.unwrap()), $res, $($name),+)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident, $e:ident, $($name:ident),+) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_trait_unwrap!(($($parsed),* , res.unwrap()), $res, $($name),+)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident, $name:ident) => ({ + let res = $res.$it; + if res.is_some() { + $crate::lib::std::option::Option::Some(($($parsed),* , res.unwrap() )) + } else { + $crate::lib::std::option::Option::None + } + }); +); + +permutation_trait!(FnA A, FnB B, FnC C, FnD D, FnE E, FnF F, FnG G, FnH H, FnI I, FnJ J, FnK K, FnL L, FnM M, FnN N, FnO O, FnP P, FnQ Q, FnR R, FnS S, FnT T, FnU U); diff --git a/third_party/rust/nom/src/bytes/complete.rs b/third_party/rust/nom/src/bytes/complete.rs new file mode 100644 index 0000000000..8bf42204ba --- /dev/null +++ b/third_party/rust/nom/src/bytes/complete.rs @@ -0,0 +1,658 @@ +//! parsers recognizing bytes streams, complete input version + +use crate::error::ErrorKind; +use crate::error::ParseError; +use crate::internal::{Err, IResult}; +use crate::lib::std::ops::RangeFrom; +use crate::lib::std::result::Result::*; +use crate::traits::{Compare, CompareResult, FindSubstring, FindToken, InputIter, InputLength, InputTake, InputTakeAtPosition, Slice, ToUsize}; + +/// Recognizes a pattern +/// +/// The input data will be compared to the tag combinator's argument and will return the part of +/// the input that matches the argument +/// +/// It will return `Err(Err::Error((_, ErrorKind::Tag)))` if the input doesn't match the pattern +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, &str> { +/// tag("Hello")(s) +/// } +/// +/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); +/// assert_eq!(parser("Something"), Err(Err::Error(("Something", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// ``` +pub fn tag<'a, T: 'a, Input: 'a, Error: ParseError<Input>>(tag: T) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTake + Compare<T>, + T: InputLength + Clone, +{ + move |i: Input| { + let tag_len = tag.input_len(); + let t = tag.clone(); + let res: IResult<_, _, Error> = match i.compare(t) { + CompareResult::Ok => Ok(i.take_split(tag_len)), + _ => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(Error::from_error_kind(i, e))) + } + }; + res + } +} + +/// Recognizes a case insensitive pattern +/// +/// The input data will be compared to the tag combinator's argument and will return the part of +/// the input that matches the argument with no regard to case +/// +/// It will return `Err(Err::Error((_, ErrorKind::Tag)))` if the input doesn't match the pattern +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::tag_no_case; +/// +/// fn parser(s: &str) -> IResult<&str, &str> { +/// tag_no_case("hello")(s) +/// } +/// +/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); +/// assert_eq!(parser("hello, World!"), Ok((", World!", "hello"))); +/// assert_eq!(parser("HeLlO, World!"), Ok((", World!", "HeLlO"))); +/// assert_eq!(parser("Something"), Err(Err::Error(("Something", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// ``` +pub fn tag_no_case<T, Input, Error: ParseError<Input>>(tag: T) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTake + Compare<T>, + T: InputLength + Clone, +{ + move |i: Input| { + let tag_len = tag.input_len(); + let t = tag.clone(); + + let res: IResult<_, _, Error> = match (i).compare_no_case(t) { + CompareResult::Ok => Ok(i.take_split(tag_len)), + _ => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(Error::from_error_kind(i, e))) + } + }; + res + } +} + +/// Parse till certain characters are met +/// +/// The parser will return the longest slice till one of the characters of the combinator's argument are met. +/// +/// It doesn't consume the matched character, +/// +/// It will return a `Err::Error(("", ErrorKind::IsNot))` if the pattern wasn't met +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::is_not; +/// +/// fn not_space(s: &str) -> IResult<&str, &str> { +/// is_not(" \t\r\n")(s) +/// } +/// +/// assert_eq!(not_space("Hello, World!"), Ok((" World!", "Hello,"))); +/// assert_eq!(not_space("Sometimes\t"), Ok(("\t", "Sometimes"))); +/// assert_eq!(not_space("Nospace"), Ok(("", "Nospace"))); +/// assert_eq!(not_space(""), Err(Err::Error(("", ErrorKind::IsNot)))); +/// ``` +pub fn is_not<T, Input, Error: ParseError<Input>>(arr: T) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + T: InputLength + FindToken<<Input as InputTakeAtPosition>::Item>, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::IsNot; + i.split_at_position1_complete(|c| arr.find_token(c), e) + } +} + +/// Returns the longest slice of the matches the pattern +/// +/// The parser will return the longest slice consisting of the characters in provided in the +/// combinator's argument +/// +/// It will return a `Err(Err::Error((_, ErrorKind::IsA)))` if the pattern wasn't met +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::is_a; +/// +/// fn hex(s: &str) -> IResult<&str, &str> { +/// is_a("1234567890ABCDEF")(s) +/// } +/// +/// assert_eq!(hex("123 and voila"), Ok((" and voila", "123"))); +/// assert_eq!(hex("DEADBEEF and others"), Ok((" and others", "DEADBEEF"))); +/// assert_eq!(hex("BADBABEsomething"), Ok(("something", "BADBABE"))); +/// assert_eq!(hex("D15EA5E"), Ok(("", "D15EA5E"))); +/// assert_eq!(hex(""), Err(Err::Error(("", ErrorKind::IsA)))); +/// ``` +pub fn is_a<T, Input, Error: ParseError<Input>>(arr: T) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + T: InputLength + FindToken<<Input as InputTakeAtPosition>::Item>, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::IsA; + i.split_at_position1_complete(|c| !arr.find_token(c), e) + } +} + +/// Returns the longest input slice (if any) that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_while; +/// use nom::character::is_alphabetic; +/// +/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while(is_alphabetic)(s) +/// } +/// +/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(alpha(b"12345"), Ok((&b"12345"[..], &b""[..]))); +/// assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); +/// assert_eq!(alpha(b""), Ok((&b""[..], &b""[..]))); +/// ``` +pub fn take_while<F, Input, Error: ParseError<Input>>(cond: F) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, +{ + move |i: Input| i.split_at_position_complete(|c| !cond(c)) +} + +/// Returns the longest (atleast 1) input slice that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// It will return an `Err(Err::Error((_, ErrorKind::TakeWhile1)))` if the pattern wasn't met +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_while1; +/// use nom::character::is_alphabetic; +/// +/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while1(is_alphabetic)(s) +/// } +/// +/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); +/// assert_eq!(alpha(b"12345"), Err(Err::Error((&b"12345"[..], ErrorKind::TakeWhile1)))); +/// ``` +pub fn take_while1<F, Input, Error: ParseError<Input>>(cond: F) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::TakeWhile1; + i.split_at_position1_complete(|c| !cond(c), e) + } +} + +/// Returns the longest (m <= len <= n) input slice that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// It will return an `Err::Error((_, ErrorKind::TakeWhileMN))` if the pattern wasn't met or is out +/// of range (m <= len <= n) +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_while_m_n; +/// use nom::character::is_alphabetic; +/// +/// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while_m_n(3, 6, is_alphabetic)(s) +/// } +/// +/// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(short_alpha(b"lengthy"), Ok((&b"y"[..], &b"length"[..]))); +/// assert_eq!(short_alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); +/// assert_eq!(short_alpha(b"ed"), Err(Err::Error((&b"ed"[..], ErrorKind::TakeWhileMN)))); +/// assert_eq!(short_alpha(b"12345"), Err(Err::Error((&b"12345"[..], ErrorKind::TakeWhileMN)))); +/// ``` +pub fn take_while_m_n<F, Input, Error: ParseError<Input>>(m: usize, n: usize, cond: F) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTake + InputIter + InputLength + Slice<RangeFrom<usize>>, + F: Fn(<Input as InputIter>::Item) -> bool, +{ + move |i: Input| { + let input = i; + + match input.position(|c| !cond(c)) { + Some(idx) => { + if idx >= m { + if idx <= n { + let res: IResult<_, _, Error> = if let Some(index) = input.slice_index(idx) { + Ok(input.take_split(index)) + } else { + Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + }; + res + } else { + let res: IResult<_, _, Error> = if let Some(index) = input.slice_index(n) { + Ok(input.take_split(index)) + } else { + Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + }; + res + } + } else { + let e = ErrorKind::TakeWhileMN; + Err(Err::Error(Error::from_error_kind(input, e))) + } + } + None => { + let len = input.input_len(); + if len >= n { + match input.slice_index(n) { + Some(index) => Ok(input.take_split(index)), + None => Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + } + } else { + if len >= m && len <= n { + let res: IResult<_, _, Error> = Ok((input.slice(len..), input)); + res + } else { + let e = ErrorKind::TakeWhileMN; + Err(Err::Error(Error::from_error_kind(input, e))) + } + } + } + } + } +} + +/// Returns the longest input slice (if any) till a predicate is met +/// +/// The parser will return the longest slice till the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_till; +/// +/// fn till_colon(s: &str) -> IResult<&str, &str> { +/// take_till(|c| c == ':')(s) +/// } +/// +/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); +/// assert_eq!(till_colon(":empty matched"), Ok((":empty matched", ""))); //allowed +/// assert_eq!(till_colon("12345"), Ok(("", "12345"))); +/// assert_eq!(till_colon(""), Ok(("", ""))); +/// ``` +pub fn take_till<F, Input, Error: ParseError<Input>>(cond: F) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, +{ + move |i: Input| i.split_at_position_complete(|c| cond(c)) +} + +/// Returns the longest (atleast 1) input slice till a predicate is met +/// +/// The parser will return the longest slice till the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// It will return `Err(Err::Error((_, ErrorKind::TakeTill1)))` if the input is empty or the +/// predicate matches the first input +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_till1; +/// +/// fn till_colon(s: &str) -> IResult<&str, &str> { +/// take_till1(|c| c == ':')(s) +/// } +/// +/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); +/// assert_eq!(till_colon(":empty matched"), Err(Err::Error((":empty matched", ErrorKind::TakeTill1)))); +/// assert_eq!(till_colon("12345"), Ok(("", "12345"))); +/// assert_eq!(till_colon(""), Err(Err::Error(("", ErrorKind::TakeTill1)))); +/// ``` +pub fn take_till1<F, Input, Error: ParseError<Input>>(cond: F) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::TakeTill1; + i.split_at_position1_complete(|c| cond(c), e) + } +} + +/// Returns an input slice containing the first N input elements (Input[..N]) +/// +/// It will return `Err(Err::Error((_, ErrorKind::Eof)))` if the input is shorter than the argument +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take; +/// +/// fn take6(s: &str) -> IResult<&str, &str> { +/// take(6usize)(s) +/// } +/// +/// assert_eq!(take6("1234567"), Ok(("7", "123456"))); +/// assert_eq!(take6("things"), Ok(("", "things"))); +/// assert_eq!(take6("short"), Err(Err::Error(("short", ErrorKind::Eof)))); +/// assert_eq!(take6(""), Err(Err::Error(("", ErrorKind::Eof)))); +/// ``` +pub fn take<C, Input, Error: ParseError<Input>>(count: C) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputIter + InputTake, + C: ToUsize, +{ + let c = count.to_usize(); + move |i: Input| match i.slice_index(c) { + None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), + Some(index) => Ok(i.take_split(index)), + } +} + +/// Returns the longest input slice till it matches the pattern. +/// +/// It doesn't consume the pattern. It will return `Err(Err::Error((_, ErrorKind::TakeUntil)))` +/// if the pattern wasn't met +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_until; +/// +/// fn until_eof(s: &str) -> IResult<&str, &str> { +/// take_until("eof")(s) +/// } +/// +/// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world"))); +/// assert_eq!(until_eof("hello, world"), Err(Err::Error(("hello, world", ErrorKind::TakeUntil)))); +/// assert_eq!(until_eof(""), Err(Err::Error(("", ErrorKind::TakeUntil)))); +/// ``` +pub fn take_until<T, Input, Error: ParseError<Input>>(tag: T) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTake + FindSubstring<T>, + T: InputLength + Clone, +{ + move |i: Input| { + let t = tag.clone(); + let res: IResult<_, _, Error> = match i.find_substring(t) { + None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), + Some(index) => Ok(i.take_split(index)), + }; + res + } +} + +/// Matches a byte string with escaped characters. +/// +/// * The first argument matches the normal characters (it must not accept the control character), +/// * the second argument is the control character (like `\` in most languages), +/// * the third argument matches the escaped characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use nom::character::complete::digit1; +/// use nom::bytes::complete::escaped; +/// use nom::character::complete::one_of; +/// +/// fn esc(s: &str) -> IResult<&str, &str> { +/// escaped(digit1, '\\', one_of(r#""n\"#))(s) +/// } +/// +/// assert_eq!(esc("123;"), Ok((";", "123"))); +/// assert_eq!(esc(r#"12\"34;"#), Ok((";", r#"12\"34"#))); +/// ``` +/// +pub fn escaped<Input, Error, F, G, O1, O2>(normal: F, control_char: char, escapable: G) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter, + <Input as InputIter>::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult<Input, O1, Error>, + G: Fn(Input) -> IResult<Input, O2, Error>, + Error: ParseError<Input>, +{ + use crate::traits::AsChar; + + move |input: Input| { + let mut i = input.clone(); + + while i.input_len() > 0 { + match normal(i.clone()) { + Ok((i2, _)) => { + if i2.input_len() == 0 { + return Ok((input.slice(input.input_len()..), input)); + } else { + i = i2; + } + } + Err(Err::Error(_)) => { + // unwrap() should be safe here since index < $i.input_len() + if i.iter_elements().next().unwrap().as_char() == control_char { + let next = control_char.len_utf8(); + if next >= i.input_len() { + return Err(Err::Error(Error::from_error_kind(input, ErrorKind::Escaped))); + } else { + match escapable(i.slice(next..)) { + Ok((i2, _)) => { + if i2.input_len() == 0 { + return Ok((input.slice(input.input_len()..), input)); + } else { + i = i2; + } + } + Err(e) => return Err(e), + } + } + } else { + let index = input.offset(&i); + if index == 0 { + return Err(Err::Error(Error::from_error_kind(input, ErrorKind::Escaped))); + } + return Ok(input.take_split(index)); + } + } + Err(e) => { + return Err(e); + } + } + } + + Ok((input.slice(input.input_len()..), input)) + } +} + +#[doc(hidden)] +pub fn escapedc<Input, Error, F, G, O1, O2>(i: Input, normal: F, control_char: char, escapable: G) -> IResult<Input, Input, Error> +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter, + <Input as InputIter>::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult<Input, O1, Error>, + G: Fn(Input) -> IResult<Input, O2, Error>, + Error: ParseError<Input>, +{ + escaped(normal, control_char, escapable)(i) +} + +/// Matches a byte string with escaped characters. +/// +/// * The first argument matches the normal characters (it must not match the control character), +/// * the second argument is the control character (like `\` in most languages), +/// * the third argument matches the escaped characters and transforms them. +/// +/// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character) +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use std::str::from_utf8; +/// use nom::bytes::complete::escaped_transform; +/// use nom::character::complete::alpha1; +/// +/// fn parser(input: &str) -> IResult<&str, String> { +/// escaped_transform( +/// alpha1, +/// '\\', +/// |i:&str| alt!(i, +/// tag!("\\") => { |_| "\\" } +/// | tag!("\"") => { |_| "\"" } +/// | tag!("n") => { |_| "\n" } +/// ) +/// )(input) +/// } +/// +/// assert_eq!(parser("ab\\\"cd"), Ok(("", String::from("ab\"cd")))); +/// ``` +#[cfg(feature = "alloc")] +pub fn escaped_transform<Input, Error, F, G, O1, O2, ExtendItem, Output>( + normal: F, + control_char: char, + transform: G, +) -> impl Fn(Input) -> IResult<Input, Output, Error> +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter, + Input: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + O1: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + O2: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + Output: core::iter::Extend<<Input as crate::traits::ExtendInto>::Item>, + Output: core::iter::Extend<<O1 as crate::traits::ExtendInto>::Item>, + Output: core::iter::Extend<<O2 as crate::traits::ExtendInto>::Item>, + <Input as InputIter>::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult<Input, O1, Error>, + G: Fn(Input) -> IResult<Input, O2, Error>, + Error: ParseError<Input>, +{ + use crate::traits::AsChar; + + move |input: Input| { + let mut index = 0; + let mut res = input.new_builder(); + + let i = input.clone(); + + while index < i.input_len() { + let remainder = i.slice(index..); + match normal(remainder.clone()) { + Ok((i2, o)) => { + o.extend_into(&mut res); + if i2.input_len() == 0 { + return Ok((i.slice(i.input_len()..), res)); + } else { + index = input.offset(&i2); + } + } + Err(Err::Error(_)) => { + // unwrap() should be safe here since index < $i.input_len() + if remainder.iter_elements().next().unwrap().as_char() == control_char { + let next = index + control_char.len_utf8(); + let input_len = input.input_len(); + + if next >= input_len { + return Err(Err::Error(Error::from_error_kind(remainder, ErrorKind::EscapedTransform))); + } else { + match transform(i.slice(next..)) { + Ok((i2, o)) => { + o.extend_into(&mut res); + if i2.input_len() == 0 { + return Ok((i.slice(i.input_len()..), res)); + } else { + index = input.offset(&i2); + } + } + Err(e) => return Err(e), + } + } + } else { + if index == 0 { + return Err(Err::Error(Error::from_error_kind(remainder, ErrorKind::EscapedTransform))); + } + return Ok((remainder, res)); + } + } + Err(e) => return Err(e), + } + } + Ok((input.slice(index..), res)) + } +} + +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn escaped_transformc<Input, Error, F, G, O1, O2, ExtendItem, Output>( + i: Input, + normal: F, + control_char: char, + transform: G, +) -> IResult<Input, Output, Error> +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter, + Input: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + O1: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + O2: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + Output: core::iter::Extend<<Input as crate::traits::ExtendInto>::Item>, + Output: core::iter::Extend<<O1 as crate::traits::ExtendInto>::Item>, + Output: core::iter::Extend<<O2 as crate::traits::ExtendInto>::Item>, + <Input as InputIter>::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult<Input, O1, Error>, + G: Fn(Input) -> IResult<Input, O2, Error>, + Error: ParseError<Input>, +{ + escaped_transform(normal, control_char, transform)(i) + +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn complete_take_while_m_n_utf8_all_matching() { + let result: IResult<&str, &str> = super::take_while_m_n(1, 4, |c: char| c.is_alphabetic())("øn"); + assert_eq!(result, Ok(("", "øn"))); + } + + #[test] + fn complete_take_while_m_n_utf8_all_matching_substring() { + let result: IResult<&str, &str> = super::take_while_m_n(1, 1, |c: char| c.is_alphabetic())("øn"); + assert_eq!(result, Ok(("n", "ø"))); + } +} diff --git a/third_party/rust/nom/src/bytes/macros.rs b/third_party/rust/nom/src/bytes/macros.rs new file mode 100644 index 0000000000..1e24ba8851 --- /dev/null +++ b/third_party/rust/nom/src/bytes/macros.rs @@ -0,0 +1,938 @@ +//! Byte level parsers and combinators +//! +#[allow(unused_variables)] + +/// `tag!(&[T]: nom::AsBytes) => &[T] -> IResult<&[T], &[T]>` +/// declares a byte array as a suite to recognize +/// +/// consumes the recognized characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(x, tag!("abcd")); +/// let r = x(&b"abcdefgh"[..]); +/// assert_eq!(r, Ok((&b"efgh"[..], &b"abcd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! tag ( + ($i:expr, $tag: expr) => ({ + $crate::bytes::streaming::tag($tag)($i) + }); +); + +/// `tag_no_case!(&[T]) => &[T] -> IResult<&[T], &[T]>` +/// declares a case insensitive ascii string as a suite to recognize +/// +/// consumes the recognized characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(test, tag_no_case!("ABcd")); +/// +/// let r = test(&b"aBCdefgh"[..]); +/// assert_eq!(r, Ok((&b"efgh"[..], &b"aBCd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! tag_no_case ( + ($i:expr, $tag: expr) => ({ + $crate::bytes::streaming::tag_no_case($tag)($i) + }); +); + +/// `is_not!(&[T:AsBytes]) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes that do not appear in the provided array +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!( not_space, is_not!( " \t\r\n" ) ); +/// +/// let r = not_space(&b"abcdefgh\nijkl"[..]); +/// assert_eq!(r, Ok((&b"\nijkl"[..], &b"abcdefgh"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! is_not ( + ($input:expr, $arr:expr) => ({ + $crate::bytes::streaming::is_not($arr)($input) + }); +); + +/// `is_a!(&[T]) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes that appear in the provided array +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(abcd, is_a!( "abcd" )); +/// +/// let r1 = abcd(&b"aaaaefgh"[..]); +/// assert_eq!(r1, Ok((&b"efgh"[..], &b"aaaa"[..]))); +/// +/// let r2 = abcd(&b"dcbaefgh"[..]); +/// assert_eq!(r2, Ok((&b"efgh"[..], &b"dcba"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! is_a ( + ($input:expr, $arr:expr) => ({ + $crate::bytes::streaming::is_a($arr)($input) + }); +); + +/// `escaped!(T -> IResult<T, T>, U, T -> IResult<T, T>) => T -> IResult<T, T> where T: InputIter, +/// U: AsChar` +/// matches a byte string with escaped characters. +/// +/// The first argument matches the normal characters (it must not accept the control character), +/// the second argument is the control character (like `\` in most languages), +/// the third argument matches the escaped characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::character::complete::digit1; +/// # fn main() { +/// named!(esc, escaped!(call!(digit1), '\\', one_of!("\"n\\"))); +/// assert_eq!(esc(&b"123;"[..]), Ok((&b";"[..], &b"123"[..]))); +/// assert_eq!(esc(&b"12\\\"34;"[..]), Ok((&b";"[..], &b"12\\\"34"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! escaped ( + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped!($i, |i| $submac1!(i, $($args)*), $control_char, |i| $submac2!(i, $($args2)*)) + } + ); + ($i:expr, $normal:expr, $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped!($i, $normal, $control_char, |i| $submac2!(i, $($args2)*)) + } + ); + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $escapable:expr ) => ( + { + escaped!($i, |i| $submac1!(i, $($args)*), $control_char, $escapable) + } + ); + ($i:expr, $normal:expr, $control_char: expr, $escapable:expr) => ( + { + $crate::bytes::complete::escapedc($i, $normal, $control_char, $escapable) + } + ); +); + +/// `escaped_transform!(&[T] -> IResult<&[T], &[T]>, T, &[T] -> IResult<&[T], &[T]>) => &[T] -> IResult<&[T], Vec<T>>` +/// matches a byte string with escaped characters. +/// +/// The first argument matches the normal characters (it must not match the control character), +/// the second argument is the control character (like `\` in most languages), +/// the third argument matches the escaped characters and transforms them. +/// +/// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character) +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::character::complete::alpha1; +/// # use nom::lib::std::str::from_utf8; +/// # fn main() { +/// fn to_s(i:Vec<u8>) -> String { +/// String::from_utf8_lossy(&i).into_owned() +/// } +/// +/// named!(transform < String >, +/// map!( +/// escaped_transform!(call!(alpha1), '\\', +/// alt!( +/// tag!("\\") => { |_| &b"\\"[..] } +/// | tag!("\"") => { |_| &b"\""[..] } +/// | tag!("n") => { |_| &b"\n"[..] } +/// ) +/// ), to_s +/// ) +/// ); +/// assert_eq!(transform(&b"ab\\\"cd"[..]), Ok((&b""[..], String::from("ab\"cd")))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! escaped_transform ( + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped_transform!($i, |i| $submac1!(i, $($args)*), $control_char, |i| $submac2!(i, $($args2)*)) + } + ); + ($i:expr, $normal:expr, $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped_transform!($i, $normal, $control_char, |i| $submac2!(i, $($args2)*)) + } + ); + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $transform:expr ) => ( + { + escaped_transform!($i, |i| $submac1!(i, $($args)*), $control_char, $transform) + } + ); + ($i:expr, $normal:expr, $control_char: expr, $transform:expr) => ( + { + $crate::bytes::complete::escaped_transformc($i, $normal, $control_char, $transform) + } + ); +); + +/// `take_while!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes until the provided function fails. +/// +/// The argument is either a function `T -> bool` or a macro returning a `bool`. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::character::is_alphanumeric; +/// # fn main() { +/// named!( alpha, take_while!( is_alphanumeric ) ); +/// +/// let r = alpha(&b"abcd\nefgh"[..]); +/// assert_eq!(r, Ok((&b"\nefgh"[..], &b"abcd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_while ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ({ + let res: $crate::IResult<_, _, _> = take_while!($input, (|c| $submac!(c, $($args)*))); + res + }); + ($input:expr, $f:expr) => ( + $crate::bytes::streaming::take_while($f)($input) + ); +); + +/// `take_while1!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest (non empty) list of bytes until the provided function fails. +/// +/// The argument is either a function `&[T] -> bool` or a macro returning a `bool` +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind}; +/// # use nom::character::is_alphanumeric; +/// # fn main() { +/// named!( alpha, take_while1!( is_alphanumeric ) ); +/// +/// let r = alpha(&b"abcd\nefgh"[..]); +/// assert_eq!(r, Ok((&b"\nefgh"[..], &b"abcd"[..]))); +/// let r = alpha(&b"\nefgh"[..]); +/// assert_eq!(r, Err(Err::Error(error_position!(&b"\nefgh"[..], ErrorKind::TakeWhile1)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_while1 ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ({ + let res: $crate::IResult<_, _, _> = take_while1!($input, (|c| $submac!(c, $($args)*))); + res + }); + ($input:expr, $f:expr) => ( + $crate::bytes::streaming::take_while1($f)($input) + ); +); + +/// `take_while_m_n!(m: usize, n: usize, T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns a list of bytes or characters for which the provided function returns true. +/// the returned list's size will be at least m, and at most n +/// +/// The argument is either a function `T -> bool` or a macro returning a `bool`. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::character::is_alphanumeric; +/// # fn main() { +/// named!( alpha, take_while_m_n!(3, 6, is_alphanumeric ) ); +/// +/// let r = alpha(&b"abcd\nefgh"[..]); +/// assert_eq!(r, Ok((&b"\nefgh"[..], &b"abcd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_while_m_n ( + ($input:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => ({ + let res: $crate::IResult<_, _, _> = take_while_m_n!($input, $m, $n, (|c| $submac!(c, $($args)*))); + res + }); + ($input:expr, $m:expr, $n:expr, $f:expr) => ( + $crate::bytes::streaming::take_while_m_n($m, $n, $f)($input) + ); +); + +/// `take_till!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes until the provided function succeeds +/// +/// The argument is either a function `&[T] -> bool` or a macro returning a `bool`. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!( till_colon, take_till!(|ch| ch == b':') ); +/// +/// let r = till_colon(&b"abcd:efgh"[..]); +/// assert_eq!(r, Ok((&b":efgh"[..], &b"abcd"[..]))); +/// let r2 = till_colon(&b":abcdefgh"[..]); // empty match is allowed +/// assert_eq!(r2, Ok((&b":abcdefgh"[..], &b""[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_till ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ({ + let res: $crate::IResult<_, _, _> = take_till!($input, (|c| $submac!(c, $($args)*))); + res + }); + ($input:expr, $f:expr) => ( + $crate::bytes::streaming::take_till($f)($input) + ); +); + +/// `take_till1!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest non empty list of bytes until the provided function succeeds +/// +/// The argument is either a function `&[T] -> bool` or a macro returning a `bool`. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind}; +/// # fn main() { +/// named!( till1_colon, take_till1!(|ch| ch == b':') ); +/// +/// let r = till1_colon(&b"abcd:efgh"[..]); +/// assert_eq!(r, Ok((&b":efgh"[..], &b"abcd"[..]))); +/// +/// let r2 = till1_colon(&b":abcdefgh"[..]); // empty match is error +/// assert_eq!(r2, Err(Err::Error(error_position!(&b":abcdefgh"[..], ErrorKind::TakeTill1)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_till1 ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ({ + let res: $crate::IResult<_, _, _> = take_till1!($input, (|c| $submac!(c, $($args)*))); + res + }); + ($input:expr, $f:expr) => ( + $crate::bytes::streaming::take_till1($f)($input) + ); +); + +/// `take!(nb) => &[T] -> IResult<&[T], &[T]>` +/// generates a parser consuming the specified number of bytes +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// // Desmond parser +/// named!(take5, take!( 5 ) ); +/// +/// let a = b"abcdefgh"; +/// +/// assert_eq!(take5(&a[..]), Ok((&b"fgh"[..], &b"abcde"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take ( + ($i:expr, $count:expr) => ({ + let c = $count as usize; + let res: $crate::IResult<_,_,_> = $crate::bytes::streaming::take(c)($i); + res + }); +); + +/// `take_str!(nb) => &[T] -> IResult<&[T], &str>` +/// same as take! but returning a &str +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(take5( &[u8] ) -> &str, take_str!( 5 ) ); +/// +/// let a = b"abcdefgh"; +/// +/// assert_eq!(take5(&a[..]), Ok((&b"fgh"[..], "abcde"))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_str ( + ( $i:expr, $size:expr ) => ( + { + let input: &[u8] = $i; + + map_res!(input, take!($size), $crate::lib::std::str::from_utf8) + } + ); +); + +/// `take_until!(tag) => &[T] -> IResult<&[T], &[T]>` +/// consumes data until it finds the specified tag. +/// +/// The remainder still contains the tag. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(x, take_until!("foo")); +/// let r = x(&b"abcd foo efgh"[..]); +/// assert_eq!(r, Ok((&b"foo efgh"[..], &b"abcd "[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_until ( + ($i:expr, $substr:expr) => ({ + let res: $crate::IResult<_,_,_> = $crate::bytes::streaming::take_until($substr)($i); + res + }); +); + +/// `take_until1!(tag) => &[T] -> IResult<&[T], &[T]>` +/// consumes data (at least one byte) until it finds the specified tag +/// +/// The remainder still contains the tag. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(x, take_until1!("foo")); +/// +/// let r = x(&b"abcd foo efgh"[..]); +/// +/// assert_eq!(r, Ok((&b"foo efgh"[..], &b"abcd "[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_until1 ( + ($i:expr, $substr:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::{Err,Needed,IResult,error::ErrorKind}; + use $crate::InputLength; + use $crate::FindSubstring; + use $crate::InputTake; + let input = $i; + + let res: IResult<_,_> = match input.find_substring($substr) { + None => { + Err(Err::Incomplete(Needed::Size(1 + $substr.input_len()))) + }, + Some(0) => { + let e = ErrorKind::TakeUntil; + Err(Err::Error(error_position!($i, e))) + }, + Some(index) => { + Ok($i.take_split(index)) + }, + }; + res + } + ); +); + +#[cfg(test)] +mod tests { + use crate::internal::{Err, Needed, IResult}; + #[cfg(feature = "alloc")] + use crate::lib::std::string::String; + #[cfg(feature = "alloc")] + use crate::lib::std::vec::Vec; + use crate::character::streaming::{alpha1 as alpha, alphanumeric1 as alphanumeric, digit1 as digit, hex_digit1 as hex_digit, multispace1 as multispace, oct_digit1 as oct_digit, space1 as space}; + use crate::error::ErrorKind; + use crate::character::is_alphabetic; + + #[cfg(feature = "alloc")] + macro_rules! one_of ( + ($i:expr, $inp: expr) => ( + { + use $crate::Err; + use $crate::Slice; + use $crate::AsChar; + use $crate::FindToken; + use $crate::InputIter; + + match ($i).iter_elements().next().map(|c| { + $inp.find_token(c) + }) { + None => Err::<_,_>(Err::Incomplete(Needed::Size(1))), + Some(false) => Err(Err::Error(error_position!($i, ErrorKind::OneOf))), + //the unwrap should be safe here + Some(true) => Ok(($i.slice(1..), $i.iter_elements().next().unwrap().as_char())) + } + } + ); + ); + + #[test] + fn is_a() { + named!(a_or_b, is_a!(&b"ab"[..])); + + let a = &b"abcd"[..]; + assert_eq!(a_or_b(a), Ok((&b"cd"[..], &b"ab"[..]))); + + let b = &b"bcde"[..]; + assert_eq!(a_or_b(b), Ok((&b"cde"[..], &b"b"[..]))); + + let c = &b"cdef"[..]; + assert_eq!(a_or_b(c), Err(Err::Error(error_position!(c, ErrorKind::IsA)))); + + let d = &b"bacdef"[..]; + assert_eq!(a_or_b(d), Ok((&b"cdef"[..], &b"ba"[..]))); + } + + #[test] + fn is_not() { + named!(a_or_b, is_not!(&b"ab"[..])); + + let a = &b"cdab"[..]; + assert_eq!(a_or_b(a), Ok((&b"ab"[..], &b"cd"[..]))); + + let b = &b"cbde"[..]; + assert_eq!(a_or_b(b), Ok((&b"bde"[..], &b"c"[..]))); + + let c = &b"abab"[..]; + assert_eq!(a_or_b(c), Err(Err::Error(error_position!(c, ErrorKind::IsNot)))); + + let d = &b"cdefba"[..]; + assert_eq!(a_or_b(d), Ok((&b"ba"[..], &b"cdef"[..]))); + + let e = &b"e"[..]; + assert_eq!(a_or_b(e), Err(Err::Incomplete(Needed::Size(1)))); + } + + #[cfg(feature = "alloc")] + #[allow(unused_variables)] + #[test] + fn escaping() { + named!(esc, escaped!(call!(alpha), '\\', one_of!("\"n\\"))); + assert_eq!(esc(&b"abcd;"[..]), Ok((&b";"[..], &b"abcd"[..]))); + assert_eq!(esc(&b"ab\\\"cd;"[..]), Ok((&b";"[..], &b"ab\\\"cd"[..]))); + assert_eq!(esc(&b"\\\"abcd;"[..]), Ok((&b";"[..], &b"\\\"abcd"[..]))); + assert_eq!(esc(&b"\\n;"[..]), Ok((&b";"[..], &b"\\n"[..]))); + assert_eq!(esc(&b"ab\\\"12"[..]), Ok((&b"12"[..], &b"ab\\\""[..]))); + assert_eq!(esc(&b"AB\\"[..]), Err(Err::Error(error_position!(&b"AB\\"[..], ErrorKind::Escaped)))); + assert_eq!( + esc(&b"AB\\A"[..]), + Err(Err::Error(error_node_position!( + &b"AB\\A"[..], + ErrorKind::Escaped, + error_position!(&b"A"[..], ErrorKind::OneOf) + ))) + ); + + named!(esc2, escaped!(call!(digit), '\\', one_of!("\"n\\"))); + assert_eq!(esc2(&b"12\\nnn34"[..]), Ok((&b"nn34"[..], &b"12\\n"[..]))); + } + + #[cfg(feature = "alloc")] + #[test] + fn escaping_str() { + named!(esc<&str, &str>, escaped!(call!(alpha), '\\', one_of!("\"n\\"))); + assert_eq!(esc("abcd;"), Ok((";", "abcd"))); + assert_eq!(esc("ab\\\"cd;"), Ok((";", "ab\\\"cd"))); + assert_eq!(esc("\\\"abcd;"), Ok((";", "\\\"abcd"))); + assert_eq!(esc("\\n;"), Ok((";", "\\n"))); + assert_eq!(esc("ab\\\"12"), Ok(("12", "ab\\\""))); + assert_eq!(esc("AB\\"), Err(Err::Error(error_position!("AB\\", ErrorKind::Escaped)))); + assert_eq!( + esc("AB\\A"), + Err(Err::Error(error_node_position!( + "AB\\A", + ErrorKind::Escaped, + error_position!("A", ErrorKind::OneOf) + ))) + ); + + named!(esc2<&str, &str>, escaped!(call!(digit), '\\', one_of!("\"n\\"))); + assert_eq!(esc2("12\\nnn34"), Ok(("nn34", "12\\n"))); + + named!(esc3<&str, &str>, escaped!(call!(alpha), '\u{241b}', one_of!("\"n"))); + assert_eq!(esc3("ab␛ncd;"), Ok((";", "ab␛ncd"))); + } + + #[cfg(feature = "alloc")] + fn to_s(i: Vec<u8>) -> String { + String::from_utf8_lossy(&i).into_owned() + } + + #[cfg(feature = "alloc")] + #[test] + fn escape_transform() { + use crate::lib::std::str; + + named!( + esc<String>, + map!( + escaped_transform!( + alpha, + '\\', + alt!( + tag!("\\") => { |_| &b"\\"[..] } + | tag!("\"") => { |_| &b"\""[..] } + | tag!("n") => { |_| &b"\n"[..] } + ) + ), + to_s + ) + ); + + assert_eq!(esc(&b"abcd;"[..]), Ok((&b";"[..], String::from("abcd")))); + assert_eq!(esc(&b"ab\\\"cd;"[..]), Ok((&b";"[..], String::from("ab\"cd")))); + assert_eq!(esc(&b"\\\"abcd;"[..]), Ok((&b";"[..], String::from("\"abcd")))); + assert_eq!(esc(&b"\\n;"[..]), Ok((&b";"[..], String::from("\n")))); + assert_eq!(esc(&b"ab\\\"12"[..]), Ok((&b"12"[..], String::from("ab\"")))); + assert_eq!(esc(&b"AB\\"[..]), Err(Err::Error(error_position!(&b"\\"[..], ErrorKind::EscapedTransform)))); + assert_eq!( + esc(&b"AB\\A"[..]), + Err(Err::Error(error_node_position!( + &b"AB\\A"[..], + ErrorKind::EscapedTransform, + error_position!(&b"A"[..], ErrorKind::Alt) + ))) + ); + + named!( + esc2<String>, + map!( + escaped_transform!( + call!(alpha), + '&', + alt!( + tag!("egrave;") => { |_| str::as_bytes("è") } + | tag!("agrave;") => { |_| str::as_bytes("à") } + ) + ), + to_s + ) + ); + assert_eq!(esc2(&b"abèDEF;"[..]), Ok((&b";"[..], String::from("abèDEF")))); + assert_eq!(esc2(&b"abèDàEF;"[..]), Ok((&b";"[..], String::from("abèDàEF")))); + } + + #[cfg(feature = "std")] + #[test] + fn escape_transform_str() { + named!(esc<&str, String>, escaped_transform!(alpha, '\\', + alt!( + tag!("\\") => { |_| "\\" } + | tag!("\"") => { |_| "\"" } + | tag!("n") => { |_| "\n" } + )) + ); + + assert_eq!(esc("abcd;"), Ok((";", String::from("abcd")))); + assert_eq!(esc("ab\\\"cd;"), Ok((";", String::from("ab\"cd")))); + assert_eq!(esc("\\\"abcd;"), Ok((";", String::from("\"abcd")))); + assert_eq!(esc("\\n;"), Ok((";", String::from("\n")))); + assert_eq!(esc("ab\\\"12"), Ok(("12", String::from("ab\"")))); + assert_eq!(esc("AB\\"), Err(Err::Error(error_position!("\\", ErrorKind::EscapedTransform)))); + assert_eq!( + esc("AB\\A"), + Err(Err::Error(error_node_position!( + "AB\\A", + ErrorKind::EscapedTransform, + error_position!("A", ErrorKind::Alt) + ))) + ); + + named!(esc2<&str, String>, escaped_transform!(alpha, '&', + alt!( + tag!("egrave;") => { |_| "è" } + | tag!("agrave;") => { |_| "à" } + )) + ); + assert_eq!(esc2("abèDEF;"), Ok((";", String::from("abèDEF")))); + assert_eq!(esc2("abèDàEF;"), Ok((";", String::from("abèDàEF")))); + + named!(esc3<&str, String>, escaped_transform!(alpha, '␛', + alt!( + tag!("0") => { |_| "\0" } | + tag!("n") => { |_| "\n" }))); + assert_eq!(esc3("a␛0bc␛n"), Ok(("", String::from("a\0bc\n")))); + } + + #[test] + fn take_str_test() { + let a = b"omnomnom"; + + let res: IResult<_,_,(&[u8], ErrorKind)> = take_str!(&a[..], 5u32); + assert_eq!(res, Ok((&b"nom"[..], "omnom"))); + + let res: IResult<_,_,(&[u8], ErrorKind)> = take_str!(&a[..], 9u32); + assert_eq!(res, Err(Err::Incomplete(Needed::Size(9)))); + } + + #[test] + fn take_until_incomplete() { + named!(y, take_until!("end")); + assert_eq!(y(&b"nd"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(y(&b"123"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(y(&b"123en"[..]), Err(Err::Incomplete(Needed::Size(3)))); + } + + #[test] + fn take_until_incomplete_s() { + named!(ys<&str, &str>, take_until!("end")); + assert_eq!(ys("123en"), Err(Err::Incomplete(Needed::Size(3)))); + } + + #[test] + fn recognize() { + named!(x, recognize!(delimited!(tag!("<!--"), take!(5usize), tag!("-->")))); + let r = x(&b"<!-- abc --> aaa"[..]); + assert_eq!(r, Ok((&b" aaa"[..], &b"<!-- abc -->"[..]))); + + let semicolon = &b";"[..]; + + named!(ya, recognize!(alpha)); + let ra = ya(&b"abc;"[..]); + assert_eq!(ra, Ok((semicolon, &b"abc"[..]))); + + named!(yd, recognize!(digit)); + let rd = yd(&b"123;"[..]); + assert_eq!(rd, Ok((semicolon, &b"123"[..]))); + + named!(yhd, recognize!(hex_digit)); + let rhd = yhd(&b"123abcDEF;"[..]); + assert_eq!(rhd, Ok((semicolon, &b"123abcDEF"[..]))); + + named!(yod, recognize!(oct_digit)); + let rod = yod(&b"1234567;"[..]); + assert_eq!(rod, Ok((semicolon, &b"1234567"[..]))); + + named!(yan, recognize!(alphanumeric)); + let ran = yan(&b"123abc;"[..]); + assert_eq!(ran, Ok((semicolon, &b"123abc"[..]))); + + named!(ys, recognize!(space)); + let rs = ys(&b" \t;"[..]); + assert_eq!(rs, Ok((semicolon, &b" \t"[..]))); + + named!(yms, recognize!(multispace)); + let rms = yms(&b" \t\r\n;"[..]); + assert_eq!(rms, Ok((semicolon, &b" \t\r\n"[..]))); + } + + #[test] + fn take_while() { + named!(f, take_while!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"abcd123"; + let d = b"123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&c[..]), Ok((&d[..], &b[..]))); + assert_eq!(f(&d[..]), Ok((&d[..], &a[..]))); + } + + #[test] + fn take_while1() { + named!(f, take_while1!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"abcd123"; + let d = b"123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&c[..]), Ok((&b"123"[..], &b[..]))); + assert_eq!(f(&d[..]), Err(Err::Error(error_position!(&d[..], ErrorKind::TakeWhile1)))); + } + + #[test] + fn take_while_m_n() { + named!(x, take_while_m_n!(2, 4, is_alphabetic)); + let a = b""; + let b = b"a"; + let c = b"abc"; + let d = b"abc123"; + let e = b"abcde"; + let f = b"123"; + + assert_eq!(x(&a[..]), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!(x(&b[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(x(&c[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(x(&d[..]), Ok((&b"123"[..], &c[..]))); + assert_eq!(x(&e[..]), Ok((&b"e"[..], &b"abcd"[..]))); + assert_eq!(x(&f[..]), Err(Err::Error(error_position!(&f[..], ErrorKind::TakeWhileMN)))); + } + + #[test] + fn take_till() { + + named!(f, take_till!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"123abcd"; + let d = b"123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Ok((&b"abcd"[..], &b""[..]))); + assert_eq!(f(&c[..]), Ok((&b"abcd"[..], &b"123"[..]))); + assert_eq!(f(&d[..]), Err(Err::Incomplete(Needed::Size(1)))); + } + + #[test] + fn take_till1() { + + named!(f, take_till1!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"123abcd"; + let d = b"123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Err(Err::Error(error_position!(&b[..], ErrorKind::TakeTill1)))); + assert_eq!(f(&c[..]), Ok((&b"abcd"[..], &b"123"[..]))); + assert_eq!(f(&d[..]), Err(Err::Incomplete(Needed::Size(1)))); + } + + #[test] + fn take_while_utf8() { + named!(f<&str,&str>, take_while!(|c:char| { c != '點' })); + + assert_eq!(f(""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f("abcd"), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f("abcd點"), Ok(("點", "abcd"))); + assert_eq!(f("abcd點a"), Ok(("點a", "abcd"))); + + named!(g<&str,&str>, take_while!(|c:char| { c == '點' })); + + assert_eq!(g(""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(g("點abcd"), Ok(("abcd", "點"))); + assert_eq!(g("點點點a"), Ok(("a", "點點點"))); + } + + #[test] + fn take_till_utf8() { + named!(f<&str,&str>, take_till!(|c:char| { c == '點' })); + + assert_eq!(f(""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f("abcd"), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f("abcd點"), Ok(("點", "abcd"))); + assert_eq!(f("abcd點a"), Ok(("點a", "abcd"))); + + named!(g<&str,&str>, take_till!(|c:char| { c != '點' })); + + assert_eq!(g(""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(g("點abcd"), Ok(("abcd", "點"))); + assert_eq!(g("點點點a"), Ok(("a", "點點點"))); + } + + #[test] + fn take_utf8() { + named!(f<&str,&str>, take!(3)); + + assert_eq!(f(""), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(f("ab"), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(f("點"), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(f("ab點cd"), Ok(("cd", "ab點"))); + assert_eq!(f("a點bcd"), Ok(("cd", "a點b"))); + assert_eq!(f("a點b"), Ok(("", "a點b"))); + + named!(g<&str,&str>, take_while!(|c:char| { c == '點' })); + + assert_eq!(g(""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(g("點abcd"), Ok(("abcd", "點"))); + assert_eq!(g("點點點a"), Ok(("a", "點點點"))); + } + + #[test] + fn take_while_m_n_utf8() { + named!(parser<&str, &str>, take_while_m_n!(1, 1, |c| c == 'A' || c == '😃')); + assert_eq!(parser("A!"), Ok(("!", "A"))); + assert_eq!(parser("😃!"), Ok(("!", "😃"))); + } + + #[test] + fn take_while_m_n_utf8_full_match() { + named!(parser<&str, &str>, take_while_m_n!(1, 1, |c: char| c.is_alphabetic())); + assert_eq!(parser("øn"), Ok(("n", "ø"))); + } + + #[cfg(nightly)] + use test::Bencher; + + #[cfg(nightly)] + #[bench] + fn take_while_bench(b: &mut Bencher) { + + named!(f, take_while!(is_alphabetic)); + b.iter(|| f(&b"abcdefghijklABCDEejfrfrjgro12aa"[..])); + } + + #[test] + #[cfg(feature = "std")] + fn recognize_take_while() { + use crate::character::is_alphanumeric; + named!(x, take_while!(is_alphanumeric)); + named!(y, recognize!(x)); + assert_eq!(x(&b"ab."[..]), Ok((&b"."[..], &b"ab"[..]))); + println!("X: {:?}", x(&b"ab"[..])); + assert_eq!(y(&b"ab."[..]), Ok((&b"."[..], &b"ab"[..]))); + } + + #[test] + fn length_bytes() { + use crate::number::streaming::le_u8; + named!(x, length_data!(le_u8)); + assert_eq!(x(b"\x02..>>"), Ok((&b">>"[..], &b".."[..]))); + assert_eq!(x(b"\x02.."), Ok((&[][..], &b".."[..]))); + assert_eq!(x(b"\x02."), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!(x(b"\x02"), Err(Err::Incomplete(Needed::Size(2)))); + + named!(y, do_parse!(tag!("magic") >> b: length_data!(le_u8) >> (b))); + assert_eq!(y(b"magic\x02..>>"), Ok((&b">>"[..], &b".."[..]))); + assert_eq!(y(b"magic\x02.."), Ok((&[][..], &b".."[..]))); + assert_eq!(y(b"magic\x02."), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!(y(b"magic\x02"), Err(Err::Incomplete(Needed::Size(2)))); + } + + #[cfg(feature = "alloc")] + #[test] + fn case_insensitive() { + named!(test, tag_no_case!("ABcd")); + assert_eq!(test(&b"aBCdefgh"[..]), Ok((&b"efgh"[..], &b"aBCd"[..]))); + assert_eq!(test(&b"abcdefgh"[..]), Ok((&b"efgh"[..], &b"abcd"[..]))); + assert_eq!(test(&b"ABCDefgh"[..]), Ok((&b"efgh"[..], &b"ABCD"[..]))); + assert_eq!(test(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(test(&b"Hello"[..]), Err(Err::Error(error_position!(&b"Hello"[..], ErrorKind::Tag)))); + assert_eq!(test(&b"Hel"[..]), Err(Err::Error(error_position!(&b"Hel"[..], ErrorKind::Tag)))); + + named!(test2<&str, &str>, tag_no_case!("ABcd")); + assert_eq!(test2("aBCdefgh"), Ok(("efgh", "aBCd"))); + assert_eq!(test2("abcdefgh"), Ok(("efgh", "abcd"))); + assert_eq!(test2("ABCDefgh"), Ok(("efgh", "ABCD"))); + assert_eq!(test2("ab"), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(test2("Hello"), Err(Err::Error(error_position!(&"Hello"[..], ErrorKind::Tag)))); + assert_eq!(test2("Hel"), Err(Err::Error(error_position!(&"Hel"[..], ErrorKind::Tag)))); + } + + #[test] + fn tag_fixed_size_array() { + named!(test, tag!([0x42])); + named!(test2, tag!(&[0x42])); + let input = [0x42, 0x00]; + assert_eq!(test(&input), Ok((&b"\x00"[..], &b"\x42"[..]))); + assert_eq!(test2(&input), Ok((&b"\x00"[..], &b"\x42"[..]))); + } +} diff --git a/third_party/rust/nom/src/bytes/mod.rs b/third_party/rust/nom/src/bytes/mod.rs new file mode 100644 index 0000000000..13b20126f4 --- /dev/null +++ b/third_party/rust/nom/src/bytes/mod.rs @@ -0,0 +1,7 @@ +//! parsers recognizing bytes streams + +#[macro_use] +mod macros; +pub mod streaming; +pub mod complete; + diff --git a/third_party/rust/nom/src/bytes/streaming.rs b/third_party/rust/nom/src/bytes/streaming.rs new file mode 100644 index 0000000000..f6e6706888 --- /dev/null +++ b/third_party/rust/nom/src/bytes/streaming.rs @@ -0,0 +1,645 @@ +//! parsers recognizing bytes streams, streaming version + +use crate::error::ErrorKind; +use crate::error::ParseError; +use crate::internal::{Err, IResult, Needed}; +use crate::lib::std::ops::RangeFrom; +use crate::lib::std::result::Result::*; +use crate::traits::{Compare, CompareResult, FindSubstring, FindToken, InputIter, InputLength, InputTake, InputTakeAtPosition, Slice, ToUsize}; + +/// Recognizes a pattern +/// +/// The input data will be compared to the tag combinator's argument and will return the part of +/// the input that matches the argument +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::tag; +/// +/// fn parser(s: &str) -> IResult<&str, &str> { +/// tag("Hello")(s) +/// } +/// +/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); +/// assert_eq!(parser("Something"), Err(Err::Error(("Something", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Incomplete(Needed::Size(5)))); +/// ``` +pub fn tag<'a, T: 'a, Input: 'a, Error: ParseError<Input>>(tag: T) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTake + Compare<T>, + T: InputLength + Clone, +{ + move |i: Input| { + let tag_len = tag.input_len(); + let t = tag.clone(); + + let res: IResult<_, _, Error> = match i.compare(t) { + CompareResult::Ok => Ok(i.take_split(tag_len)), + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Size(tag_len))), + CompareResult::Error => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(Error::from_error_kind(i, e))) + } + }; + res + } +} + +/// Recognizes a case insensitive pattern +/// +/// The input data will be compared to the tag combinator's argument and will return the part of +/// the input that matches the argument with no regard to case +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::tag_no_case; +/// +/// fn parser(s: &str) -> IResult<&str, &str> { +/// tag_no_case("hello")(s) +/// } +/// +/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); +/// assert_eq!(parser("hello, World!"), Ok((", World!", "hello"))); +/// assert_eq!(parser("HeLlO, World!"), Ok((", World!", "HeLlO"))); +/// assert_eq!(parser("Something"), Err(Err::Error(("Something", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Incomplete(Needed::Size(5)))); +/// ``` +pub fn tag_no_case<T, Input, Error: ParseError<Input>>(tag: T) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTake + Compare<T>, + T: InputLength + Clone, +{ + move |i: Input| { + let tag_len = tag.input_len(); + let t = tag.clone(); + + let res: IResult<_, _, Error> = match (i).compare_no_case(t) { + CompareResult::Ok => Ok(i.take_split(tag_len)), + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Size(tag_len))), + CompareResult::Error => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(Error::from_error_kind(i, e))) + } + }; + res + } +} + +/// Parse till certain characters are met +/// +/// The parser will return the longest slice till one of the characters of the combinator's argument are met. +/// +/// It doesn't consume the matched character, +/// +/// It will return a `Err::Incomplete(Needed::Size(1))` if the pattern wasn't met +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::is_not; +/// +/// fn not_space(s: &str) -> IResult<&str, &str> { +/// is_not(" \t\r\n")(s) +/// } +/// +/// assert_eq!(not_space("Hello, World!"), Ok((" World!", "Hello,"))); +/// assert_eq!(not_space("Sometimes\t"), Ok(("\t", "Sometimes"))); +/// assert_eq!(not_space("Nospace"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(not_space(""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +pub fn is_not<T, Input, Error: ParseError<Input>>(arr: T) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + T: InputLength + FindToken<<Input as InputTakeAtPosition>::Item>, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::IsNot; + i.split_at_position1(|c| arr.find_token(c), e) + } +} + +/// Returns the longest slice of the matches the pattern +/// +/// The parser will return the longest slice consisting of the characters in provided in the +/// combinator's argument +/// +/// # Streaming specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` if the pattern wasn't met +/// or if the pattern reaches the end of the input +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::is_a; +/// +/// fn hex(s: &str) -> IResult<&str, &str> { +/// is_a("1234567890ABCDEF")(s) +/// } +/// +/// assert_eq!(hex("123 and voila"), Ok((" and voila", "123"))); +/// assert_eq!(hex("DEADBEEF and others"), Ok((" and others", "DEADBEEF"))); +/// assert_eq!(hex("BADBABEsomething"), Ok(("something", "BADBABE"))); +/// assert_eq!(hex("D15EA5E"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(hex(""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +pub fn is_a<T, Input, Error: ParseError<Input>>(arr: T) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + T: InputLength + FindToken<<Input as InputTakeAtPosition>::Item>, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::IsA; + i.split_at_position1(|c| !arr.find_token(c), e) + } +} + +/// Returns the longest input slice (if any) that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` if the pattern reaches the end of the input +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_while; +/// use nom::character::is_alphabetic; +/// +/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while(is_alphabetic)(s) +/// } +/// +/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(alpha(b"12345"), Ok((&b"12345"[..], &b""[..]))); +/// assert_eq!(alpha(b"latin"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(alpha(b""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +pub fn take_while<F, Input, Error: ParseError<Input>>(cond: F) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, +{ + move |i: Input| i.split_at_position(|c| !cond(c)) +} + +/// Returns the longest (atleast 1) input slice that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// It will return an `Err(Err::Error((_, ErrorKind::TakeWhile1)))` if the pattern wasn't met +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` or if the pattern reaches the end of the input. +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_while1; +/// use nom::character::is_alphabetic; +/// +/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while1(is_alphabetic)(s) +/// } +/// +/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(alpha(b"latin"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(alpha(b"12345"), Err(Err::Error((&b"12345"[..], ErrorKind::TakeWhile1)))); +/// ``` +pub fn take_while1<F, Input, Error: ParseError<Input>>(cond: F) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::TakeWhile1; + i.split_at_position1(|c| !cond(c), e) + } +} + +/// Returns the longest (m <= len <= n) input slice that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// It will return an `Err::Error((_, ErrorKind::TakeWhileMN))` if the pattern wasn't met +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` if the pattern reaches the end of the input or is too short. +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_while_m_n; +/// use nom::character::is_alphabetic; +/// +/// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while_m_n(3, 6, is_alphabetic)(s) +/// } +/// +/// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(short_alpha(b"lengthy"), Ok((&b"y"[..], &b"length"[..]))); +/// assert_eq!(short_alpha(b"latin"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(short_alpha(b"ed"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(short_alpha(b"12345"), Err(Err::Error((&b"12345"[..], ErrorKind::TakeWhileMN)))); +/// ``` +pub fn take_while_m_n<F, Input, Error: ParseError<Input>>(m: usize, n: usize, cond: F) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTake + InputIter + InputLength + Slice<RangeFrom<usize>>, + F: Fn(<Input as InputIter>::Item) -> bool, +{ + move |i: Input| { + let input = i; + + match input.position(|c| !cond(c)) { + Some(idx) => { + if idx >= m { + if idx <= n { + let res: IResult<_, _, Error> = if let Some(index) = input.slice_index(idx) { + Ok(input.take_split(index)) + } else { + Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + }; + res + } else { + let res: IResult<_, _, Error> = if let Some(index) = input.slice_index(n) { + Ok(input.take_split(index)) + } else { + Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + }; + res + } + } else { + let e = ErrorKind::TakeWhileMN; + Err(Err::Error(Error::from_error_kind(input, e))) + } + } + None => { + let len = input.input_len(); + if len >= n { + match input.slice_index(n) { + Some(index) => Ok(input.take_split(index)), + None => Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + } + } else { + let needed = if m > len { m - len } else { 1 }; + Err(Err::Incomplete(Needed::Size(needed))) + } + } + } + } +} + +/// Returns the longest input slice (if any) till a predicate is met +/// +/// The parser will return the longest slice till the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` if the match reaches the +/// end of input or if there was not match +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_till; +/// +/// fn till_colon(s: &str) -> IResult<&str, &str> { +/// take_till(|c| c == ':')(s) +/// } +/// +/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); +/// assert_eq!(till_colon(":empty matched"), Ok((":empty matched", ""))); //allowed +/// assert_eq!(till_colon("12345"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(till_colon(""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +pub fn take_till<F, Input, Error: ParseError<Input>>(cond: F) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, +{ + move |i: Input| i.split_at_position(|c| cond(c)) +} + +/// Returns the longest (atleast 1) input slice till a predicate is met +/// +/// The parser will return the longest slice till the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` if the match reaches the +/// end of input or if there was not match +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_till1; +/// +/// fn till_colon(s: &str) -> IResult<&str, &str> { +/// take_till1(|c| c == ':')(s) +/// } +/// +/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); +/// assert_eq!(till_colon(":empty matched"), Err(Err::Error((":empty matched", ErrorKind::TakeTill1)))); +/// assert_eq!(till_colon("12345"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(till_colon(""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +pub fn take_till1<F, Input, Error: ParseError<Input>>(cond: F) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTakeAtPosition, + F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::TakeTill1; + i.split_at_position1(|c| cond(c), e) + } +} + +/// Returns an input slice containing the first N input elements (Input[..N]) +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(N))` where N is the +/// argument if the input is less than the length provided +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take; +/// +/// fn take6(s: &str) -> IResult<&str, &str> { +/// take(6usize)(s) +/// } +/// +/// assert_eq!(take6("1234567"), Ok(("7", "123456"))); +/// assert_eq!(take6("things"), Ok(("", "things"))); +/// assert_eq!(take6("short"), Err(Err::Incomplete(Needed::Size(6)))); //N doesn't change +/// assert_eq!(take6(""), Err(Err::Incomplete(Needed::Size(6)))); +/// ``` +pub fn take<C, Input, Error: ParseError<Input>>(count: C) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputIter + InputTake, + C: ToUsize, +{ + let c = count.to_usize(); + move |i: Input| match i.slice_index(c) { + None => Err(Err::Incomplete(Needed::Size(c))), + Some(index) => Ok(i.take_split(index)), + } +} + +/// Returns the longest input slice till it matches the pattern. +/// +/// It doesn't consume the pattern +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(N))` if the input doesn't +/// contain the pattern or if the input is smaller than the pattern +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_until; +/// +/// fn until_eof(s: &str) -> IResult<&str, &str> { +/// take_until("eof")(s) +/// } +/// +/// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world"))); +/// assert_eq!(until_eof("hello, world"), Err(Err::Incomplete(Needed::Size(3)))); +/// assert_eq!(until_eof(""), Err(Err::Incomplete(Needed::Size(3)))); +/// ``` +pub fn take_until<T, Input, Error: ParseError<Input>>(tag: T) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: InputTake + FindSubstring<T>, + T: InputLength + Clone, +{ + move |i: Input| { + let len = tag.input_len(); + let t = tag.clone(); + + let res: IResult<_, _, Error> = match i.find_substring(t) { + None => Err(Err::Incomplete(Needed::Size(len))), + Some(index) => Ok(i.take_split(index)), + }; + res + } +} + +/// Matches a byte string with escaped characters. +/// +/// * The first argument matches the normal characters (it must not accept the control character), +/// * the second argument is the control character (like `\` in most languages), +/// * the third argument matches the escaped characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use nom::character::complete::digit1; +/// use nom::bytes::streaming::escaped; +/// use nom::character::streaming::one_of; +/// +/// fn esc(s: &str) -> IResult<&str, &str> { +/// escaped(digit1, '\\', one_of("\"n\\"))(s) +/// } +/// +/// assert_eq!(esc("123;"), Ok((";", "123"))); +/// assert_eq!(esc("12\\\"34;"), Ok((";", "12\\\"34"))); +/// ``` +/// +pub fn escaped<Input, Error, F, G, O1, O2>(normal: F, control_char: char, escapable: G) -> impl Fn(Input) -> IResult<Input, Input, Error> +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter, + <Input as InputIter>::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult<Input, O1, Error>, + G: Fn(Input) -> IResult<Input, O2, Error>, + Error: ParseError<Input>, +{ + use crate::traits::AsChar; + + move |input: Input| { + let mut i = input.clone(); + + while i.input_len() > 0 { + match normal(i.clone()) { + Ok((i2, _)) => { + if i2.input_len() == 0 { + return Err(Err::Incomplete(Needed::Unknown)); + } else { + i = i2; + } + } + Err(Err::Error(_)) => { + // unwrap() should be safe here since index < $i.input_len() + if i.iter_elements().next().unwrap().as_char() == control_char { + let next = control_char.len_utf8(); + if next >= i.input_len() { + return Err(Err::Incomplete(Needed::Size(1))); + } else { + match escapable(i.slice(next..)) { + Ok((i2, _)) => { + if i2.input_len() == 0 { + return Err(Err::Incomplete(Needed::Unknown)); + } else { + i = i2; + } + } + Err(e) => return Err(e), + } + } + } else { + let index = input.offset(&i); + return Ok(input.take_split(index)); + } + } + Err(e) => { + return Err(e); + } + } + } + + Err(Err::Incomplete(Needed::Unknown)) + } +} + +#[doc(hidden)] +pub fn escapedc<Input, Error, F, G, O1, O2>(i: Input, normal: F, control_char: char, escapable: G) -> IResult<Input, Input, Error> +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter, + <Input as InputIter>::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult<Input, O1, Error>, + G: Fn(Input) -> IResult<Input, O2, Error>, + Error: ParseError<Input>, +{ + escaped(normal, control_char, escapable)(i) +} + +/// Matches a byte string with escaped characters. +/// +/// * The first argument matches the normal characters (it must not match the control character), +/// * the second argument is the control character (like `\` in most languages), +/// * the third argument matches the escaped characters and transforms them. +/// +/// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character) +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use std::str::from_utf8; +/// use nom::bytes::streaming::escaped_transform; +/// use nom::character::streaming::alpha1; +/// +/// fn parser(input: &str) -> IResult<&str, String> { +/// escaped_transform( +/// alpha1, +/// '\\', +/// |i:&str| alt!(i, +/// tag!("\\") => { |_| "\\" } +/// | tag!("\"") => { |_| "\"" } +/// | tag!("n") => { |_| "\n" } +/// ) +/// )(input) +/// } +/// +/// assert_eq!(parser("ab\\\"cd\""), Ok(("\"", String::from("ab\"cd")))); +/// ``` +#[cfg(feature = "alloc")] +pub fn escaped_transform<Input, Error, F, G, O1, O2, ExtendItem, Output>( + normal: F, + control_char: char, + transform: G, +) -> impl Fn(Input) -> IResult<Input, Output, Error> +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter, + Input: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + O1: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + O2: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + Output: core::iter::Extend<<Input as crate::traits::ExtendInto>::Item>, + Output: core::iter::Extend<<O1 as crate::traits::ExtendInto>::Item>, + Output: core::iter::Extend<<O2 as crate::traits::ExtendInto>::Item>, + <Input as InputIter>::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult<Input, O1, Error>, + G: Fn(Input) -> IResult<Input, O2, Error>, + Error: ParseError<Input>, +{ + use crate::traits::AsChar; + + move |input: Input| { + let mut index = 0; + let mut res = input.new_builder(); + + let i = input.clone(); + + while index < i.input_len() { + let remainder = i.slice(index..); + match normal(remainder.clone()) { + Ok((i2, o)) => { + o.extend_into(&mut res); + if i2.input_len() == 0 { + return Err(Err::Incomplete(Needed::Unknown)); + } else { + index = input.offset(&i2); + } + } + Err(Err::Error(_)) => { + // unwrap() should be safe here since index < $i.input_len() + if remainder.iter_elements().next().unwrap().as_char() == control_char { + let next = index + control_char.len_utf8(); + let input_len = input.input_len(); + + if next >= input_len { + return Err(Err::Incomplete(Needed::Unknown)); + } else { + match transform(i.slice(next..)) { + Ok((i2, o)) => { + o.extend_into(&mut res); + if i2.input_len() == 0 { + return Err(Err::Incomplete(Needed::Unknown)); + } else { + index = input.offset(&i2); + } + } + Err(e) => return Err(e), + } + } + } else { + return Ok((remainder, res)); + } + } + Err(e) => return Err(e), + } + } + Err(Err::Incomplete(Needed::Unknown)) + } +} + +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn escaped_transformc<Input, Error, F, G, O1, O2, ExtendItem, Output>( + i: Input, + normal: F, + control_char: char, + transform: G, +) -> IResult<Input, Output, Error> +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter, + Input: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + O1: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + O2: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, + Output: core::iter::Extend<<Input as crate::traits::ExtendInto>::Item>, + Output: core::iter::Extend<<O1 as crate::traits::ExtendInto>::Item>, + Output: core::iter::Extend<<O2 as crate::traits::ExtendInto>::Item>, + <Input as InputIter>::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult<Input, O1, Error>, + G: Fn(Input) -> IResult<Input, O2, Error>, + Error: ParseError<Input>, +{ + escaped_transform(normal, control_char, transform)(i) + +} diff --git a/third_party/rust/nom/src/character/complete.rs b/third_party/rust/nom/src/character/complete.rs new file mode 100644 index 0000000000..beddc3fa43 --- /dev/null +++ b/third_party/rust/nom/src/character/complete.rs @@ -0,0 +1,1082 @@ +//! Character specific parsers and combinators, complete input version. +//! +//! Functions recognizing specific characters. + +use crate::internal::{Err, IResult}; +use crate::error::ParseError; +use crate::lib::std::ops::{Range, RangeFrom, RangeTo}; +use crate::traits::{AsChar, FindToken, InputIter, InputLength, InputTakeAtPosition, Slice}; +use crate::traits::{Compare, CompareResult}; +use crate::error::ErrorKind; + +/// Recognizes one character. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind}; +/// # use nom::character::complete::char; +/// # fn main() { +/// assert_eq!(char::<_, (&str, ErrorKind)>('a')("abc"), Ok(("bc", 'a'))); +/// assert_eq!(char::<_, (&str, ErrorKind)>('a')("bc"), Err(Err::Error(("bc", ErrorKind::Char)))); +/// assert_eq!(char::<_, (&str, ErrorKind)>('a')(""), Err(Err::Error(("", ErrorKind::Char)))); +/// # } +/// ``` +pub fn char<I, Error: ParseError<I>>(c: char) -> impl Fn(I) -> IResult<I, char, Error> +where + I: Slice<RangeFrom<usize>> + InputIter, + <I as InputIter>::Item: AsChar, +{ + move |i: I| match (i).iter_elements().next().map(|t| { + let b = t.as_char() == c; + (&c, b) + }) { + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + _ => Err(Err::Error(Error::from_char(i, c))), + } +} + +/// Recognizes one of the provided characters. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind}; +/// # use nom::character::complete::one_of; +/// # fn main() { +/// assert_eq!(one_of::<_, _, (&str, ErrorKind)>("abc")("b"), Ok(("", 'b'))); +/// assert_eq!(one_of::<_, _, (&str, ErrorKind)>("a")("bc"), Err(Err::Error(("bc", ErrorKind::OneOf)))); +/// assert_eq!(one_of::<_, _, (&str, ErrorKind)>("a")(""), Err(Err::Error(("", ErrorKind::OneOf)))); +/// # } +/// ``` +pub fn one_of<I, T, Error: ParseError<I>>(list: T) -> impl Fn(I) -> IResult<I, char, Error> +where + I: Slice<RangeFrom<usize>> + InputIter, + <I as InputIter>::Item: AsChar + Copy, + T: FindToken<<I as InputIter>::Item>, +{ + move |i: I| match (i).iter_elements().next().map(|c| (c, list.find_token(c))) { + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + _ => Err(Err::Error(Error::from_error_kind(i, ErrorKind::OneOf))), + } +} + +/// Recognizes a character that is not in the provided characters. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind}; +/// # use nom::character::complete::none_of; +/// # fn main() { +/// assert_eq!(none_of::<_, _, (&str, ErrorKind)>("abc")("z"), Ok(("", 'z'))); +/// assert_eq!(none_of::<_, _, (&str, ErrorKind)>("ab")("a"), Err(Err::Error(("a", ErrorKind::NoneOf)))); +/// assert_eq!(none_of::<_, _, (&str, ErrorKind)>("a")(""), Err(Err::Error(("", ErrorKind::NoneOf)))); +/// # } +/// ``` +pub fn none_of<I, T, Error: ParseError<I>>(list: T) -> impl Fn(I) -> IResult<I, char, Error> +where + I: Slice<RangeFrom<usize>> + InputIter, + <I as InputIter>::Item: AsChar + Copy, + T: FindToken<<I as InputIter>::Item>, +{ + move |i: I| match (i).iter_elements().next().map(|c| (c, !list.find_token(c))) { + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + _ => Err(Err::Error(Error::from_error_kind(i, ErrorKind::NoneOf))), + } +} + +/// Recognizes the string "\r\n". +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult}; +/// # use nom::character::complete::crlf; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// crlf(input) +/// } +/// +/// assert_eq!(parser("\r\nc"), Ok(("c", "\r\n"))); +/// assert_eq!(parser("ab\r\nc"), Err(Err::Error(("ab\r\nc", ErrorKind::CrLf)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::CrLf)))); +/// # } +/// ``` +pub fn crlf<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: Slice<Range<usize>> + Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: InputIter, + T: Compare<&'static str>, +{ + match input.compare("\r\n") { + //FIXME: is this the right index? + CompareResult::Ok => Ok((input.slice(2..), input.slice(0..2))), + _ => { + let e: ErrorKind = ErrorKind::CrLf; + Err(Err::Error(E::from_error_kind(input, e))) + } + } +} + +//FIXME: there's still an incomplete +/// Recognizes a string of any char except '\r' or '\n'. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::not_line_ending; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// not_line_ending(input) +/// } +/// +/// assert_eq!(parser("ab\r\nc"), Ok(("\r\nc", "ab"))); +/// assert_eq!(parser("abc"), Ok(("", "abc"))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn not_line_ending<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: Slice<Range<usize>> + Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: InputIter + InputLength, + T: Compare<&'static str>, + <T as InputIter>::Item: AsChar, + <T as InputIter>::Item: AsChar, +{ + match input.position(|item| { + let c = item.as_char(); + c == '\r' || c == '\n' + }) { + None => { + Ok((input.slice(input.input_len()..), input)) + } + Some(index) => { + let mut it = input.slice(index..).iter_elements(); + let nth = it.next().unwrap().as_char(); + if nth == '\r' { + let sliced = input.slice(index..); + let comp = sliced.compare("\r\n"); + match comp { + //FIXME: calculate the right index + CompareResult::Ok => Ok((input.slice(index..), input.slice(..index))), + _ => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(E::from_error_kind(input, e))) + } + } + } else { + Ok((input.slice(index..), input.slice(..index))) + } + } + } +} + +/// Recognizes an end of line (both '\n' and '\r\n'). +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::line_ending; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// line_ending(input) +/// } +/// +/// assert_eq!(parser("\r\nc"), Ok(("c", "\r\n"))); +/// assert_eq!(parser("ab\r\nc"), Err(Err::Error(("ab\r\nc", ErrorKind::CrLf)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::CrLf)))); +/// # } +/// ``` +pub fn line_ending<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: Slice<Range<usize>> + Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: InputIter + InputLength, + T: Compare<&'static str>, +{ + match input.compare("\n") { + CompareResult::Ok => Ok((input.slice(1..), input.slice(0..1))), + CompareResult::Incomplete => Err(Err::Error(E::from_error_kind(input, ErrorKind::CrLf))), + CompareResult::Error => { + match input.compare("\r\n") { + //FIXME: is this the right index? + CompareResult::Ok => Ok((input.slice(2..), input.slice(0..2))), + _ => Err(Err::Error(E::from_error_kind(input, ErrorKind::CrLf))), + } + } + } +} + +/// Matches a newline character '\n'. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::newline; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, char> { +/// newline(input) +/// } +/// +/// assert_eq!(parser("\nc"), Ok(("c", '\n'))); +/// assert_eq!(parser("\r\nc"), Err(Err::Error(("\r\nc", ErrorKind::Char)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Char)))); +/// # } +/// ``` +pub fn newline<I, Error: ParseError<I>>(input: I) -> IResult<I, char, Error> +where + I: Slice<RangeFrom<usize>> + InputIter, + <I as InputIter>::Item: AsChar, +{ + char('\n')(input) +} + +/// Matches a tab character '\t'. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::tab; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, char> { +/// tab(input) +/// } +/// +/// assert_eq!(parser("\tc"), Ok(("c", '\t'))); +/// assert_eq!(parser("\r\nc"), Err(Err::Error(("\r\nc", ErrorKind::Char)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Char)))); +/// # } +/// ``` +pub fn tab<I, Error: ParseError<I>>(input: I) -> IResult<I, char, Error> +where + I: Slice<RangeFrom<usize>> + InputIter, + <I as InputIter>::Item: AsChar, +{ + char('\t')(input) +} + +/// Matches one byte as a character. Note that the input type will +/// accept a `str`, but not a `&[u8]`, unlike many other nom parsers. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{character::complete::anychar, Err, error::ErrorKind, IResult}; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, char> { +/// anychar(input) +/// } +/// +/// assert_eq!(parser("abc"), Ok(("bc",'a'))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Eof)))); +/// # } +/// ``` +pub fn anychar<T, E: ParseError<T>>(input: T) -> IResult<T, char, E> +where + T: InputIter + InputLength + Slice<RangeFrom<usize>>, + <T as InputIter>::Item: AsChar, +{ + let mut it = input.iter_indices(); + match it.next() { + None => Err(Err::Error(E::from_error_kind(input, ErrorKind::Eof))), + Some((_, c)) => match it.next() { + None => Ok((input.slice(input.input_len()..), c.as_char())), + Some((idx, _)) => Ok((input.slice(idx..), c.as_char())), + }, + } +} + +/// Recognizes zero or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z +/// +/// *complete version*: Will return the whole input if no terminating token is found (a non +/// alphabetic character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::alpha0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// alpha0(input) +/// } +/// +/// assert_eq!(parser("ab1c"), Ok(("1c", "ab"))); +/// assert_eq!(parser("1c"), Ok(("1c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn alpha0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position_complete(|item| !item.is_alpha()) +} + +/// Recognizes one or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non alphabetic character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::alpha1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// alpha1(input) +/// } +/// +/// assert_eq!(parser("aB1c"), Ok(("1c", "aB"))); +/// assert_eq!(parser("1c"), Err(Err::Error(("1c", ErrorKind::Alpha)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn alpha1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position1_complete(|item| !item.is_alpha(), ErrorKind::Alpha) +} + +/// Recognizes zero or more ASCII numerical characters: 0-9 +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::digit0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// digit0(input) +/// } +/// +/// assert_eq!(parser("21c"), Ok(("c", "21"))); +/// assert_eq!(parser("21"), Ok(("", "21"))); +/// assert_eq!(parser("a21c"), Ok(("a21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn digit0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position_complete(|item| !item.is_dec_digit()) +} + +/// Recognizes one or more ASCII numerical characters: 0-9 +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::digit1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// digit1(input) +/// } +/// +/// assert_eq!(parser("21c"), Ok(("c", "21"))); +/// assert_eq!(parser("c1"), Err(Err::Error(("c1", ErrorKind::Digit)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Digit)))); +/// # } +/// ``` +pub fn digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position1_complete(|item| !item.is_dec_digit(), ErrorKind::Digit) +} + +/// Recognizes zero or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f +/// +/// *complete version*: Will return the whole input if no terminating token is found (a non hexadecimal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::hex_digit0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// hex_digit0(input) +/// } +/// +/// assert_eq!(parser("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn hex_digit0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position_complete(|item| !item.is_hex_digit()) +} +/// Recognizes one or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non hexadecimal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::hex_digit1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// hex_digit1(input) +/// } +/// +/// assert_eq!(parser("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(parser("H2"), Err(Err::Error(("H2", ErrorKind::HexDigit)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::HexDigit)))); +/// # } +/// ``` +pub fn hex_digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position1_complete(|item| !item.is_hex_digit(), ErrorKind::HexDigit) +} + +/// Recognizes zero or more octal characters: 0-7 +/// +/// *complete version*: Will return the whole input if no terminating token is found (a non octal +/// digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::oct_digit0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// oct_digit0(input) +/// } +/// +/// assert_eq!(parser("21cZ"), Ok(("cZ", "21"))); +/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn oct_digit0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position_complete(|item| !item.is_oct_digit()) +} + +/// Recognizes one or more octal characters: 0-7 +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non octal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::oct_digit1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// oct_digit1(input) +/// } +/// +/// assert_eq!(parser("21cZ"), Ok(("cZ", "21"))); +/// assert_eq!(parser("H2"), Err(Err::Error(("H2", ErrorKind::OctDigit)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::OctDigit)))); +/// # } +/// ``` +pub fn oct_digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position1_complete(|item| !item.is_oct_digit(), ErrorKind::OctDigit) +} + +/// Recognizes zero or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z +/// +/// *complete version*: Will return the whole input if no terminating token is found (a non +/// alphanumerical character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::alphanumeric0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// alphanumeric0(input) +/// } +/// +/// assert_eq!(parser("21cZ%1"), Ok(("%1", "21cZ"))); +/// assert_eq!(parser("&Z21c"), Ok(("&Z21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn alphanumeric0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position_complete(|item| !item.is_alphanum()) +} + +/// Recognizes one or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non alphanumerical character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::alphanumeric1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// alphanumeric1(input) +/// } +/// +/// assert_eq!(parser("21cZ%1"), Ok(("%1", "21cZ"))); +/// assert_eq!(parser("&H2"), Err(Err::Error(("&H2", ErrorKind::AlphaNumeric)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::AlphaNumeric)))); +/// # } +/// ``` +pub fn alphanumeric1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position1_complete(|item| !item.is_alphanum(), ErrorKind::AlphaNumeric) +} + +/// Recognizes zero or more spaces and tabs. +/// +/// *complete version*: Will return the whole input if no terminating token is found (a non space +/// character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::space0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// space0(input) +/// } +/// +/// assert_eq!(parser(" \t21c"), Ok(("21c", " \t"))); +/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn space0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar + Clone, +{ + input.split_at_position_complete(|item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t') + }) +} + +/// Recognizes one or more spaces and tabs. +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::space1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// space1(input) +/// } +/// +/// assert_eq!(parser(" \t21c"), Ok(("21c", " \t"))); +/// assert_eq!(parser("H2"), Err(Err::Error(("H2", ErrorKind::Space)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Space)))); +/// # } +/// ``` +pub fn space1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar + Clone, +{ + input.split_at_position1_complete( + |item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t') + }, + ErrorKind::Space, + ) +} + +/// Recognizes zero or more spaces, tabs, carriage returns and line feeds. +/// +/// *complete version*: will return the whole input if no terminating token is found (a non space +/// character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::multispace0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// multispace0(input) +/// } +/// +/// assert_eq!(parser(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); +/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn multispace0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar + Clone, +{ + input.split_at_position_complete(|item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t' || c == '\r' || c == '\n') + }) +} + +/// Recognizes one or more spaces, tabs, carriage returns and line feeds. +/// +/// *complete version*: will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::multispace1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// multispace1(input) +/// } +/// +/// assert_eq!(parser(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); +/// assert_eq!(parser("H2"), Err(Err::Error(("H2", ErrorKind::MultiSpace)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::MultiSpace)))); +/// # } +/// ``` +pub fn multispace1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar + Clone, +{ + input.split_at_position1_complete( + |item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t' || c == '\r' || c == '\n') + }, + ErrorKind::MultiSpace, + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::internal::Err; + + macro_rules! assert_parse( + ($left: expr, $right: expr) => { + let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; + assert_eq!(res, $right); + }; + ); + + + #[test] + fn character() { + let empty: &[u8] = b""; + let a: &[u8] = b"abcd"; + let b: &[u8] = b"1234"; + let c: &[u8] = b"a123"; + let d: &[u8] = "azé12".as_bytes(); + let e: &[u8] = b" "; + let f: &[u8] = b" ;"; + //assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_parse!(alpha1(a), Ok((empty, a))); + assert_eq!( + alpha1(b), + Err(Err::Error((b, ErrorKind::Alpha))) + ); + assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &b"a"[..]))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12".as_bytes(), &b"az"[..]))); + assert_eq!( + digit1(a), + Err(Err::Error((a, ErrorKind::Digit))) + ); + assert_eq!(digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!( + digit1(c), + Err(Err::Error((c, ErrorKind::Digit))) + ); + assert_eq!( + digit1(d), + Err(Err::Error((d, ErrorKind::Digit))) + ); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Ok((empty, a))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Ok((empty, c))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12".as_bytes(), &b"a"[..]))); + assert_eq!( + hex_digit1(e), + Err(Err::Error((e, ErrorKind::HexDigit))) + ); + assert_eq!( + oct_digit1(a), + Err(Err::Error((a, ErrorKind::OctDigit))) + ); + assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!( + oct_digit1(c), + Err(Err::Error((c, ErrorKind::OctDigit))) + ); + assert_eq!( + oct_digit1(d), + Err(Err::Error((d, ErrorKind::OctDigit))) + ); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Ok((empty, a))); + //assert_eq!(fix_error!(b,(), alphanumeric), Ok((empty, b))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(c), Ok((empty, c))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(d), Ok(("é12".as_bytes(), &b"az"[..]))); + assert_eq!(space1::<_, (_, ErrorKind)>(e), Ok((empty, e))); + assert_eq!(space1::<_, (_, ErrorKind)>(f), Ok((&b";"[..], &b" "[..]))); + } + + #[cfg(feature = "alloc")] + #[test] + fn character_s() { + let empty = ""; + let a = "abcd"; + let b = "1234"; + let c = "a123"; + let d = "azé12"; + let e = " "; + assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Ok((empty, a))); + assert_eq!( + alpha1(b), + Err(Err::Error((b, ErrorKind::Alpha))) + ); + assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &"a"[..]))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", &"az"[..]))); + assert_eq!( + digit1(a), + Err(Err::Error((a, ErrorKind::Digit))) + ); + assert_eq!(digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!( + digit1(c), + Err(Err::Error((c, ErrorKind::Digit))) + ); + assert_eq!( + digit1(d), + Err(Err::Error((d, ErrorKind::Digit))) + ); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Ok((empty, a))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Ok((empty, c))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", &"a"[..]))); + assert_eq!( + hex_digit1(e), + Err(Err::Error((e, ErrorKind::HexDigit))) + ); + assert_eq!( + oct_digit1(a), + Err(Err::Error((a, ErrorKind::OctDigit))) + ); + assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!( + oct_digit1(c), + Err(Err::Error((c, ErrorKind::OctDigit))) + ); + assert_eq!( + oct_digit1(d), + Err(Err::Error((d, ErrorKind::OctDigit))) + ); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Ok((empty, a))); + //assert_eq!(fix_error!(b,(), alphanumeric), Ok((empty, b))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(c), Ok((empty, c))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(d), Ok(("é12", "az"))); + assert_eq!(space1::<_, (_, ErrorKind)>(e), Ok((empty, e))); + } + + use crate::traits::Offset; + #[test] + fn offset() { + let a = &b"abcd;"[..]; + let b = &b"1234;"[..]; + let c = &b"a123;"[..]; + let d = &b" \t;"[..]; + let e = &b" \t\r\n;"[..]; + let f = &b"123abcDEF;"[..]; + + match alpha1::<_, (_, ErrorKind)>(a) { + Ok((i, _)) => { + assert_eq!(a.offset(i) + i.len(), a.len()); + } + _ => panic!("wrong return type in offset test for alpha"), + } + match digit1::<_, (_, ErrorKind)>(b) { + Ok((i, _)) => { + assert_eq!(b.offset(i) + i.len(), b.len()); + } + _ => panic!("wrong return type in offset test for digit"), + } + match alphanumeric1::<_, (_, ErrorKind)>(c) { + Ok((i, _)) => { + assert_eq!(c.offset(i) + i.len(), c.len()); + } + _ => panic!("wrong return type in offset test for alphanumeric"), + } + match space1::<_, (_, ErrorKind)>(d) { + Ok((i, _)) => { + assert_eq!(d.offset(i) + i.len(), d.len()); + } + _ => panic!("wrong return type in offset test for space"), + } + match multispace1::<_, (_, ErrorKind)>(e) { + Ok((i, _)) => { + assert_eq!(e.offset(i) + i.len(), e.len()); + } + _ => panic!("wrong return type in offset test for multispace"), + } + match hex_digit1::<_, (_, ErrorKind)>(f) { + Ok((i, _)) => { + assert_eq!(f.offset(i) + i.len(), f.len()); + } + _ => panic!("wrong return type in offset test for hex_digit"), + } + match oct_digit1::<_, (_, ErrorKind)>(f) { + Ok((i, _)) => { + assert_eq!(f.offset(i) + i.len(), f.len()); + } + _ => panic!("wrong return type in offset test for oct_digit"), + } + } + + #[test] + fn is_not_line_ending_bytes() { + let a: &[u8] = b"ab12cd\nefgh"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(a), Ok((&b"\nefgh"[..], &b"ab12cd"[..]))); + + let b: &[u8] = b"ab12cd\nefgh\nijkl"; + assert_eq!( + not_line_ending::<_, (_, ErrorKind)>(b), + Ok((&b"\nefgh\nijkl"[..], &b"ab12cd"[..])) + ); + + let c: &[u8] = b"ab12cd\r\nefgh\nijkl"; + assert_eq!( + not_line_ending::<_, (_, ErrorKind)>(c), + Ok((&b"\r\nefgh\nijkl"[..], &b"ab12cd"[..])) + ); + + let d: &[u8] = b"ab12cd"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(d), Ok((&[][..], &d[..]))); + } + + #[test] + fn is_not_line_ending_str() { + + /* + let a: &str = "ab12cd\nefgh"; + assert_eq!(not_line_ending(a), Ok((&"\nefgh"[..], &"ab12cd"[..]))); + + let b: &str = "ab12cd\nefgh\nijkl"; + assert_eq!(not_line_ending(b), Ok((&"\nefgh\nijkl"[..], &"ab12cd"[..]))); + + let c: &str = "ab12cd\r\nefgh\nijkl"; + assert_eq!(not_line_ending(c), Ok((&"\r\nefgh\nijkl"[..], &"ab12cd"[..]))); + + let d = "βèƒôřè\nÂßÇáƒƭèř"; + assert_eq!(not_line_ending(d), Ok((&"\nÂßÇáƒƭèř"[..], &"βèƒôřè"[..]))); + + let e = "βèƒôřè\r\nÂßÇáƒƭèř"; + assert_eq!(not_line_ending(e), Ok((&"\r\nÂßÇáƒƭèř"[..], &"βèƒôřè"[..]))); + */ + + let f = "βèƒôřè\rÂßÇáƒƭèř"; + assert_eq!( + not_line_ending(f), + Err(Err::Error((f, ErrorKind::Tag))) + ); + + let g2: &str = "ab12cd"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(g2), Ok(("", g2))); + } + + #[test] + fn hex_digit_test() { + let i = &b"0123456789abcdefABCDEF;"[..]; + assert_parse!(hex_digit1(i), Ok((&b";"[..], &i[..i.len() - 1]))); + + let i = &b"g"[..]; + assert_parse!( + hex_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::HexDigit))) + ); + + let i = &b"G"[..]; + assert_parse!( + hex_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::HexDigit))) + ); + + assert!(crate::character::is_hex_digit(b'0')); + assert!(crate::character::is_hex_digit(b'9')); + assert!(crate::character::is_hex_digit(b'a')); + assert!(crate::character::is_hex_digit(b'f')); + assert!(crate::character::is_hex_digit(b'A')); + assert!(crate::character::is_hex_digit(b'F')); + assert!(!crate::character::is_hex_digit(b'g')); + assert!(!crate::character::is_hex_digit(b'G')); + assert!(!crate::character::is_hex_digit(b'/')); + assert!(!crate::character::is_hex_digit(b':')); + assert!(!crate::character::is_hex_digit(b'@')); + assert!(!crate::character::is_hex_digit(b'\x60')); + } + + #[test] + fn oct_digit_test() { + let i = &b"01234567;"[..]; + assert_parse!(oct_digit1(i), Ok((&b";"[..], &i[..i.len() - 1]))); + + let i = &b"8"[..]; + assert_parse!( + oct_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::OctDigit))) + ); + + assert!(crate::character::is_oct_digit(b'0')); + assert!(crate::character::is_oct_digit(b'7')); + assert!(!crate::character::is_oct_digit(b'8')); + assert!(!crate::character::is_oct_digit(b'9')); + assert!(!crate::character::is_oct_digit(b'a')); + assert!(!crate::character::is_oct_digit(b'A')); + assert!(!crate::character::is_oct_digit(b'/')); + assert!(!crate::character::is_oct_digit(b':')); + assert!(!crate::character::is_oct_digit(b'@')); + assert!(!crate::character::is_oct_digit(b'\x60')); + } + + #[test] + fn full_line_windows() { + //let not_line_ending = |i:&[u8]| take_while(|c| c != b'\r' && c != b'\n')(i); + + named!( + take_full_line<(&[u8], &[u8])>, + tuple!(not_line_ending, line_ending) + ); + let input = b"abc\r\n"; + let output = take_full_line(input); + assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\r\n"[..])))); + } + + #[test] + fn full_line_unix() { + //let not_line_ending = |i:&[u8]| take_while(|c| c != b'\n')(i); + named!( + take_full_line<(&[u8], &[u8])>, + tuple!(not_line_ending, line_ending) + ); + let input = b"abc\n"; + let output = take_full_line(input); + assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\n"[..])))); + } + + #[test] + fn check_windows_lineending() { + let input = b"\r\n"; + let output = line_ending(&input[..]); + assert_parse!(output, Ok((&b""[..], &b"\r\n"[..]))); + } + + #[test] + fn check_unix_lineending() { + let input = b"\n"; + let output = line_ending(&input[..]); + assert_parse!(output, Ok((&b""[..], &b"\n"[..]))); + } + + #[test] + fn cr_lf() { + assert_parse!(crlf(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..]))); + assert_parse!(crlf(&b"\r"[..]), Err(Err::Error(error_position!(&b"\r"[..], ErrorKind::CrLf)))); + assert_parse!( + crlf(&b"\ra"[..]), + Err(Err::Error(error_position!(&b"\ra"[..], ErrorKind::CrLf))) + ); + + assert_parse!(crlf("\r\na"), Ok(("a", "\r\n"))); + assert_parse!(crlf("\r"), Err(Err::Error(error_position!(&"\r"[..], ErrorKind::CrLf)))); + assert_parse!( + crlf("\ra"), + Err(Err::Error(error_position!("\ra", ErrorKind::CrLf))) + ); + } + + #[test] + fn end_of_line() { + assert_parse!(line_ending(&b"\na"[..]), Ok((&b"a"[..], &b"\n"[..]))); + assert_parse!(line_ending(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..]))); + assert_parse!(line_ending(&b"\r"[..]), Err(Err::Error(error_position!(&b"\r"[..], ErrorKind::CrLf)))); + assert_parse!( + line_ending(&b"\ra"[..]), + Err(Err::Error(error_position!(&b"\ra"[..], ErrorKind::CrLf))) + ); + + assert_parse!(line_ending("\na"), Ok(("a", "\n"))); + assert_parse!(line_ending("\r\na"), Ok(("a", "\r\n"))); + assert_parse!(line_ending("\r"), Err(Err::Error(error_position!(&"\r"[..], ErrorKind::CrLf)))); + assert_parse!( + line_ending("\ra"), + Err(Err::Error(error_position!("\ra", ErrorKind::CrLf))) + ); + } +} diff --git a/third_party/rust/nom/src/character/macros.rs b/third_party/rust/nom/src/character/macros.rs new file mode 100644 index 0000000000..097ee409fe --- /dev/null +++ b/third_party/rust/nom/src/character/macros.rs @@ -0,0 +1,112 @@ +/// Character level parsers + +/// matches one of the provided characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(simple<char>, one_of!(&b"abc"[..])); +/// assert_eq!(simple(b"a123"), Ok((&b"123"[..], 'a'))); +/// +/// named!(a_or_b<&str, char>, one_of!("ab汉")); +/// assert_eq!(a_or_b("汉jiosfe"), Ok(("jiosfe", '汉'))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! one_of ( + ($i:expr, $inp: expr) => ( $crate::character::streaming::one_of($inp)($i) ); +); + +/// matches anything but the provided characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind}; +/// # fn main() { +/// named!(no_letter_a<char>, none_of!(&b"abc"[..])); +/// assert_eq!(no_letter_a(b"123"), Ok((&b"23"[..], '1'))); +/// +/// named!(err_on_single_quote<char>, none_of!(&b"'"[..])); +/// assert_eq!(err_on_single_quote(b"'jiosfe"), Err(Err::Error(error_position!(&b"'jiosfe"[..], ErrorKind::NoneOf)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! none_of ( + ($i:expr, $inp: expr) => ( $crate::character::streaming::none_of($inp)($i) ); +); + +/// matches one character: `char!(char) => &[u8] -> IResult<&[u8], char> +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind}; +/// # fn main() { +/// named!(match_letter_a<char>, char!('a')); +/// assert_eq!(match_letter_a(b"abc"), Ok((&b"bc"[..],'a'))); +/// +/// assert_eq!(match_letter_a(b"123cdef"), Err(Err::Error(error_position!(&b"123cdef"[..], ErrorKind::Char)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! char ( + ($i:expr, $c: expr) => ( $crate::character::streaming::char($c)($i) ); +); + +#[cfg(test)] +mod tests { + use crate::internal::Err; + use crate::error::ErrorKind; + + #[test] + fn one_of() { + named!(f<char>, one_of!("ab")); + + let a = &b"abcd"[..]; + assert_eq!(f(a), Ok((&b"bcd"[..], 'a'))); + + let b = &b"cde"[..]; + assert_eq!(f(b), Err(Err::Error(error_position!(b, ErrorKind::OneOf)))); + + named!(utf8(&str) -> char, + one_of!("+\u{FF0B}")); + + assert!(utf8("+").is_ok()); + assert!(utf8("\u{FF0B}").is_ok()); + } + + #[test] + fn none_of() { + named!(f<char>, none_of!("ab")); + + let a = &b"abcd"[..]; + assert_eq!(f(a), Err(Err::Error(error_position!(a, ErrorKind::NoneOf)))); + + let b = &b"cde"[..]; + assert_eq!(f(b), Ok((&b"de"[..], 'c'))); + } + + #[test] + fn char() { + named!(f<char>, char!('c')); + + let a = &b"abcd"[..]; + assert_eq!(f(a), Err(Err::Error(error_position!(a, ErrorKind::Char)))); + + let b = &b"cde"[..]; + assert_eq!(f(b), Ok((&b"de"[..], 'c'))); + } + + #[test] + fn char_str() { + named!(f<&str, char>, char!('c')); + + let a = &"abcd"[..]; + assert_eq!(f(a), Err(Err::Error(error_position!(a, ErrorKind::Char)))); + + let b = &"cde"[..]; + assert_eq!(f(b), Ok((&"de"[..], 'c'))); + } +} diff --git a/third_party/rust/nom/src/character/mod.rs b/third_party/rust/nom/src/character/mod.rs new file mode 100644 index 0000000000..3f0899aa8c --- /dev/null +++ b/third_party/rust/nom/src/character/mod.rs @@ -0,0 +1,101 @@ +//! character specific parsers and combinators +//! +//! functions recognizing specific characters + +#[macro_use] +mod macros; + +pub mod streaming; +pub mod complete; + +/// Tests if byte is ASCII alphabetic: A-Z, a-z +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_alphabetic; +/// assert_eq!(is_alphabetic(b'9'), false); +/// assert_eq!(is_alphabetic(b'a'), true); +/// ``` +#[inline] +pub fn is_alphabetic(chr: u8) -> bool { + (chr >= 0x41 && chr <= 0x5A) || (chr >= 0x61 && chr <= 0x7A) +} + +/// Tests if byte is ASCII digit: 0-9 +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_digit; +/// assert_eq!(is_digit(b'a'), false); +/// assert_eq!(is_digit(b'9'), true); +/// ``` +#[inline] +pub fn is_digit(chr: u8) -> bool { + chr >= 0x30 && chr <= 0x39 +} + +/// Tests if byte is ASCII hex digit: 0-9, A-F, a-f +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_hex_digit; +/// assert_eq!(is_hex_digit(b'a'), true); +/// assert_eq!(is_hex_digit(b'9'), true); +/// assert_eq!(is_hex_digit(b'A'), true); +/// assert_eq!(is_hex_digit(b'x'), false); +/// ``` +#[inline] +pub fn is_hex_digit(chr: u8) -> bool { + (chr >= 0x30 && chr <= 0x39) || (chr >= 0x41 && chr <= 0x46) || (chr >= 0x61 && chr <= 0x66) +} + +/// Tests if byte is ASCII octal digit: 0-7 +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_oct_digit; +/// assert_eq!(is_oct_digit(b'a'), false); +/// assert_eq!(is_oct_digit(b'9'), false); +/// assert_eq!(is_oct_digit(b'6'), true); +/// ``` +#[inline] +pub fn is_oct_digit(chr: u8) -> bool { + chr >= 0x30 && chr <= 0x37 +} + +/// Tests if byte is ASCII alphanumeric: A-Z, a-z, 0-9 +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_alphanumeric; +/// assert_eq!(is_alphanumeric(b'-'), false); +/// assert_eq!(is_alphanumeric(b'a'), true); +/// assert_eq!(is_alphanumeric(b'9'), true); +/// assert_eq!(is_alphanumeric(b'A'), true); +/// ``` +#[inline] +pub fn is_alphanumeric(chr: u8) -> bool { + is_alphabetic(chr) || is_digit(chr) +} + +/// Tests if byte is ASCII space or tab +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_space; +/// assert_eq!(is_space(b'\n'), false); +/// assert_eq!(is_space(b'\r'), false); +/// assert_eq!(is_space(b' '), true); +/// assert_eq!(is_space(b'\t'), true); +/// ``` +#[inline] +pub fn is_space(chr: u8) -> bool { + chr == b' ' || chr == b'\t' +} + diff --git a/third_party/rust/nom/src/character/streaming.rs b/third_party/rust/nom/src/character/streaming.rs new file mode 100644 index 0000000000..f62daceeb4 --- /dev/null +++ b/third_party/rust/nom/src/character/streaming.rs @@ -0,0 +1,1010 @@ +//! character specific parsers and combinators, streaming version +//! +//! functions recognizing specific characters + +use crate::internal::{Err, IResult, Needed}; +use crate::error::ParseError; +use crate::lib::std::ops::{Range, RangeFrom, RangeTo}; +use crate::traits::{AsChar, FindToken, InputIter, InputLength, InputTakeAtPosition, Slice}; +use crate::traits::{Compare, CompareResult}; + +use crate::error::ErrorKind; + +/// Recognizes one character. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::character::streaming::char; +/// # fn main() { +/// assert_eq!(char::<_, (_, ErrorKind)>('a')(&b"abc"[..]), Ok((&b"bc"[..], 'a'))); +/// assert_eq!(char::<_, (_, ErrorKind)>('a')(&b"bc"[..]), Err(Err::Error((&b"bc"[..], ErrorKind::Char)))); +/// assert_eq!(char::<_, (_, ErrorKind)>('a')(&b""[..]), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn char<I, Error: ParseError<I>>(c: char) -> impl Fn(I) -> IResult<I, char, Error> +where + I: Slice<RangeFrom<usize>> + InputIter, + <I as InputIter>::Item: AsChar, +{ + move |i: I| match (i).iter_elements().next().map(|t| { + let b = t.as_char() == c; + (&c, b) + }) { + None => Err(Err::Incomplete(Needed::Size(1))), + Some((_, false)) => { + Err(Err::Error(Error::from_char(i, c))) + } + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + } +} + +/// Recognizes one of the provided characters. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::character::streaming::one_of; +/// # fn main() { +/// assert_eq!(one_of::<_, _, (_, ErrorKind)>("abc")("b"), Ok(("", 'b'))); +/// assert_eq!(one_of::<_, _, (_, ErrorKind)>("a")("bc"), Err(Err::Error(("bc", ErrorKind::OneOf)))); +/// assert_eq!(one_of::<_, _, (_, ErrorKind)>("a")(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn one_of<I, T, Error: ParseError<I>>(list: T) -> impl Fn(I) -> IResult<I, char, Error> +where + I: Slice<RangeFrom<usize>> + InputIter, + <I as InputIter>::Item: AsChar + Copy, + T: FindToken<<I as InputIter>::Item>, +{ + move |i: I| match (i).iter_elements().next().map(|c| (c, list.find_token(c))) { + None => Err(Err::Incomplete(Needed::Size(1))), + Some((_, false)) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::OneOf))), + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + } +} + +/// Recognizes a character that is not in the provided characters. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::character::streaming::none_of; +/// # fn main() { +/// assert_eq!(none_of::<_, _, (_, ErrorKind)>("abc")("z"), Ok(("", 'z'))); +/// assert_eq!(none_of::<_, _, (_, ErrorKind)>("ab")("a"), Err(Err::Error(("a", ErrorKind::NoneOf)))); +/// assert_eq!(none_of::<_, _, (_, ErrorKind)>("a")(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn none_of<I, T, Error: ParseError<I>>(list: T) -> impl Fn(I) -> IResult<I, char, Error> +where + I: Slice<RangeFrom<usize>> + InputIter, + <I as InputIter>::Item: AsChar + Copy, + T: FindToken<<I as InputIter>::Item>, +{ + move |i: I| match (i).iter_elements().next().map(|c| (c, !list.find_token(c))) { + None => Err(Err::Incomplete(Needed::Size(1))), + Some((_, false)) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::NoneOf))), + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + } +} + +/// Recognizes the string "\r\n". +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::crlf; +/// # fn main() { +/// assert_eq!(crlf::<_, (_, ErrorKind)>("\r\nc"), Ok(("c", "\r\n"))); +/// assert_eq!(crlf::<_, (_, ErrorKind)>("ab\r\nc"), Err(Err::Error(("ab\r\nc", ErrorKind::CrLf)))); +/// assert_eq!(crlf::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(2)))); +/// # } +/// ``` +pub fn crlf<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: Slice<Range<usize>> + Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: InputIter, + T: Compare<&'static str>, +{ + match input.compare("\r\n") { + //FIXME: is this the right index? + CompareResult::Ok => Ok((input.slice(2..), input.slice(0..2))), + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Size(2))), + CompareResult::Error => { + let e: ErrorKind = ErrorKind::CrLf; + Err(Err::Error(E::from_error_kind(input, e))) + } + } +} + +/// Recognizes a string of any char except '\r' or '\n'. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::not_line_ending; +/// # fn main() { +/// assert_eq!(not_line_ending::<_, (_, ErrorKind)>("ab\r\nc"), Ok(("\r\nc", "ab"))); +/// assert_eq!(not_line_ending::<_, (_, ErrorKind)>("abc"), Err(Err::Incomplete(Needed::Unknown))); +/// assert_eq!(not_line_ending::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Unknown))); +/// # } +/// ``` +pub fn not_line_ending<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: Slice<Range<usize>> + Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: InputIter + InputLength, + T: Compare<&'static str>, + <T as InputIter>::Item: AsChar, + <T as InputIter>::Item: AsChar, +{ + match input.position(|item| { + let c = item.as_char(); + c == '\r' || c == '\n' + }) { + None => { + Err(Err::Incomplete(Needed::Unknown)) + } + Some(index) => { + let mut it = input.slice(index..).iter_elements(); + let nth = it.next().unwrap().as_char(); + if nth == '\r' { + let sliced = input.slice(index..); + let comp = sliced.compare("\r\n"); + match comp { + //FIXME: calculate the right index + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Unknown)), + CompareResult::Error => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(E::from_error_kind(input, e))) + } + CompareResult::Ok => Ok((input.slice(index..), input.slice(..index))), + } + } else { + Ok((input.slice(index..), input.slice(..index))) + } + } + } +} + +/// Recognizes an end of line (both '\n' and '\r\n'). +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::line_ending; +/// # fn main() { +/// assert_eq!(line_ending::<_, (_, ErrorKind)>("\r\nc"), Ok(("c", "\r\n"))); +/// assert_eq!(line_ending::<_, (_, ErrorKind)>("ab\r\nc"), Err(Err::Error(("ab\r\nc", ErrorKind::CrLf)))); +/// assert_eq!(line_ending::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn line_ending<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: Slice<Range<usize>> + Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: InputIter + InputLength, + T: Compare<&'static str>, +{ + match input.compare("\n") { + CompareResult::Ok => Ok((input.slice(1..), input.slice(0..1))), + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Size(1))), + CompareResult::Error => { + match input.compare("\r\n") { + //FIXME: is this the right index? + CompareResult::Ok => Ok((input.slice(2..), input.slice(0..2))), + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Size(2))), + CompareResult::Error => Err(Err::Error(E::from_error_kind(input, ErrorKind::CrLf))), + } + } + } +} + +/// Matches a newline character '\\n'. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::newline; +/// # fn main() { +/// assert_eq!(newline::<_, (_, ErrorKind)>("\nc"), Ok(("c", '\n'))); +/// assert_eq!(newline::<_, (_, ErrorKind)>("\r\nc"), Err(Err::Error(("\r\nc", ErrorKind::Char)))); +/// assert_eq!(newline::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn newline<I, Error: ParseError<I>>(input: I) -> IResult<I, char, Error> +where + I: Slice<RangeFrom<usize>> + InputIter, + <I as InputIter>::Item: AsChar, +{ + char('\n')(input) +} + +/// Matches a tab character '\t'. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::tab; +/// # fn main() { +/// assert_eq!(tab::<_, (_, ErrorKind)>("\tc"), Ok(("c", '\t'))); +/// assert_eq!(tab::<_, (_, ErrorKind)>("\r\nc"), Err(Err::Error(("\r\nc", ErrorKind::Char)))); +/// assert_eq!(tab::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn tab<I, Error: ParseError<I>>(input: I) -> IResult<I, char, Error> +where + I: Slice<RangeFrom<usize>> + InputIter, + <I as InputIter>::Item: AsChar, +{ + char('\t')(input) +} + +/// Matches one byte as a character. Note that the input type will +/// accept a `str`, but not a `&[u8]`, unlike many other nom parsers. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{character::streaming::anychar, Err, error::ErrorKind, IResult, Needed}; +/// # fn main() { +/// assert_eq!(anychar::<_, (_, ErrorKind)>("abc"), Ok(("bc",'a'))); +/// assert_eq!(anychar::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn anychar<T, E: ParseError<T>>(input: T) -> IResult<T, char, E> +where + T: InputIter + InputLength + Slice<RangeFrom<usize>>, + <T as InputIter>::Item: AsChar, +{ + let mut it = input.iter_indices(); + match it.next() { + None => Err(Err::Incomplete(Needed::Size(1))), + Some((_, c)) => match it.next() { + None => Ok((input.slice(input.input_len()..), c.as_char())), + Some((idx, _)) => Ok((input.slice(idx..), c.as_char())), + }, + } +} + +/// Recognizes zero or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non alphabetic character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::alpha0; +/// # fn main() { +/// assert_eq!(alpha0::<_, (_, ErrorKind)>("ab1c"), Ok(("1c", "ab"))); +/// assert_eq!(alpha0::<_, (_, ErrorKind)>("1c"), Ok(("1c", ""))); +/// assert_eq!(alpha0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn alpha0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position(|item| !item.is_alpha()) +} + +/// Recognizes one or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non alphabetic character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::alpha1; +/// # fn main() { +/// assert_eq!(alpha1::<_, (_, ErrorKind)>("aB1c"), Ok(("1c", "aB"))); +/// assert_eq!(alpha1::<_, (_, ErrorKind)>("1c"), Err(Err::Error(("1c", ErrorKind::Alpha)))); +/// assert_eq!(alpha1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn alpha1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position1(|item| !item.is_alpha(), ErrorKind::Alpha) +} + +/// Recognizes zero or more ASCII numerical characters: 0-9 +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::digit0; +/// # fn main() { +/// assert_eq!(digit0::<_, (_, ErrorKind)>("21c"), Ok(("c", "21"))); +/// assert_eq!(digit0::<_, (_, ErrorKind)>("a21c"), Ok(("a21c", ""))); +/// assert_eq!(digit0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn digit0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position(|item| !item.is_dec_digit()) +} + +/// Recognizes one or more ASCII numerical characters: 0-9 +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::digit1; +/// # fn main() { +/// assert_eq!(digit1::<_, (_, ErrorKind)>("21c"), Ok(("c", "21"))); +/// assert_eq!(digit1::<_, (_, ErrorKind)>("c1"), Err(Err::Error(("c1", ErrorKind::Digit)))); +/// assert_eq!(digit1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position1(|item| !item.is_dec_digit(), ErrorKind::Digit) +} + +/// Recognizes zero or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non hexadecimal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::hex_digit0; +/// # fn main() { +/// assert_eq!(hex_digit0::<_, (_, ErrorKind)>("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(hex_digit0::<_, (_, ErrorKind)>("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(hex_digit0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn hex_digit0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position(|item| !item.is_hex_digit()) +} + +/// Recognizes one or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non hexadecimal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::hex_digit1; +/// # fn main() { +/// assert_eq!(hex_digit1::<_, (_, ErrorKind)>("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(hex_digit1::<_, (_, ErrorKind)>("H2"), Err(Err::Error(("H2", ErrorKind::HexDigit)))); +/// assert_eq!(hex_digit1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn hex_digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position1(|item| !item.is_hex_digit(), ErrorKind::HexDigit) +} + +/// Recognizes zero or more octal characters: 0-7 +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non octal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::oct_digit0; +/// # fn main() { +/// assert_eq!(oct_digit0::<_, (_, ErrorKind)>("21cZ"), Ok(("cZ", "21"))); +/// assert_eq!(oct_digit0::<_, (_, ErrorKind)>("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(oct_digit0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn oct_digit0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position(|item| !item.is_oct_digit()) +} + +/// Recognizes one or more octal characters: 0-7 +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non octal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::oct_digit1; +/// # fn main() { +/// assert_eq!(oct_digit1::<_, (_, ErrorKind)>("21cZ"), Ok(("cZ", "21"))); +/// assert_eq!(oct_digit1::<_, (_, ErrorKind)>("H2"), Err(Err::Error(("H2", ErrorKind::OctDigit)))); +/// assert_eq!(oct_digit1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn oct_digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position1(|item| !item.is_oct_digit(), ErrorKind::OctDigit) +} + +/// Recognizes zero or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non alphanumerical character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::alphanumeric0; +/// # fn main() { +/// assert_eq!(alphanumeric0::<_, (_, ErrorKind)>("21cZ%1"), Ok(("%1", "21cZ"))); +/// assert_eq!(alphanumeric0::<_, (_, ErrorKind)>("&Z21c"), Ok(("&Z21c", ""))); +/// assert_eq!(alphanumeric0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn alphanumeric0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position(|item| !item.is_alphanum()) +} + +/// Recognizes one or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non alphanumerical character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::alphanumeric1; +/// # fn main() { +/// assert_eq!(alphanumeric1::<_, (_, ErrorKind)>("21cZ%1"), Ok(("%1", "21cZ"))); +/// assert_eq!(alphanumeric1::<_, (_, ErrorKind)>("&H2"), Err(Err::Error(("&H2", ErrorKind::AlphaNumeric)))); +/// assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn alphanumeric1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + input.split_at_position1(|item| !item.is_alphanum(), ErrorKind::AlphaNumeric) +} + +/// Recognizes zero or more spaces and tabs. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::space0; +/// # fn main() { +/// assert_eq!(space0::<_, (_, ErrorKind)>(" \t21c"), Ok(("21c", " \t"))); +/// assert_eq!(space0::<_, (_, ErrorKind)>("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(space0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn space0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar + Clone, +{ + input.split_at_position(|item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t') + }) +} +/// Recognizes one or more spaces and tabs. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::space1; +/// # fn main() { +/// assert_eq!(space1::<_, (_, ErrorKind)>(" \t21c"), Ok(("21c", " \t"))); +/// assert_eq!(space1::<_, (_, ErrorKind)>("H2"), Err(Err::Error(("H2", ErrorKind::Space)))); +/// assert_eq!(space1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn space1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar + Clone, +{ + input.split_at_position1( + |item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t') + }, + ErrorKind::Space, + ) +} + +/// Recognizes zero or more spaces, tabs, carriage returns and line feeds. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::multispace0; +/// # fn main() { +/// assert_eq!(multispace0::<_, (_, ErrorKind)>(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); +/// assert_eq!(multispace0::<_, (_, ErrorKind)>("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(multispace0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn multispace0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar + Clone, +{ + input.split_at_position(|item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t' || c == '\r' || c == '\n') + }) +} + +/// Recognizes one or more spaces, tabs, carriage returns and line feeds. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::multispace1; +/// # fn main() { +/// assert_eq!(multispace1::<_, (_, ErrorKind)>(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); +/// assert_eq!(multispace1::<_, (_, ErrorKind)>("H2"), Err(Err::Error(("H2", ErrorKind::MultiSpace)))); +/// assert_eq!(multispace1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn multispace1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar + Clone, +{ + input.split_at_position1( + |item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t' || c == '\r' || c == '\n') + }, + ErrorKind::MultiSpace, + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::internal::{Err, Needed}; + use crate::error::ErrorKind; + + macro_rules! assert_parse( + ($left: expr, $right: expr) => { + let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; + assert_eq!(res, $right); + }; + ); + + #[test] + fn anychar_str() { + use super::anychar; + assert_eq!(anychar::<_, (&str, ErrorKind)>("Ә"), Ok(("", 'Ә'))); + } + + #[test] + fn character() { + let a: &[u8] = b"abcd"; + let b: &[u8] = b"1234"; + let c: &[u8] = b"a123"; + let d: &[u8] = "azé12".as_bytes(); + let e: &[u8] = b" "; + let f: &[u8] = b" ;"; + //assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_parse!(alpha1(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + alpha1(b), + Err(Err::Error((b, ErrorKind::Alpha))) + ); + assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &b"a"[..]))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12".as_bytes(), &b"az"[..]))); + assert_eq!( + digit1(a), + Err(Err::Error((a, ErrorKind::Digit))) + ); + assert_eq!(digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + digit1(c), + Err(Err::Error((c, ErrorKind::Digit))) + ); + assert_eq!( + digit1(d), + Err(Err::Error((d, ErrorKind::Digit))) + ); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12".as_bytes(), &b"a"[..]))); + assert_eq!( + hex_digit1(e), + Err(Err::Error((e, ErrorKind::HexDigit))) + ); + assert_eq!( + oct_digit1(a), + Err(Err::Error((a, ErrorKind::OctDigit))) + ); + assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + oct_digit1(c), + Err(Err::Error((c, ErrorKind::OctDigit))) + ); + assert_eq!( + oct_digit1(d), + Err(Err::Error((d, ErrorKind::OctDigit))) + ); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + //assert_eq!(fix_error!(b,(), alphanumeric1), Ok((empty, b))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(c), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(d), Ok(("é12".as_bytes(), &b"az"[..]))); + assert_eq!(space1::<_, (_, ErrorKind)>(e), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(space1::<_, (_, ErrorKind)>(f), Ok((&b";"[..], &b" "[..]))); + } + + #[cfg(feature = "alloc")] + #[test] + fn character_s() { + let a = "abcd"; + let b = "1234"; + let c = "a123"; + let d = "azé12"; + let e = " "; + assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + alpha1(b), + Err(Err::Error((b, ErrorKind::Alpha))) + ); + assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &"a"[..]))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", &"az"[..]))); + assert_eq!( + digit1(a), + Err(Err::Error((a, ErrorKind::Digit))) + ); + assert_eq!(digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + digit1(c), + Err(Err::Error((c, ErrorKind::Digit))) + ); + assert_eq!( + digit1(d), + Err(Err::Error((d, ErrorKind::Digit))) + ); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", &"a"[..]))); + assert_eq!( + hex_digit1(e), + Err(Err::Error((e, ErrorKind::HexDigit))) + ); + assert_eq!( + oct_digit1(a), + Err(Err::Error((a, ErrorKind::OctDigit))) + ); + assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + oct_digit1(c), + Err(Err::Error((c, ErrorKind::OctDigit))) + ); + assert_eq!( + oct_digit1(d), + Err(Err::Error((d, ErrorKind::OctDigit))) + ); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + //assert_eq!(fix_error!(b,(), alphanumeric1), Ok((empty, b))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(c), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(d), Ok(("é12", "az"))); + assert_eq!(space1::<_, (_, ErrorKind)>(e), Err(Err::Incomplete(Needed::Size(1)))); + } + + use crate::traits::Offset; + #[test] + fn offset() { + let a = &b"abcd;"[..]; + let b = &b"1234;"[..]; + let c = &b"a123;"[..]; + let d = &b" \t;"[..]; + let e = &b" \t\r\n;"[..]; + let f = &b"123abcDEF;"[..]; + + match alpha1::<_, (_, ErrorKind)>(a) { + Ok((i, _)) => { + assert_eq!(a.offset(i) + i.len(), a.len()); + } + _ => panic!("wrong return type in offset test for alpha"), + } + match digit1::<_, (_, ErrorKind)>(b) { + Ok((i, _)) => { + assert_eq!(b.offset(i) + i.len(), b.len()); + } + _ => panic!("wrong return type in offset test for digit"), + } + match alphanumeric1::<_, (_, ErrorKind)>(c) { + Ok((i, _)) => { + assert_eq!(c.offset(i) + i.len(), c.len()); + } + _ => panic!("wrong return type in offset test for alphanumeric"), + } + match space1::<_, (_, ErrorKind)>(d) { + Ok((i, _)) => { + assert_eq!(d.offset(i) + i.len(), d.len()); + } + _ => panic!("wrong return type in offset test for space"), + } + match multispace1::<_, (_, ErrorKind)>(e) { + Ok((i, _)) => { + assert_eq!(e.offset(i) + i.len(), e.len()); + } + _ => panic!("wrong return type in offset test for multispace"), + } + match hex_digit1::<_, (_, ErrorKind)>(f) { + Ok((i, _)) => { + assert_eq!(f.offset(i) + i.len(), f.len()); + } + _ => panic!("wrong return type in offset test for hex_digit"), + } + match oct_digit1::<_, (_, ErrorKind)>(f) { + Ok((i, _)) => { + assert_eq!(f.offset(i) + i.len(), f.len()); + } + _ => panic!("wrong return type in offset test for oct_digit"), + } + } + + #[test] + fn is_not_line_ending_bytes() { + let a: &[u8] = b"ab12cd\nefgh"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(a), Ok((&b"\nefgh"[..], &b"ab12cd"[..]))); + + let b: &[u8] = b"ab12cd\nefgh\nijkl"; + assert_eq!( + not_line_ending::<_, (_, ErrorKind)>(b), + Ok((&b"\nefgh\nijkl"[..], &b"ab12cd"[..])) + ); + + let c: &[u8] = b"ab12cd\r\nefgh\nijkl"; + assert_eq!( + not_line_ending::<_, (_, ErrorKind)>(c), + Ok((&b"\r\nefgh\nijkl"[..], &b"ab12cd"[..])) + ); + + let d: &[u8] = b"ab12cd"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(d), Err(Err::Incomplete(Needed::Unknown))); + } + + #[test] + fn is_not_line_ending_str() { + /* + let a: &str = "ab12cd\nefgh"; + assert_eq!(not_line_ending(a), Ok((&"\nefgh"[..], &"ab12cd"[..]))); + + let b: &str = "ab12cd\nefgh\nijkl"; + assert_eq!(not_line_ending(b), Ok((&"\nefgh\nijkl"[..], &"ab12cd"[..]))); + + let c: &str = "ab12cd\r\nefgh\nijkl"; + assert_eq!(not_line_ending(c), Ok((&"\r\nefgh\nijkl"[..], &"ab12cd"[..]))); + + let d = "βèƒôřè\nÂßÇáƒƭèř"; + assert_eq!(not_line_ending(d), Ok((&"\nÂßÇáƒƭèř"[..], &"βèƒôřè"[..]))); + + let e = "βèƒôřè\r\nÂßÇáƒƭèř"; + assert_eq!(not_line_ending(e), Ok((&"\r\nÂßÇáƒƭèř"[..], &"βèƒôřè"[..]))); + */ + + let f = "βèƒôřè\rÂßÇáƒƭèř"; + assert_eq!( + not_line_ending(f), + Err(Err::Error((f, ErrorKind::Tag))) + ); + + let g2: &str = "ab12cd"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(g2), Err(Err::Incomplete(Needed::Unknown))); + } + + #[test] + fn hex_digit_test() { + let i = &b"0123456789abcdefABCDEF;"[..]; + assert_parse!(hex_digit1(i), Ok((&b";"[..], &i[..i.len() - 1]))); + + let i = &b"g"[..]; + assert_parse!( + hex_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::HexDigit))) + ); + + let i = &b"G"[..]; + assert_parse!( + hex_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::HexDigit))) + ); + + assert!(crate::character::is_hex_digit(b'0')); + assert!(crate::character::is_hex_digit(b'9')); + assert!(crate::character::is_hex_digit(b'a')); + assert!(crate::character::is_hex_digit(b'f')); + assert!(crate::character::is_hex_digit(b'A')); + assert!(crate::character::is_hex_digit(b'F')); + assert!(!crate::character::is_hex_digit(b'g')); + assert!(!crate::character::is_hex_digit(b'G')); + assert!(!crate::character::is_hex_digit(b'/')); + assert!(!crate::character::is_hex_digit(b':')); + assert!(!crate::character::is_hex_digit(b'@')); + assert!(!crate::character::is_hex_digit(b'\x60')); + } + + #[test] + fn oct_digit_test() { + let i = &b"01234567;"[..]; + assert_parse!(oct_digit1(i), Ok((&b";"[..], &i[..i.len() - 1]))); + + let i = &b"8"[..]; + assert_parse!( + oct_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::OctDigit))) + ); + + assert!(crate::character::is_oct_digit(b'0')); + assert!(crate::character::is_oct_digit(b'7')); + assert!(!crate::character::is_oct_digit(b'8')); + assert!(!crate::character::is_oct_digit(b'9')); + assert!(!crate::character::is_oct_digit(b'a')); + assert!(!crate::character::is_oct_digit(b'A')); + assert!(!crate::character::is_oct_digit(b'/')); + assert!(!crate::character::is_oct_digit(b':')); + assert!(!crate::character::is_oct_digit(b'@')); + assert!(!crate::character::is_oct_digit(b'\x60')); + } + + #[test] + fn full_line_windows() { + named!( + take_full_line<(&[u8], &[u8])>, + tuple!(not_line_ending, line_ending) + ); + let input = b"abc\r\n"; + let output = take_full_line(input); + assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\r\n"[..])))); + } + + #[test] + fn full_line_unix() { + named!( + take_full_line<(&[u8], &[u8])>, + tuple!(not_line_ending, line_ending) + ); + let input = b"abc\n"; + let output = take_full_line(input); + assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\n"[..])))); + } + + #[test] + fn check_windows_lineending() { + let input = b"\r\n"; + let output = line_ending(&input[..]); + assert_parse!(output, Ok((&b""[..], &b"\r\n"[..]))); + } + + #[test] + fn check_unix_lineending() { + let input = b"\n"; + let output = line_ending(&input[..]); + assert_parse!(output, Ok((&b""[..], &b"\n"[..]))); + } + + #[test] + fn cr_lf() { + assert_parse!(crlf(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..]))); + assert_parse!(crlf(&b"\r"[..]), Err(Err::Incomplete(Needed::Size(2)))); + assert_parse!( + crlf(&b"\ra"[..]), + Err(Err::Error(error_position!(&b"\ra"[..], ErrorKind::CrLf))) + ); + + assert_parse!(crlf("\r\na"), Ok(("a", "\r\n"))); + assert_parse!(crlf("\r"), Err(Err::Incomplete(Needed::Size(2)))); + assert_parse!( + crlf("\ra"), + Err(Err::Error(error_position!("\ra", ErrorKind::CrLf))) + ); + } + + #[test] + fn end_of_line() { + assert_parse!(line_ending(&b"\na"[..]), Ok((&b"a"[..], &b"\n"[..]))); + assert_parse!(line_ending(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..]))); + assert_parse!(line_ending(&b"\r"[..]), Err(Err::Incomplete(Needed::Size(2)))); + assert_parse!( + line_ending(&b"\ra"[..]), + Err(Err::Error(error_position!(&b"\ra"[..], ErrorKind::CrLf))) + ); + + assert_parse!(line_ending("\na"), Ok(("a", "\n"))); + assert_parse!(line_ending("\r\na"), Ok(("a", "\r\n"))); + assert_parse!(line_ending("\r"), Err(Err::Incomplete(Needed::Size(2)))); + assert_parse!( + line_ending("\ra"), + Err(Err::Error(error_position!("\ra", ErrorKind::CrLf))) + ); + } +} diff --git a/third_party/rust/nom/src/combinator/macros.rs b/third_party/rust/nom/src/combinator/macros.rs new file mode 100644 index 0000000000..59df06f660 --- /dev/null +++ b/third_party/rust/nom/src/combinator/macros.rs @@ -0,0 +1,1201 @@ +//! Macro combinators +//! +//! Macros are used to make combination easier, +//! since they often do not depend on the type +//! of the data they manipulate or return. +//! +//! There is a trick to make them easier to assemble, +//! combinators are defined like this: +//! +//! ```ignore +//! macro_rules! tag ( +//! ($i:expr, $inp: expr) => ( +//! { +//! ... +//! } +//! ); +//! ); +//! ``` +//! +//! But when used in other combinators, are Used +//! like this: +//! +//! ```ignore +//! named!(my_function, tag!("abcd")); +//! ``` +//! +//! Internally, other combinators will rewrite +//! that call to pass the input as first argument: +//! +//! ```ignore +//! macro_rules! named ( +//! ($name:ident, $submac:ident!( $($args:tt)* )) => ( +//! fn $name<'a>( i: &'a [u8] ) -> IResult<'a,&[u8], &[u8]> { +//! $submac!(i, $($args)*) +//! } +//! ); +//! ); +//! ``` +//! +//! If you want to call a combinator directly, you can +//! do it like this: +//! +//! ```ignore +//! let res = { tag!(input, "abcd"); } +//! ``` +//! +//! Combinators must have a specific variant for +//! non-macro arguments. Example: passing a function +//! to take_while! instead of another combinator. +//! +//! ```ignore +//! macro_rules! take_while( +//! ($input:expr, $submac:ident!( $($args:tt)* )) => ( +//! { +//! ... +//! } +//! ); +//! +//! // wrap the function in a macro to pass it to the main implementation +//! ($input:expr, $f:expr) => ( +//! take_while!($input, call!($f)); +//! ); +//! ); +//! ``` +#[allow(unused_variables)] + +/// Makes a function from a parser combination +/// +/// The type can be set up if the compiler needs +/// more information +/// +/// Function-like declaration: +/// ``` +/// # use nom::{named, tag}; +/// named!(my_function( &[u8] ) -> &[u8], tag!("abcd")); +/// ``` +/// Alternative declaration. First type parameter is input, second is output: +/// ``` +/// # use nom::{named, tag}; +/// named!(my_function<&[u8], &[u8]>, tag!("abcd")); +/// ``` +/// This one will have `&[u8]` as input type, `&[u8]` as output type: +/// ``` +/// # use nom::{named, tag}; +/// named!(my_function, tag!("abcd")); +/// ``` +/// Will use `&[u8]` as output type: +/// ``` +/// # use nom::{named, tag}; +/// named!(my_function<&[u8]>, tag!("abcd")); +/// ``` +/// Prefix them with 'pub' to make the functions public: +/// ``` +/// # use nom::{named, tag}; +/// named!(pub my_function, tag!("abcd")); +/// ``` +/// Prefix them with 'pub(crate)' to make the functions public within the crate: +/// ``` +/// # use nom::{named, tag}; +/// named!(pub(crate) my_function, tag!("abcd")); +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! named ( + (#$($args:tt)*) => ( + named_attr!(#$($args)*); + ); + ($vis:vis $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, ($i, $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($vis:vis $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { + $submac!(i, $($args)*) + } + ); + ($vis:vis $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, ($i, $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($vis:vis $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $vis fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, (&[u8], $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($vis:vis $name:ident, $submac:ident!( $($args:tt)* )) => ( + $vis fn $name( i: &[u8] ) -> $crate::IResult<&[u8], &[u8], (&[u8], $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); +); + +/// Makes a function from a parser combination with arguments. +/// +/// ```ignore +/// //takes [`&[u8]`] as input +/// named_args!(tagged(open_tag: &[u8], close_tag: &[u8])<&str>, +/// delimited!(tag!(open_tag), map_res!(take!(4), str::from_utf8), tag!(close_tag)) +/// ); + +/// //takes `&str` as input +/// named_args!(tagged(open_tag: &str, close_tag: &str)<&str, &str>, +/// delimited!(tag!(open_tag), take!(4), tag!(close_tag)) +/// ); +/// ``` +/// +/// Note: if using arguments that way gets hard to read, it is always +/// possible to write the equivalent parser definition manually, like +/// this: +/// +/// ```ignore +/// fn tagged(input: &[u8], open_tag: &[u8], close_tag: &[u8]) -> IResult<&[u8], &str> { +/// // the first combinator in the tree gets the input as argument. It is then +/// // passed from one combinator to the next through macro rewriting +/// delimited!(input, +/// tag!(open_tag), take!(4), tag!(close_tag) +/// ) +/// ); +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! named_args { + ($vis:vis $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + $vis fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> { + $submac!(input, $($args)*) + } + }; + + ($vis:vis $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + $vis fn $func_name<'this_is_probably_unique_i_hope_please, 'a>( + input: &'this_is_probably_unique_i_hope_please [u8], $( $arg : $typ ),*) -> + $crate::IResult<&'this_is_probably_unique_i_hope_please [u8], $return_type> + { + $submac!(input, $($args)*) + } + }; + + ($vis:vis $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $input_type:ty, $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + $vis fn $func_name(input: $input_type, $( $arg : $typ ),*) -> $crate::IResult<$input_type, $return_type> { + $submac!(input, $($args)*) + } + }; + + ($vis:vis $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $input_type:ty, $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + $vis fn $func_name<'a>( + input: $input_type, $( $arg : $typ ),*) + -> $crate::IResult<$input_type, $return_type> + { + $submac!(input, $($args)*) + } + }; +} + +/// Makes a function from a parser combination, with attributes +/// +/// The usage of this macro is almost identical to `named!`, except that +/// you also pass attributes to be attached to the generated function. +/// This is ideal for adding documentation to your parser. +/// +/// Create my_function as if you wrote it with the doc comment /// My Func: +/// ``` +/// # use nom::{named_attr, tag}; +/// named_attr!(#[doc = "My Func"], my_function( &[u8] ) -> &[u8], tag!("abcd")); +/// ``` +/// Also works for pub functions, and multiple lines: +/// ``` +/// # use nom::{named_attr, tag}; +/// named_attr!(#[doc = "My Func\nRecognise abcd"], pub my_function, tag!("abcd")); +/// ``` +/// Multiple attributes can be passed if required: +/// ``` +/// # use nom::{named_attr, tag}; +/// named_attr!(#[doc = "My Func"] #[inline(always)], pub my_function, tag!("abcd")); +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! named_attr ( + ($(#[$attr:meta])*, $vis:vis $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + $vis fn $name( i: $i ) -> $crate::IResult<$i,$o, ($i, $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $vis:vis $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $vis:vis $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, ($i, $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $vis:vis $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + $vis fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, (&[u8], $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $vis:vis $name:ident, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + $vis fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], (&[u8], $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); +); + +/// Used to wrap common expressions and function as macros +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # fn main() { +/// fn take_wrapper(input: &[u8], i: u8) -> IResult<&[u8], &[u8]> { take!(input, i * 10) } +/// +/// // will make a parser taking 20 bytes +/// named!(parser, call!(take_wrapper, 2)); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! call ( + ($i:expr, $fun:expr) => ( $fun( $i ) ); + ($i:expr, $fun:expr, $($args:expr),* ) => ( $fun( $i, $($args),* ) ); +); + +//FIXME: error rewrite +/// Prevents backtracking if the child parser fails +/// +/// This parser will do an early return instead of sending +/// its result to the parent parser. +/// +/// If another `return_error!` combinator is present in the parent +/// chain, the error will be wrapped and another early +/// return will be made. +/// +/// This makes it easy to build report on which parser failed, +/// where it failed in the input, and the chain of parsers +/// that led it there. +/// +/// Additionally, the error chain contains number identifiers +/// that can be matched to provide useful error messages. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(err_test<&[u8], &[u8]>, alt!( +/// tag!("abcd") | +/// preceded!(tag!("efgh"), return_error!(ErrorKind::Eof, +/// do_parse!( +/// tag!("ijkl") >> +/// res: return_error!(ErrorKind::Tag, tag!("mnop")) >> +/// (res) +/// ) +/// ) +/// ) +/// )); +/// let a = &b"efghblah"[..]; +/// let b = &b"efghijklblah"[..]; +/// let c = &b"efghijklmnop"[..]; +/// +/// let blah = &b"blah"[..]; +/// +/// let res_a = err_test(a); +/// let res_b = err_test(b); +/// let res_c = err_test(c); +/// assert_eq!(res_a, Err(Err::Failure(error_node_position!(blah, ErrorKind::Eof, error_position!(blah, ErrorKind::Tag))))); +/// assert_eq!(res_b, Err(Err::Failure(error_node_position!(&b"ijklblah"[..], ErrorKind::Eof, +/// error_node_position!(blah, ErrorKind::Tag, error_position!(blah, ErrorKind::Tag)))) +/// )); +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! return_error ( + ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let i_ = $i.clone(); + let cl = || { + $submac!(i_, $($args)*) + }; + + match cl() { + Err(Err::Incomplete(x)) => Err(Err::Incomplete(x)), + Ok((i, o)) => Ok((i, o)), + Err(Err::Error(e)) | Err(Err::Failure(e)) => { + return Err(Err::Failure($crate::error::append_error($i, $code, e))) + } + } + } + ); + ($i:expr, $code:expr, $f:expr) => ( + return_error!($i, $code, call!($f)); + ); + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let i_ = $i.clone(); + let cl = || { + $submac!(i_, $($args)*) + }; + + match cl() { + Err(Err::Incomplete(x)) => Err(Err::Incomplete(x)), + Ok((i, o)) => Ok((i, o)), + Err(Err::Error(e)) | Err(Err::Failure(e)) => { + return Err(Err::Failure(e)) + } + } + } + ); + ($i:expr, $f:expr) => ( + return_error!($i, call!($f)); + ); +); + +//FIXME: error rewrite +/// Add an error if the child parser fails +/// +/// While `return_error!` does an early return and avoids backtracking, +/// add_return_error! backtracks normally. It just provides more context +/// for an error +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::collections; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(err_test, add_return_error!(ErrorKind::Tag, tag!("abcd"))); +/// +/// let a = &b"efghblah"[..]; +/// let res_a = err_test(a); +/// assert_eq!(res_a, Err(Err::Error(error_node_position!(a, ErrorKind::Tag, error_position!(a, ErrorKind::Tag))))); +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! add_return_error ( + ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind}; + + match $submac!($i, $($args)*) { + Ok((i, o)) => Ok((i, o)), + Err(Err::Error(e)) => { + Err(Err::Error(error_node_position!($i, $code, e))) + }, + Err(Err::Failure(e)) => { + Err(Err::Failure(error_node_position!($i, $code, e))) + }, + Err(e) => Err(e), + } + } + ); + ($i:expr, $code:expr, $f:expr) => ( + add_return_error!($i, $code, call!($f)); + ); +); + +/// replaces a `Incomplete` returned by the child parser +/// with an `Error` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::collections; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(take_5, complete!(take!(5))); +/// +/// let a = &b"abcd"[..]; +/// let res_a = take_5(a); +/// assert_eq!(res_a, Err(Err::Error(error_position!(a, ErrorKind::Complete)))); +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! complete ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::completec($i, move |i| { $submac!(i, $($args)*) }) + ); + ($i:expr, $f:expr) => ( + complete!($i, call!($f)); + ); +); + +/// A bit like `std::try!`, this macro will return the remaining input and +/// parsed value if the child parser returned `Ok`, and will do an early +/// return for the `Err` side. +/// +/// this can provide more flexibility than `do_parse!` if needed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # use nom::IResult; +/// +/// fn take_add(input:&[u8], size: u8) -> IResult<&[u8], &[u8]> { +/// let (i1, length) = try_parse!(input, map_opt!(nom::number::streaming::be_u8, |sz| size.checked_add(sz))); +/// let (i2, data) = try_parse!(i1, take!(length)); +/// return Ok((i2, data)); +/// } +/// # fn main() { +/// let arr1 = [1, 2, 3, 4, 5]; +/// let r1 = take_add(&arr1[..], 1); +/// assert_eq!(r1, Ok((&[4,5][..], &[2,3][..]))); +/// +/// let arr2 = [0xFE, 2, 3, 4, 5]; +/// // size is overflowing +/// let r1 = take_add(&arr2[..], 42); +/// assert_eq!(r1, Err(Err::Error(error_position!(&[254, 2,3,4,5][..], ErrorKind::MapOpt)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! try_parse ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match $submac!($i, $($args)*) { + Ok((i,o)) => (i,o), + Err(e) => return Err(e), + } + }); + ($i:expr, $f:expr) => ( + try_parse!($i, call!($f)) + ); +); + +/// `map!(I -> IResult<I, O>, O -> P) => I -> IResult<I, P>` +/// +/// maps a function on the result of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// # fn main() { +/// +/// named!(parse<&str, usize>, map!(digit1, |s| s.len())); +/// +/// // the parser will count how many characters were returned by digit1 +/// assert_eq!(parse("123456"), Ok(("", 6))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parse("abc"), Err(Err::Error(error_position!("abc", ErrorKind::Digit)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! map( + // Internal parser, do not use directly + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + $crate::combinator::mapc($i, move |i| {$submac!(i, $($args)*)}, $g) + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + map!(__impl $i, $submac!($($args)*), $g); + ); + ($i:expr, $f:expr, $g:expr) => ( + map!(__impl $i, call!($f), $g); + ); +); + +/// `map_res!(I -> IResult<I, O>, O -> Result<P>) => I -> IResult<I, P>` +/// maps a function returning a Result on the output of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// # fn main() { +/// +/// named!(parse<&str, u8>, map_res!(digit1, |s: &str| s.parse::<u8>())); +/// +/// // the parser will convert the result of digit1 to a number +/// assert_eq!(parse("123"), Ok(("", 123))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parse("abc"), Err(Err::Error(error_position!("abc", ErrorKind::Digit)))); +/// +/// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) +/// assert_eq!(parse("123456"), Err(Err::Error(error_position!("123456", ErrorKind::MapRes)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! map_res ( + // Internal parser, do not use directly + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + $crate::combinator::map_resc($i, move |i| {$submac!(i, $($args)*)}, move |i| {$submac2!(i, $($args2)*)}) + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + map_res!(__impl $i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + map_res!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + map_res!(__impl $i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + map_res!(__impl $i, call!($f), $submac!($($args)*)); + ); +); + +/// `map_opt!(I -> IResult<I, O>, O -> Option<P>) => I -> IResult<I, P>` +/// maps a function returning an Option on the output of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// # fn main() { +/// +/// named!(parser<&str, u8>, map_opt!(digit1, |s: &str| s.parse::<u8>().ok())); +/// +/// // the parser will convert the result of digit1 to a number +/// assert_eq!(parser("123"), Ok(("", 123))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Digit)))); +/// +/// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) +/// assert_eq!(parser("123456"), Err(Err::Error(("123456", ErrorKind::MapOpt)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! map_opt ( + // Internal parser, do not use directly + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + $crate::combinator::map_optc($i, move |i| {$submac!(i, $($args)*)}, move |i| {$submac2!(i, $($args2)*)}) + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + map_opt!(__impl $i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + map_opt!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + map_opt!(__impl $i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + map_opt!(__impl $i, call!($f), $submac!($($args)*)); + ); +); + +/// `parse_to!(O) => I -> IResult<I, O>` +/// uses the `parse` method from `std::str::FromStr` to convert the current +/// input to the specified type +/// +/// this will completely consume the input +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// # fn main() { +/// +/// named!(parser<&str, u8>, parse_to!(u8)); +/// +/// assert_eq!(parser("123"), Ok(("", 123))); +/// +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::ParseTo)))); +/// +/// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) +/// assert_eq!(parser("123456"), Err(Err::Error(("123456", ErrorKind::ParseTo)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! parse_to ( + ($i:expr, $t:ty ) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option; + use $crate::lib::std::option::Option::*; + use $crate::{Err,error::ErrorKind}; + + use $crate::ParseTo; + use $crate::Slice; + use $crate::InputLength; + + let res: Option<$t> = ($i).parse_to(); + match res { + Some(output) => Ok(($i.slice($i.input_len()..), output)), + None => Err(Err::Error($crate::error::make_error($i, ErrorKind::ParseTo))) + } + } + ); +); + +/// `verify!(I -> IResult<I, O>, O -> bool) => I -> IResult<I, O>` +/// returns the result of the child parser if it satisfies a verification function +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(check<u32>, verify!(nom::number::streaming::be_u32, |val: &u32| *val < 3)); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! verify ( + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + $crate::combinator::verifyc($i, |i| $submac!(i, $($args)*), $g) + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + $crate::combinator::verifyc($i, |i| $submac!(i, $($args)*), |&o| $submac2!(o, $($args2)*)) + ); + ($i:expr, $f:expr, $g:expr) => ( + $crate::combinator::verify($f, $g)($i) + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::verify($f, |&o| $submac!(o, $($args)*))($i) + ); +); + +/// `value!(T, R -> IResult<R, S> ) => R -> IResult<R, T>` +/// +/// or `value!(T) => R -> IResult<R, T>` +/// +/// If the child parser was successful, return the value. +/// If no child parser is provided, always return the value +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(x<u8>, value!(42, delimited!(tag!("<!--"), take!(5), tag!("-->")))); +/// named!(y<u8>, delimited!(tag!("<!--"), value!(42), tag!("-->"))); +/// let r = x(&b"<!-- abc --> aaa"[..]); +/// assert_eq!(r, Ok((&b" aaa"[..], 42))); +/// +/// let r2 = y(&b"<!----> aaa"[..]); +/// assert_eq!(r2, Ok((&b" aaa"[..], 42))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! value ( + ($i:expr, $res:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::valuec($i, $res, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $res:expr, $f:expr) => ( + $crate::combinator::valuec($i, $res, $f) + ); + ($i:expr, $res:expr) => ( + Ok(($i, $res)) + ); +); + +/// `opt!(I -> IResult<I,O>) => I -> IResult<I, Option<O>>` +/// make the underlying parser optional +/// +/// returns an Option of the returned type. This parser returns `Some(result)` if the child parser +/// succeeds,`None` if it fails, and `Incomplete` if it did not have enough data to decide +/// +/// *Warning*: if you are using `opt` for some kind of optional ending token (like an end of line), +/// you should combine it with `complete` to make sure it works. +/// +/// As an example, `opt!(tag!("\r\n"))` will return `Incomplete` if it receives an empty input, +/// because `tag` does not have enough input to decide. +/// On the contrary, `opt!(complete!(tag!("\r\n")))` would return `None` as produced value, +/// since `complete!` transforms an `Incomplete` in an `Error`. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!( o<&[u8], Option<&[u8]> >, opt!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdef"; +/// let b = b"bcdefg"; +/// assert_eq!(o(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); +/// assert_eq!(o(&b[..]), Ok((&b"bcdefg"[..], None))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! opt( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + $crate::combinator::optc($i, |i| $submac!(i, $($args)*)) + } + ); + ($i:expr, $f:expr) => ( + $crate::combinator::opt($f)($i) + ); +); + +/// `opt_res!(I -> IResult<I,O>) => I -> IResult<I, Result<nom::Err,O>>` +/// make the underlying parser optional +/// +/// returns a Result, with Err containing the parsing error +/// +/// ```ignore +/// # #[macro_use] extern crate nom; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!( o<&[u8], Result<&[u8], nom::Err<&[u8]> > >, opt_res!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdef"; +/// let b = b"bcdefg"; +/// assert_eq!(o(&a[..]), Ok((&b"ef"[..], Ok(&b"abcd"[..]))); +/// assert_eq!(o(&b[..]), Ok((&b"bcdefg"[..], Err(error_position!(&b[..], ErrorKind::Tag)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! opt_res ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + Ok((i,o)) => Ok((i, Ok(o))), + Err(Err::Error(e)) => Ok(($i, Err(Err::Error(e)))), + // in case of failure, we return a real error + Err(e) => Err(e) + } + } + ); + ($i:expr, $f:expr) => ( + opt_res!($i, call!($f)); + ); +); + +/// `cond!(bool, I -> IResult<I,O>) => I -> IResult<I, Option<O>>` +/// Conditional combinator +/// +/// Wraps another parser and calls it if the +/// condition is met. This combinator returns +/// an Option of the return type of the child +/// parser. +/// +/// This is especially useful if a parser depends +/// on the value returned by a preceding parser in +/// a `do_parse!`. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # fn main() { +/// fn f_true(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> { +/// cond!(i, true, tag!("abcd")) +/// } +/// +/// fn f_false(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> { +/// cond!(i, false, tag!("abcd")) +/// } +/// +/// let a = b"abcdef"; +/// assert_eq!(f_true(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); +/// +/// assert_eq!(f_false(&a[..]), Ok((&b"abcdef"[..], None))); +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! cond( + ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::condc($i, $cond, |i| $submac!(i, $($args)*) ) + ); + ($i:expr, $cond:expr, $f:expr) => ( + $crate::combinator::cond($cond, $f)($i) + ); +); + +/// `peek!(I -> IResult<I,O>) => I -> IResult<I, O>` +/// returns a result without consuming the input +/// +/// the embedded parser may return Err(Err::Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(ptag, peek!( tag!( "abcd" ) ) ); +/// +/// let r = ptag(&b"abcdefgh"[..]); +/// assert_eq!(r, Ok((&b"abcdefgh"[..], &b"abcd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! peek( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::peekc($i, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr) => ( + $crate::combinator::peek($f)($i) + ); +); + +/// `not!(I -> IResult<I,O>) => I -> IResult<I, ()>` +/// returns a result only if the embedded parser returns Error or Err(Err::Incomplete) +/// does not consume the input +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(not_e, do_parse!( +/// res: tag!("abc") >> +/// not!(char!('e')) >> +/// (res) +/// )); +/// +/// let r = not_e(&b"abcd"[..]); +/// assert_eq!(r, Ok((&b"d"[..], &b"abc"[..]))); +/// +/// let r2 = not_e(&b"abce"[..]); +/// assert_eq!(r2, Err(Err::Error(error_position!(&b"e"[..], ErrorKind::Not)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! not( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::notc($i, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr) => ( + $crate::combinator::not($f)($i) + ); +); + +/// `tap!(name: I -> IResult<I,O> => { block }) => I -> IResult<I, O>` +/// allows access to the parser's result without affecting it +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::str; +/// # fn main() { +/// named!(ptag, tap!(res: tag!( "abcd" ) => { println!("recognized {}", str::from_utf8(res).unwrap()) } ) ); +/// +/// let r = ptag(&b"abcdefgh"[..]); +/// assert_eq!(r, Ok((&b"efgh"[..], &b"abcd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! tap ( + ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,Needed,IResult}; + + match $submac!($i, $($args)*) { + Ok((i,o)) => { + let $name = o; + $e; + Ok((i, $name)) + }, + Err(e) => Err(Err::convert(e)), + } + } + ); + ($i:expr, $name: ident: $f:expr => $e:expr) => ( + tap!($i, $name: call!($f) => $e); + ); +); + +/// `eof!()` returns its input if it is at the end of input data +/// +/// When we're at the end of the data, this combinator +/// will succeed +/// +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::str; +/// # use nom::{Err, error::ErrorKind}; +/// # fn main() { +/// named!(parser, eof!()); +/// +/// assert_eq!(parser(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +/// assert_eq!(parser(&b""[..]), Ok((&b""[..], &b""[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! eof ( + ($i:expr,) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind}; + + use $crate::InputLength; + if ($i).input_len() == 0 { + Ok(($i, $i)) + } else { + Err(Err::Error(error_position!($i, ErrorKind::Eof))) + } + } + ); +); + +/// `exact!()` will fail if the child parser does not consume the whole data +/// +/// TODO: example +#[macro_export(local_inner_macros)] +macro_rules! exact ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ({ + terminated!($i, $submac!( $($args)*), eof!()) + }); + ($i:expr, $f:expr) => ( + exact!($i, call!($f)); + ); +); + +/// `recognize!(I -> IResult<I, O> ) => I -> IResult<I, I>` +/// if the child parser was successful, return the consumed input as produced value +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(x, recognize!(delimited!(tag!("<!--"), take!(5), tag!("-->")))); +/// let r = x(&b"<!-- abc --> aaa"[..]); +/// assert_eq!(r, Ok((&b" aaa"[..], &b"<!-- abc -->"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! recognize ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::recognizec($i, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr) => ( + $crate::combinator::recognize($f)($i) + ); +); + +#[cfg(test)] +mod tests { + use crate::internal::{Err, IResult, Needed}; + use crate::error::ParseError; + use crate::error::ErrorKind; + #[cfg(feature = "alloc")] + use crate::lib::std::boxed::Box; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $tag: expr) => ({ + use $crate::lib::std::result::Result::*; + use $crate::{Err,Needed,IResult,error::ErrorKind}; + use $crate::{Compare,CompareResult,InputLength,Slice}; + + let res: IResult<_,_> = match ($i).compare($tag) { + CompareResult::Ok => { + let blen = $tag.input_len(); + Ok(($i.slice(blen..), $i.slice(..blen))) + }, + CompareResult::Incomplete => { + Err(Err::Incomplete(Needed::Size($tag.input_len()))) + }, + CompareResult::Error => { + let e:ErrorKind = ErrorKind::Tag; + Err(Err::Error($crate::error::make_error($i, e))) + } + }; + res + }); + ); + + macro_rules! take( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res:IResult<&[u8],&[u8]> = if $i.len() < cnt { + Err($crate::Err::Incomplete($crate::Needed::Size(cnt))) + } else { + Ok((&$i[cnt..],&$i[0..cnt])) + }; + res + } + ); + ); + + mod pub_named_mod { + named!(pub tst, tag!("abcd")); + } + + #[test] + fn pub_named_test() { + let a = &b"abcd"[..]; + let res = pub_named_mod::tst(a); + assert_eq!(res, Ok((&b""[..], a))); + } + + mod pub_crate_named_mod { + named!(pub(crate) tst, tag!("abcd")); + } + + #[test] + fn pub_crate_named_test() { + let a = &b"abcd"[..]; + let res = pub_crate_named_mod::tst(a); + assert_eq!(res, Ok((&b""[..], a))); + } + + #[test] + fn apply_test() { + fn sum2(a: u8, b: u8) -> u8 { + a + b + } + fn sum3(a: u8, b: u8, c: u8) -> u8 { + a + b + c + } + let a = call!(1, sum2, 2); + let b = call!(1, sum3, 2, 3); + + assert_eq!(a, 3); + assert_eq!(b, 6); + } + + #[test] + fn opt() { + named!(opt_abcd<&[u8],Option<&[u8]> >, opt!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"bcdefg"[..]; + let c = &b"ab"[..]; + assert_eq!(opt_abcd(a), Ok((&b"ef"[..], Some(&b"abcd"[..])))); + assert_eq!(opt_abcd(b), Ok((&b"bcdefg"[..], None))); + assert_eq!(opt_abcd(c), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + fn opt_res() { + named!(opt_res_abcd<&[u8], Result<&[u8], Err<(&[u8], ErrorKind)>> >, opt_res!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"bcdefg"[..]; + let c = &b"ab"[..]; + assert_eq!(opt_res_abcd(a), Ok((&b"ef"[..], Ok(&b"abcd"[..])))); + assert_eq!( + opt_res_abcd(b), + Ok(( + &b"bcdefg"[..], + Err(Err::Error(error_position!(b, ErrorKind::Tag))) + )) + ); + assert_eq!(opt_res_abcd(c), Err(Err::Incomplete(Needed::Size(4)))); + } + + use crate::lib::std::convert::From; + #[derive(Debug, PartialEq)] + pub struct CustomError(&'static str); + impl<I> From<(I, ErrorKind)> for CustomError { + fn from(_: (I, ErrorKind)) -> Self { + CustomError("test") + } + } + + impl<I> ParseError<I> for CustomError { + fn from_error_kind(_: I, _: ErrorKind) -> Self { + CustomError("from_error_kind") + } + + fn append(_: I, _: ErrorKind, _: CustomError) -> Self { + CustomError("append") + } + } + + + #[test] + #[cfg(feature = "alloc")] + fn cond() { + fn f_true(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { + fix_error!(i, CustomError, cond!(true, tag!("abcd"))) + } + + fn f_false(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { + fix_error!(i, CustomError, cond!(false, tag!("abcd"))) + } + + assert_eq!(f_true(&b"abcdef"[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); + assert_eq!(f_true(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(f_true(&b"xxx"[..]), Err(Err::Error(CustomError("test")))); + + assert_eq!(f_false(&b"abcdef"[..]), Ok((&b"abcdef"[..], None))); + assert_eq!(f_false(&b"ab"[..]), Ok((&b"ab"[..], None))); + assert_eq!(f_false(&b"xxx"[..]), Ok((&b"xxx"[..], None))); + } + + #[test] + #[cfg(feature = "alloc")] + fn cond_wrapping() { + // Test that cond!() will wrap a given identifier in the call!() macro. + named!(tag_abcd, tag!("abcd")); + fn f_true(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { + fix_error!(i, CustomError, cond!(true, tag_abcd)) + } + + fn f_false(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { + fix_error!(i, CustomError, cond!(false, tag_abcd)) + } + + assert_eq!(f_true(&b"abcdef"[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); + assert_eq!(f_true(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(f_true(&b"xxx"[..]), Err(Err::Error(CustomError("test")))); + + assert_eq!(f_false(&b"abcdef"[..]), Ok((&b"abcdef"[..], None))); + assert_eq!(f_false(&b"ab"[..]), Ok((&b"ab"[..], None))); + assert_eq!(f_false(&b"xxx"[..]), Ok((&b"xxx"[..], None))); + } + + #[test] + fn peek() { + named!(peek_tag<&[u8],&[u8]>, peek!(tag!("abcd"))); + + assert_eq!(peek_tag(&b"abcdef"[..]), Ok((&b"abcdef"[..], &b"abcd"[..]))); + assert_eq!(peek_tag(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!( + peek_tag(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn not() { + named!(not_aaa<()>, not!(tag!("aaa"))); + assert_eq!( + not_aaa(&b"aaa"[..]), + Err(Err::Error(error_position!(&b"aaa"[..], ErrorKind::Not))) + ); + assert_eq!(not_aaa(&b"aa"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(not_aaa(&b"abcd"[..]), Ok((&b"abcd"[..], ()))); + } + + #[test] + fn verify() { + named!(test, verify!(take!(5), |slice: &[u8]| slice[0] == b'a')); + assert_eq!(test(&b"bcd"[..]), Err(Err::Incomplete(Needed::Size(5)))); + assert_eq!( + test(&b"bcdefg"[..]), + Err(Err::Error(error_position!( + &b"bcdefg"[..], + ErrorKind::Verify + ))) + ); + assert_eq!(test(&b"abcdefg"[..]), Ok((&b"fg"[..], &b"abcde"[..]))); + } + + #[test] + fn parse_to() { + let res: IResult<_, _, (&str, ErrorKind)> = parse_to!("ab", usize); + + assert_eq!( + res, + Err(Err::Error(error_position!( + "ab", + ErrorKind::ParseTo + ))) + ); + + let res: IResult<_, _, (&str, ErrorKind)> = parse_to!("42", usize); + + assert_eq!(res, Ok(("", 42))); + //assert_eq!(ErrorKind::convert(ErrorKind::ParseTo), ErrorKind::ParseTo::<u64>); + } + +} diff --git a/third_party/rust/nom/src/combinator/mod.rs b/third_party/rust/nom/src/combinator/mod.rs new file mode 100644 index 0000000000..933bed834b --- /dev/null +++ b/third_party/rust/nom/src/combinator/mod.rs @@ -0,0 +1,859 @@ +//! general purpose combinators + +#![allow(unused_imports)] + +#[cfg(feature = "alloc")] +use crate::lib::std::boxed::Box; + +#[cfg(feature = "std")] +use crate::lib::std::fmt::Debug; +use crate::internal::*; +use crate::error::ParseError; +use crate::traits::{AsChar, InputIter, InputLength, InputTakeAtPosition, ParseTo}; +use crate::lib::std::ops::{Range, RangeFrom, RangeTo}; +use crate::lib::std::borrow::Borrow; +use crate::traits::{Compare, CompareResult, Offset, Slice}; +use crate::error::ErrorKind; +use crate::lib::std::mem::transmute; + +#[macro_use] +mod macros; + +/// Return the remaining input +/// +/// ```rust +/// # use nom::error::ErrorKind; +/// use nom::combinator::rest; +/// assert_eq!(rest::<_,(_, ErrorKind)>("abc"), Ok(("", "abc"))); +/// assert_eq!(rest::<_,(_, ErrorKind)>(""), Ok(("", ""))); +/// ``` +#[inline] +pub fn rest<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: Slice<Range<usize>> + Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: InputLength, +{ + Ok((input.slice(input.input_len()..), input)) +} + +/// Return the length of the remaining input +/// +/// ```rust +/// # use nom::error::ErrorKind; +/// use nom::combinator::rest_len; +/// assert_eq!(rest_len::<_,(_, ErrorKind)>("abc"), Ok(("abc", 3))); +/// assert_eq!(rest_len::<_,(_, ErrorKind)>(""), Ok(("", 0))); +/// ``` +#[inline] +pub fn rest_len<T, E: ParseError<T>>(input: T) -> IResult<T, usize, E> +where + T: Slice<Range<usize>> + Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: InputLength, +{ + let len = input.input_len(); + Ok((input, len)) +} + +/// maps a function on the result of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// use nom::combinator::map; +/// # fn main() { +/// +/// let parse = map(digit1, |s: &str| s.len()); +/// +/// // the parser will count how many characters were returned by digit1 +/// assert_eq!(parse("123456"), Ok(("", 6))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit)))); +/// # } +/// ``` +pub fn map<I, O1, O2, E: ParseError<I>, F, G>(first: F, second: G) -> impl Fn(I) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(O1) -> O2, +{ + move |input: I| { + let (input, o1) = first(input)?; + Ok((input, second(o1))) + } +} + +#[doc(hidden)] +pub fn mapc<I, O1, O2, E: ParseError<I>, F, G>(input: I, first: F, second: G) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(O1) -> O2, +{ + map(first, second)(input) +} + +/// applies a function returning a Result over the result of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// use nom::combinator::map_res; +/// # fn main() { +/// +/// let parse = map_res(digit1, |s: &str| s.parse::<u8>()); +/// +/// // the parser will convert the result of digit1 to a number +/// assert_eq!(parse("123"), Ok(("", 123))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit)))); +/// +/// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) +/// assert_eq!(parse("123456"), Err(Err::Error(("123456", ErrorKind::MapRes)))); +/// # } +/// ``` +pub fn map_res<I: Clone, O1, O2, E: ParseError<I>, E2, F, G>(first: F, second: G) -> impl Fn(I) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(O1) -> Result<O2, E2>, +{ + move |input: I| { + let i = input.clone(); + let (input, o1) = first(input)?; + match second(o1) { + Ok(o2) => Ok((input, o2)), + Err(_) => Err(Err::Error(E::from_error_kind(i, ErrorKind::MapRes))), + } + } +} + +#[doc(hidden)] +pub fn map_resc<I: Clone, O1, O2, E: ParseError<I>, E2, F, G>(input: I, first: F, second: G) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(O1) -> Result<O2, E2>, +{ + map_res(first, second)(input) +} + +/// applies a function returning an Option over the result of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// use nom::combinator::map_opt; +/// # fn main() { +/// +/// let parse = map_opt(digit1, |s: &str| s.parse::<u8>().ok()); +/// +/// // the parser will convert the result of digit1 to a number +/// assert_eq!(parse("123"), Ok(("", 123))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit)))); +/// +/// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) +/// assert_eq!(parse("123456"), Err(Err::Error(("123456", ErrorKind::MapOpt)))); +/// # } +/// ``` +pub fn map_opt<I: Clone, O1, O2, E: ParseError<I>, F, G>(first: F, second: G) -> impl Fn(I) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(O1) -> Option<O2>, +{ + move |input: I| { + let i = input.clone(); + let (input, o1) = first(input)?; + match second(o1) { + Some(o2) => Ok((input, o2)), + None => Err(Err::Error(E::from_error_kind(i, ErrorKind::MapOpt))), + } + } +} + +#[doc(hidden)] +pub fn map_optc<I: Clone, O1, O2, E: ParseError<I>, F, G>(input: I, first: F, second: G) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(O1) -> Option<O2>, +{ + map_opt(first, second)(input) +} + +/// applies a parser over the result of another one +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// use nom::bytes::complete::take; +/// use nom::combinator::map_parser; +/// # fn main() { +/// +/// let parse = map_parser(take(5u8), digit1); +/// +/// assert_eq!(parse("12345"), Ok(("", "12345"))); +/// assert_eq!(parse("123ab"), Ok(("", "123"))); +/// assert_eq!(parse("123"), Err(Err::Error(("123", ErrorKind::Eof)))); +/// # } +/// ``` +pub fn map_parser<I: Clone, O1, O2, E: ParseError<I>, F, G>(first: F, second: G) -> impl Fn(I) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(O1) -> IResult<O1, O2, E>, + O1: InputLength, +{ + move |input: I| { + let (input, o1) = first(input)?; + let (_, o2) = second(o1)?; + Ok((input, o2)) + } +} + +#[doc(hidden)] +pub fn map_parserc<I: Clone, O1, O2, E: ParseError<I>, F, G>(input: I, first: F, second: G) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(O1) -> IResult<O1, O2, E>, + O1: InputLength, +{ + map_parser(first, second)(input) +} + +/// creates a new parser from the output of the first parser, then apply that parser over the rest of the input +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::bytes::complete::take; +/// use nom::number::complete::be_u8; +/// use nom::combinator::flat_map; +/// # fn main() { +/// +/// let parse = flat_map(be_u8, take); +/// +/// assert_eq!(parse(&[2, 0, 1, 2][..]), Ok((&[2][..], &[0, 1][..]))); +/// assert_eq!(parse(&[4, 0, 1, 2][..]), Err(Err::Error((&[0, 1, 2][..], ErrorKind::Eof)))); +/// # } +/// ``` +pub fn flat_map<I, O1, O2, E: ParseError<I>, F, G, H>(first: F, second: G) -> impl Fn(I) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(O1) -> H, + H: Fn(I) -> IResult<I, O2, E> +{ + move |input: I| { + let (input, o1) = first(input)?; + second(o1)(input) + } +} + +/// optional parser: will return None if not successful +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::opt; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// fn parser(i: &str) -> IResult<&str, Option<&str>> { +/// opt(alpha1)(i) +/// } +/// +/// assert_eq!(parser("abcd;"), Ok((";", Some("abcd")))); +/// assert_eq!(parser("123;"), Ok(("123;", None))); +/// # } +/// ``` +pub fn opt<I:Clone, O, E: ParseError<I>, F>(f: F) -> impl Fn(I) -> IResult<I, Option<O>, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + move |input: I| { + let i = input.clone(); + match f(input) { + Ok((i, o)) => Ok((i, Some(o))), + Err(Err::Error(_)) => Ok((i, None)), + Err(e) => Err(e), + } + } +} + +#[doc(hidden)] +pub fn optc<I:Clone, O, E: ParseError<I>, F>(input: I, f: F) -> IResult<I, Option<O>, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + opt(f)(input) +} + +/// calls the parser if the condition is met +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::cond; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// fn parser(b: bool, i: &str) -> IResult<&str, Option<&str>> { +/// cond(b, alpha1)(i) +/// } +/// +/// assert_eq!(parser(true, "abcd;"), Ok((";", Some("abcd")))); +/// assert_eq!(parser(false, "abcd;"), Ok(("abcd;", None))); +/// assert_eq!(parser(true, "123;"), Err(Err::Error(("123;", ErrorKind::Alpha)))); +/// assert_eq!(parser(false, "123;"), Ok(("123;", None))); +/// # } +/// ``` +pub fn cond<I:Clone, O, E: ParseError<I>, F>(b: bool, f: F) -> impl Fn(I) -> IResult<I, Option<O>, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + move |input: I| { + if b { + match f(input) { + Ok((i, o)) => Ok((i, Some(o))), + Err(e) => Err(e), + } + } else { + Ok((input, None)) + } + } +} + +#[doc(hidden)] +pub fn condc<I:Clone, O, E: ParseError<I>, F>(input: I, b: bool, f: F) -> IResult<I, Option<O>, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + cond(b, f)(input) +} + +/// tries to apply its parser without consuming the input +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::peek; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = peek(alpha1); +/// +/// assert_eq!(parser("abcd;"), Ok(("abcd;", "abcd"))); +/// assert_eq!(parser("123;"), Err(Err::Error(("123;", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn peek<I:Clone, O, E: ParseError<I>, F>(f: F) -> impl Fn(I) -> IResult<I, O, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + move |input: I| { + let i = input.clone(); + match f(input) { + Ok((_, o)) => Ok((i, o)), + Err(e) => Err(e), + } + } +} + +#[doc(hidden)] +pub fn peekc<I:Clone, O, E: ParseError<I>, F>(input: I, f: F) -> IResult<I, O, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + peek(f)(input) +} + +/// transforms Incomplete into Error +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::bytes::streaming::take; +/// use nom::combinator::complete; +/// # fn main() { +/// +/// let parser = complete(take(5u8)); +/// +/// assert_eq!(parser("abcdefg"), Ok(("fg", "abcde"))); +/// assert_eq!(parser("abcd"), Err(Err::Error(("abcd", ErrorKind::Complete)))); +/// # } +/// ``` +pub fn complete<I: Clone, O, E: ParseError<I>, F>(f: F) -> impl Fn(I) -> IResult<I, O, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + move |input: I| { + let i = input.clone(); + match f(input) { + Err(Err::Incomplete(_)) => { + Err(Err::Error(E::from_error_kind(i, ErrorKind::Complete))) + }, + rest => rest + } + } +} + +#[doc(hidden)] +pub fn completec<I: Clone, O, E: ParseError<I>, F>(input: I, f: F) -> IResult<I, O, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + complete(f)(input) +} + +/// succeeds if all the input has been consumed by its child parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::all_consuming; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = all_consuming(alpha1); +/// +/// assert_eq!(parser("abcd"), Ok(("", "abcd"))); +/// assert_eq!(parser("abcd;"),Err(Err::Error((";", ErrorKind::Eof)))); +/// assert_eq!(parser("123abcd;"),Err(Err::Error(("123abcd;", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn all_consuming<I, O, E: ParseError<I>, F>(f: F) -> impl Fn(I) -> IResult<I, O, E> +where + I: InputLength, + F: Fn(I) -> IResult<I, O, E>, +{ + move |input: I| { + let (input, res) = f(input)?; + if input.input_len() == 0 { + Ok((input, res)) + } else { + Err(Err::Error(E::from_error_kind(input, ErrorKind::Eof))) + } + } +} + +/// returns the result of the child parser if it satisfies a verification function +/// +/// the verification function takes as argument a reference to the output of the +/// parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::verify; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = verify(alpha1, |s: &str| s.len() == 4); +/// +/// assert_eq!(parser("abcd"), Ok(("", "abcd"))); +/// assert_eq!(parser("abcde"), Err(Err::Error(("abcde", ErrorKind::Verify)))); +/// assert_eq!(parser("123abcd;"),Err(Err::Error(("123abcd;", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn verify<I: Clone, O1, O2, E: ParseError<I>, F, G>(first: F, second: G) -> impl Fn(I) -> IResult<I, O1, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(&O2) -> bool, + O1: Borrow<O2>, + O2: ?Sized, +{ + move |input: I| { + let i = input.clone(); + let (input, o) = first(input)?; + + if second(o.borrow()) { + Ok((input, o)) + } else { + Err(Err::Error(E::from_error_kind(i, ErrorKind::Verify))) + } + } +} + +#[doc(hidden)] +pub fn verifyc<I: Clone, O1, O2, E: ParseError<I>, F, G>(input: I, first: F, second: G) -> IResult<I, O1, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(&O2) -> bool, + O1: Borrow<O2>, + O2: ?Sized, +{ + verify(first, second)(input) +} + +/// returns the provided value if the child parser succeeds +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::value; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = value(1234, alpha1); +/// +/// assert_eq!(parser("abcd"), Ok(("", 1234))); +/// assert_eq!(parser("123abcd;"), Err(Err::Error(("123abcd;", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn value<I, O1: Clone, O2, E: ParseError<I>, F>(val: O1, parser: F) -> impl Fn(I) -> IResult<I, O1, E> +where + F: Fn(I) -> IResult<I, O2, E>, +{ + move |input: I| { + parser(input).map(|(i, _)| (i, val.clone())) + } +} + +#[doc(hidden)] +pub fn valuec<I, O1: Clone, O2, E: ParseError<I>, F>(input: I, val: O1, parser: F) -> IResult<I, O1, E> +where + F: Fn(I) -> IResult<I, O2, E>, +{ + value(val, parser)(input) +} + +/// succeeds if the child parser returns an error +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::not; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = not(alpha1); +/// +/// assert_eq!(parser("123"), Ok(("123", ()))); +/// assert_eq!(parser("abcd"), Err(Err::Error(("abcd", ErrorKind::Not)))); +/// # } +/// ``` +pub fn not<I: Clone, O, E: ParseError<I>, F>(parser: F) -> impl Fn(I) -> IResult<I, (), E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + move |input: I| { + let i = input.clone(); + match parser(input) { + Ok(_) => Err(Err::Error(E::from_error_kind(i, ErrorKind::Not))), + Err(Err::Error(_)) => Ok((i, ())), + Err(e) => Err(e), + } + } +} + +#[doc(hidden)] +pub fn notc<I: Clone, O, E: ParseError<I>, F>(input: I, parser: F) -> IResult<I, (), E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + not(parser)(input) +} + +/// if the child parser was successful, return the consumed input as produced value +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::recognize; +/// use nom::character::complete::{char, alpha1}; +/// use nom::sequence::separated_pair; +/// # fn main() { +/// +/// let parser = recognize(separated_pair(alpha1, char(','), alpha1)); +/// +/// assert_eq!(parser("abcd,efgh"), Ok(("", "abcd,efgh"))); +/// assert_eq!(parser("abcd;"),Err(Err::Error((";", ErrorKind::Char)))); +/// # } +/// ``` +pub fn recognize<I: Clone + Offset + Slice<RangeTo<usize>>, O, E: ParseError<I>, F>(parser: F) -> impl Fn(I) -> IResult<I, I, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + move |input: I| { + let i = input.clone(); + match parser(i) { + Ok((i, _)) => { + let index = input.offset(&i); + Ok((i, input.slice(..index))) + }, + Err(e) => Err(e), + } + } +} + +#[doc(hidden)] +pub fn recognizec<I: Clone + Offset + Slice<RangeTo<usize>>, O, E: ParseError<I>, F>(input: I, parser: F) -> IResult<I, I, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + recognize(parser)(input) +} + +/// transforms an error to failure +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::cut; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = cut(alpha1); +/// +/// assert_eq!(parser("abcd;"), Ok((";", "abcd"))); +/// assert_eq!(parser("123;"), Err(Err::Failure(("123;", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn cut<I: Clone + Slice<RangeTo<usize>>, O, E: ParseError<I>, F>(parser: F) -> impl Fn(I) -> IResult<I, O, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + move |input: I| { + let i = input.clone(); + match parser(i) { + Err(Err::Error(e)) => Err(Err::Failure(e)), + rest => rest, + } + } +} + +#[doc(hidden)] +pub fn cutc<I: Clone + Slice<RangeTo<usize>>, O, E: ParseError<I>, F>(input: I, parser: F) -> IResult<I, O, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + cut(parser)(input) +} + +/// creates an iterator from input data and a parser +/// +/// call the iterator's [finish] method to get the remaining input if successful, +/// or the error value if we encountered an error +/// +/// ```rust +/// use nom::{combinator::iterator, IResult, bytes::complete::tag, character::complete::alpha1, sequence::terminated}; +/// use std::collections::HashMap; +/// +/// let data = "abc|defg|hijkl|mnopqr|123"; +/// let mut it = iterator(data, terminated(alpha1, tag("|"))); +/// +/// let parsed = it.map(|v| (v, v.len())).collect::<HashMap<_,_>>(); +/// let res: IResult<_,_> = it.finish(); +/// +/// assert_eq!(parsed, [("abc", 3usize), ("defg", 4), ("hijkl", 5), ("mnopqr", 6)].iter().cloned().collect()); +/// assert_eq!(res, Ok(("123", ()))); +/// ``` +pub fn iterator<Input, Output, Error, F>(input: Input, f: F) -> ParserIterator<Input, Error, F> +where + F: Fn(Input) -> IResult<Input, Output, Error>, + Error: ParseError<Input> { + + ParserIterator { + iterator: f, + input, + state: State::Running, + } +} + +/// main structure associated to the [iterator] function +pub struct ParserIterator<I, E, F> { + iterator: F, + input: I, + state: State<E>, +} + +impl<I: Clone, E: Clone, F> ParserIterator<I, E, F> { + /// returns the remaining input if parsing was successful, or the error if we encountered an error + pub fn finish(self) -> IResult<I, (), E> { + match &self.state { + State::Running | State::Done => Ok((self.input.clone(), ())), + State::Failure(e) => Err(Err::Failure(e.clone())), + State::Incomplete(i) => Err(Err::Incomplete(i.clone())), + } + } +} + +impl<'a, Input ,Output ,Error, F> core::iter::Iterator for &'a mut ParserIterator<Input, Error, F> + where + F: Fn(Input) -> IResult<Input, Output, Error>, + Input: Clone +{ + type Item = Output; + + fn next(&mut self) -> Option<Self::Item> { + if let State::Running = self.state { + let input = self.input.clone(); + + match (self.iterator)(input) { + Ok((i, o)) => { + self.input = i; + Some(o) + }, + Err(Err::Error(_)) => { + self.state = State::Done; + None + }, + Err(Err::Failure(e)) => { + self.state = State::Failure(e); + None + }, + Err(Err::Incomplete(i)) => { + self.state = State::Incomplete(i); + None + }, + } + } else { + None + } + } +} + +enum State<E> { + Running, + Done, + Failure(E), + Incomplete(Needed), +} + + +#[cfg(test)] +mod tests { + use super::*; + use crate::internal::{Err, IResult, Needed}; + use crate::error::ParseError; + use crate::bytes::complete::take; + use crate::number::complete::be_u8; + + macro_rules! assert_parse( + ($left: expr, $right: expr) => { + let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; + assert_eq!(res, $right); + }; + ); + + /*#[test] + fn t1() { + let v1:Vec<u8> = vec![1,2,3]; + let v2:Vec<u8> = vec![4,5,6]; + let d = Ok((&v1[..], &v2[..])); + let res = d.flat_map(print); + assert_eq!(res, Ok((&v2[..], ()))); + }*/ + + + /* + #[test] + fn end_of_input() { + let not_over = &b"Hello, world!"[..]; + let is_over = &b""[..]; + named!(eof_test, eof!()); + + let res_not_over = eof_test(not_over); + assert_eq!(res_not_over, Err(Err::Error(error_position!(not_over, ErrorKind::Eof)))); + + let res_over = eof_test(is_over); + assert_eq!(res_over, Ok((is_over, is_over))); + } + */ + + #[test] + fn rest_on_slices() { + let input: &[u8] = &b"Hello, world!"[..]; + let empty: &[u8] = &b""[..]; + assert_parse!(rest(input), Ok((empty, input))); + } + + #[test] + fn rest_on_strs() { + let input: &str = "Hello, world!"; + let empty: &str = ""; + assert_parse!(rest(input), Ok((empty, input))); + } + + #[test] + fn rest_len_on_slices() { + let input: &[u8] = &b"Hello, world!"[..]; + assert_parse!(rest_len(input), Ok((input, input.len()))); + } + + use crate::lib::std::convert::From; + impl From<u32> for CustomError { + fn from(_: u32) -> Self { + CustomError + } + } + + impl<I> ParseError<I> for CustomError { + fn from_error_kind(_: I, _: ErrorKind) -> Self { + CustomError + } + + fn append(_: I, _: ErrorKind, _: CustomError) -> Self { + CustomError + } + } + + struct CustomError; + #[allow(dead_code)] + fn custom_error(input: &[u8]) -> IResult<&[u8], &[u8], CustomError> { + //fix_error!(input, CustomError, alphanumeric) + crate::character::streaming::alphanumeric1(input) + } + + #[test] + fn test_flat_map() { + let input: &[u8] = &[3, 100, 101, 102, 103, 104][..]; + assert_parse!(flat_map(be_u8, take)(input), Ok((&[103, 104][..], &[100, 101, 102][..]))); + } + + #[test] + fn test_map_opt() { + let input: &[u8] = &[50][..]; + assert_parse!(map_opt(be_u8, |u| if u < 20 {Some(u)} else {None})(input), Err(Err::Error((&[50][..], ErrorKind::MapOpt)))); + assert_parse!(map_opt(be_u8, |u| if u > 20 {Some(u)} else {None})(input), Ok((&[][..], 50))); + } + + #[test] + fn test_map_parser() { + let input: &[u8] = &[100, 101, 102, 103, 104][..]; + assert_parse!(map_parser(take(4usize), take(2usize))(input), Ok((&[104][..], &[100, 101][..]))); + } + + #[test] + fn test_all_consuming() { + let input: &[u8] = &[100, 101, 102][..]; + assert_parse!(all_consuming(take(2usize))(input), Err(Err::Error((&[102][..], ErrorKind::Eof)))); + assert_parse!(all_consuming(take(3usize))(input), Ok((&[][..], &[100, 101, 102][..]))); + } + + #[test] + #[allow(unused)] + fn test_verify_ref() { + use crate::bytes::complete::take; + + let parser1 = verify(take(3u8), |s: &[u8]| s == &b"abc"[..]); + + assert_eq!(parser1(&b"abcd"[..]), Ok((&b"d"[..], &b"abc"[..]))); + assert_eq!(parser1(&b"defg"[..]), Err(Err::Error((&b"defg"[..], ErrorKind::Verify)))); + + fn parser2(i: &[u8]) -> IResult<&[u8], u32> { + verify(crate::number::streaming::be_u32, |val: &u32| *val < 3)(i) + } + } + + #[test] + #[cfg(feature = "alloc")] + fn test_verify_alloc() { + use crate::bytes::complete::take; + let parser1 = verify(map(take(3u8), |s: &[u8]| s.to_vec()), |s: &[u8]| s == &b"abc"[..]); + + assert_eq!(parser1(&b"abcd"[..]), Ok((&b"d"[..], (&b"abc").to_vec()))); + assert_eq!(parser1(&b"defg"[..]), Err(Err::Error((&b"defg"[..], ErrorKind::Verify)))); + } +} diff --git a/third_party/rust/nom/src/error.rs b/third_party/rust/nom/src/error.rs new file mode 100644 index 0000000000..150516121f --- /dev/null +++ b/third_party/rust/nom/src/error.rs @@ -0,0 +1,776 @@ +//! Error management +//! +//! Parsers are generic over their error type, requiring that it implements +//! the `error::ParseError<Input>` trait. + +/// this trait must be implemented by the error type of a nom parser +/// +/// There are already implementations of it for `(Input, ErrorKind)` +/// and `VerboseError<Input>`. +/// +/// It provides methods to create an error from some combinators, +/// and combine existing errors in combinators like `alt` +pub trait ParseError<I>: Sized { + /// creates an error from the input position and an [ErrorKind] + fn from_error_kind(input: I, kind: ErrorKind) -> Self; + + /// combines an existing error with a new one created from the input + /// position and an [ErrorKind]. This is useful when backtracking + /// through a parse tree, accumulating error context on the way + fn append(input: I, kind: ErrorKind, other: Self) -> Self; + + /// creates an error from an input position and an expected character + fn from_char(input: I, _: char) -> Self { + Self::from_error_kind(input, ErrorKind::Char) + } + + /// combines two existing error. This function is used to compare errors + /// generated in various branches of [alt] + fn or(self, other: Self) -> Self { + other + } + + /// create a new error from an input position, a static string and an existing error. + /// This is used mainly in the [context] combinator, to add user friendly information + /// to errors when backtracking through a parse tree + fn add_context(_input: I, _ctx: &'static str, other: Self) -> Self { + other + } +} + +impl<I> ParseError<I> for (I, ErrorKind) { + fn from_error_kind(input: I, kind: ErrorKind) -> Self { + (input, kind) + } + + fn append(_: I, _: ErrorKind, other: Self) -> Self { + other + } +} + +impl<I> ParseError<I> for () { + fn from_error_kind(_: I, _: ErrorKind) -> Self {} + + fn append(_: I, _: ErrorKind, _: Self) -> Self {} +} + +/// creates an error from the input position and an [ErrorKind] +pub fn make_error<I, E: ParseError<I>>(input: I, kind: ErrorKind) -> E { + E::from_error_kind(input, kind) +} + +/// combines an existing error with a new one created from the input +/// position and an [ErrorKind]. This is useful when backtracking +/// through a parse tree, accumulating error context on the way +pub fn append_error<I, E: ParseError<I>>(input: I, kind: ErrorKind, other: E) -> E { + E::append(input, kind, other) +} + +/// this error type accumulates errors and their position when backtracking +/// through a parse tree. With some post processing (cf `examples/json.rs`), +/// it can be used to display user friendly error messages +#[cfg(feature = "alloc")] +#[derive(Clone, Debug, PartialEq)] +pub struct VerboseError<I> { + /// list of errors accumulated by `VerboseError`, containing the affected + /// part of input data, and some context + pub errors: crate::lib::std::vec::Vec<(I, VerboseErrorKind)>, +} + +#[cfg(feature = "alloc")] +#[derive(Clone, Debug, PartialEq)] +/// error context for `VerboseError` +pub enum VerboseErrorKind { + /// static string added by the `context` function + Context(&'static str), + /// indicates which character was expected by the `char` function + Char(char), + /// error kind given by various nom parsers + Nom(ErrorKind), +} + +#[cfg(feature = "alloc")] +impl<I> ParseError<I> for VerboseError<I> { + fn from_error_kind(input: I, kind: ErrorKind) -> Self { + VerboseError { + errors: vec![(input, VerboseErrorKind::Nom(kind))], + } + } + + fn append(input: I, kind: ErrorKind, mut other: Self) -> Self { + other.errors.push((input, VerboseErrorKind::Nom(kind))); + other + } + + fn from_char(input: I, c: char) -> Self { + VerboseError { + errors: vec![(input, VerboseErrorKind::Char(c))], + } + } + + fn add_context(input: I, ctx: &'static str, mut other: Self) -> Self { + other.errors.push((input, VerboseErrorKind::Context(ctx))); + other + } +} + +use crate::internal::{Err, IResult}; + +/// create a new error from an input position, a static string and an existing error. +/// This is used mainly in the [context] combinator, to add user friendly information +/// to errors when backtracking through a parse tree +pub fn context<I: Clone, E: ParseError<I>, F, O>(context: &'static str, f: F) -> impl Fn(I) -> IResult<I, O, E> +where + F: Fn(I) -> IResult<I, O, E>, +{ + move |i: I| match f(i.clone()) { + Ok(o) => Ok(o), + Err(Err::Incomplete(i)) => Err(Err::Incomplete(i)), + Err(Err::Error(e)) => Err(Err::Error(E::add_context(i, context, e))), + Err(Err::Failure(e)) => Err(Err::Failure(E::add_context(i, context, e))), + } +} + +/// transforms a `VerboseError` into a trace with input position information +#[cfg(feature = "alloc")] +pub fn convert_error(input: &str, e: VerboseError<&str>) -> crate::lib::std::string::String { + use crate::lib::std::fmt::Write; + use crate::traits::Offset; + + let mut result = crate::lib::std::string::String::new(); + + for (i, (substring, kind)) in e.errors.iter().enumerate() { + let offset = input.offset(substring); + + if input.is_empty() { + match kind { + VerboseErrorKind::Char(c) => write!(&mut result, "{}: expected '{}', got empty input\n\n", i, c), + VerboseErrorKind::Context(s) => write!(&mut result, "{}: in {}, got empty input\n\n", i, s), + VerboseErrorKind::Nom(e) => write!(&mut result, "{}: in {:?}, got empty input\n\n", i, e), + } + } else { + let prefix = &input.as_bytes()[..offset]; + + // Count the number of newlines in the first `offset` bytes of input + let line_number = prefix.iter().filter(|&&b| b == b'\n').count() + 1; + + // Find the line that includes the subslice: + // Find the *last* newline before the substring starts + let line_begin = prefix.iter().rev().position(|&b| b == b'\n').map(|pos| offset - pos).unwrap_or(0); + + // Find the full line after that newline + let line = input[line_begin..].lines().next().unwrap_or(&input[line_begin..]).trim_end(); + + // The (1-indexed) column number is the offset of our substring into that line + let column_number = line.offset(substring) + 1; + + match kind { + VerboseErrorKind::Char(c) => if let Some(actual) = substring.chars().next() { + write!( + &mut result, + "{i}: at line {line_number}:\n\ + {line}\n\ + {caret:>column$}\n\ + expected '{expected}', found {actual}\n\n", + i = i, + line_number = line_number, + line = line, + caret = '^', + column = column_number, + expected = c, + actual = actual, + ) + } else { + write!( + &mut result, + "{i}: at line {line_number}:\n\ + {line}\n\ + {caret:>column$}\n\ + expected '{expected}', got end of input\n\n", + i = i, + line_number = line_number, + line = line, + caret = '^', + column = column_number, + expected = c, + ) + }, + VerboseErrorKind::Context(s) => write!( + &mut result, + "{i}: at line {line_number}, in {context}:\n\ + {line}\n\ + {caret:>column$}\n\n", + i = i, + line_number = line_number, + context = s, + line = line, + caret = '^', + column = column_number, + ), + VerboseErrorKind::Nom(e) => write!( + &mut result, + "{i}: at line {line_number}, in {nom_err:?}:\n\ + {line}\n\ + {caret:>column$}\n\n", + i = i, + line_number = line_number, + nom_err = e, + line = line, + caret = '^', + column = column_number, + ), + } + } + // Because `write!` to a `String` is infallible, this `unwrap` is fine. + .unwrap(); + } + + result +} + +/// indicates which parser returned an error +#[cfg_attr(rustfmt, rustfmt_skip)] +#[derive(Debug,PartialEq,Eq,Hash,Clone,Copy)] +#[allow(deprecated,missing_docs)] +pub enum ErrorKind { + Tag, + MapRes, + MapOpt, + Alt, + IsNot, + IsA, + SeparatedList, + SeparatedNonEmptyList, + Many0, + Many1, + ManyTill, + Count, + TakeUntil, + LengthValue, + TagClosure, + Alpha, + Digit, + HexDigit, + OctDigit, + AlphaNumeric, + Space, + MultiSpace, + LengthValueFn, + Eof, + Switch, + TagBits, + OneOf, + NoneOf, + Char, + CrLf, + RegexpMatch, + RegexpMatches, + RegexpFind, + RegexpCapture, + RegexpCaptures, + TakeWhile1, + Complete, + Fix, + Escaped, + EscapedTransform, + NonEmpty, + ManyMN, + Not, + Permutation, + Verify, + TakeTill1, + TakeWhileMN, + ParseTo, + TooLarge, + Many0Count, + Many1Count, + Float, +} + +#[cfg_attr(rustfmt, rustfmt_skip)] +#[allow(deprecated)] +/// converts an ErrorKind to a number +pub fn error_to_u32(e: &ErrorKind) -> u32 { + match *e { + ErrorKind::Tag => 1, + ErrorKind::MapRes => 2, + ErrorKind::MapOpt => 3, + ErrorKind::Alt => 4, + ErrorKind::IsNot => 5, + ErrorKind::IsA => 6, + ErrorKind::SeparatedList => 7, + ErrorKind::SeparatedNonEmptyList => 8, + ErrorKind::Many1 => 9, + ErrorKind::Count => 10, + ErrorKind::TakeUntil => 12, + ErrorKind::LengthValue => 15, + ErrorKind::TagClosure => 16, + ErrorKind::Alpha => 17, + ErrorKind::Digit => 18, + ErrorKind::AlphaNumeric => 19, + ErrorKind::Space => 20, + ErrorKind::MultiSpace => 21, + ErrorKind::LengthValueFn => 22, + ErrorKind::Eof => 23, + ErrorKind::Switch => 27, + ErrorKind::TagBits => 28, + ErrorKind::OneOf => 29, + ErrorKind::NoneOf => 30, + ErrorKind::Char => 40, + ErrorKind::CrLf => 41, + ErrorKind::RegexpMatch => 42, + ErrorKind::RegexpMatches => 43, + ErrorKind::RegexpFind => 44, + ErrorKind::RegexpCapture => 45, + ErrorKind::RegexpCaptures => 46, + ErrorKind::TakeWhile1 => 47, + ErrorKind::Complete => 48, + ErrorKind::Fix => 49, + ErrorKind::Escaped => 50, + ErrorKind::EscapedTransform => 51, + ErrorKind::NonEmpty => 56, + ErrorKind::ManyMN => 57, + ErrorKind::HexDigit => 59, + ErrorKind::OctDigit => 61, + ErrorKind::Many0 => 62, + ErrorKind::Not => 63, + ErrorKind::Permutation => 64, + ErrorKind::ManyTill => 65, + ErrorKind::Verify => 66, + ErrorKind::TakeTill1 => 67, + ErrorKind::TakeWhileMN => 69, + ErrorKind::ParseTo => 70, + ErrorKind::TooLarge => 71, + ErrorKind::Many0Count => 72, + ErrorKind::Many1Count => 73, + ErrorKind::Float => 74, + } +} + +impl ErrorKind { + #[cfg_attr(rustfmt, rustfmt_skip)] + #[allow(deprecated)] + /// converts an ErrorKind to a text description + pub fn description(&self) -> &str { + match *self { + ErrorKind::Tag => "Tag", + ErrorKind::MapRes => "Map on Result", + ErrorKind::MapOpt => "Map on Option", + ErrorKind::Alt => "Alternative", + ErrorKind::IsNot => "IsNot", + ErrorKind::IsA => "IsA", + ErrorKind::SeparatedList => "Separated list", + ErrorKind::SeparatedNonEmptyList => "Separated non empty list", + ErrorKind::Many0 => "Many0", + ErrorKind::Many1 => "Many1", + ErrorKind::Count => "Count", + ErrorKind::TakeUntil => "Take until", + ErrorKind::LengthValue => "Length followed by value", + ErrorKind::TagClosure => "Tag closure", + ErrorKind::Alpha => "Alphabetic", + ErrorKind::Digit => "Digit", + ErrorKind::AlphaNumeric => "AlphaNumeric", + ErrorKind::Space => "Space", + ErrorKind::MultiSpace => "Multiple spaces", + ErrorKind::LengthValueFn => "LengthValueFn", + ErrorKind::Eof => "End of file", + ErrorKind::Switch => "Switch", + ErrorKind::TagBits => "Tag on bitstream", + ErrorKind::OneOf => "OneOf", + ErrorKind::NoneOf => "NoneOf", + ErrorKind::Char => "Char", + ErrorKind::CrLf => "CrLf", + ErrorKind::RegexpMatch => "RegexpMatch", + ErrorKind::RegexpMatches => "RegexpMatches", + ErrorKind::RegexpFind => "RegexpFind", + ErrorKind::RegexpCapture => "RegexpCapture", + ErrorKind::RegexpCaptures => "RegexpCaptures", + ErrorKind::TakeWhile1 => "TakeWhile1", + ErrorKind::Complete => "Complete", + ErrorKind::Fix => "Fix", + ErrorKind::Escaped => "Escaped", + ErrorKind::EscapedTransform => "EscapedTransform", + ErrorKind::NonEmpty => "NonEmpty", + ErrorKind::ManyMN => "Many(m, n)", + ErrorKind::HexDigit => "Hexadecimal Digit", + ErrorKind::OctDigit => "Octal digit", + ErrorKind::Not => "Negation", + ErrorKind::Permutation => "Permutation", + ErrorKind::ManyTill => "ManyTill", + ErrorKind::Verify => "predicate verification", + ErrorKind::TakeTill1 => "TakeTill1", + ErrorKind::TakeWhileMN => "TakeWhileMN", + ErrorKind::ParseTo => "Parse string to the specified type", + ErrorKind::TooLarge => "Needed data size is too large", + ErrorKind::Many0Count => "Count occurrence of >=0 patterns", + ErrorKind::Many1Count => "Count occurrence of >=1 patterns", + ErrorKind::Float => "Float", + } + } +} + +/// creates a parse error from a `nom::ErrorKind` +/// and the position in the input +#[allow(unused_variables)] +#[macro_export(local_inner_macros)] +macro_rules! error_position( + ($input:expr, $code:expr) => ({ + $crate::error::make_error($input, $code) + }); +); + +/// creates a parse error from a `nom::ErrorKind`, +/// the position in the input and the next error in +/// the parsing tree. +#[allow(unused_variables)] +#[macro_export(local_inner_macros)] +macro_rules! error_node_position( + ($input:expr, $code:expr, $next:expr) => ({ + $crate::error::append_error($input, $code, $next) + }); +); + +/* + +#[cfg(feature = "std")] +use $crate::lib::std::any::Any; +#[cfg(feature = "std")] +use $crate::lib::std::{error,fmt}; +#[cfg(feature = "std")] +impl<E: fmt::Debug+Any> error::Error for Err<E> { + fn description(&self) -> &str { + self.description() + } +} + +#[cfg(feature = "std")] +impl<E: fmt::Debug> fmt::Display for Err<E> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.description()) + } +} +*/ + +//FIXME: error rewrite +/// translate parser result from IResult<I,O,u32> to IResult<I,O,E> with a custom type +/// +/// ``` +/// # //FIXME +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # use std::convert::From; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// # /* +/// # // will add a Custom(42) error to the error chain +/// # named!(err_test, add_return_error!(ErrorKind::Custom(42u32), tag!("abcd"))); +/// # +/// # #[derive(Debug,Clone,PartialEq)] +/// # pub struct ErrorStr(String); +/// # +/// # // Convert to IResult<&[u8], &[u8], ErrorStr> +/// # impl From<u32> for ErrorStr { +/// # fn from(i: u32) -> Self { +/// # ErrorStr(format!("custom error code: {}", i)) +/// # } +/// # } +/// # +/// # named!(parser<&[u8], &[u8], ErrorStr>, +/// # fix_error!(ErrorStr, err_test) +/// # ); +/// # +/// # let a = &b"efghblah"[..]; +/// # assert_eq!(parser(a), Err(Err::Error(Context::Code(a, ErrorKind::Custom(ErrorStr("custom error code: 42".to_string())))))); +/// # */ +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! fix_error ( + ($i:expr, $t:ty, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match $submac!($i, $($args)*) { + Ok((i,o)) => Ok((i,o)), + Err(e) => { + let e2 = match e { + Err::Error(err) => { + Err::Error(err.into()) + }, + Err::Failure(err) => { + Err::Failure(err.into()) + }, + Err::Incomplete(e) => Err::Incomplete(e), + }; + Err(e2) + } + } + } + ); + ($i:expr, $t:ty, $f:expr) => ( + fix_error!($i, $t, call!($f)); + ); +); + +/// `flat_map!(R -> IResult<R,S>, S -> IResult<S,T>) => R -> IResult<R, T>` +/// +/// combines a parser R -> IResult<R,S> and +/// a parser S -> IResult<S,T> to return another +/// parser R -> IResult<R,T> +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind}; +/// use nom::number::complete::recognize_float; +/// +/// named!(parser<&str, f64>, flat_map!(recognize_float, parse_to!(f64))); +/// +/// assert_eq!(parser("123.45;"), Ok((";", 123.45))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! flat_map( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + flat_map!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + flat_map!(__impl $i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + flat_map!(__impl $i, call!($f), $submac!($($args)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + flat_map!(__impl $i, call!($f), call!($g)); + ); + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + $crate::combinator::map_parserc($i, move |i| {$submac!(i, $($args)*)}, move |i| {$submac2!(i, $($args2)*)}) + ); +); + +#[cfg(test)] +#[cfg(feature = "alloc")] +mod tests { + use super::*; + use crate::character::complete::char; + + #[test] + fn convert_error_panic() { + let input = ""; + + let result: IResult<_, _, VerboseError<&str>> = char('x')(input); + } +} + +/* +#[cfg(feature = "alloc")] +use lib::std::{vec::Vec, collections::HashMap}; + +#[cfg(feature = "std")] +use lib::std::hash::Hash; + +#[cfg(feature = "std")] +pub fn add_error_pattern<'a, I: Clone + Hash + Eq, O, E: Clone + Hash + Eq>( + h: &mut HashMap<VerboseError<I>, &'a str>, + e: VerboseError<I>, + message: &'a str, +) -> bool { + h.insert(e, message); + true +} + +pub fn slice_to_offsets(input: &[u8], s: &[u8]) -> (usize, usize) { + let start = input.as_ptr(); + let off1 = s.as_ptr() as usize - start as usize; + let off2 = off1 + s.len(); + (off1, off2) +} + +#[cfg(feature = "std")] +pub fn prepare_errors<O, E: Clone>(input: &[u8], e: VerboseError<&[u8]>) -> Option<Vec<(ErrorKind, usize, usize)>> { + let mut v: Vec<(ErrorKind, usize, usize)> = Vec::new(); + + for (p, kind) in e.errors.drain(..) { + let (o1, o2) = slice_to_offsets(input, p); + v.push((kind, o1, o2)); + } + + v.reverse(); + Some(v) +} + +#[cfg(feature = "std")] +pub fn print_error<O, E: Clone>(input: &[u8], res: VerboseError<&[u8]>) { + if let Some(v) = prepare_errors(input, res) { + let colors = generate_colors(&v); + println!("parser codes: {}", print_codes(&colors, &HashMap::new())); + println!("{}", print_offsets(input, 0, &v)); + } else { + println!("not an error"); + } +} + +#[cfg(feature = "std")] +pub fn generate_colors<E>(v: &[(ErrorKind, usize, usize)]) -> HashMap<u32, u8> { + let mut h: HashMap<u32, u8> = HashMap::new(); + let mut color = 0; + + for &(ref c, _, _) in v.iter() { + h.insert(error_to_u32(c), color + 31); + color = color + 1 % 7; + } + + h +} + +pub fn code_from_offset(v: &[(ErrorKind, usize, usize)], offset: usize) -> Option<u32> { + let mut acc: Option<(u32, usize, usize)> = None; + for &(ref ek, s, e) in v.iter() { + let c = error_to_u32(ek); + if s <= offset && offset <= e { + if let Some((_, start, end)) = acc { + if start <= s && e <= end { + acc = Some((c, s, e)); + } + } else { + acc = Some((c, s, e)); + } + } + } + if let Some((code, _, _)) = acc { + return Some(code); + } else { + return None; + } +} + +#[cfg(feature = "alloc")] +pub fn reset_color(v: &mut Vec<u8>) { + v.push(0x1B); + v.push(b'['); + v.push(0); + v.push(b'm'); +} + +#[cfg(feature = "alloc")] +pub fn write_color(v: &mut Vec<u8>, color: u8) { + v.push(0x1B); + v.push(b'['); + v.push(1); + v.push(b';'); + let s = color.to_string(); + let bytes = s.as_bytes(); + v.extend(bytes.iter().cloned()); + v.push(b'm'); +} + +#[cfg(feature = "std")] +#[cfg_attr(feature = "cargo-clippy", allow(implicit_hasher))] +pub fn print_codes(colors: &HashMap<u32, u8>, names: &HashMap<u32, &str>) -> String { + let mut v = Vec::new(); + for (code, &color) in colors { + if let Some(&s) = names.get(code) { + let bytes = s.as_bytes(); + write_color(&mut v, color); + v.extend(bytes.iter().cloned()); + } else { + let s = code.to_string(); + let bytes = s.as_bytes(); + write_color(&mut v, color); + v.extend(bytes.iter().cloned()); + } + reset_color(&mut v); + v.push(b' '); + } + reset_color(&mut v); + + String::from_utf8_lossy(&v[..]).into_owned() +} + +#[cfg(feature = "std")] +pub fn print_offsets(input: &[u8], from: usize, offsets: &[(ErrorKind, usize, usize)]) -> String { + let mut v = Vec::with_capacity(input.len() * 3); + let mut i = from; + let chunk_size = 8; + let mut current_code: Option<u32> = None; + let mut current_code2: Option<u32> = None; + + let colors = generate_colors(&offsets); + + for chunk in input.chunks(chunk_size) { + let s = format!("{:08x}", i); + for &ch in s.as_bytes().iter() { + v.push(ch); + } + v.push(b'\t'); + + let mut k = i; + let mut l = i; + for &byte in chunk { + if let Some(code) = code_from_offset(&offsets, k) { + if let Some(current) = current_code { + if current != code { + reset_color(&mut v); + current_code = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } else { + current_code = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } + v.push(CHARS[(byte >> 4) as usize]); + v.push(CHARS[(byte & 0xf) as usize]); + v.push(b' '); + k = k + 1; + } + + reset_color(&mut v); + + if chunk_size > chunk.len() { + for _ in 0..(chunk_size - chunk.len()) { + v.push(b' '); + v.push(b' '); + v.push(b' '); + } + } + v.push(b'\t'); + + for &byte in chunk { + if let Some(code) = code_from_offset(&offsets, l) { + if let Some(current) = current_code2 { + if current != code { + reset_color(&mut v); + current_code2 = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } else { + current_code2 = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } + if (byte >= 32 && byte <= 126) || byte >= 128 { + v.push(byte); + } else { + v.push(b'.'); + } + l = l + 1; + } + reset_color(&mut v); + + v.push(b'\n'); + i = i + chunk_size; + } + + String::from_utf8_lossy(&v[..]).into_owned() +} +*/ diff --git a/third_party/rust/nom/src/internal.rs b/third_party/rust/nom/src/internal.rs new file mode 100644 index 0000000000..401f98a906 --- /dev/null +++ b/third_party/rust/nom/src/internal.rs @@ -0,0 +1,185 @@ +//! Basic types to build the parsers + +use self::Needed::*; +use crate::error::ErrorKind; + +/// Holds the result of parsing functions +/// +/// It depends on I, the input type, O, the output type, and E, the error type (by default u32) +/// +/// The `Ok` side is a pair containing the remainder of the input (the part of the data that +/// was not parsed) and the produced value. The `Err` side contains an instance of `nom::Err`. +/// +pub type IResult<I, O, E=(I,ErrorKind)> = Result<(I, O), Err<E>>; + +/// Contains information on needed data if a parser returned `Incomplete` +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Needed { + /// needs more data, but we do not know how much + Unknown, + /// contains the required data size + Size(usize), +} + +impl Needed { + /// indicates if we know how many bytes we need + pub fn is_known(&self) -> bool { + *self != Unknown + } + + /// Maps a `Needed` to `Needed` by applying a function to a contained `Size` value. + #[inline] + pub fn map<F: Fn(usize) -> usize>(self, f: F) -> Needed { + match self { + Unknown => Unknown, + Size(n) => Size(f(n)), + } + } +} + +/// The `Err` enum indicates the parser was not successful +/// +/// It has three cases: +/// +/// * `Incomplete` indicates that more data is needed to decide. The `Needed` enum +/// can contain how many additional bytes are necessary. If you are sure your parser +/// is working on full data, you can wrap your parser with the `complete` combinator +/// to transform that case in `Error` +/// * `Error` means some parser did not succeed, but another one might (as an example, +/// when testing different branches of an `alt` combinator) +/// * `Failure` indicates an unrecoverable error. As an example, if you recognize a prefix +/// to decide on the next parser to apply, and that parser fails, you know there's no need +/// to try other parsers, you were already in the right branch, so the data is invalid +/// +#[derive(Debug, Clone, PartialEq)] +pub enum Err<E> { + /// There was not enough data + Incomplete(Needed), + /// The parser had an error (recoverable) + Error(E), + /// The parser had an unrecoverable error: we got to the right + /// branch and we know other branches won't work, so backtrack + /// as fast as possible + Failure(E), +} + +impl<E> Err<E> { + /// tests if the result is Incomplete + pub fn is_incomplete(&self) -> bool { + if let Err::Incomplete(_) = self { + true + } else { + false + } + } + + /// Applies the given function to the inner error + pub fn map<E2, F>(self, f: F) -> Err<E2> + where F: FnOnce(E) -> E2 + { + match self { + Err::Incomplete(n) => Err::Incomplete(n), + Err::Failure(t) => Err::Failure(f(t)), + Err::Error(t) => Err::Error(f(t)), + } + } + + /// automatically converts between errors if the underlying type supports it + pub fn convert<F>(e: Err<F>) -> Self + where E: From<F> + { + e.map(Into::into) + } +} + +impl<T> Err<(T, ErrorKind)> { + /// maps `Err<(T, ErrorKind)>` to `Err<(U, ErrorKind)>` with the given F: T -> U + pub fn map_input<U, F>(self, f: F) -> Err<(U, ErrorKind)> + where F: FnOnce(T) -> U { + match self { + Err::Incomplete(n) => Err::Incomplete(n), + Err::Failure((input, k)) => Err::Failure((f(input), k)), + Err::Error((input, k)) => Err::Error((f(input), k)), + } + } +} + +#[cfg(feature = "std")] +impl Err<(&[u8], ErrorKind)> { + /// Obtaining ownership + pub fn to_owned(self) -> Err<(Vec<u8>, ErrorKind)> { + self.map_input(ToOwned::to_owned) + } +} + +#[cfg(feature = "std")] +impl Err<(&str, ErrorKind)> { + /// automatically converts between errors if the underlying type supports it + pub fn to_owned(self) -> Err<(String, ErrorKind)> { + self.map_input(ToOwned::to_owned) + } +} + +impl<E: Eq> Eq for Err<E> {} + +#[cfg(feature = "std")] +use std::fmt; + +#[cfg(feature = "std")] +impl<E> fmt::Display for Err<E> +where + E: fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Err::Incomplete(Needed::Size(u)) => write!(f, "Parsing requires {} bytes/chars", u), + Err::Incomplete(Needed::Unknown) => write!(f, "Parsing requires more data"), + Err::Failure(c) => write!(f, "Parsing Failure: {:?}", c), + Err::Error(c) => write!(f, "Parsing Error: {:?}", c), + } + } +} + +#[cfg(feature = "std")] +use std::error::Error; + +#[cfg(feature = "std")] +impl<E> Error for Err<E> +where + E: fmt::Debug, +{ + fn source(&self) -> Option<&(dyn Error + 'static)> { + None // no underlying error + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::error::ErrorKind; + + #[doc(hidden)] + #[macro_export] + macro_rules! assert_size ( + ($t:ty, $sz:expr) => ( + assert_eq!(crate::lib::std::mem::size_of::<$t>(), $sz); + ); + ); + + #[test] + #[cfg(target_pointer_width = "64")] + fn size_test() { + assert_size!(IResult<&[u8], &[u8], (&[u8], u32)>, 40); + assert_size!(IResult<&str, &str, u32>, 40); + assert_size!(Needed, 16); + assert_size!(Err<u32>, 24); + assert_size!(ErrorKind, 1); + } + + #[test] + fn err_map_test() { + let e = Err::Error(1); + assert_eq!(e.map(|v| v + 1), Err::Error(2)); + } + +} diff --git a/third_party/rust/nom/src/lib.rs b/third_party/rust/nom/src/lib.rs new file mode 100644 index 0000000000..c37410d7d1 --- /dev/null +++ b/third_party/rust/nom/src/lib.rs @@ -0,0 +1,513 @@ +//! # nom, eating data byte by byte +//! +//! nom is a parser combinator library with a focus on safe parsing, +//! streaming patterns, and as much as possible zero copy. +//! +//! ## Example +//! +//! ```rust +//! extern crate nom; +//! +//! use nom::{ +//! IResult, +//! bytes::complete::{tag, take_while_m_n}, +//! combinator::map_res, +//! sequence::tuple}; +//! +//! #[derive(Debug,PartialEq)] +//! pub struct Color { +//! pub red: u8, +//! pub green: u8, +//! pub blue: u8, +//! } +//! +//! fn from_hex(input: &str) -> Result<u8, std::num::ParseIntError> { +//! u8::from_str_radix(input, 16) +//! } +//! +//! fn is_hex_digit(c: char) -> bool { +//! c.is_digit(16) +//! } +//! +//! fn hex_primary(input: &str) -> IResult<&str, u8> { +//! map_res( +//! take_while_m_n(2, 2, is_hex_digit), +//! from_hex +//! )(input) +//! } +//! +//! fn hex_color(input: &str) -> IResult<&str, Color> { +//! let (input, _) = tag("#")(input)?; +//! let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?; +//! +//! Ok((input, Color { red, green, blue })) +//! } +//! +//! fn main() { +//! assert_eq!(hex_color("#2F14DF"), Ok(("", Color { +//! red: 47, +//! green: 20, +//! blue: 223, +//! }))); +//! } +//! ``` +//! +//! The code is available on [Github](https://github.com/Geal/nom) +//! +//! There are a few [guides](https://github.com/Geal/nom/tree/master/doc) with more details +//! about [the design of nom macros](https://github.com/Geal/nom/blob/master/doc/how_nom_macros_work.md), +//! [how to write parsers](https://github.com/Geal/nom/blob/master/doc/making_a_new_parser_from_scratch.md), +//! or the [error management system](https://github.com/Geal/nom/blob/master/doc/error_management.md). +//! +//! **Looking for a specific combinator? Read the +//! ["choose a combinator" guide](https://github.com/Geal/nom/blob/master/doc/choosing_a_combinator.md)** +//! +//! If you are upgrading to nom 5.0, please read the +//! [migration document](https://github.com/Geal/nom/blob/master/doc/upgrading_to_nom_5.md). +//! +//! See also the [FAQ](https://github.com/Geal/nom/blob/master/doc/FAQ.md). +//! +//! ## Parser combinators +//! +//! Parser combinators are an approach to parsers that is very different from +//! software like [lex](https://en.wikipedia.org/wiki/Lex_(software)) and +//! [yacc](https://en.wikipedia.org/wiki/Yacc). Instead of writing the grammar +//! in a separate syntax and generating the corresponding code, you use very small +//! functions with very specific purposes, like "take 5 bytes", or "recognize the +//! word 'HTTP'", and assemble them in meaningful patterns like "recognize +//! 'HTTP', then a space, then a version". +//! The resulting code is small, and looks like the grammar you would have +//! written with other parser approaches. +//! +//! This gives us a few advantages: +//! +//! - the parsers are small and easy to write +//! - the parsers components are easy to reuse (if they're general enough, please add them to nom!) +//! - the parsers components are easy to test separately (unit tests and property-based tests) +//! - the parser combination code looks close to the grammar you would have written +//! - you can build partial parsers, specific to the data you need at the moment, and ignore the rest +//! +//! Here is an example of one such parser, to recognize text between parentheses: +//! +//! ```rust +//! use nom::{ +//! IResult, +//! sequence::delimited, +//! // see the "streaming/complete" paragraph lower for an explanation of these submodules +//! character::complete::char, +//! bytes::complete::is_not +//! }; +//! +//! fn parens(input: &str) -> IResult<&str, &str> { +//! delimited(char('('), is_not(")"), char(')'))(input) +//! } +//! ``` +//! +//! It defines a function named `parens` which will recognize a sequence of the +//! character `(`, the longest byte array not containing `)`, then the character +//! `)`, and will return the byte array in the middle. +//! +//! Here is another parser, written without using nom's combinators this time: +//! +//! ```rust +//! #[macro_use] +//! extern crate nom; +//! +//! use nom::{IResult, Err, Needed}; +//! +//! # fn main() { +//! fn take4(i: &[u8]) -> IResult<&[u8], &[u8]>{ +//! if i.len() < 4 { +//! Err(Err::Incomplete(Needed::Size(4))) +//! } else { +//! Ok((&i[4..], &i[0..4])) +//! } +//! } +//! # } +//! ``` +//! +//! This function takes a byte array as input, and tries to consume 4 bytes. +//! Writing all the parsers manually, like this, is dangerous, despite Rust's +//! safety features. There are still a lot of mistakes one can make. That's why +//! nom provides a list of function and macros to help in developing parsers. +//! +//! With functions, you would write it like this: +//! +//! ```rust +//! use nom::{IResult, bytes::streaming::take}; +//! fn take4(input: &str) -> IResult<&str, &str> { +//! take(4u8)(input) +//! } +//! ``` +//! +//! With macros, you would write it like this: +//! +//! ```rust +//! #[macro_use] +//! extern crate nom; +//! +//! # fn main() { +//! named!(take4, take!(4)); +//! # } +//! ``` +//! +//! nom has used macros for combinators from versions 1 to 4, and from version +//! 5, it proposes new combinators as functions, but still allows the macros style +//! (macros have been rewritten to use the functions under the hood). +//! For new parsers, we recommend using the functions instead of macros, since +//! rustc messages will be much easier to understand. +//! +//! +//! A parser in nom is a function which, for an input type `I`, an output type `O` +//! and an optional error type `E`, will have the following signature: +//! +//! ```rust,ignore +//! fn parser(input: I) -> IResult<I, O, E>; +//! ``` +//! +//! Or like this, if you don't want to specify a custom error type (it will be `u32` by default): +//! +//! ```rust,ignore +//! fn parser(input: I) -> IResult<I, O>; +//! ``` +//! +//! `IResult` is an alias for the `Result` type: +//! +//! ```rust +//! use nom::{Needed, error::ErrorKind}; +//! +//! type IResult<I, O, E = (I,ErrorKind)> = Result<(I, O), Err<E>>; +//! +//! enum Err<E> { +//! Incomplete(Needed), +//! Error(E), +//! Failure(E), +//! } +//! ``` +//! +//! It can have the following values: +//! +//! - a correct result `Ok((I,O))` with the first element being the remaining of the input (not parsed yet), and the second the output value; +//! - an error `Err(Err::Error(c))` with `c` an error that can be built from the input position and a parser specific error +//! - an error `Err(Err::Incomplete(Needed))` indicating that more input is necessary. `Needed` can indicate how much data is needed +//! - an error `Err(Err::Failure(c))`. It works like the `Error` case, except it indicates an unrecoverable error: we cannot backtrack and test another parser +//! +//! Please refer to the ["choose a combinator" guide](https://github.com/Geal/nom/blob/master/doc/choosing_a_combinator.md) for an exhaustive list of parsers. +//! See also the rest of the documentation [here](https://github.com/Geal/nom/blob/master/doc). +//! . +//! +//! ## Making new parsers with function combinators +//! +//! nom is based on functions that generate parsers, with a signature like +//! this: `(arguments) -> impl Fn(Input) -> IResult<Input, Output, Error>`. +//! The arguments of a combinator can be direct values (like `take` which uses +//! a number of bytes or character as argument) or even other parsers (like +//! `delimited` which takes as argument 3 parsers, and returns the result of +//! the second one if all are successful). +//! +//! Here are some examples: +//! +//! ```rust +//! use nom::IResult; +//! use nom::bytes::complete::{tag, take}; +//! fn abcd_parser(i: &str) -> IResult<&str, &str> { +//! tag("abcd")(i) // will consume bytes if the input begins with "abcd" +//! } +//! +//! fn take_10(i: &[u8]) -> IResult<&[u8], &[u8]> { +//! take(10u8)(i) // will consume and return 10 bytes of input +//! } +//! ``` +//! +//! ## Combining parsers +//! +//! There are higher level patterns, like the **`alt`** combinator, which +//! provides a choice between multiple parsers. If one branch fails, it tries +//! the next, and returns the result of the first parser that succeeds: +//! +//! ```rust +//! use nom::IResult; +//! use nom::branch::alt; +//! use nom::bytes::complete::tag; +//! +//! let alt_tags = alt((tag("abcd"), tag("efgh"))); +//! +//! assert_eq!(alt_tags(&b"abcdxxx"[..]), Ok((&b"xxx"[..], &b"abcd"[..]))); +//! assert_eq!(alt_tags(&b"efghxxx"[..]), Ok((&b"xxx"[..], &b"efgh"[..]))); +//! assert_eq!(alt_tags(&b"ijklxxx"[..]), Err(nom::Err::Error((&b"ijklxxx"[..], nom::error::ErrorKind::Tag)))); +//! ``` +//! +//! The **`opt`** combinator makes a parser optional. If the child parser returns +//! an error, **`opt`** will still succeed and return None: +//! +//! ```rust +//! use nom::{IResult, combinator::opt, bytes::complete::tag}; +//! fn abcd_opt(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> { +//! opt(tag("abcd"))(i) +//! } +//! +//! assert_eq!(abcd_opt(&b"abcdxxx"[..]), Ok((&b"xxx"[..], Some(&b"abcd"[..])))); +//! assert_eq!(abcd_opt(&b"efghxxx"[..]), Ok((&b"efghxxx"[..], None))); +//! ``` +//! +//! **`many0`** applies a parser 0 or more times, and returns a vector of the aggregated results: +//! +//! ```rust +//! # #[macro_use] extern crate nom; +//! # #[cfg(feature = "alloc")] +//! # fn main() { +//! use nom::{IResult, multi::many0, bytes::complete::tag}; +//! use std::str; +//! +//! fn multi(i: &str) -> IResult<&str, Vec<&str>> { +//! many0(tag("abcd"))(i) +//! } +//! +//! let a = "abcdef"; +//! let b = "abcdabcdef"; +//! let c = "azerty"; +//! assert_eq!(multi(a), Ok(("ef", vec!["abcd"]))); +//! assert_eq!(multi(b), Ok(("ef", vec!["abcd", "abcd"]))); +//! assert_eq!(multi(c), Ok(("azerty", Vec::new()))); +//! # } +//! # #[cfg(not(feature = "alloc"))] +//! # fn main() {} +//! ``` +//! +//! Here are some basic combining macros available: +//! +//! - **`opt`**: will make the parser optional (if it returns the `O` type, the new parser returns `Option<O>`) +//! - **`many0`**: will apply the parser 0 or more times (if it returns the `O` type, the new parser returns `Vec<O>`) +//! - **`many1`**: will apply the parser 1 or more times +//! +//! There are more complex (and more useful) parsers like `tuple!`, which is +//! used to apply a series of parsers then assemble their results. +//! +//! Example with `tuple`: +//! +//! ```rust +//! # #[macro_use] extern crate nom; +//! # fn main() { +//! use nom::{error::ErrorKind, Needed, +//! number::streaming::be_u16, +//! bytes::streaming::{tag, take}, +//! sequence::tuple}; +//! +//! let tpl = tuple((be_u16, take(3u8), tag("fg"))); +//! +//! assert_eq!( +//! tpl(&b"abcdefgh"[..]), +//! Ok(( +//! &b"h"[..], +//! (0x6162u16, &b"cde"[..], &b"fg"[..]) +//! )) +//! ); +//! assert_eq!(tpl(&b"abcde"[..]), Err(nom::Err::Incomplete(Needed::Size(2)))); +//! let input = &b"abcdejk"[..]; +//! assert_eq!(tpl(input), Err(nom::Err::Error((&input[5..], ErrorKind::Tag)))); +//! # } +//! ``` +//! +//! But you can also use a sequence of combinators written in imperative style, +//! thanks to the `?` operator: +//! +//! ```rust +//! # #[macro_use] extern crate nom; +//! # fn main() { +//! use nom::{IResult, bytes::complete::tag}; +//! +//! #[derive(Debug, PartialEq)] +//! struct A { +//! a: u8, +//! b: u8 +//! } +//! +//! fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Ok((i,1)) } +//! fn ret_int2(i:&[u8]) -> IResult<&[u8], u8> { Ok((i,2)) } +//! +//! fn f(i: &[u8]) -> IResult<&[u8], A> { +//! // if successful, the parser returns `Ok((remaining_input, output_value))` that we can destructure +//! let (i, _) = tag("abcd")(i)?; +//! let (i, a) = ret_int1(i)?; +//! let (i, _) = tag("efgh")(i)?; +//! let (i, b) = ret_int2(i)?; +//! +//! Ok((i, A { a, b })) +//! } +//! +//! let r = f(b"abcdefghX"); +//! assert_eq!(r, Ok((&b"X"[..], A{a: 1, b: 2}))); +//! # } +//! ``` +//! +//! ## Streaming / Complete +//! +//! Some of nom's modules have `streaming` or `complete` submodules. They hold +//! different variants of the same combinators. +//! +//! A streaming parser assumes that we might not have all of the input data. +//! This can happen with some network protocol or large file parsers, where the +//! input buffer can be full and need to be resized or refilled. +//! +//! A complete parser assumes that we already have all of the input data. +//! This will be the common case with small files that can be read entirely to +//! memory. +//! +//! Here is how it works in practice: +//! +//! ```rust +//! use nom::{IResult, Err, Needed, error::ErrorKind, bytes, character}; +//! +//! fn take_streaming(i: &[u8]) -> IResult<&[u8], &[u8]> { +//! bytes::streaming::take(4u8)(i) +//! } +//! +//! fn take_complete(i: &[u8]) -> IResult<&[u8], &[u8]> { +//! bytes::complete::take(4u8)(i) +//! } +//! +//! // both parsers will take 4 bytes as expected +//! assert_eq!(take_streaming(&b"abcde"[..]), Ok((&b"e"[..], &b"abcd"[..]))); +//! assert_eq!(take_complete(&b"abcde"[..]), Ok((&b"e"[..], &b"abcd"[..]))); +//! +//! // if the input is smaller than 4 bytes, the streaming parser +//! // will return `Incomplete` to indicate that we need more data +//! assert_eq!(take_streaming(&b"abc"[..]), Err(Err::Incomplete(Needed::Size(4)))); +//! +//! // but the complete parser will return an error +//! assert_eq!(take_complete(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +//! +//! // the alpha0 function recognizes 0 or more alphabetic characters +//! fn alpha0_streaming(i: &str) -> IResult<&str, &str> { +//! character::streaming::alpha0(i) +//! } +//! +//! fn alpha0_complete(i: &str) -> IResult<&str, &str> { +//! character::complete::alpha0(i) +//! } +//! +//! // if there's a clear limit to the recognized characters, both parsers work the same way +//! assert_eq!(alpha0_streaming("abcd;"), Ok((";", "abcd"))); +//! assert_eq!(alpha0_complete("abcd;"), Ok((";", "abcd"))); +//! +//! // but when there's no limit, the streaming version returns `Incomplete`, because it cannot +//! // know if more input data should be recognized. The whole input could be "abcd;", or +//! // "abcde;" +//! assert_eq!(alpha0_streaming("abcd"), Err(Err::Incomplete(Needed::Size(1)))); +//! +//! // while the complete version knows that all of the data is there +//! assert_eq!(alpha0_complete("abcd"), Ok(("", "abcd"))); +//! ``` +//! **Going further:** read the [guides](https://github.com/Geal/nom/tree/master/doc)! +#![cfg_attr(all(not(feature = "std"), feature = "alloc"), feature(alloc))] +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(feature = "cargo-clippy", allow(doc_markdown))] +#![cfg_attr(nightly, feature(test))] +#![deny(missing_docs)] +#![warn(missing_doc_code_examples)] + +#[cfg(all(not(feature = "std"), feature = "alloc"))] +#[macro_use] +extern crate alloc; +#[cfg(feature = "regexp_macros")] +#[macro_use] +extern crate lazy_static; +extern crate memchr; +#[cfg(feature = "regexp")] +pub extern crate regex; +#[cfg(feature = "lexical")] +extern crate lexical_core; +#[cfg(nightly)] +extern crate test; +#[cfg(test)] +extern crate doc_comment; + +//FIXME: reactivate doctest once https://github.com/rust-lang/rust/issues/62210 is done +//#[cfg(doctest)] +//doc_comment::doctest!("../README.md"); + +/// Lib module to re-export everything needed from `std` or `core`/`alloc`. This is how `serde` does +/// it, albeit there it is not public. +pub mod lib { + /// `std` facade allowing `std`/`core` to be interchangeable. Reexports `alloc` crate optionally, + /// as well as `core` or `std` + #[cfg(not(feature = "std"))] + /// internal std exports for no_std compatibility + pub mod std { + #[cfg(feature = "alloc")] + #[cfg_attr(feature = "alloc", macro_use)] + pub use alloc::{boxed, string, vec}; + + pub use core::{cmp, convert, fmt, iter, mem, ops, option, result, slice, str, borrow}; + + /// internal reproduction of std prelude + pub mod prelude { + pub use core::prelude as v1; + } + } + + #[cfg(feature = "std")] + /// internal std exports for no_std compatibility + pub mod std { + pub use std::{alloc, boxed, cmp, collections, convert, fmt, hash, iter, mem, ops, option, result, slice, str, string, vec, borrow}; + + /// internal reproduction of std prelude + pub mod prelude { + pub use std::prelude as v1; + } + } + + #[cfg(feature = "regexp")] + pub use regex; +} + +pub use self::traits::*; +pub use self::util::*; +pub use self::internal::*; +pub use self::methods::*; +pub use self::bits::*; +pub use self::whitespace::*; + +#[cfg(feature = "regexp")] +pub use self::regexp::*; +pub use self::str::*; + +#[macro_use] +mod util; + +#[macro_use] +pub mod error; + +#[macro_use] +mod internal; +mod traits; +#[macro_use] +pub mod combinator; +#[macro_use] +pub mod branch; +#[macro_use] +pub mod sequence; +#[macro_use] +pub mod multi; +#[macro_use] +pub mod methods; + +#[macro_use] +pub mod bytes; +#[macro_use] +pub mod bits; + +#[macro_use] +pub mod character; + +#[macro_use] +pub mod whitespace; + +#[cfg(feature = "regexp")] +#[macro_use] +mod regexp; + +mod str; + +#[macro_use] +pub mod number; diff --git a/third_party/rust/nom/src/methods.rs b/third_party/rust/nom/src/methods.rs new file mode 100644 index 0000000000..520f1e8bfd --- /dev/null +++ b/third_party/rust/nom/src/methods.rs @@ -0,0 +1,20 @@ +//! method combinators + +/// do not use: method combinators moved to the nom-methods crate +#[macro_export] +macro_rules! method ( + ($($args:tt)*) => (compile_error!("method combinators moved to the nom-methods crate");); +); + +/// do not use: method combinators moved to the nom-methods crate +#[macro_export] +macro_rules! call_m ( + ($($args:tt)*) => (compile_error!("method combinators moved to the nom-methods crate");); +); + +/// do not use: method combinators moved to the nom-methods crate +#[macro_export] +macro_rules! apply_m ( + ($($args:tt)*) => (compile_error!("method combinators moved to the nom-methods crate");); +); + diff --git a/third_party/rust/nom/src/multi/macros.rs b/third_party/rust/nom/src/multi/macros.rs new file mode 100644 index 0000000000..10b66b8424 --- /dev/null +++ b/third_party/rust/nom/src/multi/macros.rs @@ -0,0 +1,988 @@ +//! Parsers for applying parsers multiple times + +/// `separated_list!(I -> IResult<I,T>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` +/// separated_list(sep, X) returns a Vec<X> +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::separated_list; +/// use nom::bytes::complete::tag; +/// +/// # fn main() { +/// named!(parser<&str, Vec<&str>>, separated_list!(tag("|"), tag("abc"))); +/// +/// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); +/// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); +/// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// assert_eq!(parser("def|abc"), Ok(("def|abc", vec![]))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! separated_list( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + separated_list!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_list!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_list!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::multi::separated_listc($i, $f, $g) + ); +); + +/// `separated_nonempty_list!(I -> IResult<I,T>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` +/// separated_nonempty_list(sep, X) returns a Vec<X> +/// +/// it will return an error if there is no element in the list +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::separated_nonempty_list; +/// use nom::bytes::complete::tag; +/// +/// # fn main() { +/// named!(parser<&str, Vec<&str>>, separated_nonempty_list!(tag("|"), tag("abc"))); +/// +/// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); +/// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); +/// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("def|abc"), Err(Err::Error(("def|abc", ErrorKind::Tag)))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! separated_nonempty_list( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + separated_nonempty_list!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_nonempty_list!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_nonempty_list!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::multi::separated_nonempty_listc($i, $f, $g) + ); +); + +/// `many0!(I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` +/// Applies the parser 0 or more times and returns the list of results in a Vec. +/// +/// The embedded parser may return Incomplete. +/// +/// `many0` will only return `Error` if the embedded parser does not consume any input +/// (to avoid infinite loops). +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many0!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]),Ok((&b"efgh"[..], res))); +/// assert_eq!(multi(&b[..]),Ok((&b"azerty"[..], Vec::new()))); +/// # } +/// ``` +/// +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! many0( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + many0!($i, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr) => ( + $crate::multi::many0c($i, $f) + ); +); + +/// `many1!(I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` +/// Applies the parser 1 or more times and returns the list of results in a Vec +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many1!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]), Ok((&b"efgh"[..], res))); +/// assert_eq!(multi(&b[..]), Err(Err::Error(error_position!(&b[..], ErrorKind::Tag)))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! many1( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + many1!($i, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr) => ( + $crate::multi::many1c($i, $f) + ); +); + +/// `many_till!(I -> IResult<I,O>, I -> IResult<I,P>) => I -> IResult<I, (Vec<O>, P)>` +/// Applies the first parser until the second applies. Returns a tuple containing the list +/// of results from the first in a Vec and the result of the second. +/// +/// The first embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) ); +/// +/// let a = b"abcdabcdefghabcd"; +/// let b = b"efghabcd"; +/// let c = b"azerty"; +/// +/// let res_a = (vec![&b"abcd"[..], &b"abcd"[..]], &b"efgh"[..]); +/// let res_b: (Vec<&[u8]>, &[u8]) = (Vec::new(), &b"efgh"[..]); +/// assert_eq!(multi(&a[..]),Ok((&b"abcd"[..], res_a))); +/// assert_eq!(multi(&b[..]),Ok((&b"abcd"[..], res_b))); +/// assert_eq!(multi(&c[..]), Err(Err::Error(error_node_position!(&c[..], ErrorKind::ManyTill, +/// error_position!(&c[..], ErrorKind::Tag))))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! many_till( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + many_till!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + many_till!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + many_till!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::multi::many_tillc($i, $f, $g) + ); +); + +/// `many_m_n!(usize, usize, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` +/// Applies the parser between m and n times (n included) and returns the list of +/// results in a Vec +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, tag!( "abcd" ) ) ); +/// +/// let a = b"abcdefgh"; +/// let b = b"abcdabcdefgh"; +/// let c = b"abcdabcdabcdabcdabcdefgh"; +/// +/// assert_eq!(multi(&a[..]), Err(Err::Error(error_position!(&b"efgh"[..], ErrorKind::Tag)))); +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&b[..]),Ok((&b"efgh"[..], res))); +/// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&c[..]),Ok((&b"abcdefgh"[..], res2))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! many_m_n( + ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => ( + many_m_n!($i, $m, $n, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $m:expr, $n: expr, $f:expr) => ( + $crate::multi::many_m_nc($i, $m, $n, $f) + ); +); + +/// `many0_count!(I -> IResult<I,O>) => I -> IResult<I, usize>` +/// Applies the parser 0 or more times and returns the number of times the parser was applied. +/// +/// `many0_count` will only return `Error` if the embedded parser does not consume any input +/// (to avoid infinite loops). +/// +/// ``` +/// #[macro_use] extern crate nom; +/// use nom::character::streaming::digit1; +/// +/// named!(number<&[u8], usize>, many0_count!(pair!(digit1, tag!(",")))); +/// +/// fn main() { +/// assert_eq!(number(&b"123,45,abc"[..]), Ok((&b"abc"[..], 2))); +/// } +/// ``` +/// +#[macro_export] +macro_rules! many0_count { + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::multi::many0_countc($i, |i| $submac!(i, $($args)*)) + ); + + ($i:expr, $f:expr) => ( + $crate::multi::many0_countc($i, $f) + ); +} + +/// `many1_count!(I -> IResult<I,O>) => I -> IResult<I, usize>` +/// Applies the parser 1 or more times and returns the number of times the parser was applied. +/// +/// ``` +/// #[macro_use] extern crate nom; +/// use nom::character::streaming::digit1; +/// +/// named!(number<&[u8], usize>, many1_count!(pair!(digit1, tag!(",")))); +/// +/// fn main() { +/// assert_eq!(number(&b"123,45,abc"[..]), Ok((&b"abc"[..], 2))); +/// } +/// ``` +/// +#[macro_export] +macro_rules! many1_count { + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::multi::many1_countc($i, |i| $submac!(i, $($args)*)) + ); + + ($i:expr, $f:expr) => ( + $crate::multi::many1_countc($i, $f) + ); +} + +/// `count!(I -> IResult<I,O>, nb) => I -> IResult<I, Vec<O>>` +/// Applies the child parser a specified number of times +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(counter< Vec<&[u8]> >, count!( tag!( "abcd" ), 2 ) ); +/// +/// let a = b"abcdabcdabcdef"; +/// let b = b"abcdefgh"; +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// +/// assert_eq!(counter(&a[..]),Ok((&b"abcdef"[..], res))); +/// assert_eq!(counter(&b[..]), Err(Err::Error(error_position!(&b"efgh"[..], ErrorKind::Tag)))); +/// # } +/// ``` +/// +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! count( + ($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => ( + count!($i, |i| $submac!(i, $($args)*), $count) + ); + ($i:expr, $f:expr, $count: expr) => ( + $crate::multi::count($f, $count)($i) + ); +); + +/// `length_count!(I -> IResult<I, nb>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` +/// gets a number from the first parser, then applies the second parser that many times +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// # use nom::error::ErrorKind; +/// use nom::number::complete::be_u8; +/// # fn main() { +/// named!(parser<Vec<&[u8]>>, length_count!(be_u8, tag!("abc"))); +/// +/// assert_eq!(parser(&b"\x02abcabcabc"[..]), Ok(((&b"abc"[..], vec![&b"abc"[..], &b"abc"[..]])))); +/// assert_eq!(parser(&b"\x04abcabcabc"[..]), Err(Err::Incomplete(Needed::Size(3)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +#[cfg(feature = "alloc")] +macro_rules! length_count( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match $submac!($i, $($args)*) { + Err(e) => Err(Err::convert(e)), + Ok((i, o)) => { + match count!(i, $submac2!($($args2)*), o as usize) { + Err(e) => Err(Err::convert(e)), + Ok((i2, o2)) => Ok((i2, o2)) + } + } + } + } + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + length_count!($i, $submac!($($args)*), call!($g)); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + length_count!($i, call!($f), $submac!($($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + length_count!($i, call!($f), call!($g)); + ); +); + +/// `length_data!(I -> IResult<I, nb>) => O` +/// +/// `length_data` gets a number from the first parser, then takes a subslice of the input +/// of that size and returns that subslice +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// # use nom::error::ErrorKind; +/// use nom::number::complete::be_u8; +/// # fn main() { +/// named!(parser, length_data!(be_u8)); +/// +/// assert_eq!(parser(&b"\x06abcabcabc"[..]), Ok((&b"abc"[..], &b"abcabc"[..]))); +/// assert_eq!(parser(&b"\x06abc"[..]), Err(Err::Incomplete(Needed::Size(6)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! length_data( + ($i:expr, $submac:ident!( $($args:tt)* )) => ({ + $crate::multi::length_data(|i| $submac!(i, $($args)*))($i) + }); + + ($i:expr, $f:expr) => ( + $crate::multi::length_data($f)($i) + ); +); + +/// `length_value!(I -> IResult<I, nb>, I -> IResult<I,O>) => I -> IResult<I, O>` +/// +/// Gets a number from the first parser, takes a subslice of the input of that size, +/// then applies the second parser on that subslice. If the second parser returns +/// `Incomplete`, `length_value` will return an error +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// # use nom::error::ErrorKind; +/// use nom::number::complete::be_u8; +/// use nom::character::complete::alpha0; +/// use nom::bytes::complete::tag; +/// # fn main() { +/// named!(parser, length_value!(be_u8, alpha0)); +/// +/// assert_eq!(parser(&b"\x06abcabcabc"[..]), Ok((&b"abc"[..], &b"abcabc"[..]))); +/// assert_eq!(parser(&b"\x06abc"[..]), Err(Err::Incomplete(Needed::Size(6)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! length_value( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + length_value!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + length_value!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + length_value!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::multi::length_valuec($i, $f, $g); + ); +); + +/// `fold_many0!(I -> IResult<I,O>, R, Fn(R, O) -> R) => I -> IResult<I, R>` +/// Applies the parser 0 or more times and folds the list of return values +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, +/// fold_many0!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]),Ok((&b"efgh"[..], res))); +/// assert_eq!(multi(&b[..]),Ok((&b"azerty"[..], Vec::new()))); +/// # } +/// ``` +/// 0 or more +#[macro_export(local_inner_macros)] +macro_rules! fold_many0( + ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $fold_f:expr) => ( + fold_many0!($i, |i| $submac!(i, $($args)*), $init, $fold_f) + ); + ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( + $crate::multi::fold_many0($f, $init, $fold_f)($i) + ); +); + +/// `fold_many1!(I -> IResult<I,O>, R, Fn(R, O) -> R) => I -> IResult<I, R>` +/// Applies the parser 1 or more times and folds the list of return values +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, +/// fold_many1!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]),Ok((&b"efgh"[..], res))); +/// assert_eq!(multi(&b[..]), Err(Err::Error(error_position!(&b[..], ErrorKind::Many1)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! fold_many1( + ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $fold_f:expr) => ( + fold_many1!($i, |i| $submac!(i, $($args)*), $init, $fold_f) + ); + ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( + $crate::multi::fold_many1c($i, $f, $init, $fold_f) + ); + ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( + fold_many1!($i, call!($f), $init, $fold_f); + ); +); + +/// `fold_many_m_n!(usize, usize, I -> IResult<I,O>, R, Fn(R, O) -> R) => I -> IResult<I, R>` +/// Applies the parser between m and n times (n included) and folds the list of return value +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, +/// fold_many_m_n!(2, 4, tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdefgh"; +/// let b = b"abcdabcdefgh"; +/// let c = b"abcdabcdabcdabcdabcdefgh"; +/// +/// assert_eq!(multi(&a[..]), Err(Err::Error(error_position!(&a[..], ErrorKind::ManyMN)))); +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&b[..]),Ok((&b"efgh"[..], res))); +/// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&c[..]),Ok((&b"abcdefgh"[..], res2))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! fold_many_m_n( + ($i:expr, $m:expr, $n:expr, $submac:ident!( $($args:tt)* ), $init:expr, $fold_f:expr) => ( + fold_many_m_n!($i, $m, $n, |i| $submac!(i, $($args)*), $init, $fold_f) + ); + ($i:expr, $m:expr, $n:expr, $f:expr, $init:expr, $fold_f:expr) => ( + $crate::multi::fold_many_m_nc($i, $m, $n, $f, $init, $fold_f) + ); +); + +#[cfg(test)] +mod tests { + use crate::internal::{Err, IResult, Needed}; + use crate::error::ParseError; + use crate::lib::std::str::{self, FromStr}; + #[cfg(feature = "alloc")] + use crate::lib::std::vec::Vec; + use crate::character::streaming::digit1 as digit; + use crate::number::streaming::{be_u16, be_u8}; + use crate::error::ErrorKind; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes<T: $crate::AsBytes>(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); + ); + + macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + use $crate::lib::std::cmp::min; + let len = $i.len(); + let blen = $bytes.len(); + let m = min(len, blen); + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: IResult<_,_,_> = if reduced != b { + Err($crate::Err::Error($crate::error::make_error($i, $crate::error::ErrorKind::Tag))) + } else if m < blen { + Err($crate::Err::Incomplete(Needed::Size(blen))) + } else { + Ok((&$i[blen..], reduced)) + }; + res + } + ); + ); + + #[test] + #[cfg(feature = "alloc")] + fn separated_list() { + named!(multi<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!("abcd"))); + named!(multi_empty<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!(""))); + named!(multi_longsep<&[u8],Vec<&[u8]> >, separated_list!(tag!(".."), tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcd,abcdef"[..]; + let c = &b"azerty"[..]; + let d = &b",,abc"[..]; + let e = &b"abcd,abcd,ef"[..]; + let f = &b"abc"[..]; + let g = &b"abcd."[..]; + let h = &b"abcd,abc"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Ok((&b"ef"[..], res1))); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Ok((&b"ef"[..], res2))); + assert_eq!(multi(c), Ok((&b"azerty"[..], Vec::new()))); + assert_eq!(multi_empty(d), Err(Err::Error(error_position!(d, ErrorKind::SeparatedList)))); + //let res3 = vec![&b""[..], &b""[..], &b""[..]]; + //assert_eq!(multi_empty(d),Ok((&b"abc"[..], res3))); + let res4 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(e), Ok((&b",ef"[..], res4))); + + assert_eq!(multi(f), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi_longsep(g), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!(multi(h), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn separated_nonempty_list() { + named!(multi<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(","), tag!("abcd"))); + named!(multi_longsep<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(".."), tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcd,abcdef"[..]; + let c = &b"azerty"[..]; + let d = &b"abcd,abcd,ef"[..]; + + let f = &b"abc"[..]; + let g = &b"abcd."[..]; + let h = &b"abcd,abc"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Ok((&b"ef"[..], res1))); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Ok((&b"ef"[..], res2))); + assert_eq!(multi(c), Err(Err::Error(error_position!(c, ErrorKind::Tag)))); + let res3 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(d), Ok((&b",ef"[..], res3))); + + assert_eq!(multi(f), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi_longsep(g), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!(multi(h), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn many0() { + named!(tag_abcd, tag!("abcd")); + named!(tag_empty, tag!("")); + named!( multi<&[u8],Vec<&[u8]> >, many0!(tag_abcd) ); + named!( multi_empty<&[u8],Vec<&[u8]> >, many0!(tag_empty) ); + + assert_eq!(multi(&b"abcdef"[..]), Ok((&b"ef"[..], vec![&b"abcd"[..]]))); + assert_eq!(multi(&b"abcdabcdefgh"[..]), Ok((&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]]))); + assert_eq!(multi(&b"azerty"[..]), Ok((&b"azerty"[..], Vec::new()))); + assert_eq!(multi(&b"abcdab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi(&b""[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!( + multi_empty(&b"abcdef"[..]), + Err(Err::Error(error_position!(&b"abcdef"[..], ErrorKind::Many0))) + ); + } + + #[cfg(nightly)] + use test::Bencher; + + #[cfg(nightly)] + #[bench] + fn many0_bench(b: &mut Bencher) { + named!(multi<&[u8],Vec<&[u8]> >, many0!(tag!("abcd"))); + b.iter(|| multi(&b"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"[..])); + } + + #[test] + #[cfg(feature = "alloc")] + fn many1() { + named!(multi<&[u8],Vec<&[u8]> >, many1!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcdabcdefgh"[..]; + let c = &b"azerty"[..]; + let d = &b"abcdab"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Ok((&b"ef"[..], res1))); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Ok((&b"efgh"[..], res2))); + assert_eq!(multi(c), Err(Err::Error(error_position!(c, ErrorKind::Tag)))); + assert_eq!(multi(d), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn many_till() { + named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) ); + + let a = b"abcdabcdefghabcd"; + let b = b"efghabcd"; + let c = b"azerty"; + + let res_a = (vec![&b"abcd"[..], &b"abcd"[..]], &b"efgh"[..]); + let res_b: (Vec<&[u8]>, &[u8]) = (Vec::new(), &b"efgh"[..]); + assert_eq!(multi(&a[..]), Ok((&b"abcd"[..], res_a))); + assert_eq!(multi(&b[..]), Ok((&b"abcd"[..], res_b))); + assert_eq!( + multi(&c[..]), + Err(Err::Error(error_node_position!( + &c[..], + ErrorKind::ManyTill, + error_position!(&c[..], ErrorKind::Tag) + ))) + ); + } + + #[test] + #[cfg(feature = "std")] + fn infinite_many() { + fn tst(input: &[u8]) -> IResult<&[u8], &[u8]> { + println!("input: {:?}", input); + Err(Err::Error(error_position!(input, ErrorKind::Tag))) + } + + // should not go into an infinite loop + named!(multi0<&[u8],Vec<&[u8]> >, many0!(tst)); + let a = &b"abcdef"[..]; + assert_eq!(multi0(a), Ok((a, Vec::new()))); + + named!(multi1<&[u8],Vec<&[u8]> >, many1!(tst)); + let a = &b"abcdef"[..]; + assert_eq!(multi1(a), Err(Err::Error(error_position!(a, ErrorKind::Tag)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn many_m_n() { + named!(multi<&[u8],Vec<&[u8]> >, many_m_n!(2, 4, tag!("Abcd"))); + + let a = &b"Abcdef"[..]; + let b = &b"AbcdAbcdefgh"[..]; + let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; + let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; + let e = &b"AbcdAb"[..]; + + assert_eq!(multi(a), Err(Err::Error(error_position!(&b"ef"[..], ErrorKind::Tag)))); + let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(b), Ok((&b"efgh"[..], res1))); + let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(c), Ok((&b"efgh"[..], res2))); + let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(d), Ok((&b"Abcdefgh"[..], res3))); + assert_eq!(multi(e), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn count() { + const TIMES: usize = 2; + named!(tag_abc, tag!("abc")); + named!( cnt_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); + + assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Ok((&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]]))); + assert_eq!(cnt_2(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(cnt_2(&b"abcab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(cnt_2(&b"xxx"[..]), Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))); + assert_eq!( + cnt_2(&b"xxxabcabcdef"[..]), + Err(Err::Error(error_position!(&b"xxxabcabcdef"[..], ErrorKind::Tag))) + ); + assert_eq!( + cnt_2(&b"abcxxxabcdef"[..]), + Err(Err::Error(error_position!(&b"xxxabcdef"[..], ErrorKind::Tag))) + ); + } + + #[test] + #[cfg(feature = "alloc")] + fn count_zero() { + const TIMES: usize = 0; + named!(tag_abc, tag!("abc")); + named!( counter_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); + + let done = &b"abcabcabcdef"[..]; + let parsed_done = Vec::new(); + let rest = done; + let incomplete_1 = &b"ab"[..]; + let parsed_incompl_1 = Vec::new(); + let incomplete_2 = &b"abcab"[..]; + let parsed_incompl_2 = Vec::new(); + let error = &b"xxx"[..]; + let error_remain = &b"xxx"[..]; + let parsed_err = Vec::new(); + let error_1 = &b"xxxabcabcdef"[..]; + let parsed_err_1 = Vec::new(); + let error_1_remain = &b"xxxabcabcdef"[..]; + let error_2 = &b"abcxxxabcdef"[..]; + let parsed_err_2 = Vec::new(); + let error_2_remain = &b"abcxxxabcdef"[..]; + + assert_eq!(counter_2(done), Ok((rest, parsed_done))); + assert_eq!(counter_2(incomplete_1), Ok((incomplete_1, parsed_incompl_1))); + assert_eq!(counter_2(incomplete_2), Ok((incomplete_2, parsed_incompl_2))); + assert_eq!(counter_2(error), Ok((error_remain, parsed_err))); + assert_eq!(counter_2(error_1), Ok((error_1_remain, parsed_err_1))); + assert_eq!(counter_2(error_2), Ok((error_2_remain, parsed_err_2))); + } + + #[derive(Debug, Clone, PartialEq)] + pub struct NilError; + + impl<I> From<(I,ErrorKind)> for NilError { + fn from(_: (I, ErrorKind)) -> Self { + NilError + } + } + + impl<I> ParseError<I> for NilError { + fn from_error_kind(_: I, _: ErrorKind) -> NilError { + NilError + } + fn append(_: I, _: ErrorKind, _: NilError) -> NilError { + NilError + } + } + + named!(pub number<u32>, map_res!( + map_res!( + digit, + str::from_utf8 + ), + FromStr::from_str + )); + + #[test] + #[cfg(feature = "alloc")] + fn length_count() { + named!(tag_abc, tag!(&b"abc"[..])); + named!( cnt<&[u8], Vec<&[u8]> >, length_count!(number, tag_abc) ); + + assert_eq!(cnt(&b"2abcabcabcdef"[..]), Ok((&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]]))); + assert_eq!(cnt(&b"2ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(cnt(&b"3abcab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(cnt(&b"xxx"[..]), Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Digit)))); + assert_eq!( + cnt(&b"2abcxxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn length_data() { + named!( take<&[u8], &[u8]>, length_data!(number) ); + + assert_eq!(take(&b"6abcabcabcdef"[..]), Ok((&b"abcdef"[..], &b"abcabc"[..]))); + assert_eq!(take(&b"3ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(take(&b"xxx"[..]), Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Digit)))); + assert_eq!(take(&b"2abcxxx"[..]), Ok((&b"cxxx"[..], &b"ab"[..]))); + } + + #[test] + fn length_value_test() { + named!(length_value_1<&[u8], u16 >, length_value!(be_u8, be_u16)); + named!(length_value_2<&[u8], (u8, u8) >, length_value!(be_u8, tuple!(be_u8, be_u8))); + + let i1 = [0, 5, 6]; + assert_eq!(length_value_1(&i1), Err(Err::Error(error_position!(&b""[..], ErrorKind::Complete)))); + assert_eq!(length_value_2(&i1), Err(Err::Error(error_position!(&b""[..], ErrorKind::Complete)))); + + let i2 = [1, 5, 6, 3]; + assert_eq!( + length_value_1(&i2), + Err(Err::Error(error_position!(&i2[1..2], ErrorKind::Complete))) + ); + assert_eq!( + length_value_2(&i2), + Err(Err::Error(error_position!(&i2[1..2], ErrorKind::Complete))) + ); + + let i3 = [2, 5, 6, 3, 4, 5, 7]; + assert_eq!(length_value_1(&i3), Ok((&i3[3..], 1286))); + assert_eq!(length_value_2(&i3), Ok((&i3[3..], (5, 6)))); + + let i4 = [3, 5, 6, 3, 4, 5]; + assert_eq!(length_value_1(&i4), Ok((&i4[4..], 1286))); + assert_eq!(length_value_2(&i4), Ok((&i4[4..], (5, 6)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn fold_many0() { + fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> { + acc.push(item); + acc + }; + named!(tag_abcd, tag!("abcd")); + named!(tag_empty, tag!("")); + named!( multi<&[u8],Vec<&[u8]> >, fold_many0!(tag_abcd, Vec::new(), fold_into_vec) ); + named!( multi_empty<&[u8],Vec<&[u8]> >, fold_many0!(tag_empty, Vec::new(), fold_into_vec) ); + + assert_eq!(multi(&b"abcdef"[..]), Ok((&b"ef"[..], vec![&b"abcd"[..]]))); + assert_eq!(multi(&b"abcdabcdefgh"[..]), Ok((&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]]))); + assert_eq!(multi(&b"azerty"[..]), Ok((&b"azerty"[..], Vec::new()))); + assert_eq!(multi(&b"abcdab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi(&b""[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!( + multi_empty(&b"abcdef"[..]), + Err(Err::Error(error_position!(&b"abcdef"[..], ErrorKind::Many0))) + ); + } + + #[test] + #[cfg(feature = "alloc")] + fn fold_many1() { + fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> { + acc.push(item); + acc + }; + named!(multi<&[u8],Vec<&[u8]> >, fold_many1!(tag!("abcd"), Vec::new(), fold_into_vec)); + + let a = &b"abcdef"[..]; + let b = &b"abcdabcdefgh"[..]; + let c = &b"azerty"[..]; + let d = &b"abcdab"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Ok((&b"ef"[..], res1))); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Ok((&b"efgh"[..], res2))); + assert_eq!(multi(c), Err(Err::Error(error_position!(c, ErrorKind::Many1)))); + assert_eq!(multi(d), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn fold_many_m_n() { + fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> { + acc.push(item); + acc + }; + named!(multi<&[u8],Vec<&[u8]> >, fold_many_m_n!(2, 4, tag!("Abcd"), Vec::new(), fold_into_vec)); + + let a = &b"Abcdef"[..]; + let b = &b"AbcdAbcdefgh"[..]; + let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; + let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; + let e = &b"AbcdAb"[..]; + + assert_eq!(multi(a), Err(Err::Error(error_position!(a, ErrorKind::ManyMN)))); + let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(b), Ok((&b"efgh"[..], res1))); + let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(c), Ok((&b"efgh"[..], res2))); + let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(d), Ok((&b"Abcdefgh"[..], res3))); + assert_eq!(multi(e), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + fn many0_count() { + named!( + count0_nums(&[u8]) -> usize, + many0_count!(pair!(digit, tag!(","))) + ); + + assert_eq!(count0_nums(&b"123,junk"[..]), Ok((&b"junk"[..], 1))); + + assert_eq!(count0_nums(&b"123,45,junk"[..]), Ok((&b"junk"[..], 2))); + + assert_eq!(count0_nums(&b"1,2,3,4,5,6,7,8,9,0,junk"[..]), Ok((&b"junk"[..], 10))); + + assert_eq!(count0_nums(&b"hello"[..]), Ok((&b"hello"[..], 0))); + } + + #[test] + fn many1_count() { + named!( + count1_nums(&[u8]) -> usize, + many1_count!(pair!(digit, tag!(","))) + ); + + assert_eq!(count1_nums(&b"123,45,junk"[..]), Ok((&b"junk"[..], 2))); + + assert_eq!(count1_nums(&b"1,2,3,4,5,6,7,8,9,0,junk"[..]), Ok((&b"junk"[..], 10))); + + assert_eq!( + count1_nums(&b"hello"[..]), + Err(Err::Error(error_position!(&b"hello"[..], ErrorKind::Many1Count))) + ); + } + +} diff --git a/third_party/rust/nom/src/multi/mod.rs b/third_party/rust/nom/src/multi/mod.rs new file mode 100644 index 0000000000..e8f26dd771 --- /dev/null +++ b/third_party/rust/nom/src/multi/mod.rs @@ -0,0 +1,997 @@ +//! combinators applying their child parser multiple times + +#[macro_use] +mod macros; + +use crate::internal::{Err, IResult, Needed}; +use crate::error::ParseError; +use crate::traits::{InputLength, InputTake, ToUsize}; +#[cfg(feature = "alloc")] +use crate::lib::std::vec::Vec; +use crate::error::ErrorKind; + +/// Repeats the embedded parser until it fails +/// and returns the results in a `Vec`. +/// +/// # Arguments +/// * `f` The parser to apply. +/// +/// *Note*: if the parser passed to `many0` accepts empty inputs +/// (like `alpha0` or `digit0`), `many0` will return an error, +/// to prevent going into an infinite loop +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many0; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// many0(tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Ok(("123123", vec![]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// ``` +#[cfg(feature = "alloc")] +pub fn many0<I, O, E, F>(f: F) -> impl Fn(I) -> IResult<I, Vec<O>, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + move |i: I| { + let mut acc = crate::lib::std::vec::Vec::with_capacity(4); + let mut i = i.clone(); + loop { + match f(i.clone()) { + Err(Err::Error(_)) => return Ok((i, acc)), + Err(e) => return Err(e), + Ok((i1, o)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many0))); + } + + i = i1; + acc.push(o); + } + } + } + } +} +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn many0c<I, O, E, F>(input: I, f: F) -> IResult<I, Vec<O>, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + many0(f)(input) +} + +/// Runs the embedded parser until it fails and +/// returns the results in a `Vec`. Fails if +/// the embedded parser does not produce at least +/// one result. +/// +/// # Arguments +/// * `f` The parser to apply. +/// +/// *Note*: if the parser passed to `many1` accepts empty inputs +/// (like `alpha0` or `digit0`), `many1` will return an error, +/// to prevent going into an infinite loop +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many1; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// many1(tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Err(Err::Error(("123123", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// ``` +#[cfg(feature = "alloc")] +pub fn many1<I, O, E, F>(f: F) -> impl Fn(I) -> IResult<I, Vec<O>, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + move |i: I| { + let mut i = i.clone(); + match f(i.clone()) { + Err(Err::Error(err)) => return Err(Err::Error(E::append(i, ErrorKind::Many1, err))), + Err(e) => return Err(e), + Ok((i1, o)) => { + let mut acc = crate::lib::std::vec::Vec::with_capacity(4); + acc.push(o); + i = i1; + + loop { + match f(i.clone()) { + Err(Err::Error(_)) => return Ok((i, acc)), + Err(e) => return Err(e), + Ok((i1, o)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1))); + } + + i = i1; + acc.push(o); + } + } + } + } + } + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn many1c<I, O, E, F>(input: I, f: F) -> IResult<I, Vec<O>, E> +where + I: Clone + Copy + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + many1(f)(input) +} + +/// Applies the parser `f` until the parser `g` produces +/// a result. Returns a pair consisting of the results of +/// `f` in a `Vec` and the result of `g`. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many_till; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> { +/// many_till(tag("abc"), tag("end"))(s) +/// }; +/// +/// assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end")))); +/// assert_eq!(parser("abc123end"), Err(Err::Error(("123end", ErrorKind::Tag)))); +/// assert_eq!(parser("123123end"), Err(Err::Error(("123123end", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("abcendefg"), Ok(("efg", (vec!["abc"], "end")))); +/// ``` +#[cfg(feature = "alloc")] +pub fn many_till<I, O, P, E, F, G>(f: F, g: G) -> impl Fn(I) -> IResult<I, (Vec<O>, P), E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(I) -> IResult<I, P, E>, + E: ParseError<I>, +{ + move |i: I| { + let mut res = crate::lib::std::vec::Vec::new(); + let mut i = i.clone(); + loop { + match g(i.clone()) { + Ok((i1, o)) => return Ok((i1, (res, o))), + Err(Err::Error(_)) => { + match f(i.clone()) { + Err(Err::Error(err)) => + return Err(Err::Error(E::append(i, ErrorKind::ManyTill, err))), + Err(e) => return Err(e), + Ok((i1, o)) => { + // loop trip must always consume (otherwise infinite loops) + if i1 == i { + return Err(Err::Error(E::from_error_kind(i1, ErrorKind::ManyTill))); + } + + res.push(o); + i = i1; + } + } + }, + Err(e) => return Err(e), + } + } + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn many_tillc<I, O, P, E, F, G>(i: I, f: F, g: G) -> IResult<I, (Vec<O>, P), E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(I) -> IResult<I, P, E>, + E: ParseError<I>, +{ + many_till(f, g)(i) +} + +/// Alternates between two parsers to produce +/// a list of elements. +/// # Arguments +/// * `sep` Parses the separator between list elements. +/// * `f` Parses the elements of the list. +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::separated_list; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// separated_list(tag("|"), tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); +/// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); +/// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// assert_eq!(parser("def|abc"), Ok(("def|abc", vec![]))); +/// ``` +#[cfg(feature = "alloc")] +pub fn separated_list<I, O, O2, E, F, G>(sep: G, f: F) -> impl Fn(I) -> IResult<I, Vec<O>, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(I) -> IResult<I, O2, E>, + E: ParseError<I>, +{ + move |i: I| { + let mut res = Vec::new(); + let mut i = i.clone(); + + match f(i.clone()) { + Err(Err::Error(_)) => return Ok((i, res)), + Err(e) => return Err(e), + Ok((i1, o)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i1, ErrorKind::SeparatedList))); + } + + res.push(o); + i = i1; + } + } + + loop { + match sep(i.clone()) { + Err(Err::Error(_)) => return Ok((i, res)), + Err(e) => return Err(e), + Ok((i1, _)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i1, ErrorKind::SeparatedList))); + } + + match f(i1.clone()) { + Err(Err::Error(_)) => return Ok((i, res)), + Err(e) => return Err(e), + Ok((i2, o)) => { + if i2 == i { + return Err(Err::Error(E::from_error_kind(i2, ErrorKind::SeparatedList))); + } + + res.push(o); + i = i2; + } + } + } + } + } + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn separated_listc<I, O, O2, E, F, G>(i: I, sep: G, f: F) -> IResult<I, Vec<O>, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(I) -> IResult<I, O2, E>, + E: ParseError<I>, +{ + separated_list(sep, f)(i) +} + +/// Alternates between two parsers to produce +/// a list of elements. Fails if the element +/// parser does not produce at least one element. +/// # Arguments +/// * `sep` Parses the separator between list elements. +/// * `f` Parses the elements of the list. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::separated_nonempty_list; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// separated_nonempty_list(tag("|"), tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); +/// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); +/// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("def|abc"), Err(Err::Error(("def|abc", ErrorKind::Tag)))); +/// ``` +#[cfg(feature = "alloc")] +pub fn separated_nonempty_list<I, O, O2, E, F, G>(sep: G, f: F) -> impl Fn(I) -> IResult<I, Vec<O>, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(I) -> IResult<I, O2, E>, + E: ParseError<I>, +{ + move |i: I| { + let mut res = Vec::new(); + let mut i = i.clone(); + + // Parse the first element + match f(i.clone()) { + Err(e)=> return Err(e), + Ok((i1, o)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i1, ErrorKind::SeparatedList))); + } + + res.push(o); + i = i1; + } + } + + loop { + match sep(i.clone()) { + Err(Err::Error(_)) => return Ok((i, res)), + Err(e) => return Err(e), + Ok((i1, _)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i1, ErrorKind::SeparatedList))); + } + + match f(i1.clone()) { + Err(Err::Error(_)) => return Ok((i, res)), + Err(e) => return Err(e), + Ok((i2, o)) => { + if i2 == i { + return Err(Err::Error(E::from_error_kind(i2, ErrorKind::SeparatedList))); + } + + res.push(o); + i = i2; + } + } + } + } + } + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn separated_nonempty_listc<I, O, O2, E, F, G>(i: I, sep: G, f: F) -> IResult<I, Vec<O>, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(I) -> IResult<I, O2, E>, + E: ParseError<I>, +{ + separated_nonempty_list(sep, f)(i) +} + +/// Repeats the embedded parser `n` times or until it fails +/// and returns the results in a `Vec`. Fails if the +/// embedded parser does not succeed at least `m` times. +/// # Arguments +/// * `m` The minimum number of iterations. +/// * `n` The maximum number of iterations. +/// * `f` The parser to apply. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many_m_n; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// many_m_n(0, 2, tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Ok(("123123", vec![]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); +/// ``` +#[cfg(feature = "alloc")] +pub fn many_m_n<I, O, E, F>(m: usize, n: usize, f: F) -> impl Fn(I) -> IResult<I, Vec<O>, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + move |i: I| { + let mut res = crate::lib::std::vec::Vec::with_capacity(m); + let mut input = i.clone(); + let mut count: usize = 0; + + if n == 0 { + return Ok((i, vec!())) + } + + loop { + let _i = input.clone(); + match f(_i) { + Ok((i, o)) => { + // do not allow parsers that do not consume input (causes infinite loops) + if i == input { + return Err(Err::Error(E::from_error_kind(input, ErrorKind::ManyMN))); + } + + res.push(o); + input = i; + count += 1; + + if count == n { + return Ok((input, res)); + } + } + Err(Err::Error(e)) => { + if count < m { + return Err(Err::Error(E::append(input, ErrorKind::ManyMN, e))); + } else { + return Ok((input, res)); + } + } + Err(e) => { + return Err(e); + } + } + } + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn many_m_nc<I, O, E, F>(i: I, m: usize, n: usize, f: F) -> IResult<I, Vec<O>, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + many_m_n(m, n, f)(i) +} + +/// Repeats the embedded parser until it fails +/// and returns the number of successful iterations. +/// # Arguments +/// * `f` The parser to apply. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many0_count; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, usize> { +/// many0_count(tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", 2))); +/// assert_eq!(parser("abc123"), Ok(("123", 1))); +/// assert_eq!(parser("123123"), Ok(("123123", 0))); +/// assert_eq!(parser(""), Ok(("", 0))); +/// ``` +pub fn many0_count<I, O, E, F>(f: F) -> impl Fn(I) -> IResult<I, usize, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + move |i: I| { + let mut input = i.clone(); + let mut count = 0; + + loop { + let input_ = input.clone(); + match f(input_) { + Ok((i, _)) => { + // loop trip must always consume (otherwise infinite loops) + if i == input { + return Err(Err::Error(E::from_error_kind(input, ErrorKind::Many0Count))); + } + + input = i; + count += 1; + } + + Err(Err::Error(_)) => return Ok((input, count)), + + Err(e) => return Err(e), + } + } + } +} + +#[doc(hidden)] +pub fn many0_countc<I, O, E, F>(i: I, f: F) -> IResult<I, usize, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + many0_count(f)(i) +} + +/// Repeats the embedded parser until it fails +/// and returns the number of successful iterations. +/// Fails if the embedded parser does not succeed +/// at least once. +/// # Arguments +/// * `f` The parser to apply. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many1_count; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, usize> { +/// many1_count(tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", 2))); +/// assert_eq!(parser("abc123"), Ok(("123", 1))); +/// assert_eq!(parser("123123"), Err(Err::Error(("123123", ErrorKind::Many1Count)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Many1Count)))); +/// ``` +pub fn many1_count<I, O, E, F>(f: F) -> impl Fn(I) -> IResult<I, usize, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + move |i: I| { + let i_ = i.clone(); + match f(i_) { + Err(Err::Error(_)) => Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1Count))), + Err(i) => Err(i), + Ok((i1, _)) => { + let mut count = 1; + let mut input = i1; + + loop { + let input_ = input.clone(); + match f(input_) { + Err(Err::Error(_)) => return Ok((input, count)), + Err(e) => return Err(e), + Ok((i, _)) => { + if i == input { + return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1Count))); + } + + count += 1; + input = i; + } + } + } + } + } + } +} + +#[doc(hidden)] +pub fn many1_countc<I, O, E, F>(i: I, f: F) -> IResult<I, usize, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + many1_count(f)(i) +} + +/// Runs the embedded parser a specified number +/// of times. Returns the results in a `Vec`. +/// # Arguments +/// * `f` The parser to apply. +/// * `count` How often to apply the parser. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::count; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// count(tag("abc"), 2)(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// assert_eq!(parser("123123"), Err(Err::Error(("123123", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); +/// ``` +#[cfg(feature = "alloc")] +pub fn count<I, O, E, F>(f: F, count: usize) -> impl Fn(I) -> IResult<I, Vec<O>, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + move |i: I | { + let mut input = i.clone(); + let mut res = crate::lib::std::vec::Vec::new(); + + for _ in 0..count { + let input_ = input.clone(); + match f(input_) { + Ok((i, o)) => { + res.push(o); + input = i; + } + Err(Err::Error(e)) => { + return Err(Err::Error(E::append(i, ErrorKind::Count, e))); + } + Err(e) => { + return Err(e); + } + } + } + + Ok((input, res)) + } +} + +/// Applies a parser until it fails and accumulates +/// the results using a given function and initial value. +/// # Arguments +/// * `f` The parser to apply. +/// * `init` The initial value. +/// * `g` The function that combines a result of `f` with +/// the current accumulator. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::fold_many0; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// fold_many0( +/// tag("abc"), +/// Vec::new(), +/// |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// } +/// )(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Ok(("123123", vec![]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// ``` +pub fn fold_many0<I, O, E, F, G, R>(f: F, init: R, g: G) -> impl Fn(I) -> IResult<I, R, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(R, O) -> R, + E: ParseError<I>, + R: Clone, +{ + move |i: I| { + let mut res = init.clone(); + let mut input = i.clone(); + + loop { + let i_ = input.clone(); + match f(i_) { + Ok((i, o)) => { + // loop trip must always consume (otherwise infinite loops) + if i == input { + return Err(Err::Error(E::from_error_kind(input, ErrorKind::Many0))); + } + + res = g(res, o); + input = i; + } + Err(Err::Error(_)) => { + return Ok((input, res)); + } + Err(e) => { + return Err(e); + } + } + } + } +} + +#[doc(hidden)] +pub fn fold_many0c<I, O, E, F, G, R>(i: I, f: F, init: R, g: G) -> IResult<I, R, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(R, O) -> R, + E: ParseError<I>, + R: Clone, +{ + fold_many0(f, init, g)(i) +} + +/// Applies a parser until it fails and accumulates +/// the results using a given function and initial value. +/// Fails if the embedded parser does not succeed at least +/// once. +/// # Arguments +/// * `f` The parser to apply. +/// * `init` The initial value. +/// * `g` The function that combines a result of `f` with +/// the current accumulator. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::fold_many1; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// fold_many1( +/// tag("abc"), +/// Vec::new(), +/// |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// } +/// )(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Err(Err::Error(("123123", ErrorKind::Many1)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Many1)))); +/// ``` +pub fn fold_many1<I, O, E, F, G, R>(f: F, init: R, g: G) -> impl Fn(I) -> IResult<I, R, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(R, O) -> R, + E: ParseError<I>, + R: Clone, +{ + move |i: I| { + let _i = i.clone(); + let init = init.clone(); + match f(_i) { + Err(Err::Error(_)) => Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1))), + Err(e) => return Err(e), + Ok((i1, o1)) => { + let mut acc = g(init, o1); + let mut input = i1; + + loop { + let _input = input.clone(); + match f(_input) { + Err(Err::Error(_)) => { + break; + } + Err(e) => return Err(e), + Ok((i, o)) => { + if i == input { + return Err(Err::Failure(E::from_error_kind(i, ErrorKind::Many1))); + } + + acc = g(acc, o); + input = i; + } + } + } + + Ok((input, acc)) + } + } + } +} + +#[doc(hidden)] +pub fn fold_many1c<I, O, E, F, G, R>(i: I, f: F, init: R, g: G) -> IResult<I, R, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(R, O) -> R, + E: ParseError<I>, + R: Clone, +{ + fold_many1(f, init, g)(i) +} + +/// Applies a parser `n` times or until it fails and accumulates +/// the results using a given function and initial value. +/// Fails if the embedded parser does not succeed at least `m` +/// times. +/// # Arguments +/// * `m` The minimum number of iterations. +/// * `n` The maximum number of iterations. +/// * `f` The parser to apply. +/// * `init` The initial value. +/// * `g` The function that combines a result of `f` with +/// the current accumulator. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::fold_many_m_n; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// fold_many_m_n( +/// 0, +/// 2, +/// tag("abc"), +/// Vec::new(), +/// |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// } +/// )(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Ok(("123123", vec![]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); +/// ``` +pub fn fold_many_m_n<I, O, E, F, G, R>(m: usize, n: usize, f: F, init: R, g: G) -> impl Fn(I) ->IResult<I, R, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(R, O) -> R, + E: ParseError<I>, + R: Clone, +{ + move |i: I| { + let mut acc = init.clone(); + let mut input = i.clone(); + for count in 0..n { + let _input = input.clone(); + match f(_input) { + Ok((i, o)) => { + // do not allow parsers that do not consume input (causes infinite loops) + if i == input { + return Err(Err::Error(E::from_error_kind(i, ErrorKind::ManyMN))); + } + + acc = g(acc, o); + input = i; + } + //FInputXMError: handle failure properly + Err(Err::Error(_)) => if count < m { + return Err(Err::Error(E::from_error_kind(i, ErrorKind::ManyMN))); + } else { + break; + } + Err(e) => return Err(e), + } + } + + Ok((input, acc)) + } +} + +#[doc(hidden)] +pub fn fold_many_m_nc<I, O, E, F, G, R>(i: I, m: usize, n: usize, f: F, init: R, g: G) -> IResult<I, R, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult<I, O, E>, + G: Fn(R, O) -> R, + E: ParseError<I>, + R: Clone, +{ + fold_many_m_n(m, n, f, init, g)(i) +} + +/// Gets a number from the parser and returns a +/// subslice of the input of that size. +/// If the parser returns Incomplete, +/// length_data will return an error. +/// # Arguments +/// * `f` The parser to apply. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u16; +/// use nom::multi::length_data; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// length_data(be_u16)(s) +/// } +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"efg"[..], &b"abc"[..]))); +/// assert_eq!(parser(b"\x00\x03"), Err(Err::Incomplete(Size(3)))); +/// ``` +pub fn length_data<I, N, E, F>(f: F) -> impl Fn(I) -> IResult<I, I, E> +where + I: Clone + InputLength + InputTake, + N: Copy + ToUsize, + F: Fn(I) -> IResult<I, N, E>, + E: ParseError<I>, +{ + move |i: I| { + let (i, length) = f(i)?; + + let length: usize = length.to_usize(); + + if i.input_len() < length { + Err(Err::Incomplete(Needed::Size(length))) + } else { + Ok(i.take_split(length)) + } + } +} + +/// Gets a number from the first parser, +/// takes a subslice of the input of that size, +/// then applies the second parser on that subslice. +/// If the second parser returns Incomplete, +/// length_value will return an error. +/// # Arguments +/// * `f` The parser to apply. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u16; +/// use nom::multi::length_value; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// length_value(be_u16, tag("abc"))(s) +/// } +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"efg"[..], &b"abc"[..]))); +/// assert_eq!(parser(b"\x00\x03123123"), Err(Err::Error((&b"123"[..], ErrorKind::Tag)))); +/// assert_eq!(parser(b"\x00\x03"), Err(Err::Incomplete(Size(3)))); +/// ``` +pub fn length_value<I, O, N, E, F, G>(f: F, g: G) -> impl Fn(I) -> IResult<I, O, E> +where + I: Clone + InputLength + InputTake, + N: Copy + ToUsize, + F: Fn(I) -> IResult<I, N, E>, + G: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + move |i: I| { + let (i, length) = f(i)?; + + let length: usize = length.to_usize(); + + if i.input_len() < length { + Err(Err::Incomplete(Needed::Size(length))) + } else { + let (rest, i) = i.take_split(length); + match g(i.clone()) { + Err(Err::Incomplete(_)) => + Err(Err::Error(E::from_error_kind(i, ErrorKind::Complete))), + Err(e) => Err(e), + Ok((_, o)) => Ok((rest,o)), + } + } + } +} + +#[doc(hidden)] +pub fn length_valuec<I, O, N, E, F, G>(i: I, f: F, g: G) -> IResult<I, O, E> +where + I: Clone + InputLength + InputTake, + N: Copy + ToUsize, + F: Fn(I) -> IResult<I, N, E>, + G: Fn(I) -> IResult<I, O, E>, + E: ParseError<I>, +{ + length_value(f, g)(i) +} diff --git a/third_party/rust/nom/src/number/complete.rs b/third_party/rust/nom/src/number/complete.rs new file mode 100644 index 0000000000..b8d423271c --- /dev/null +++ b/third_party/rust/nom/src/number/complete.rs @@ -0,0 +1,1198 @@ +//! parsers recognizing numbers, complete input version + +use crate::internal::*; +use crate::error::ParseError; +use crate::traits::{AsChar, InputIter, InputLength, InputTakeAtPosition}; +use crate::lib::std::ops::{RangeFrom, RangeTo}; +use crate::traits::{Offset, Slice}; +use crate::error::{ErrorKind, make_error}; +use crate::character::complete::{char, digit1}; +use crate::combinator::{opt, cut, map, recognize}; +use crate::branch::alt; +use crate::sequence::{tuple, pair}; + +/// Recognizes an unsigned 1 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u8; +/// +/// let parser = |s| { +/// be_u8(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_u8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> { + if i.len() < 1 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + Ok((&i[1..], i[0])) + } +} + +/// Recognizes a big endian unsigned 2 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u16; +/// +/// let parser = |s| { +/// be_u16(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0003))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_u16<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> { + if i.len() < 2 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[0] as u16) << 8) + i[1] as u16; + Ok((&i[2..], res)) + } +} + +/// Recognizes a big endian unsigned 3 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u24; +/// +/// let parser = |s| { +/// be_u24(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x000305))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_u24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 3 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[0] as u32) << 16) + ((i[1] as u32) << 8) + (i[2] as u32); + Ok((&i[3..], res)) + } +} + +/// Recognizes a big endian unsigned 4 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u32; +/// +/// let parser = |s| { +/// be_u32(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x00030507))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_u32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 4 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[0] as u32) << 24) + ((i[1] as u32) << 16) + ((i[2] as u32) << 8) + i[3] as u32; + Ok((&i[4..], res)) + } +} + +/// Recognizes a big endian unsigned 8 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u64; +/// +/// let parser = |s| { +/// be_u64(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_u64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> { + if i.len() < 8 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[0] as u64) << 56) + ((i[1] as u64) << 48) + ((i[2] as u64) << 40) + ((i[3] as u64) << 32) + ((i[4] as u64) << 24) + + ((i[5] as u64) << 16) + ((i[6] as u64) << 8) + i[7] as u64; + Ok((&i[8..], res)) + } +} + +/// Recognizes a big endian unsigned 16 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u128; +/// +/// let parser = |s| { +/// be_u128(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn be_u128<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> { + if i.len() < 16 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[0] as u128) << 120) + + ((i[1] as u128) << 112) + + ((i[2] as u128) << 104) + + ((i[3] as u128) << 96) + + ((i[4] as u128) << 88) + + ((i[5] as u128) << 80) + + ((i[6] as u128) << 72) + + ((i[7] as u128) << 64) + + ((i[8] as u128) << 56) + + ((i[9] as u128) << 48) + + ((i[10] as u128) << 40) + + ((i[11] as u128) << 32) + + ((i[12] as u128) << 24) + + ((i[13] as u128) << 16) + + ((i[14] as u128) << 8) + + i[15] as u128; + Ok((&i[16..], res)) + } +} + +/// Recognizes a signed 1 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i8; +/// +/// let parser = |s| { +/// be_i8(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_i8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> { + map!(i, be_u8, |x| x as i8) +} + +/// Recognizes a big endian signed 2 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i16; +/// +/// let parser = |s| { +/// be_i16(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0003))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> { + map!(i, be_u16, |x| x as i16) +} + +/// Recognizes a big endian signed 3 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i24; +/// +/// let parser = |s| { +/// be_i24(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x000305))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_i24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + // Same as the unsigned version but we need to sign-extend manually here + map!(i, be_u24, |x| if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + }) +} + +/// Recognizes a big endian signed 4 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i32; +/// +/// let parser = |s| { +/// be_i32(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x00030507))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_i32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + map!(i, be_u32, |x| x as i32) +} + +/// Recognizes a big endian signed 8 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i64; +/// +/// let parser = |s| { +/// be_i64(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_i64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> { + map!(i, be_u64, |x| x as i64) +} + +/// Recognizes a big endian signed 16 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i128; +/// +/// let parser = |s| { +/// be_i128(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn be_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> { + map!(i, be_u128, |x| x as i128) +} + +/// Recognizes an unsigned 1 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u8; +/// +/// let parser = |s| { +/// le_u8(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_u8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> { + if i.len() < 1 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + Ok((&i[1..], i[0])) + } +} + +/// Recognizes a little endian unsigned 2 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u16; +/// +/// let parser = |s| { +/// le_u16(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_u16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> { + if i.len() < 2 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[1] as u16) << 8) + i[0] as u16; + Ok((&i[2..], res)) + } +} + +/// Recognizes a little endian unsigned 3 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u24; +/// +/// let parser = |s| { +/// le_u24(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x050300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_u24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 3 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = (i[0] as u32) + ((i[1] as u32) << 8) + ((i[2] as u32) << 16); + Ok((&i[3..], res)) + } +} + +/// Recognizes a little endian unsigned 4 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u32; +/// +/// let parser = |s| { +/// le_u32(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x07050300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_u32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 4 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[3] as u32) << 24) + ((i[2] as u32) << 16) + ((i[1] as u32) << 8) + i[0] as u32; + Ok((&i[4..], res)) + } +} + +/// Recognizes a little endian unsigned 8 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u64; +/// +/// let parser = |s| { +/// le_u64(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_u64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> { + if i.len() < 8 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[7] as u64) << 56) + ((i[6] as u64) << 48) + ((i[5] as u64) << 40) + ((i[4] as u64) << 32) + ((i[3] as u64) << 24) + + ((i[2] as u64) << 16) + ((i[1] as u64) << 8) + i[0] as u64; + Ok((&i[8..], res)) + } +} + +/// Recognizes a little endian unsigned 16 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u128; +/// +/// let parser = |s| { +/// le_u128(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn le_u128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> { + if i.len() < 16 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[15] as u128) << 120) + + ((i[14] as u128) << 112) + + ((i[13] as u128) << 104) + + ((i[12] as u128) << 96) + + ((i[11] as u128) << 88) + + ((i[10] as u128) << 80) + + ((i[9] as u128) << 72) + + ((i[8] as u128) << 64) + + ((i[7] as u128) << 56) + + ((i[6] as u128) << 48) + + ((i[5] as u128) << 40) + + ((i[4] as u128) << 32) + + ((i[3] as u128) << 24) + + ((i[2] as u128) << 16) + + ((i[1] as u128) << 8) + + i[0] as u128; + Ok((&i[16..], res)) + } +} + +/// Recognizes a signed 1 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i8; +/// +/// let parser = |s| { +/// le_i8(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_i8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> { + map!(i, le_u8, |x| x as i8) +} + +/// Recognizes a little endian signed 2 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i16; +/// +/// let parser = |s| { +/// le_i16(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> { + map!(i, le_u16, |x| x as i16) +} + +/// Recognizes a little endian signed 3 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i24; +/// +/// let parser = |s| { +/// le_i24(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x050300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_i24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + // Same as the unsigned version but we need to sign-extend manually here + map!(i, le_u24, |x| if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + }) +} + +/// Recognizes a little endian signed 4 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i32; +/// +/// let parser = |s| { +/// le_i32(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x07050300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_i32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + map!(i, le_u32, |x| x as i32) +} + +/// Recognizes a little endian signed 8 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i64; +/// +/// let parser = |s| { +/// le_i64(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_i64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> { + map!(i, le_u64, |x| x as i64) +} + +/// Recognizes a little endian signed 16 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i128; +/// +/// let parser = |s| { +/// le_i128(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn le_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> { + map!(i, le_u128, |x| x as i128) +} + +/// Recognizes a big endian 4 bytes floating point number +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_f32; +/// +/// let parser = |s| { +/// be_f32(s) +/// }; +/// +/// assert_eq!(parser(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> { + match be_u32(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f32::from_bits(o))), + } +} + +/// Recognizes a big endian 8 bytes floating point number +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_f64; +/// +/// let parser = |s| { +/// be_f64(s) +/// }; +/// +/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> { + match be_u64(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f64::from_bits(o))), + } +} + +/// Recognizes a little endian 4 bytes floating point number +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_f32; +/// +/// let parser = |s| { +/// le_f32(s) +/// }; +/// +/// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> { + match le_u32(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f32::from_bits(o))), + } +} + +/// Recognizes a little endian 8 bytes floating point number +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_f64; +/// +/// let parser = |s| { +/// le_f64(s) +/// }; +/// +/// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> { + match le_u64(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f64::from_bits(o))), + } +} + +/// Recognizes a hex-encoded integer +/// +/// *complete version*: will parse until the end of input if it has less than 8 bytes +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::hex_u32; +/// +/// let parser = |s| { +/// hex_u32(s) +/// }; +/// +/// assert_eq!(parser(b"01AE"), Ok((&b""[..], 0x01AE))); +/// assert_eq!(parser(b"abc"), Ok((&b""[..], 0x0ABC))); +/// assert_eq!(parser(b"ggg"), Err(Err::Error((&b"ggg"[..], ErrorKind::IsA)))); +/// ``` +#[inline] +pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], u32, E> { + let (i, o) = crate::bytes::complete::is_a(&b"0123456789abcdefABCDEF"[..])(input)?; + // Do not parse more than 8 characters for a u32 + let (parsed, remaining) = if o.len() <= 8 { + (o, i) + } else { + (&input[..8], &input[8..]) + }; + + let res = parsed + .iter() + .rev() + .enumerate() + .map(|(k, &v)| { + let digit = v as char; + digit.to_digit(16).unwrap_or(0) << (k * 4) + }) + .sum(); + + Ok((remaining, res)) +} + +/// Recognizes floating point number in a byte string and returns the corresponding slice +/// +/// *complete version*: can parse until the end of input +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::recognize_float; +/// +/// let parser = |s| { +/// recognize_float(s) +/// }; +/// +/// assert_eq!(parser("11e-1"), Ok(("", "11e-1"))); +/// assert_eq!(parser("123E-02"), Ok(("", "123E-02"))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", "123"))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[allow(unused_imports)] +#[cfg_attr(rustfmt, rustfmt_skip)] +pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: Clone + Offset, + T: InputIter, + <T as InputIter>::Item: AsChar, + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar, +{ + recognize( + tuple(( + opt(alt((char('+'), char('-')))), + alt(( + map(tuple((digit1, opt(pair(char('.'), opt(digit1))))), |_| ()), + map(tuple((char('.'), digit1)), |_| ()) + )), + opt(tuple(( + alt((char('e'), char('E'))), + opt(alt((char('+'), char('-')))), + cut(digit1) + ))) + )) + )(input) +} + +/// Recognizes floating point number in a byte string and returns a f32 +/// +/// *complete version*: can parse until the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::float; +/// +/// let parser = |s| { +/// float(s) +/// }; +/// +/// assert_eq!(parser("11e-1"), Ok(("", 1.1))); +/// assert_eq!(parser("123E-02"), Ok(("", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[cfg(not(feature = "lexical"))] +pub fn float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E> +where + T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: Clone + Offset, + T: InputIter + InputLength + crate::traits::ParseTo<f32>, + <T as InputIter>::Item: AsChar, + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar +{ + match recognize_float(input) { + Err(e) => Err(e), + Ok((i, s)) => match s.parse_to() { + Some(n) => Ok((i, n)), + None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float))) + } + } +} + +/// Recognizes floating point number in a byte string and returns a f32 +/// +/// *complete version*: can parse until the end of input +/// +/// this function uses the lexical-core crate for float parsing by default, you +/// can deactivate it by removing the "lexical" feature +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::float; +/// +/// let parser = |s| { +/// float(s) +/// }; +/// +/// assert_eq!(parser("1.1"), Ok(("", 1.1))); +/// assert_eq!(parser("123E-02"), Ok(("", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float)))); +/// ``` +#[cfg(feature = "lexical")] +pub fn float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E> +where + T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>, +{ + match ::lexical_core::parse_partial(input.as_bytes()) { + Ok((value, processed)) => Ok((input.slice(processed..), value)), + Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float))) + } +} + +/// Recognizes floating point number in a byte string and returns a f64 +/// +/// *complete version*: can parse until the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::double; +/// +/// let parser = |s| { +/// double(s) +/// }; +/// +/// assert_eq!(parser("11e-1"), Ok(("", 1.1))); +/// assert_eq!(parser("123E-02"), Ok(("", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[cfg(not(feature = "lexical"))] +pub fn double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E> +where + T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: Clone + Offset, + T: InputIter + InputLength + crate::traits::ParseTo<f64>, + <T as InputIter>::Item: AsChar, + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar +{ + match recognize_float(input) { + Err(e) => Err(e), + Ok((i, s)) => match s.parse_to() { + Some(n) => Ok((i, n)), + None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float))) + } + } +} + +/// Recognizes floating point number in a byte string and returns a f64 +/// +/// *complete version*: can parse until the end of input +/// +/// this function uses the lexical-core crate for float parsing by default, you +/// can deactivate it by removing the "lexical" feature +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::double; +/// +/// let parser = |s| { +/// double(s) +/// }; +/// +/// assert_eq!(parser("1.1"), Ok(("", 1.1))); +/// assert_eq!(parser("123E-02"), Ok(("", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float)))); +/// ``` +#[cfg(feature = "lexical")] +pub fn double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E> +where + T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>, +{ + match ::lexical_core::parse_partial(input.as_bytes()) { + Ok((value, processed)) => Ok((input.slice(processed..), value)), + Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float))) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::internal::Err; + use crate::error::ErrorKind; + + macro_rules! assert_parse( + ($left: expr, $right: expr) => { + let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; + assert_eq!(res, $right); + }; + ); + + #[test] + fn i8_tests() { + assert_parse!(be_i8(&[0x00]), Ok((&b""[..], 0))); + assert_parse!(be_i8(&[0x7f]), Ok((&b""[..], 127))); + assert_parse!(be_i8(&[0xff]), Ok((&b""[..], -1))); + assert_parse!(be_i8(&[0x80]), Ok((&b""[..], -128))); + } + + #[test] + fn i16_tests() { + assert_parse!(be_i16(&[0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(be_i16(&[0x7f, 0xff]), Ok((&b""[..], 32_767_i16))); + assert_parse!(be_i16(&[0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!(be_i16(&[0x80, 0x00]), Ok((&b""[..], -32_768_i16))); + } + + #[test] + fn u24_tests() { + assert_parse!(be_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(be_u24(&[0x00, 0xFF, 0xFF]), Ok((&b""[..], 65_535_u32))); + assert_parse!(be_u24(&[0x12, 0x34, 0x56]), Ok((&b""[..], 1_193_046_u32))); + } + + #[test] + fn i24_tests() { + assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32))); + assert_parse!(be_i24(&[0xFF, 0x00, 0x00]), Ok((&b""[..], -65_536_i32))); + assert_parse!(be_i24(&[0xED, 0xCB, 0xAA]), Ok((&b""[..], -1_193_046_i32))); + } + + #[test] + fn i32_tests() { + assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!( + be_i32(&[0x7f, 0xff, 0xff, 0xff]), + Ok((&b""[..], 2_147_483_647_i32)) + ); + assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!( + be_i32(&[0x80, 0x00, 0x00, 0x00]), + Ok((&b""[..], -2_147_483_648_i32)) + ); + } + + #[test] + fn i64_tests() { + assert_parse!( + be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], 9_223_372_036_854_775_807_i64)) + ); + assert_parse!( + be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], -9_223_372_036_854_775_808_i64)) + ); + } + + #[test] + #[cfg(stable_i128)] + fn i128_tests() { + assert_parse!( + be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + be_i128(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128)) + ); + assert_parse!( + be_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + be_i128(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128)) + ); + } + + #[test] + fn le_i8_tests() { + assert_parse!(le_i8(&[0x00]), Ok((&b""[..], 0))); + assert_parse!(le_i8(&[0x7f]), Ok((&b""[..], 127))); + assert_parse!(le_i8(&[0xff]), Ok((&b""[..], -1))); + assert_parse!(le_i8(&[0x80]), Ok((&b""[..], -128))); + } + + #[test] + fn le_i16_tests() { + assert_parse!(le_i16(&[0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(le_i16(&[0xff, 0x7f]), Ok((&b""[..], 32_767_i16))); + assert_parse!(le_i16(&[0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!(le_i16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16))); + } + + #[test] + fn le_u24_tests() { + assert_parse!(le_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(le_u24(&[0xFF, 0xFF, 0x00]), Ok((&b""[..], 65_535_u32))); + assert_parse!(le_u24(&[0x56, 0x34, 0x12]), Ok((&b""[..], 1_193_046_u32))); + } + + #[test] + fn le_i24_tests() { + assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32))); + assert_parse!(le_i24(&[0x00, 0x00, 0xFF]), Ok((&b""[..], -65_536_i32))); + assert_parse!(le_i24(&[0xAA, 0xCB, 0xED]), Ok((&b""[..], -1_193_046_i32))); + } + + #[test] + fn le_i32_tests() { + assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!( + le_i32(&[0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 2_147_483_647_i32)) + ); + assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!( + le_i32(&[0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -2_147_483_648_i32)) + ); + } + + #[test] + fn le_i64_tests() { + assert_parse!( + le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 9_223_372_036_854_775_807_i64)) + ); + assert_parse!( + le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -9_223_372_036_854_775_808_i64)) + ); + } + + #[test] + #[cfg(stable_i128)] + fn le_i128_tests() { + assert_parse!( + le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128)) + ); + assert_parse!( + le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128)) + ); + } + + #[test] + fn be_f32_tests() { + assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32))); + assert_parse!( + be_f32(&[0x4d, 0x31, 0x1f, 0xd8]), + Ok((&b""[..], 185_728_392_f32)) + ); + } + + #[test] + fn be_f64_tests() { + assert_parse!( + be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0_f64)) + ); + assert_parse!( + be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00]), + Ok((&b""[..], 185_728_392_f64)) + ); + } + + #[test] + fn le_f32_tests() { + assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32))); + assert_parse!( + le_f32(&[0xd8, 0x1f, 0x31, 0x4d]), + Ok((&b""[..], 185_728_392_f32)) + ); + } + + #[test] + fn le_f64_tests() { + assert_parse!( + le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0_f64)) + ); + assert_parse!( + le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41]), + Ok((&b""[..], 185_728_392_f64)) + ); + } + + #[test] + fn hex_u32_tests() { + assert_parse!( + hex_u32(&b";"[..]), + Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA))) + ); + assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255))); + assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138))); + assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058))); + assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058))); + assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347))); + assert_parse!( + hex_u32(&b"c5a31be201;"[..]), + Ok((&b"01;"[..], 3_315_801_058)) + ); + assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295))); + assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0))); + assert_parse!(hex_u32(&b"12af"[..]), Ok((&b""[..], 0x12af))); + } + + #[test] + #[cfg(feature = "std")] + fn float_test() { + let mut test_cases = vec![ + "+3.14", + "3.14", + "-3.14", + "0", + "0.0", + "1.", + ".789", + "-.5", + "1e7", + "-1E-7", + ".3e-2", + "1.e4", + "1.2e4", + "12.34", + "-1.234E-12", + "-1.234e-12", + ]; + + for test in test_cases.drain(..) { + let expected32 = str::parse::<f32>(test).unwrap(); + let expected64 = str::parse::<f64>(test).unwrap(); + + println!("now parsing: {} -> {}", test, expected32); + + let larger = format!("{}", test); + assert_parse!(recognize_float(&larger[..]), Ok(("", test))); + + assert_parse!(float(larger.as_bytes()), Ok((&b""[..], expected32))); + assert_parse!(float(&larger[..]), Ok(("", expected32))); + + assert_parse!(double(larger.as_bytes()), Ok((&b""[..], expected64))); + assert_parse!(double(&larger[..]), Ok(("", expected64))); + } + + let remaining_exponent = "-1.234E-"; + assert_parse!( + recognize_float(remaining_exponent), + Err(Err::Failure(("", ErrorKind::Digit))) + ); + } + +} diff --git a/third_party/rust/nom/src/number/macros.rs b/third_party/rust/nom/src/number/macros.rs new file mode 100644 index 0000000000..cb8c2a9470 --- /dev/null +++ b/third_party/rust/nom/src/number/macros.rs @@ -0,0 +1,265 @@ +//! parsers recognizing numbers + +/// if the parameter is nom::Endianness::Big, parse a big endian u16 integer, +/// otherwise a little endian u16 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be<u16>, u16!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// +/// named!(le<u16>, u16!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! u16 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_u16($i) } else { $crate::number::streaming::le_u16($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian u32 integer, +/// otherwise a little endian u32 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be<u32>, u32!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// +/// named!(le<u32>, u32!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! u32 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_u32($i) } else { $crate::number::streaming::le_u32($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian u64 integer, +/// otherwise a little endian u64 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be<u64>, u64!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// +/// named!(le<u64>, u64!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! u64 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_u64($i) } else { $crate::number::streaming::le_u64($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian u128 integer, +/// otherwise a little endian u128 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be<u128>, u128!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// +/// named!(le<u128>, u128!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +#[cfg(stable_i128)] +macro_rules! u128 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_u128($i) } else { $crate::number::streaming::le_u128($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian i16 integer, +/// otherwise a little endian i16 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be<i16>, i16!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// +/// named!(le<i16>, i16!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! i16 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_i16($i) } else { $crate::number::streaming::le_i16($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian i32 integer, +/// otherwise a little endian i32 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be<i32>, i32!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// +/// named!(le<i32>, i32!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! i32 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_i32($i) } else { $crate::number::streaming::le_i32($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian i64 integer, +/// otherwise a little endian i64 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be<i64>, i64!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// +/// named!(le<i64>, i64!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! i64 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_i64($i) } else { $crate::number::streaming::le_i64($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian i64 integer, +/// otherwise a little endian i64 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be<i128>, i128!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// +/// named!(le<i128>, i128!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +#[cfg(stable_i128)] +macro_rules! i128 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_i128($i) } else { $crate::number::streaming::le_i128($i) } } );); + +#[cfg(test)] +mod tests { + use crate::number::Endianness; + + #[test] + fn configurable_endianness() { + named!(be_tst16<u16>, u16!(Endianness::Big)); + named!(le_tst16<u16>, u16!(Endianness::Little)); + assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16))); + assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16))); + + named!(be_tst32<u32>, u32!(Endianness::Big)); + named!(le_tst32<u32>, u32!(Endianness::Little)); + assert_eq!( + be_tst32(&[0x12, 0x00, 0x60, 0x00]), + Ok((&b""[..], 302_014_464_u32)) + ); + assert_eq!( + le_tst32(&[0x12, 0x00, 0x60, 0x00]), + Ok((&b""[..], 6_291_474_u32)) + ); + + named!(be_tst64<u64>, u64!(Endianness::Big)); + named!(le_tst64<u64>, u64!(Endianness::Little)); + assert_eq!( + be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), + Ok((&b""[..], 1_297_142_246_100_992_000_u64)) + ); + assert_eq!( + le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), + Ok((&b""[..], 36_028_874_334_666_770_u64)) + ); + + named!(be_tsti16<i16>, i16!(Endianness::Big)); + named!(le_tsti16<i16>, i16!(Endianness::Little)); + assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16))); + assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16))); + + named!(be_tsti32<i32>, i32!(Endianness::Big)); + named!(le_tsti32<i32>, i32!(Endianness::Little)); + assert_eq!( + be_tsti32(&[0x00, 0x12, 0x60, 0x00]), + Ok((&b""[..], 1_204_224_i32)) + ); + assert_eq!( + le_tsti32(&[0x00, 0x12, 0x60, 0x00]), + Ok((&b""[..], 6_296_064_i32)) + ); + + named!(be_tsti64<i64>, i64!(Endianness::Big)); + named!(le_tsti64<i64>, i64!(Endianness::Little)); + assert_eq!( + be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), + Ok((&b""[..], 71_881_672_479_506_432_i64)) + ); + assert_eq!( + le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), + Ok((&b""[..], 36_028_874_334_732_032_i64)) + ); + } + + //FIXME + /* + #[test] + #[cfg(feature = "std")] + fn manual_configurable_endianness_test() { + let x = 1; + let int_parse: Box<Fn(&[u8]) -> IResult<&[u8], u16, (&[u8], ErrorKind)>> = if x == 2 { + Box::new(be_u16) + } else { + Box::new(le_u16) + }; + println!("{:?}", int_parse(&b"3"[..])); + assert_eq!(int_parse(&[0x80, 0x00]), Ok((&b""[..], 128_u16))); + } + */ +} diff --git a/third_party/rust/nom/src/number/mod.rs b/third_party/rust/nom/src/number/mod.rs new file mode 100644 index 0000000000..9be4c38a45 --- /dev/null +++ b/third_party/rust/nom/src/number/mod.rs @@ -0,0 +1,17 @@ +//! parsers recognizing numbers + +#[macro_use] +mod macros; + +pub mod streaming; +pub mod complete; + +/// Configurable endianness +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Endianness { + /// big endian + Big, + /// little endian + Little, +} + diff --git a/third_party/rust/nom/src/number/streaming.rs b/third_party/rust/nom/src/number/streaming.rs new file mode 100644 index 0000000000..15a89a4976 --- /dev/null +++ b/third_party/rust/nom/src/number/streaming.rs @@ -0,0 +1,1207 @@ +//! parsers recognizing numbers, streaming version + +use crate::internal::*; +use crate::error::{ErrorKind, ParseError}; +use crate::traits::{AsChar, InputIter, InputLength, InputTakeAtPosition}; +use crate::lib::std::ops::{RangeFrom, RangeTo}; +use crate::traits::{Offset, Slice}; +use crate::character::streaming::{char, digit1}; +use crate::sequence::{pair, tuple}; +use crate::combinator::{cut, map, opt, recognize}; +use crate::branch::alt; + +/// Recognizes an unsigned 1 byte integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u8; +/// +/// let parser = be_u8::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +#[inline] +pub fn be_u8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> { + if i.len() < 1 { + Err(Err::Incomplete(Needed::Size(1))) + } else { + Ok((&i[1..], i[0])) + } +} + +/// Recognizes a big endian unsigned 2 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u16; +/// +/// let parser = |s| { +/// be_u16::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// ``` +#[inline] +pub fn be_u16<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> { + if i.len() < 2 { + Err(Err::Incomplete(Needed::Size(2))) + } else { + let res = ((i[0] as u16) << 8) + i[1] as u16; + Ok((&i[2..], res)) + } +} + +/// Recognizes a big endian unsigned 3 byte integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u24; +/// +/// let parser = |s| { +/// be_u24::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x000102))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3)))); +/// ``` +#[inline] +pub fn be_u24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 3 { + Err(Err::Incomplete(Needed::Size(3))) + } else { + let res = ((i[0] as u32) << 16) + ((i[1] as u32) << 8) + (i[2] as u32); + Ok((&i[3..], res)) + } +} + +/// Recognizes a big endian unsigned 4 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u32; +/// +/// let parser = |s| { +/// be_u32::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn be_u32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 4 { + Err(Err::Incomplete(Needed::Size(4))) + } else { + let res = ((i[0] as u32) << 24) + ((i[1] as u32) << 16) + ((i[2] as u32) << 8) + i[3] as u32; + Ok((&i[4..], res)) + } +} + +/// Recognizes a big endian unsigned 8 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u64; +/// +/// let parser = |s| { +/// be_u64::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn be_u64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> { + if i.len() < 8 { + Err(Err::Incomplete(Needed::Size(8))) + } else { + let res = ((i[0] as u64) << 56) + ((i[1] as u64) << 48) + ((i[2] as u64) << 40) + ((i[3] as u64) << 32) + ((i[4] as u64) << 24) + + ((i[5] as u64) << 16) + ((i[6] as u64) << 8) + i[7] as u64; + Ok((&i[8..], res)) + } +} + +/// Recognizes a big endian unsigned 16 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u128; +/// +/// let parser = |s| { +/// be_u128::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn be_u128<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> { + if i.len() < 16 { + Err(Err::Incomplete(Needed::Size(16))) + } else { + let res = ((i[0] as u128) << 120) + + ((i[1] as u128) << 112) + + ((i[2] as u128) << 104) + + ((i[3] as u128) << 96) + + ((i[4] as u128) << 88) + + ((i[5] as u128) << 80) + + ((i[6] as u128) << 72) + + ((i[7] as u128) << 64) + + ((i[8] as u128) << 56) + + ((i[9] as u128) << 48) + + ((i[10] as u128) << 40) + + ((i[11] as u128) << 32) + + ((i[12] as u128) << 24) + + ((i[13] as u128) << 16) + + ((i[14] as u128) << 8) + + i[15] as u128; + Ok((&i[16..], res)) + } +} + +/// Recognizes a signed 1 byte integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i8; +/// +/// let parser = be_i8::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +#[inline] +pub fn be_i8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> { + map!(i, be_u8, |x| x as i8) +} + +/// Recognizes a big endian signed 2 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i16; +/// +/// let parser = be_i16::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(2)))); +/// ``` +#[inline] +pub fn be_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> { + map!(i, be_u16, |x| x as i16) +} + +/// Recognizes a big endian signed 3 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i24; +/// +/// let parser = be_i24::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x000102))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(3)))); +/// ``` +#[inline] +pub fn be_i24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + // Same as the unsigned version but we need to sign-extend manually here + map!(i, be_u24, |x| if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + }) +} + +/// Recognizes a big endian signed 4 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i32; +/// +/// let parser = be_i32::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn be_i32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + map!(i, be_u32, |x| x as i32) +} + +/// Recognizes a big endian signed 8 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i64; +/// +/// let parser = be_i64::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn be_i64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> { + map!(i, be_u64, |x| x as i64) +} + +/// Recognizes a big endian signed 16 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i128; +/// +/// let parser = be_i128::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn be_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> { + map!(i, be_u128, |x| x as i128) +} + +/// Recognizes an unsigned 1 byte integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u8; +/// +/// let parser = le_u8::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +#[inline] +pub fn le_u8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> { + if i.len() < 1 { + Err(Err::Incomplete(Needed::Size(1))) + } else { + Ok((&i[1..], i[0])) + } +} + +/// Recognizes a little endian unsigned 2 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u16; +/// +/// let parser = |s| { +/// le_u16::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// ``` +#[inline] +pub fn le_u16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> { + if i.len() < 2 { + Err(Err::Incomplete(Needed::Size(2))) + } else { + let res = ((i[1] as u16) << 8) + i[0] as u16; + Ok((&i[2..], res)) + } +} + +/// Recognizes a little endian unsigned 3 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u24; +/// +/// let parser = |s| { +/// le_u24::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3)))); +/// ``` +#[inline] +pub fn le_u24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 3 { + Err(Err::Incomplete(Needed::Size(3))) + } else { + let res = (i[0] as u32) + ((i[1] as u32) << 8) + ((i[2] as u32) << 16); + Ok((&i[3..], res)) + } +} + +/// Recognizes a little endian unsigned 4 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u32; +/// +/// let parser = |s| { +/// le_u32::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn le_u32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 4 { + Err(Err::Incomplete(Needed::Size(4))) + } else { + let res = ((i[3] as u32) << 24) + ((i[2] as u32) << 16) + ((i[1] as u32) << 8) + i[0] as u32; + Ok((&i[4..], res)) + } +} + +/// Recognizes a little endian unsigned 8 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u64; +/// +/// let parser = |s| { +/// le_u64::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn le_u64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> { + if i.len() < 8 { + Err(Err::Incomplete(Needed::Size(8))) + } else { + let res = ((i[7] as u64) << 56) + ((i[6] as u64) << 48) + ((i[5] as u64) << 40) + ((i[4] as u64) << 32) + ((i[3] as u64) << 24) + + ((i[2] as u64) << 16) + ((i[1] as u64) << 8) + i[0] as u64; + Ok((&i[8..], res)) + } +} + +/// Recognizes a little endian unsigned 16 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u128; +/// +/// let parser = |s| { +/// le_u128::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn le_u128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> { + if i.len() < 16 { + Err(Err::Incomplete(Needed::Size(16))) + } else { + let res = ((i[15] as u128) << 120) + + ((i[14] as u128) << 112) + + ((i[13] as u128) << 104) + + ((i[12] as u128) << 96) + + ((i[11] as u128) << 88) + + ((i[10] as u128) << 80) + + ((i[9] as u128) << 72) + + ((i[8] as u128) << 64) + + ((i[7] as u128) << 56) + + ((i[6] as u128) << 48) + + ((i[5] as u128) << 40) + + ((i[4] as u128) << 32) + + ((i[3] as u128) << 24) + + ((i[2] as u128) << 16) + + ((i[1] as u128) << 8) + + i[0] as u128; + Ok((&i[16..], res)) + } +} + +/// Recognizes a signed 1 byte integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i8; +/// +/// let parser = le_i8::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +#[inline] +pub fn le_i8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> { + map!(i, le_u8, |x| x as i8) +} + +/// Recognizes a little endian signed 2 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i16; +/// +/// let parser = |s| { +/// le_i16::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// ``` +#[inline] +pub fn le_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> { + map!(i, le_u16, |x| x as i16) +} + +/// Recognizes a little endian signed 3 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i24; +/// +/// let parser = |s| { +/// le_i24::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3)))); +/// ``` +#[inline] +pub fn le_i24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + // Same as the unsigned version but we need to sign-extend manually here + map!(i, le_u24, |x| if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + }) +} + +/// Recognizes a little endian signed 4 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i32; +/// +/// let parser = |s| { +/// le_i32::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn le_i32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + map!(i, le_u32, |x| x as i32) +} + +/// Recognizes a little endian signed 8 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i64; +/// +/// let parser = |s| { +/// le_i64::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn le_i64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> { + map!(i, le_u64, |x| x as i64) +} + +/// Recognizes a little endian signed 16 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i128; +/// +/// let parser = |s| { +/// le_i128::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn le_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> { + map!(i, le_u128, |x| x as i128) +} + +/// Recognizes a big endian 4 bytes floating point number +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_f32; +/// +/// let parser = |s| { +/// be_f32::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00][..]), Ok((&b""[..], 2.640625))); +/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn be_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> { + match be_u32(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f32::from_bits(o))), + } +} + +/// Recognizes a big endian 8 bytes floating point number +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_f64; +/// +/// let parser = |s| { +/// be_f64::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn be_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> { + match be_u64(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f64::from_bits(o))), + } +} + +/// Recognizes a little endian 4 bytes floating point number +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_f32; +/// +/// let parser = |s| { +/// le_f32::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn le_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> { + match le_u32(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f32::from_bits(o))), + } +} + +/// Recognizes a little endian 8 bytes floating point number +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_f64; +/// +/// let parser = |s| { +/// le_f64::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 3145728.0))); +/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn le_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> { + match le_u64(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f64::from_bits(o))), + } +} + +/// Recognizes a hex-encoded integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::hex_u32; +/// +/// let parser = |s| { +/// hex_u32(s) +/// }; +/// +/// assert_eq!(parser(b"01AE;"), Ok((&b";"[..], 0x01AE))); +/// assert_eq!(parser(b"abc"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(parser(b"ggg"), Err(Err::Error((&b"ggg"[..], ErrorKind::IsA)))); +/// ``` +#[inline] +pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], u32, E> { + let (i, o) = crate::bytes::streaming::is_a(&b"0123456789abcdefABCDEF"[..])(input)?; + + // Do not parse more than 8 characters for a u32 + let (parsed, remaining) = if o.len() <= 8 { + (o, i) + } else { + (&input[..8], &input[8..]) + }; + + let res = parsed + .iter() + .rev() + .enumerate() + .map(|(k, &v)| { + let digit = v as char; + digit.to_digit(16).unwrap_or(0) << (k * 4) + }) + .sum(); + + Ok((remaining, res)) +} + +/// Recognizes a floating point number in text format and returns the corresponding part of the input +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::recognize_float; +/// +/// let parser = |s| { +/// recognize_float(s) +/// }; +/// +/// assert_eq!(parser("11e-1;"), Ok((";", "11e-1"))); +/// assert_eq!(parser("123E-02;"), Ok((";", "123E-02"))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", "123"))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[allow(unused_imports)] +#[cfg_attr(rustfmt, rustfmt_skip)] +pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E> +where + T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: Clone + Offset, + T: InputIter, + <T as InputIter>::Item: AsChar, + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar +{ + recognize( + tuple(( + opt(alt((char('+'), char('-')))), + alt(( + map(tuple((digit1, opt(pair(char('.'), opt(digit1))))), |_| ()), + map(tuple((char('.'), digit1)), |_| ()) + )), + opt(tuple(( + alt((char('e'), char('E'))), + opt(alt((char('+'), char('-')))), + cut(digit1) + ))) + )) + )(input) +} + +/// Recognizes floating point number in a byte string and returns a f32 +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::float; +/// +/// let parser = |s| { +/// float(s) +/// }; +/// +/// assert_eq!(parser("11e-1;"), Ok((";", 1.1))); +/// assert_eq!(parser("123E-02;"), Ok((";", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[cfg(not(feature = "lexical"))] +pub fn float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E> +where + T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: Clone + Offset, + T: InputIter + InputLength + crate::traits::ParseTo<f32>, + <T as InputIter>::Item: AsChar, + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar +{ + match recognize_float(input) { + Err(e) => Err(e), + Ok((i, s)) => match s.parse_to() { + Some(n) => Ok((i, n)), + None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float))) + } + } +} + +/// Recognizes floating point number in a byte string and returns a f32 +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::float; +/// +/// let parser = |s| { +/// float(s) +/// }; +/// +/// assert_eq!(parser("11e-1;"), Ok((";", 1.1))); +/// assert_eq!(parser("123E-02;"), Ok((";", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float)))); +/// ``` +/// +/// this function uses the lexical-core crate for float parsing by default, you +/// can deactivate it by removing the "lexical" feature +#[cfg(feature = "lexical")] +pub fn float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E> +where + T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>, +{ + match ::lexical_core::parse_partial(input.as_bytes()) { + Ok((value, processed)) => { + if processed == input.input_len() { + Err(Err::Incomplete(Needed::Unknown)) + } else { + Ok((input.slice(processed..), value)) + } + }, + Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float))) + } +} + +/// Recognizes floating point number in a byte string and returns a f64 +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::double; +/// +/// let parser = |s| { +/// double(s) +/// }; +/// +/// assert_eq!(parser("11e-1;"), Ok((";", 1.1))); +/// assert_eq!(parser("123E-02;"), Ok((";", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[cfg(not(feature = "lexical"))] +pub fn double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E> +where + T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, + T: Clone + Offset, + T: InputIter + InputLength + crate::traits::ParseTo<f64>, + <T as InputIter>::Item: AsChar, + T: InputTakeAtPosition, + <T as InputTakeAtPosition>::Item: AsChar +{ + match recognize_float(input) { + Err(e) => Err(e), + Ok((i, s)) => match s.parse_to() { + Some(n) => Ok((i, n)), + None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float))) + } + } +} + +/// Recognizes floating point number in a byte string and returns a f64 +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::double; +/// +/// let parser = |s| { +/// double(s) +/// }; +/// +/// assert_eq!(parser("11e-1;"), Ok((";", 1.1))); +/// assert_eq!(parser("123E-02;"), Ok((";", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float)))); +/// ``` +/// +/// this function uses the lexical-core crate for float parsing by default, you +/// can deactivate it by removing the "lexical" feature +#[cfg(feature = "lexical")] +pub fn double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E> +where + T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>, +{ + match ::lexical_core::parse_partial(input.as_bytes()) { + Ok((value, processed)) => { + if processed == input.input_len() { + Err(Err::Incomplete(Needed::Unknown)) + } else { + Ok((input.slice(processed..), value)) + } + }, + Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float))) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::internal::{Err, Needed}; + use crate::error::ErrorKind; + + macro_rules! assert_parse( + ($left: expr, $right: expr) => { + let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; + assert_eq!(res, $right); + }; + ); + + #[test] + fn i8_tests() { + assert_parse!(be_i8(&[0x00]), Ok((&b""[..], 0))); + assert_parse!(be_i8(&[0x7f]), Ok((&b""[..], 127))); + assert_parse!(be_i8(&[0xff]), Ok((&b""[..], -1))); + assert_parse!(be_i8(&[0x80]), Ok((&b""[..], -128))); + } + + #[test] + fn i16_tests() { + assert_parse!(be_i16(&[0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(be_i16(&[0x7f, 0xff]), Ok((&b""[..], 32_767_i16))); + assert_parse!(be_i16(&[0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!(be_i16(&[0x80, 0x00]), Ok((&b""[..], -32_768_i16))); + } + + #[test] + fn u24_tests() { + assert_parse!(be_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(be_u24(&[0x00, 0xFF, 0xFF]), Ok((&b""[..], 65_535_u32))); + assert_parse!(be_u24(&[0x12, 0x34, 0x56]), Ok((&b""[..], 1_193_046_u32))); + } + + #[test] + fn i24_tests() { + assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32))); + assert_parse!(be_i24(&[0xFF, 0x00, 0x00]), Ok((&b""[..], -65_536_i32))); + assert_parse!(be_i24(&[0xED, 0xCB, 0xAA]), Ok((&b""[..], -1_193_046_i32))); + } + + #[test] + fn i32_tests() { + assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!( + be_i32(&[0x7f, 0xff, 0xff, 0xff]), + Ok((&b""[..], 2_147_483_647_i32)) + ); + assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!( + be_i32(&[0x80, 0x00, 0x00, 0x00]), + Ok((&b""[..], -2_147_483_648_i32)) + ); + } + + #[test] + fn i64_tests() { + assert_parse!( + be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], 9_223_372_036_854_775_807_i64)) + ); + assert_parse!( + be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], -9_223_372_036_854_775_808_i64)) + ); + } + + #[test] + #[cfg(stable_i128)] + fn i128_tests() { + assert_parse!( + be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + be_i128(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128)) + ); + assert_parse!( + be_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + be_i128(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128)) + ); + } + + #[test] + fn le_i8_tests() { + assert_parse!(le_i8(&[0x00]), Ok((&b""[..], 0))); + assert_parse!(le_i8(&[0x7f]), Ok((&b""[..], 127))); + assert_parse!(le_i8(&[0xff]), Ok((&b""[..], -1))); + assert_parse!(le_i8(&[0x80]), Ok((&b""[..], -128))); + } + + #[test] + fn le_i16_tests() { + assert_parse!(le_i16(&[0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(le_i16(&[0xff, 0x7f]), Ok((&b""[..], 32_767_i16))); + assert_parse!(le_i16(&[0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!(le_i16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16))); + } + + #[test] + fn le_u24_tests() { + assert_parse!(le_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(le_u24(&[0xFF, 0xFF, 0x00]), Ok((&b""[..], 65_535_u32))); + assert_parse!(le_u24(&[0x56, 0x34, 0x12]), Ok((&b""[..], 1_193_046_u32))); + } + + #[test] + fn le_i24_tests() { + assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32))); + assert_parse!(le_i24(&[0x00, 0x00, 0xFF]), Ok((&b""[..], -65_536_i32))); + assert_parse!(le_i24(&[0xAA, 0xCB, 0xED]), Ok((&b""[..], -1_193_046_i32))); + } + + #[test] + fn le_i32_tests() { + assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!( + le_i32(&[0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 2_147_483_647_i32)) + ); + assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!( + le_i32(&[0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -2_147_483_648_i32)) + ); + } + + #[test] + fn le_i64_tests() { + assert_parse!( + le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 9_223_372_036_854_775_807_i64)) + ); + assert_parse!( + le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -9_223_372_036_854_775_808_i64)) + ); + } + + #[test] + #[cfg(stable_i128)] + fn le_i128_tests() { + assert_parse!( + le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128)) + ); + assert_parse!( + le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128)) + ); + } + + #[test] + fn be_f32_tests() { + assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32))); + assert_parse!( + be_f32(&[0x4d, 0x31, 0x1f, 0xd8]), + Ok((&b""[..], 185_728_392_f32)) + ); + } + + #[test] + fn be_f64_tests() { + assert_parse!( + be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0_f64)) + ); + assert_parse!( + be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00]), + Ok((&b""[..], 185_728_392_f64)) + ); + } + + #[test] + fn le_f32_tests() { + assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32))); + assert_parse!( + le_f32(&[0xd8, 0x1f, 0x31, 0x4d]), + Ok((&b""[..], 185_728_392_f32)) + ); + } + + #[test] + fn le_f64_tests() { + assert_parse!( + le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0_f64)) + ); + assert_parse!( + le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41]), + Ok((&b""[..], 185_728_392_f64)) + ); + } + + #[test] + fn hex_u32_tests() { + assert_parse!( + hex_u32(&b";"[..]), + Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA))) + ); + assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255))); + assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138))); + assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058))); + assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058))); + assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347))); + assert_parse!( + hex_u32(&b"c5a31be201;"[..]), + Ok((&b"01;"[..], 3_315_801_058)) + ); + assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295))); + assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0))); + assert_parse!(hex_u32(&b"12af"[..]), Err(Err::Incomplete(Needed::Size(1)))); + } + + #[test] + #[cfg(feature = "std")] + fn float_test() { + let mut test_cases = vec![ + "+3.14", + "3.14", + "-3.14", + "0", + "0.0", + "1.", + ".789", + "-.5", + "1e7", + "-1E-7", + ".3e-2", + "1.e4", + "1.2e4", + "12.34", + "-1.234E-12", + "-1.234e-12", + ]; + + for test in test_cases.drain(..) { + let expected32 = str::parse::<f32>(test).unwrap(); + let expected64 = str::parse::<f64>(test).unwrap(); + + println!("now parsing: {} -> {}", test, expected32); + + let larger = format!("{};", test); + assert_parse!(recognize_float(&larger[..]), Ok((";", test))); + + assert_parse!(float(larger.as_bytes()), Ok((&b";"[..], expected32))); + assert_parse!(float(&larger[..]), Ok((";", expected32))); + + assert_parse!(double(larger.as_bytes()), Ok((&b";"[..], expected64))); + assert_parse!(double(&larger[..]), Ok((";", expected64))); + } + + let remaining_exponent = "-1.234E-"; + assert_parse!( + recognize_float(remaining_exponent), + Err(Err::Incomplete(Needed::Size(1))) + ); + } + +} diff --git a/third_party/rust/nom/src/regexp.rs b/third_party/rust/nom/src/regexp.rs new file mode 100644 index 0000000000..fd165e1bbf --- /dev/null +++ b/third_party/rust/nom/src/regexp.rs @@ -0,0 +1,1090 @@ +#[doc(hidden)] +#[macro_export] +macro_rules! regex ( + ($re: ident, $s:expr) => ( + lazy_static! { + static ref $re: $crate::lib::regex::Regex = $crate::lib::regex::Regex::new($s).unwrap(); + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! regex_bytes ( + ($re: ident, $s:expr) => ( + lazy_static! { + static ref $re: $crate::lib::regex::bytes::Regex = $crate::lib::regex::bytes::Regex::new($s).unwrap(); + } + ); +); + +/// `re_match!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_match ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::InputLength; + use $crate::Slice; + let re = $crate::lib::regex::Regex::new($re).unwrap(); + if re.is_match(&$i) { + Ok(($i.slice($i.input_len()..), $i)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_match_static!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_match_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::InputLength; + use $crate::Slice; + regex!(RE, $re); + if RE.is_match(&$i) { + Ok(($i.slice($i.input_len()..), $i)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); + res + } + } + ) +); + +/// `re_bytes_match!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_match ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::InputLength; + use $crate::Slice; + let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); + if re.is_match(&$i) { + Ok(($i.slice($i.input_len()..), $i)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_match_static!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_match_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::InputLength; + use $crate::Slice; + regex_bytes!(RE, $re); + if RE.is_match(&$i) { + Ok(($i.slice($i.input_len()..), $i)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); + res + } + } + ) +); + +/// `re_find!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_find ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::Regex::new($re).unwrap(); + if let Some(m) = re.find(&$i) { + Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_find_static!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_find_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex!(RE, $re); + if let Some(m) = RE.find(&$i) { + Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); + res + } + } + + ) +); + +/// `re_bytes_find!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_find ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); + if let Some(m) = re.find(&$i) { + Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_find!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_find_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex_bytes!(RE, $re); + if let Some(m) = RE.find(&$i) { + Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); + res + } + } + + ) +); + +/// `re_matches!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_matches ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::Regex::new($re).unwrap(); + let v: Vec<_> = re.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_matches_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_matches_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex!(RE, $re); + let v: Vec<_> = RE.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); + res + } + } + ) +); + +/// `re_bytes_matches!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_matches ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); + let v: Vec<_> = re.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_matches_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_matches_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex_bytes!(RE, $re); + let v: Vec<_> = RE.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); + res + } + } + ) +); + +/// `re_capture!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_capture ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::Regex::new($re).unwrap(); + if let Some(c) = re.captures(&$i) { + let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_capture_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_capture_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex!(RE, $re); + if let Some(c) = RE.captures(&$i) { + let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +/// `re_bytes_capture!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_capture ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); + if let Some(c) = re.captures(&$i) { + let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_capture_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_capture_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex_bytes!(RE, $re); + if let Some(c) = RE.captures(&$i) { + let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +/// `re_captures!(regexp) => &[T] -> IResult<&[T], Vec<Vec<&[T]>>>` +/// Returns all the capture groups +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_captures ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::Regex::new($re).unwrap(); + let v:Vec<Vec<_>> = re.captures_iter(&$i) + .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()) + .map(|m| $i.slice(m.start()..m.end())).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_captures_static!(regexp) => &[T] -> IResult<&[T], Vec<Vec<&[T]>>>` +/// Returns all the capture groups. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_captures_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex!(RE, $re); + let v:Vec<Vec<_>> = RE.captures_iter(&$i) + .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +/// `re_bytes_captures!(regexp) => &[T] -> IResult<&[T], Vec<Vec<&[T]>>>` +/// Returns all the capture groups +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_captures ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); + let v:Vec<Vec<_>> = re.captures_iter(&$i) + .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_captures_static!(regexp) => &[T] -> IResult<&[T], Vec<Vec<&[T]>>>` +/// Returns all the capture groups. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_captures_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex_bytes!(RE, $re); + let v:Vec<Vec<_>> = RE.captures_iter(&$i) + .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); +#[cfg(test)] +mod tests { + #[cfg(feature = "alloc")] + use crate::lib::std::vec::Vec; + use crate::error::ErrorKind; + use crate::internal::Err; + + #[test] + fn re_match() { + named!(rm<&str,&str>, re_match!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpMatch + ),)) + ); + assert_eq!(rm("2015-09-07blah"), Ok(("", "2015-09-07blah"))); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_match_static() { + named!(rm<&str,&str>, re_match_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpMatch + ),)) + ); + assert_eq!(rm("2015-09-07blah"), Ok(("", "2015-09-07blah"))); + } + + #[test] + fn re_find() { + named!(rm<&str,&str>, re_find!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpFind + ),)) + ); + assert_eq!(rm("2015-09-07blah"), Ok(("blah", "2015-09-07"))); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_find_static() { + named!(rm<&str,&str>, re_find_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpFind + ),)) + ); + assert_eq!(rm("2015-09-07blah"), Ok(("blah", "2015-09-07"))); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_matches() { + named!(rm< &str,Vec<&str> >, re_matches!(r"\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", vec!["2015-09-07"]))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpMatches + ))) + ); + assert_eq!( + rm("aaa2015-09-07blah2015-09-09pouet"), + Ok(("pouet", vec!["2015-09-07", "2015-09-09"])) + ); + } + + #[cfg(feature = "regexp_macros")] + #[cfg(feature = "alloc")] + #[test] + fn re_matches_static() { + named!(rm< &str,Vec<&str> >, re_matches_static!(r"\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", vec!["2015-09-07"]))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpMatches + ))) + ); + assert_eq!( + rm("aaa2015-09-07blah2015-09-09pouet"), + Ok(("pouet", vec!["2015-09-07", "2015-09-09"])) + ); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_capture() { + named!(rm< &str,Vec<&str> >, re_capture!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!( + rm("blah nom 0.3.11pouet"), + Ok(("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])) + ); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm("hello nom 0.3.11 world regex 0.1.41"), + Ok(( + " world regex 0.1.41", + vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"] + )) + ); + } + + #[cfg(feature = "alloc")] + #[cfg(feature = "regexp_macros")] + #[test] + fn re_capture_static() { + named!(rm< &str,Vec<&str> >, re_capture_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!( + rm("blah nom 0.3.11pouet"), + Ok(("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])) + ); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm("hello nom 0.3.11 world regex 0.1.41"), + Ok(( + " world regex 0.1.41", + vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"] + )) + ); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_captures() { + named!(rm< &str,Vec<Vec<&str>> >, re_captures!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!( + rm("blah nom 0.3.11pouet"), + Ok(( + "pouet", + vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]] + )) + ); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm("hello nom 0.3.11 world regex 0.1.41 aaa"), + Ok(( + " aaa", + vec![ + vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"], + vec!["regex 0.1.41", "regex", "0.1.41", "0", "1", "41"], + ] + )) + ); + } + + #[cfg(feature = "alloc")] + #[cfg(feature = "regexp_macros")] + #[test] + fn re_captures_static() { + named!(rm< &str,Vec<Vec<&str>> >, re_captures_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!( + rm("blah nom 0.3.11pouet"), + Ok(( + "pouet", + vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]] + )) + ); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm("hello nom 0.3.11 world regex 0.1.41 aaa"), + Ok(( + " aaa", + vec![ + vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"], + vec!["regex 0.1.41", "regex", "0.1.41", "0", "1", "41"], + ] + )) + ); + } + + #[test] + fn re_bytes_match() { + named!(rm, re_bytes_match!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpMatch + ))) + ); + assert_eq!( + rm(&b"2015-09-07blah"[..]), + Ok((&b""[..], &b"2015-09-07blah"[..])) + ); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_match_static() { + named!(rm, re_bytes_match_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpMatch + ))) + ); + assert_eq!( + rm(&b"2015-09-07blah"[..]), + Ok((&b""[..], &b"2015-09-07blah"[..])) + ); + } + + #[test] + fn re_bytes_find() { + named!(rm, re_bytes_find!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpFind + ))) + ); + assert_eq!( + rm(&b"2015-09-07blah"[..]), + Ok((&b"blah"[..], &b"2015-09-07"[..])) + ); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_find_static() { + named!(rm, re_bytes_find_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpFind + ))) + ); + assert_eq!( + rm(&b"2015-09-07blah"[..]), + Ok((&b"blah"[..], &b"2015-09-07"[..])) + ); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_bytes_matches() { + named!(rm<Vec<&[u8]>>, re_bytes_matches!(r"\d{4}-\d{2}-\d{2}")); + assert_eq!( + rm(&b"2015-09-07"[..]), + Ok((&b""[..], vec![&b"2015-09-07"[..]])) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpMatches + ))) + ); + assert_eq!( + rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), + Ok((&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])) + ); + } + + #[cfg(feature = "alloc")] + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_matches_static() { + named!( + rm<Vec<&[u8]>>, + re_bytes_matches_static!(r"\d{4}-\d{2}-\d{2}") + ); + assert_eq!( + rm(&b"2015-09-07"[..]), + Ok((&b""[..], vec![&b"2015-09-07"[..]])) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpMatches + ))) + ); + assert_eq!( + rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), + Ok((&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])) + ); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_bytes_capture() { + named!( + rm<Vec<&[u8]>>, + re_bytes_capture!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") + ); + assert_eq!( + rm(&b"blah nom 0.3.11pouet"[..]), + Ok(( + &b"pouet"[..], + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ] + )) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), + Ok(( + &b" world regex 0.1.41"[..], + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ] + )) + ); + } + + #[cfg(feature = "alloc")] + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_capture_static() { + named!( + rm<Vec<&[u8]>>, + re_bytes_capture_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") + ); + assert_eq!( + rm(&b"blah nom 0.3.11pouet"[..]), + Ok(( + &b"pouet"[..], + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ] + )) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), + Ok(( + &b" world regex 0.1.41"[..], + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ] + )) + ); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_bytes_captures() { + named!( + rm<Vec<Vec<&[u8]>>>, + re_bytes_captures!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") + ); + assert_eq!( + rm(&b"blah nom 0.3.11pouet"[..]), + Ok(( + &b"pouet"[..], + vec![ + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ], + ] + )) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), + Ok(( + &b" aaa"[..], + vec![ + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ], + vec![ + &b"regex 0.1.41"[..], + &b"regex"[..], + &b"0.1.41"[..], + &b"0"[..], + &b"1"[..], + &b"41"[..], + ], + ] + )) + ); + } + + #[cfg(feature = "alloc")] + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_captures_static() { + named!( + rm<Vec<Vec<&[u8]>>>, + re_bytes_captures_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") + ); + assert_eq!( + rm(&b"blah nom 0.3.11pouet"[..]), + Ok(( + &b"pouet"[..], + vec![ + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ], + ] + )) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), + Ok(( + &b" aaa"[..], + vec![ + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ], + vec![ + &b"regex 0.1.41"[..], + &b"regex"[..], + &b"0.1.41"[..], + &b"0"[..], + &b"1"[..], + &b"41"[..], + ], + ] + )) + ); + } +} diff --git a/third_party/rust/nom/src/sequence/macros.rs b/third_party/rust/nom/src/sequence/macros.rs new file mode 100644 index 0000000000..a5a3351499 --- /dev/null +++ b/third_party/rust/nom/src/sequence/macros.rs @@ -0,0 +1,860 @@ +/// `tuple!(I->IResult<I,A>, I->IResult<I,B>, ... I->IResult<I,X>) => I -> IResult<I, (A, B, ..., X)>` +/// chains parsers and assemble the sub results in a tuple. +/// +/// The input type `I` must implement `nom::InputLength`. +/// +/// This combinator will count how much data is consumed by every child parser +/// and take it into account if there is not enough data +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::error::ErrorKind; +/// # use nom::number::streaming::be_u16; +/// // the return type depends of the children parsers +/// named!(parser<&[u8], (u16, &[u8], &[u8]) >, +/// tuple!( +/// be_u16 , +/// take!(3), +/// tag!("fg") +/// ) +/// ); +/// +/// # fn main() { +/// assert_eq!( +/// parser(&b"abcdefgh"[..]), +/// Ok(( +/// &b"h"[..], +/// (0x6162u16, &b"cde"[..], &b"fg"[..]) +/// )) +/// ); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! tuple ( + ($i:expr, $($rest:tt)*) => ( + { + tuple_parser!($i, (), $($rest)*) + } + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! tuple_parser ( + ($i:expr, ($($parsed:tt),*), $e:path, $($rest:tt)*) => ( + tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*); + ); + ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + let i_ = $i.clone(); + + ( $submac!(i_, $($args)*) ).and_then(|(i,o)| { + let i_ = i.clone(); + tuple_parser!(i_, (o), $($rest)*) + }) + } + ); + ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + let i_ = $i.clone(); + + ( $submac!(i_, $($args)*) ).and_then(|(i,o)| { + let i_ = i.clone(); + tuple_parser!(i_, ($($parsed)* , o), $($rest)*) + }) + } + ); + ($i:expr, ($($parsed:tt),*), $e:path) => ( + tuple_parser!($i, ($($parsed),*), call!($e)); + ); + ($i:expr, (), $submac:ident!( $($args:tt)* )) => ( + { + let i_ = $i.clone(); + ( $submac!(i_, $($args)*) ).map(|(i,o)| (i, (o))) + } + ); + ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( + { + let i_ = $i.clone(); + ( $submac!(i_, $($args)*) ).map(|(i,o)| (i, ($($parsed),* , o))) + } + ); + ($i:expr, ($($parsed:expr),*)) => ( + { + $crate::lib::std::result::Result::Ok(($i, ($($parsed),*))) + } + ); +); + +/// `pair!(I -> IResult<I,O>, I -> IResult<I,P>) => I -> IResult<I, (O,P)>` +/// pair returns a tuple of the results of its two child parsers of both succeed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # use nom::character::complete::{alpha1, digit1}; +/// named!(parser<&str, (&str, &str)>, pair!(alpha1, digit1)); +/// +/// # fn main() { +/// assert_eq!(parser("abc123"), Ok(("", ("abc", "123")))); +/// assert_eq!(parser("123abc"), Err(Err::Error(("123abc", ErrorKind::Alpha)))); +/// assert_eq!(parser("abc;123"), Err(Err::Error((";123", ErrorKind::Digit)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! pair( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + pair!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + pair!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + pair!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::sequence::pairc($i, $f, $g) + ); +); + +/// `separated_pair!(I -> IResult<I,O>, I -> IResult<I, T>, I -> IResult<I,P>) => I -> IResult<I, (O,P)>` +/// separated_pair(X,sep,Y) returns a tuple of its first and third child parsers +/// if all 3 succeed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # use nom::character::complete::{alpha1, digit1}; +/// named!(parser<&str, (&str, &str)>, separated_pair!(alpha1, char!(','), digit1)); +/// +/// # fn main() { +/// assert_eq!(parser("abc,123"), Ok(("", ("abc", "123")))); +/// assert_eq!(parser("123,abc"), Err(Err::Error(("123,abc", ErrorKind::Alpha)))); +/// assert_eq!(parser("abc;123"), Err(Err::Error((";123", ErrorKind::Char)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! separated_pair( + ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + separated_pair!($i, |i| $submac!(i, $($args)*), $($rest)*) + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + separated_pair!($i, $f, |i| $submac!(i, $($args)*), $($rest)*) + ); + ($i:expr, $f:expr, $g:expr, $submac:ident!( $($args:tt)* )) => ( + separated_pair!($i, $f, $g, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr, $g:expr, $h:expr) => ( + $crate::sequence::separated_pairc($i, $f, $g, $h) + ); +); + +/// `preceded!(I -> IResult<I,T>, I -> IResult<I,O>) => I -> IResult<I, O>` +/// preceded returns the result of its second parser if both succeed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # use nom::character::complete::{alpha1}; +/// named!(parser<&str, &str>, preceded!(char!('-'), alpha1)); +/// +/// # fn main() { +/// assert_eq!(parser("-abc"), Ok(("", "abc"))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// assert_eq!(parser("-123"), Err(Err::Error(("123", ErrorKind::Alpha)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! preceded( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + preceded!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + preceded!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + preceded!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::sequence::precededc($i, $f, $g) + ); +); + +/// `terminated!(I -> IResult<I,O>, I -> IResult<I,T>) => I -> IResult<I, O>` +/// terminated returns the result of its first parser if both succeed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # use nom::character::complete::{alpha1}; +/// named!(parser<&str, &str>, terminated!(alpha1, char!(';'))); +/// +/// # fn main() { +/// assert_eq!(parser("abc;"), Ok(("", "abc"))); +/// assert_eq!(parser("abc,"), Err(Err::Error((",", ErrorKind::Char)))); +/// assert_eq!(parser("123;"), Err(Err::Error(("123;", ErrorKind::Alpha)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! terminated( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + terminated!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + terminated!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + terminated!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::sequence::terminatedc($i, $f, $g) + ); +); + +/// `delimited!(I -> IResult<I,T>, I -> IResult<I,O>, I -> IResult<I,U>) => I -> IResult<I, O>` +/// delimited(opening, X, closing) returns X +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::character::complete::{alpha1}; +/// named!(parens, +/// delimited!( +/// tag!("("), +/// alpha1, +/// tag!(")") +/// ) +/// ); +/// +/// # fn main() { +/// let input = &b"(test)"[..]; +/// assert_eq!(parens(input), Ok((&b""[..], &b"test"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! delimited( + ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + delimited!($i, |i| $submac!(i, $($args)*), $($rest)*) + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + delimited!($i, $f, |i| $submac!(i, $($args)*), $($rest)*) + ); + ($i:expr, $f:expr, $g:expr, $submac:ident!( $($args:tt)* )) => ( + delimited!($i, $f, $g, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr, $g:expr, $h:expr) => ( + $crate::sequence::delimitedc($i, $f, $g, $h) + ); +); + +/// `do_parse!(I->IResult<I,A> >> I->IResult<I,B> >> ... I->IResult<I,X> , ( O ) ) => I -> IResult<I, O>` +/// do_parse applies sub parsers in a sequence. +/// it can store intermediary results and make them available +/// for later parsers +/// +/// The input type `I` must implement `nom::InputLength`. +/// +/// This combinator will count how much data is consumed by every child parser +/// and take it into account if there is not enough data +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,Needed}; +/// use nom::number::streaming::be_u8; +/// +/// // this parser implements a common pattern in binary formats, +/// // the TAG-LENGTH-VALUE, where you first recognize a specific +/// // byte slice, then the next bytes indicate the length of +/// // the data, then you take that slice and return it +/// // +/// // here, we match the tag 42, take the length in the next byte +/// // and store it in `length`, then use `take!` with `length` +/// // to obtain the subslice that we store in `bytes`, then return +/// // `bytes` +/// // you can use other macro combinators inside do_parse (like the `tag` +/// // one here), or a function (like `be_u8` here), but you cannot use a +/// // module path (like `nom::be_u8`) there, because of limitations in macros +/// named!(tag_length_value, +/// do_parse!( +/// tag!( &[ 42u8 ][..] ) >> +/// length: be_u8 >> +/// bytes: take!(length) >> +/// (bytes) +/// ) +/// ); +/// +/// # fn main() { +/// let a: Vec<u8> = vec!(42, 2, 3, 4, 5); +/// let result_a: Vec<u8> = vec!(3, 4); +/// let rest_a: Vec<u8> = vec!(5); +/// assert_eq!(tag_length_value(&a[..]), Ok((&rest_a[..], &result_a[..]))); +/// +/// // here, the length is 5, but there are only 3 bytes afterwards (3, 4 and 5), +/// // so the parser will tell you that you need 7 bytes: one for the tag, +/// // one for the length, then 5 bytes +/// let b: Vec<u8> = vec!(42, 5, 3, 4, 5); +/// assert_eq!(tag_length_value(&b[..]), Err(Err::Incomplete(Needed::Size(5)))); +/// # } +/// ``` +/// +/// the result is a tuple, so you can return multiple sub results, like +/// this: +/// `do_parse!(I->IResult<I,A> >> I->IResult<I,B> >> ... I->IResult<I,X> , ( O, P ) ) => I -> IResult<I, (O,P)>` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// use nom::number::streaming::be_u8; +/// named!(tag_length_value<(u8, &[u8])>, +/// do_parse!( +/// tag!( &[ 42u8 ][..] ) >> +/// length: be_u8 >> +/// bytes: take!(length) >> +/// (length, bytes) +/// ) +/// ); +/// +/// # fn main() { +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! do_parse ( + (__impl $i:expr, ( $($rest:expr),* )) => ( + $crate::lib::std::result::Result::Ok(($i, ( $($rest),* ))) + ); + + (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) ) => ( + do_parse!(__impl $i, $submac!( $($args)* )) + ); + + (__impl $i:expr, $submac:ident!( $($args:tt)* ) ) => ( + nom_compile_error!("do_parse is missing the return value. A do_parse call must end + with a return value between parenthesis, as follows: + + do_parse!( + a: tag!(\"abcd\") >> + b: tag!(\"efgh\") >> + + ( Value { a: a, b: b } ) + "); + ); + + (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) ~ $($rest:tt)* ) => ( + nom_compile_error!("do_parse uses >> as separator, not ~"); + ); + (__impl $i:expr, $submac:ident!( $($args:tt)* ) ~ $($rest:tt)* ) => ( + nom_compile_error!("do_parse uses >> as separator, not ~"); + ); + (__impl $i:expr, $field:ident : $e:ident ~ $($rest:tt)*) => ( + do_parse!(__impl $i, $field: call!($e) ~ $($rest)*); + ); + (__impl $i:expr, $e:ident ~ $($rest:tt)*) => ( + do_parse!(__impl $i, call!($e) ~ $($rest)*); + ); + + (__impl $i:expr, $e:ident >> $($rest:tt)*) => ( + do_parse!(__impl $i, call!($e) >> $($rest)*); + ); + (__impl $i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + Err(e) => Err(e), + Ok((i,_)) => { + let i_ = i.clone(); + do_parse!(__impl i_, $($rest)*) + }, + } + } + ); + + (__impl $i:expr, $field:ident : $e:ident >> $($rest:tt)*) => ( + do_parse!(__impl $i, $field: call!($e) >> $($rest)*); + ); + + (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + Err(e) => Err(e), + Ok((i,o)) => { + let $field = o; + let i_ = i.clone(); + do_parse!(__impl i_, $($rest)*) + }, + } + } + ); + + // ending the chain + (__impl $i:expr, $e:ident >> ( $($rest:tt)* )) => ( + do_parse!(__impl $i, call!($e) >> ( $($rest)* )); + ); + + (__impl $i:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match $submac!($i, $($args)*) { + Err(e) => Err(e), + Ok((i,_)) => { + do_parse!(__finalize i, $($rest)*) + }, + } + }); + + (__impl $i:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => ( + do_parse!(__impl $i, $field: call!($e) >> ( $($rest)* ) ); + ); + + (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match $submac!($i, $($args)*) { + Err(e) => Err(e), + Ok((i,o)) => { + let $field = o; + do_parse!(__finalize i, $($rest)*) + }, + } + }); + + (__finalize $i:expr, ( $o: expr )) => ({ + use $crate::lib::std::result::Result::Ok; + Ok(($i, $o)) + }); + + (__finalize $i:expr, ( $($rest:tt)* )) => ({ + use $crate::lib::std::result::Result::Ok; + Ok(($i, ( $($rest)* ))) + }); + + ($i:expr, $($rest:tt)*) => ( + { + do_parse!(__impl $i, $($rest)*) + } + ); + ($submac:ident!( $($args:tt)* ) >> $($rest:tt)* ) => ( + nom_compile_error!("if you are using do_parse outside of a named! macro, you must + pass the input data as first argument, like this: + + let res = do_parse!(input, + a: tag!(\"abcd\") >> + b: tag!(\"efgh\") >> + ( Value { a: a, b: b } ) + );"); + ); + ($e:ident! >> $($rest:tt)* ) => ( + do_parse!( call!($e) >> $($rest)*); + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! nom_compile_error ( + (( $($args:tt)* )) => ( compile_error!($($args)*) ); +); + +#[cfg(test)] +mod tests { + use crate::internal::{Err, IResult, Needed}; + use crate::number::streaming::be_u16; + use crate::error::ErrorKind; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes<T: $crate::AsBytes>(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); + ); + + macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + use $crate::lib::std::cmp::min; + + let len = $i.len(); + let blen = $bytes.len(); + let m = min(len, blen); + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: IResult<_,_,_> = if reduced != b { + Err($crate::Err::Error(error_position!($i, $crate::error::ErrorKind::Tag))) + } else if m < blen { + Err($crate::Err::Incomplete(Needed::Size(blen))) + } else { + Ok((&$i[blen..], reduced)) + }; + res + } + ); + ); + + macro_rules! take ( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res:IResult<&[u8],&[u8],_> = if $i.len() < cnt { + Err($crate::Err::Incomplete(Needed::Size(cnt))) + } else { + Ok((&$i[cnt..],&$i[0..cnt])) + }; + res + } + ); + ); + + #[derive(PartialEq, Eq, Debug)] + struct B { + a: u8, + b: u8, + } + + #[derive(PartialEq, Eq, Debug)] + struct C { + a: u8, + b: Option<u8>, + } + + /*FIXME: convert code examples to new error management + use util::{add_error_pattern, error_to_list, print_error}; + + #[cfg(feature = "std")] + #[cfg_attr(rustfmt, rustfmt_skip)] + fn error_to_string<P: Clone + PartialEq>(e: &Context<P, u32>) -> &'static str { + let v: Vec<(P, ErrorKind<u32>)> = error_to_list(e); + // do it this way if you can use slice patterns + //match &v[..] { + // [ErrorKind::Custom(42), ErrorKind::Tag] => "missing `ijkl` tag", + // [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] => "missing `mnop` tag after `ijkl`", + // _ => "unrecognized error" + //} + + let collected: Vec<ErrorKind<u32>> = v.iter().map(|&(_, ref e)| e.clone()).collect(); + if &collected[..] == [ErrorKind::Custom(42), ErrorKind::Tag] { + "missing `ijkl` tag" + } else if &collected[..] == [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] { + "missing `mnop` tag after `ijkl`" + } else { + "unrecognized error" + } + } + + // do it this way if you can use box patterns + //use $crate::lib::std::str; + //fn error_to_string(e:Err) -> String + // match e { + // NodePosition(ErrorKind::Custom(42), i1, box Position(ErrorKind::Tag, i2)) => { + // format!("missing `ijkl` tag, found '{}' instead", str::from_utf8(i2).unwrap()) + // }, + // NodePosition(ErrorKind::Custom(42), i1, box NodePosition(ErrorKind::Custom(128), i2, box Position(ErrorKind::Tag, i3))) => { + // format!("missing `mnop` tag after `ijkl`, found '{}' instead", str::from_utf8(i3).unwrap()) + // }, + // _ => "unrecognized error".to_string() + // } + //} + */ + + #[cfg_attr(rustfmt, rustfmt_skip)] + #[allow(unused_variables)] + #[test] + fn add_err() { + named!(err_test, + preceded!( + tag!("efgh"), + add_return_error!( + //ErrorKind::Custom(42u32), + ErrorKind::Char, + do_parse!( + tag!("ijkl") >> + //res: add_return_error!(ErrorKind::Custom(128u32), tag!("mnop")) >> + res: add_return_error!(ErrorKind::Eof, tag!("mnop")) >> + (res) + ) + ) + ) + ); + let a = &b"efghblah"[..]; + let b = &b"efghijklblah"[..]; + let c = &b"efghijklmnop"[..]; + + let blah = &b"blah"[..]; + + let res_a = err_test(a); + let res_b = err_test(b); + let res_c = err_test(c); + assert_eq!(res_a, + Err(Err::Error(error_node_position!(blah, + //ErrorKind::Custom(42u32), + ErrorKind::Eof, + error_position!(blah, ErrorKind::Tag))))); + //assert_eq!(res_b, Err(Err::Error(error_node_position!(&b"ijklblah"[..], ErrorKind::Custom(42u32), + // error_node_position!(blah, ErrorKind::Custom(128u32), error_position!(blah, ErrorKind::Tag)))))); + assert_eq!(res_b, Err(Err::Error(error_node_position!(&b"ijklblah"[..], ErrorKind::Eof, + error_node_position!(blah, ErrorKind::Eof, error_position!(blah, ErrorKind::Tag)))))); + assert_eq!(res_c, Ok((&b""[..], &b"mnop"[..]))); + } + + #[cfg_attr(rustfmt, rustfmt_skip)] + #[test] + fn complete() { + named!(err_test, + do_parse!( + tag!("ijkl") >> + res: complete!(tag!("mnop")) >> + (res) + ) + ); + let a = &b"ijklmn"[..]; + + let res_a = err_test(a); + assert_eq!(res_a, + Err(Err::Error(error_position!(&b"mn"[..], ErrorKind::Complete)))); + } + + #[test] + fn pair() { + named!(tag_abc, tag!("abc")); + named!(tag_def, tag!("def")); + named!( pair_abc_def<&[u8],(&[u8], &[u8])>, pair!(tag_abc, tag_def) ); + + assert_eq!(pair_abc_def(&b"abcdefghijkl"[..]), Ok((&b"ghijkl"[..], (&b"abc"[..], &b"def"[..])))); + assert_eq!(pair_abc_def(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(pair_abc_def(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!( + pair_abc_def(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + assert_eq!( + pair_abc_def(&b"xxxdef"[..]), + Err(Err::Error(error_position!(&b"xxxdef"[..], ErrorKind::Tag))) + ); + assert_eq!( + pair_abc_def(&b"abcxxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn separated_pair() { + named!(tag_abc, tag!("abc")); + named!(tag_def, tag!("def")); + named!(tag_separator, tag!(",")); + named!( sep_pair_abc_def<&[u8],(&[u8], &[u8])>, separated_pair!(tag_abc, tag_separator, tag_def) ); + + assert_eq!( + sep_pair_abc_def(&b"abc,defghijkl"[..]), + Ok((&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))) + ); + assert_eq!(sep_pair_abc_def(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(sep_pair_abc_def(&b"abc,d"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!( + sep_pair_abc_def(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + assert_eq!( + sep_pair_abc_def(&b"xxx,def"[..]), + Err(Err::Error(error_position!(&b"xxx,def"[..], ErrorKind::Tag))) + ); + assert_eq!( + sep_pair_abc_def(&b"abc,xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn preceded() { + named!(tag_abcd, tag!("abcd")); + named!(tag_efgh, tag!("efgh")); + named!( preceded_abcd_efgh<&[u8], &[u8]>, preceded!(tag_abcd, tag_efgh) ); + + assert_eq!(preceded_abcd_efgh(&b"abcdefghijkl"[..]), Ok((&b"ijkl"[..], &b"efgh"[..]))); + assert_eq!(preceded_abcd_efgh(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(preceded_abcd_efgh(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!( + preceded_abcd_efgh(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + assert_eq!( + preceded_abcd_efgh(&b"xxxxdef"[..]), + Err(Err::Error(error_position!(&b"xxxxdef"[..], ErrorKind::Tag))) + ); + assert_eq!( + preceded_abcd_efgh(&b"abcdxxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn terminated() { + named!(tag_abcd, tag!("abcd")); + named!(tag_efgh, tag!("efgh")); + named!( terminated_abcd_efgh<&[u8], &[u8]>, terminated!(tag_abcd, tag_efgh) ); + + assert_eq!(terminated_abcd_efgh(&b"abcdefghijkl"[..]), Ok((&b"ijkl"[..], &b"abcd"[..]))); + assert_eq!(terminated_abcd_efgh(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(terminated_abcd_efgh(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!( + terminated_abcd_efgh(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + assert_eq!( + terminated_abcd_efgh(&b"xxxxdef"[..]), + Err(Err::Error(error_position!(&b"xxxxdef"[..], ErrorKind::Tag))) + ); + assert_eq!( + terminated_abcd_efgh(&b"abcdxxxx"[..]), + Err(Err::Error(error_position!(&b"xxxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn delimited() { + named!(tag_abc, tag!("abc")); + named!(tag_def, tag!("def")); + named!(tag_ghi, tag!("ghi")); + named!( delimited_abc_def_ghi<&[u8], &[u8]>, delimited!(tag_abc, tag_def, tag_ghi) ); + + assert_eq!(delimited_abc_def_ghi(&b"abcdefghijkl"[..]), Ok((&b"jkl"[..], &b"def"[..]))); + assert_eq!(delimited_abc_def_ghi(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(delimited_abc_def_ghi(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(delimited_abc_def_ghi(&b"abcdefgh"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!( + delimited_abc_def_ghi(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + assert_eq!( + delimited_abc_def_ghi(&b"xxxdefghi"[..]), + Err(Err::Error(error_position!(&b"xxxdefghi"[..], ErrorKind::Tag),)) + ); + assert_eq!( + delimited_abc_def_ghi(&b"abcxxxghi"[..]), + Err(Err::Error(error_position!(&b"xxxghi"[..], ErrorKind::Tag))) + ); + assert_eq!( + delimited_abc_def_ghi(&b"abcdefxxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn tuple_test() { + named!(tuple_3<&[u8], (u16, &[u8], &[u8]) >, + tuple!( be_u16 , take!(3), tag!("fg") ) ); + + assert_eq!(tuple_3(&b"abcdefgh"[..]), Ok((&b"h"[..], (0x6162u16, &b"cde"[..], &b"fg"[..])))); + assert_eq!(tuple_3(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(tuple_3(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!( + tuple_3(&b"abcdejk"[..]), + Err(Err::Error(error_position!(&b"jk"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn do_parse() { + fn ret_int1(i: &[u8]) -> IResult<&[u8], u8> { + Ok((i, 1)) + }; + fn ret_int2(i: &[u8]) -> IResult<&[u8], u8> { + Ok((i, 2)) + }; + + //trace_macros!(true); + named!(do_parser<&[u8], (u8, u8)>, + do_parse!( + tag!("abcd") >> + opt!(tag!("abcd")) >> + aa: ret_int1 >> + tag!("efgh") >> + bb: ret_int2 >> + tag!("efgh") >> + (aa, bb) + ) + ); + //named!(do_parser<&[u8], (u8, u8)>, + // do_parse!( + // tag!("abcd") >> aa: ret_int1 >> tag!("efgh") >> bb: ret_int2 >> tag!("efgh") >> (aa, bb) + // ) + //); + + //trace_macros!(false); + + assert_eq!(do_parser(&b"abcdabcdefghefghX"[..]), Ok((&b"X"[..], (1, 2)))); + assert_eq!(do_parser(&b"abcdefghefghX"[..]), Ok((&b"X"[..], (1, 2)))); + assert_eq!(do_parser(&b"abcdab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(do_parser(&b"abcdefghef"[..]), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[cfg_attr(rustfmt, rustfmt_skip)] + #[test] + fn do_parse_dependency() { + use crate::number::streaming::be_u8; + + named!(length_value, + do_parse!( + length: be_u8 >> + bytes: take!(length) >> + (bytes) + ) + ); + + let a = [2u8, 3, 4, 5]; + let res_a = [3u8, 4]; + assert_eq!(length_value(&a[..]), Ok((&a[3..], &res_a[..]))); + let b = [5u8, 3, 4, 5]; + assert_eq!(length_value(&b[..]), Err(Err::Incomplete(Needed::Size(5)))); + } + + /* + named!(does_not_compile, + do_parse!( + length: be_u8 >> + bytes: take!(length) + ) + ); + named!(does_not_compile_either, + do_parse!( + length: be_u8 ~ + bytes: take!(length) ~ + ( () ) + ) + ); + fn still_does_not_compile() { + let data = b"abcd"; + + let res = do_parse!( + tag!("abcd") >> + tag!("efgh") >> + ( () ) + ); + } + */ +} diff --git a/third_party/rust/nom/src/sequence/mod.rs b/third_party/rust/nom/src/sequence/mod.rs new file mode 100644 index 0000000000..a0eafb97bd --- /dev/null +++ b/third_party/rust/nom/src/sequence/mod.rs @@ -0,0 +1,313 @@ +//! combinators applying parsers in sequence + +#[macro_use] +mod macros; + +use crate::internal::IResult; +use crate::error::ParseError; + +/// Gets an object from the first parser, +/// then gets another object from the second parser. +/// +/// # Arguments +/// * `first` The first parser to apply. +/// * `second` The second parser to apply. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::sequence::pair; +/// use nom::bytes::complete::tag; +/// +/// let parser = pair(tag("abc"), tag("efg")); +/// +/// assert_eq!(parser("abcefg"), Ok(("", ("abc", "efg")))); +/// assert_eq!(parser("abcefghij"), Ok(("hij", ("abc", "efg")))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// ``` +pub fn pair<I, O1, O2, E: ParseError<I>, F, G>(first: F, second: G) -> impl Fn(I) -> IResult<I, (O1, O2), E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(I) -> IResult<I, O2, E>, +{ + move |input: I| { + let (input, o1) = first(input)?; + second(input).map(|(i, o2)| (i, (o1, o2))) + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +pub fn pairc<I, O1, O2, E: ParseError<I>, F, G>(input: I, first: F, second: G) -> IResult<I, (O1, O2), E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(I) -> IResult<I, O2, E>, +{ + pair(first, second)(input) +} + +/// Matches an object from the first parser and discards it, +/// then gets an object from the second parser. +/// +/// # Arguments +/// * `first` The opening parser. +/// * `second` The second parser to get object. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::sequence::preceded; +/// use nom::bytes::complete::tag; +/// +/// let parser = preceded(tag("abc"), tag("efg")); +/// +/// assert_eq!(parser("abcefg"), Ok(("", "efg"))); +/// assert_eq!(parser("abcefghij"), Ok(("hij", "efg"))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// ``` +pub fn preceded<I, O1, O2, E: ParseError<I>, F, G>(first: F, second: G) -> impl Fn(I) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(I) -> IResult<I, O2, E>, +{ + move |input: I| { + let (input, _) = first(input)?; + second(input) + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +pub fn precededc<I, O1, O2, E: ParseError<I>, F, G>(input: I, first: F, second: G) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(I) -> IResult<I, O2, E>, +{ + preceded(first, second)(input) +} + +/// Gets an object from the first parser, +/// then matches an object from the second parser and discards it. +/// +/// # Arguments +/// * `first` The first parser to apply. +/// * `second` The second parser to match an object. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::sequence::terminated; +/// use nom::bytes::complete::tag; +/// +/// let parser = terminated(tag("abc"), tag("efg")); +/// +/// assert_eq!(parser("abcefg"), Ok(("", "abc"))); +/// assert_eq!(parser("abcefghij"), Ok(("hij", "abc"))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// ``` +pub fn terminated<I, O1, O2, E: ParseError<I>, F, G>(first: F, second: G) -> impl Fn(I) -> IResult<I, O1, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(I) -> IResult<I, O2, E>, +{ + move |input: I| { + let (input, o1) = first(input)?; + second(input).map(|(i, _)| (i, o1)) + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +pub fn terminatedc<I, O1, O2, E: ParseError<I>, F, G>(input: I, first: F, second: G) -> IResult<I, O1, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(I) -> IResult<I, O2, E>, +{ + terminated(first, second)(input) +} + +/// Gets an object from the first parser, +/// then matches an object from the sep_parser and discards it, +/// then gets another object from the second parser. +/// +/// # Arguments +/// * `first` The first parser to apply. +/// * `sep` The separator parser to apply. +/// * `second` The second parser to apply. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::sequence::separated_pair; +/// use nom::bytes::complete::tag; +/// +/// let parser = separated_pair(tag("abc"), tag("|"), tag("efg")); +/// +/// assert_eq!(parser("abc|efg"), Ok(("", ("abc", "efg")))); +/// assert_eq!(parser("abc|efghij"), Ok(("hij", ("abc", "efg")))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// ``` +pub fn separated_pair<I, O1, O2, O3, E: ParseError<I>, F, G, H>(first: F, sep: G, second: H) -> impl Fn(I) -> IResult<I, (O1, O3), E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(I) -> IResult<I, O2, E>, + H: Fn(I) -> IResult<I, O3, E>, +{ + move |input: I| { + let (input, o1) = first(input)?; + let (input, _) = sep(input)?; + second(input).map(|(i, o2)| (i, (o1, o2))) + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +pub fn separated_pairc<I, O1, O2, O3, E: ParseError<I>, F, G, H>(input: I, first: F, sep: G, second: H) -> IResult<I, (O1, O3), E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(I) -> IResult<I, O2, E>, + H: Fn(I) -> IResult<I, O3, E>, +{ + separated_pair(first, sep, second)(input) +} + +/// Matches an object from the first parser, +/// then gets an object from the sep_parser, +/// then matches another object from the second parser. +/// +/// # Arguments +/// * `first` The first parser to apply. +/// * `sep` The separator parser to apply. +/// * `second` The second parser to apply. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::sequence::delimited; +/// use nom::bytes::complete::tag; +/// +/// let parser = delimited(tag("abc"), tag("|"), tag("efg")); +/// +/// assert_eq!(parser("abc|efg"), Ok(("", "|"))); +/// assert_eq!(parser("abc|efghij"), Ok(("hij", "|"))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// ``` +pub fn delimited<I, O1, O2, O3, E: ParseError<I>, F, G, H>(first: F, sep: G, second: H) -> impl Fn(I) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(I) -> IResult<I, O2, E>, + H: Fn(I) -> IResult<I, O3, E>, +{ + move |input: I| { + let (input, _) = first(input)?; + let (input, o2) = sep(input)?; + second(input).map(|(i, _)| (i, o2)) + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +pub fn delimitedc<I, O1, O2, O3, E: ParseError<I>, F, G, H>(input: I, first: F, sep: G, second: H) -> IResult<I, O2, E> +where + F: Fn(I) -> IResult<I, O1, E>, + G: Fn(I) -> IResult<I, O2, E>, + H: Fn(I) -> IResult<I, O3, E>, +{ + delimited(first, sep, second)(input) +} + +/// helper trait for the tuple combinator +/// +/// this trait is implemented for tuples of parsers of up to 21 elements +pub trait Tuple<I,O,E> { + /// parses the input and returns a tuple of results of each parser + fn parse(&self, input: I) -> IResult<I,O,E>; +} + +impl<Input, Output, Error: ParseError<Input>, F: Fn(Input) -> IResult<Input, Output, Error> > Tuple<Input, (Output,), Error> for (F,) { + fn parse(&self, input: Input) -> IResult<Input,(Output,),Error> { + self.0(input).map(|(i,o)| (i, (o,))) + } +} + +macro_rules! tuple_trait( + ($name1:ident $ty1:ident, $name2: ident $ty2:ident, $($name:ident $ty:ident),*) => ( + tuple_trait!(__impl $name1 $ty1, $name2 $ty2; $($name $ty),*); + ); + (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident, $($name2:ident $ty2:ident),*) => ( + tuple_trait_impl!($($name $ty),+); + tuple_trait!(__impl $($name $ty),+ , $name1 $ty1; $($name2 $ty2),*); + ); + (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident) => ( + tuple_trait_impl!($($name $ty),+); + tuple_trait_impl!($($name $ty),+, $name1 $ty1); + ); +); + +macro_rules! tuple_trait_impl( + ($($name:ident $ty: ident),+) => ( + impl< + Input: Clone, $($ty),+ , Error: ParseError<Input>, + $($name: Fn(Input) -> IResult<Input, $ty, Error>),+ + > Tuple<Input, ( $($ty),+ ), Error> for ( $($name),+ ) { + + fn parse(&self, input: Input) -> IResult<Input, ( $($ty),+ ), Error> { + tuple_trait_inner!(0, self, input, (), $($name)+) + + } + } + ); +); + +macro_rules! tuple_trait_inner( + ($it:tt, $self:expr, $input:expr, (), $head:ident $($id:ident)+) => ({ + let (i, o) = $self.$it($input.clone())?; + + succ!($it, tuple_trait_inner!($self, i, ( o ), $($id)+)) + }); + ($it:tt, $self:expr, $input:expr, ($($parsed:tt)*), $head:ident $($id:ident)+) => ({ + let (i, o) = $self.$it($input.clone())?; + + succ!($it, tuple_trait_inner!($self, i, ($($parsed)* , o), $($id)+)) + }); + ($it:tt, $self:expr, $input:expr, ($($parsed:tt)*), $head:ident) => ({ + let (i, o) = $self.$it($input.clone())?; + + Ok((i, ($($parsed)* , o))) + }); +); + +tuple_trait!(FnA A, FnB B, FnC C, FnD D, FnE E, FnF F, FnG G, FnH H, FnI I, FnJ J, FnK K, FnL L, + FnM M, FnN N, FnO O, FnP P, FnQ Q, FnR R, FnS S, FnT T, FnU U); + +/// applies a tuple of parsers one by one and returns their results as a tuple +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind}; +/// use nom::sequence::tuple; +/// use nom::character::complete::{alpha1, digit1}; +/// let parser = tuple((alpha1, digit1, alpha1)); +/// +/// assert_eq!(parser("abc123def"), Ok(("", ("abc", "123", "def")))); +/// assert_eq!(parser("123def"), Err(Err::Error(("123def", ErrorKind::Alpha)))); +/// ``` +pub fn tuple<I: Clone, O, E: ParseError<I>, List: Tuple<I,O,E>>(l: List) -> impl Fn(I) -> IResult<I, O, E> { + move |i: I| { + l.parse(i) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn single_element_tuples() { + use crate::character::complete::{alpha1, digit1}; + use crate::{Err, error::ErrorKind}; + + let parser = tuple((alpha1,)); + assert_eq!(parser("abc123def"), Ok(("123def", ("abc",)))); + assert_eq!(parser("123def"), Err(Err::Error(("123def", ErrorKind::Alpha)))); + } +} diff --git a/third_party/rust/nom/src/str.rs b/third_party/rust/nom/src/str.rs new file mode 100644 index 0000000000..71fb1a4aa1 --- /dev/null +++ b/third_party/rust/nom/src/str.rs @@ -0,0 +1,494 @@ +#[cfg(test)] +mod test { + use crate::{Err, error::ErrorKind, IResult}; + + #[test] + fn tagtr_succeed() { + const INPUT: &str = "Hello World!"; + const TAG: &str = "Hello"; + fn test(input: &str) -> IResult<&str, &str> { + tag!(input, TAG) + } + + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == " World!", + "Parser `tag` consumed leftover input." + ); + assert!( + output == TAG, + "Parser `tag` doesn't return the tag it matched on success. \ + Expected `{}`, got `{}`.", + TAG, + output + ); + } + other => panic!( + "Parser `tag` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn tagtr_incomplete() { + const INPUT: &str = "Hello"; + const TAG: &str = "Hello World!"; + + let res: IResult<_,_,(_, ErrorKind)> = tag!(INPUT, TAG); + match res { + Err(Err::Incomplete(_)) => (), + other => { + panic!( + "Parser `tag` didn't require more input when it should have. \ + Got `{:?}`.", + other + ); + } + }; + } + + #[test] + fn tagtr_error() { + const INPUT: &str = "Hello World!"; + const TAG: &str = "Random"; // TAG must be closer than INPUT. + + let res: IResult<_,_,(_, ErrorKind)> = tag!(INPUT, TAG); + match res { + Err(Err::Error(_)) => (), + other => { + panic!( + "Parser `tag` didn't fail when it should have. Got `{:?}`.`", + other + ); + } + }; + } + + #[test] + fn take_s_succeed() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + + let res: IResult<_,_,(_, ErrorKind)> = take!(INPUT, 9); + match res { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_s` consumed leftover input. Leftover `{}`.", + extra + ); + assert!( + output == CONSUMED, + "Parser `take_s` doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_s` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_until_succeed() { + const INPUT: &str = "βèƒôřèÂßÇ∂áƒƭèř"; + const FIND: &str = "ÂßÇ∂"; + const CONSUMED: &str = "βèƒôřè"; + const LEFTOVER: &str = "ÂßÇ∂áƒƭèř"; + + let res: IResult<_,_,(_, ErrorKind)> = take_until!(INPUT, FIND); + match res { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_until`\ + consumed leftover input. Leftover `{}`.", + extra + ); + assert!( + output == CONSUMED, + "Parser `take_until`\ + doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_until` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_s_incomplete() { + const INPUT: &str = "βèƒôřèÂßÇá"; + + let res: IResult<_,_,(_, ErrorKind)> = take!(INPUT, 13); + match res { + Err(Err::Incomplete(_)) => (), + other => panic!( + "Parser `take` didn't require more input when it should have. \ + Got `{:?}`.", + other + ), + } + } + + use crate::internal::Needed; + + fn is_alphabetic(c: char) -> bool { + (c as u8 >= 0x41 && c as u8 <= 0x5A) || (c as u8 >= 0x61 && c as u8 <= 0x7A) + } + + #[test] + fn take_while() { + named!(f<&str,&str>, take_while!(is_alphabetic)); + let a = ""; + let b = "abcd"; + let c = "abcd123"; + let d = "123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&c[..]), Ok((&d[..], &b[..]))); + assert_eq!(f(&d[..]), Ok((&d[..], &a[..]))); + } + + #[test] + fn take_while1() { + named!(f<&str,&str>, take_while1!(is_alphabetic)); + let a = ""; + let b = "abcd"; + let c = "abcd123"; + let d = "123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&c[..]), Ok((&"123"[..], &b[..]))); + assert_eq!( + f(&d[..]), + Err(Err::Error(error_position!(&d[..], ErrorKind::TakeWhile1))) + ); + } + + #[test] + fn take_till_s_succeed() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + fn till_s(c: char) -> bool { + c == 'á' + } + fn test(input: &str) -> IResult<&str, &str> { + take_till!(input, till_s) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_till` consumed leftover input." + ); + assert!( + output == CONSUMED, + "Parser `take_till` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_till` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_while_succeed_none() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &str = ""; + const LEFTOVER: &str = "βèƒôřèÂßÇáƒƭèř"; + fn while_s(c: char) -> bool { + c == '9' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while!(input, while_s) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_while` consumed leftover input." + ); + assert!( + output == CONSUMED, + "Parser `take_while` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_while` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn is_not_succeed() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const AVOID: &str = "£úçƙ¥á"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + fn test(input: &str) -> IResult<&str, &str> { + is_not!(input, AVOID) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `is_not` consumed leftover input. Leftover `{}`.", + extra + ); + assert!( + output == CONSUMED, + "Parser `is_not` doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `is_not` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_while_succeed_some() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + fn while_s(c: char) -> bool { + c == 'β' || c == 'è' || c == 'ƒ' || c == 'ô' || c == 'ř' || c == 'è' || c == 'Â' || c == 'ß' || c == 'Ç' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while!(input, while_s) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_while` consumed leftover input." + ); + assert!( + output == CONSUMED, + "Parser `take_while` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_while` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn is_not_fail() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const AVOID: &str = "βúçƙ¥"; + fn test(input: &str) -> IResult<&str, &str> { + is_not!(input, AVOID) + } + match test(INPUT) { + Err(Err::Error(_)) => (), + other => panic!( + "Parser `is_not` didn't fail when it should have. Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_while1_succeed() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + fn while1_s(c: char) -> bool { + c == 'β' || c == 'è' || c == 'ƒ' || c == 'ô' || c == 'ř' || c == 'è' || c == 'Â' || c == 'ß' || c == 'Ç' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while1!(input, while1_s) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_while1` consumed leftover input." + ); + assert!( + output == CONSUMED, + "Parser `take_while1` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_while1` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_until_incomplete() { + const INPUT: &str = "βèƒôřè"; + const FIND: &str = "βèƒôřèÂßÇ"; + + let res: IResult<_,_,(_, ErrorKind)> = take_until!(INPUT, FIND); + match res { + Err(Err::Incomplete(_)) => (), + other => panic!( + "Parser `take_until` didn't require more input when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn is_a_succeed() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const MATCH: &str = "βèƒôřèÂßÇ"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + fn test(input: &str) -> IResult<&str, &str> { + is_a!(input, MATCH) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `is_a` consumed leftover input. Leftover `{}`.", + extra + ); + assert!( + output == CONSUMED, + "Parser `is_a` doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `is_a` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_while1_fail() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + fn while1_s(c: char) -> bool { + c == '9' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while1!(input, while1_s) + } + match test(INPUT) { + Err(Err::Error(_)) => (), + other => panic!( + "Parser `take_while1` didn't fail when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn is_a_fail() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const MATCH: &str = "Ûñℓúçƙ¥"; + fn test(input: &str) -> IResult<&str, &str> { + is_a!(input, MATCH) + } + match test(INPUT) { + Err(Err::Error(_)) => (), + other => panic!( + "Parser `is_a` didn't fail when it should have. Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_until_error() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const FIND: &str = "Ráñδô₥"; + + let res: IResult<_,_,(_, ErrorKind)> = take_until!(INPUT, FIND); + match res { + Err(Err::Incomplete(_)) => (), + other => panic!( + "Parser `take_until` didn't fail when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + #[cfg(feature = "alloc")] + fn recognize_is_a() { + let a = "aabbab"; + let b = "ababcd"; + + named!(f <&str,&str>, recognize!(many1!(complete!(alt!( tag!("a") | tag!("b") ))))); + + assert_eq!(f(&a[..]), Ok((&a[6..], &a[..]))); + assert_eq!(f(&b[..]), Ok((&b[4..], &b[..4]))); + } + + #[test] + fn utf8_indexing() { + named!(dot(&str) -> &str, + tag!(".") + ); + + let _ = dot("點"); + } + + #[cfg(feature = "alloc")] + #[test] + fn case_insensitive() { + named!(test<&str,&str>, tag_no_case!("ABcd")); + assert_eq!(test("aBCdefgh"), Ok(("efgh", "aBCd"))); + assert_eq!(test("abcdefgh"), Ok(("efgh", "abcd"))); + assert_eq!(test("ABCDefgh"), Ok(("efgh", "ABCD"))); + + named!(test2<&str,&str>, tag_no_case!("ABcd")); + assert_eq!(test2("aBCdefgh"), Ok(("efgh", "aBCd"))); + assert_eq!(test2("abcdefgh"), Ok(("efgh", "abcd"))); + assert_eq!(test2("ABCDefgh"), Ok(("efgh", "ABCD"))); + } +} diff --git a/third_party/rust/nom/src/traits.rs b/third_party/rust/nom/src/traits.rs new file mode 100644 index 0000000000..afd16ba251 --- /dev/null +++ b/third_party/rust/nom/src/traits.rs @@ -0,0 +1,1186 @@ +//! Traits input types have to implement to work with nom combinators +//! +use crate::internal::{Err, IResult, Needed}; +use crate::error::{ParseError, ErrorKind}; +use crate::lib::std::ops::{Range, RangeFrom, RangeFull, RangeTo}; +use crate::lib::std::iter::Enumerate; +use crate::lib::std::slice::Iter; +use crate::lib::std::iter::Map; +use crate::lib::std::str::Chars; +use crate::lib::std::str::CharIndices; +use crate::lib::std::str::FromStr; +use crate::lib::std::str::from_utf8; +use memchr; + +#[cfg(feature = "alloc")] +use crate::lib::std::string::String; +#[cfg(feature = "alloc")] +use crate::lib::std::vec::Vec; + +/// abstract method to calculate the input length +pub trait InputLength { + /// calculates the input length, as indicated by its name, + /// and the name of the trait itself + fn input_len(&self) -> usize; +} + +impl<'a, T> InputLength for &'a [T] { + #[inline] + fn input_len(&self) -> usize { + self.len() + } +} + +impl<'a> InputLength for &'a str { + #[inline] + fn input_len(&self) -> usize { + self.len() + } +} + +impl<'a> InputLength for (&'a [u8], usize) { + #[inline] + fn input_len(&self) -> usize { + //println!("bit input length for ({:?}, {}):", self.0, self.1); + //println!("-> {}", self.0.len() * 8 - self.1); + self.0.len() * 8 - self.1 + } +} + +/// useful functions to calculate the offset between slices and show a hexdump of a slice +pub trait Offset { + /// offset between the first byte of self and the first byte of the argument + fn offset(&self, second: &Self) -> usize; +} + +impl Offset for [u8] { + fn offset(&self, second: &Self) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); + + snd as usize - fst as usize + } +} + +impl<'a> Offset for &'a [u8] { + fn offset(&self, second: &Self) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); + + snd as usize - fst as usize + } +} + +impl Offset for str { + fn offset(&self, second: &Self) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); + + snd as usize - fst as usize + } +} + +impl<'a> Offset for &'a str { + fn offset(&self, second: &Self) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); + + snd as usize - fst as usize + } +} + +/// Helper trait for types that can be viewed as a byte slice +pub trait AsBytes { + /// casts the input type to a byte slice + fn as_bytes(&self) -> &[u8]; +} + +impl<'a> AsBytes for &'a str { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + <str as AsBytes>::as_bytes(self) + } +} + +impl AsBytes for str { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + self.as_ref() + } +} + +impl<'a> AsBytes for &'a [u8] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + *self + } +} + +impl AsBytes for [u8] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + self + } +} + +macro_rules! as_bytes_array_impls { + ($($N:expr)+) => { + $( + impl<'a> AsBytes for &'a [u8; $N] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + *self + } + } + + impl AsBytes for [u8; $N] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + self + } + } + )+ + }; +} + +as_bytes_array_impls! { + 0 1 2 3 4 5 6 7 8 9 + 10 11 12 13 14 15 16 17 18 19 + 20 21 22 23 24 25 26 27 28 29 + 30 31 32 +} + +/// transforms common types to a char for basic token parsing +pub trait AsChar { + /// makes a char from self + fn as_char(self) -> char; + + /// tests that self is an alphabetic character + /// + /// warning: for `&str` it recognizes alphabetic + /// characters outside of the 52 ASCII letters + fn is_alpha(self) -> bool; + + /// tests that self is an alphabetic character + /// or a decimal digit + fn is_alphanum(self) -> bool; + /// tests that self is a decimal digit + fn is_dec_digit(self) -> bool; + /// tests that self is an hex digit + fn is_hex_digit(self) -> bool; + /// tests that self is an octal digit + fn is_oct_digit(self) -> bool; + /// gets the len in bytes for self + fn len(self) -> usize; +} + +impl AsChar for u8 { + #[inline] + fn as_char(self) -> char { + self as char + } + #[inline] + fn is_alpha(self) -> bool { + (self >= 0x41 && self <= 0x5A) || (self >= 0x61 && self <= 0x7A) + } + #[inline] + fn is_alphanum(self) -> bool { + self.is_alpha() || self.is_dec_digit() + } + #[inline] + fn is_dec_digit(self) -> bool { + self >= 0x30 && self <= 0x39 + } + #[inline] + fn is_hex_digit(self) -> bool { + (self >= 0x30 && self <= 0x39) || (self >= 0x41 && self <= 0x46) || (self >= 0x61 && self <= 0x66) + } + #[inline] + fn is_oct_digit(self) -> bool { + self >= 0x30 && self <= 0x37 + } + #[inline] + fn len(self) -> usize { + 1 + } +} +impl<'a> AsChar for &'a u8 { + #[inline] + fn as_char(self) -> char { + *self as char + } + #[inline] + fn is_alpha(self) -> bool { + (*self >= 0x41 && *self <= 0x5A) || (*self >= 0x61 && *self <= 0x7A) + } + #[inline] + fn is_alphanum(self) -> bool { + self.is_alpha() || self.is_dec_digit() + } + #[inline] + fn is_dec_digit(self) -> bool { + *self >= 0x30 && *self <= 0x39 + } + #[inline] + fn is_hex_digit(self) -> bool { + (*self >= 0x30 && *self <= 0x39) || (*self >= 0x41 && *self <= 0x46) || (*self >= 0x61 && *self <= 0x66) + } + #[inline] + fn is_oct_digit(self) -> bool { + *self >= 0x30 && *self <= 0x37 + } + #[inline] + fn len(self) -> usize { + 1 + } +} + +impl AsChar for char { + #[inline] + fn as_char(self) -> char { + self + } + #[inline] + fn is_alpha(self) -> bool { + self.is_ascii_alphabetic() + } + #[inline] + fn is_alphanum(self) -> bool { + self.is_alpha() || self.is_dec_digit() + } + #[inline] + fn is_dec_digit(self) -> bool { + self.is_ascii_digit() + } + #[inline] + fn is_hex_digit(self) -> bool { + self.is_ascii_hexdigit() + } + #[inline] + fn is_oct_digit(self) -> bool { + self.is_digit(8) + } + #[inline] + fn len(self) -> usize { + self.len_utf8() + } +} + +impl<'a> AsChar for &'a char { + #[inline] + fn as_char(self) -> char { + *self + } + #[inline] + fn is_alpha(self) -> bool { + self.is_ascii_alphabetic() + } + #[inline] + fn is_alphanum(self) -> bool { + self.is_alpha() || self.is_dec_digit() + } + #[inline] + fn is_dec_digit(self) -> bool { + self.is_ascii_digit() + } + #[inline] + fn is_hex_digit(self) -> bool { + self.is_ascii_hexdigit() + } + #[inline] + fn is_oct_digit(self) -> bool { + self.is_digit(8) + } + #[inline] + fn len(self) -> usize { + self.len_utf8() + } +} + +/// abstracts common iteration operations on the input type +pub trait InputIter { + /// the current input type is a sequence of that `Item` type. + /// + /// example: `u8` for `&[u8]` or `char` for &str` + type Item; + /// an iterator over the input type, producing the item and its position + /// for use with [Slice]. If we're iterating over `&str`, the position + /// corresponds to the byte index of the character + type Iter: Iterator<Item = (usize, Self::Item)>; + + /// an iterator over the input type, producing the item + type IterElem: Iterator<Item = Self::Item>; + + /// returns an iterator over the elements and their byte offsets + fn iter_indices(&self) -> Self::Iter; + /// returns an iterator over the elements + fn iter_elements(&self) -> Self::IterElem; + /// finds the byte position of the element + fn position<P>(&self, predicate: P) -> Option<usize> + where + P: Fn(Self::Item) -> bool; + /// get the byte offset from the element's position in the stream + fn slice_index(&self, count: usize) -> Option<usize>; +} + +/// abstracts slicing operations +pub trait InputTake: Sized { + /// returns a slice of `count` bytes. panics if count > length + fn take(&self, count: usize) -> Self; + /// split the stream at the `count` byte offset. panics if count > length + fn take_split(&self, count: usize) -> (Self, Self); +} + +fn star(r_u8: &u8) -> u8 { + *r_u8 +} + +impl<'a> InputIter for &'a [u8] { + type Item = u8; + type Iter = Enumerate<Self::IterElem>; + type IterElem = Map<Iter<'a, Self::Item>, fn(&u8) -> u8>; + + #[inline] + fn iter_indices(&self) -> Self::Iter { + self.iter_elements().enumerate() + } + #[inline] + fn iter_elements(&self) -> Self::IterElem { + self.iter().map(star) + } + #[inline] + fn position<P>(&self, predicate: P) -> Option<usize> + where + P: Fn(Self::Item) -> bool, + { + self.iter().position(|b| predicate(*b)) + } + #[inline] + fn slice_index(&self, count: usize) -> Option<usize> { + if self.len() >= count { + Some(count) + } else { + None + } + } +} + +impl<'a> InputTake for &'a [u8] { + #[inline] + fn take(&self, count: usize) -> Self { + &self[0..count] + } + #[inline] + fn take_split(&self, count: usize) -> (Self, Self) { + let (prefix, suffix) = self.split_at(count); + (suffix, prefix) + } +} + +impl<'a> InputIter for &'a str { + type Item = char; + type Iter = CharIndices<'a>; + type IterElem = Chars<'a>; + #[inline] + fn iter_indices(&self) -> Self::Iter { + self.char_indices() + } + #[inline] + fn iter_elements(&self) -> Self::IterElem { + self.chars() + } + fn position<P>(&self, predicate: P) -> Option<usize> + where + P: Fn(Self::Item) -> bool, + { + for (o, c) in self.char_indices() { + if predicate(c) { + return Some(o); + } + } + None + } + #[inline] + fn slice_index(&self, count: usize) -> Option<usize> { + let mut cnt = 0; + for (index, _) in self.char_indices() { + if cnt == count { + return Some(index); + } + cnt += 1; + } + if cnt == count { + return Some(self.len()); + } + None + } +} + +impl<'a> InputTake for &'a str { + #[inline] + fn take(&self, count: usize) -> Self { + &self[..count] + } + + // return byte index + #[inline] + fn take_split(&self, count: usize) -> (Self, Self) { + (&self[count..], &self[..count]) + } +} + +/// Dummy trait used for default implementations (currently only used for `InputTakeAtPosition`). +/// +/// When implementing a custom input type, it is possible to use directly the +/// default implementation: if the input type implements `InputLength`, `InputIter`, +/// `InputTake` and `Clone`, you can implement `UnspecializedInput` and get +/// a default version of `InputTakeAtPosition`. +/// +/// For performance reasons, you might want to write a custom implementation of +/// `InputTakeAtPosition` (like the one for `&[u8]`). +pub trait UnspecializedInput {} + +/// methods to take as much input as possible until the provided function returns true for the current element +/// +/// a large part of nom's basic parsers are built using this trait +pub trait InputTakeAtPosition: Sized { + /// the current input type is a sequence of that `Item` type. + /// + /// example: `u8` for `&[u8]` or `char` for &str` + type Item; + + /// looks for the first element of the input type for which the condition returns true, + /// and returns the input up to this position + /// + /// *streaming version*: if no element is found matching the condition, this will return `Incomplete` + fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> + where + P: Fn(Self::Item) -> bool; + + /// looks for the first element of the input type for which the condition returns true + /// and returns the input up to this position + /// + /// fails if the produced slice is empty + /// + /// *streaming version*: if no element is found matching the condition, this will return `Incomplete` + fn split_at_position1<P, E: ParseError<Self>>(&self, predicate: P, e: ErrorKind) -> IResult<Self, Self, E> + where + P: Fn(Self::Item) -> bool; + + /// looks for the first element of the input type for which the condition returns true, + /// and returns the input up to this position + /// + /// *complete version*: if no element is found matching the condition, this will return the whole input + fn split_at_position_complete<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> + where + P: Fn(Self::Item) -> bool; + + /// looks for the first element of the input type for which the condition returns true + /// and returns the input up to this position + /// + /// fails if the produced slice is empty + /// + /// *complete version*: if no element is found matching the condition, this will return the whole input + fn split_at_position1_complete<P, E: ParseError<Self>>(&self, predicate: P, e: ErrorKind) -> IResult<Self, Self, E> + where + P: Fn(Self::Item) -> bool; +} + +impl<T: InputLength + InputIter + InputTake + Clone + UnspecializedInput> InputTakeAtPosition for T { + type Item = <T as InputIter>::Item; + + fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> + where + P: Fn(Self::Item) -> bool, + { + match self.position(predicate) { + Some(n) => Ok(self.take_split(n)), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position1<P, E: ParseError<Self>>(&self, predicate: P, e: ErrorKind) -> IResult<Self, Self, E> + where + P: Fn(Self::Item) -> bool, + { + match self.position(predicate) { + Some(0) => Err(Err::Error(E::from_error_kind(self.clone(), e))), + Some(n) => Ok(self.take_split(n)), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position_complete<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> + where P: Fn(Self::Item) -> bool { + match self.split_at_position(predicate) { + Err(Err::Incomplete(_)) => Ok(self.take_split(self.input_len())), + res => res, + } + } + + fn split_at_position1_complete<P, E: ParseError<Self>>(&self, predicate: P, e: ErrorKind) -> IResult<Self, Self, E> + where P: Fn(Self::Item) -> bool { + match self.split_at_position1(predicate, e) { + Err(Err::Incomplete(_)) => if self.input_len() == 0 { + Err(Err::Error(E::from_error_kind(self.clone(), e))) + } else { + Ok(self.take_split(self.input_len())) + } + res => res, + } + } +} + +impl<'a> InputTakeAtPosition for &'a [u8] { + type Item = u8; + + fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> + where + P: Fn(Self::Item) -> bool, + { + match (0..self.len()).find(|b| predicate(self[*b])) { + Some(i) => Ok((&self[i..], &self[..i])), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position1<P, E: ParseError<Self>>(&self, predicate: P, e: ErrorKind) -> IResult<Self, Self, E> + where + P: Fn(Self::Item) -> bool, + { + match (0..self.len()).find(|b| predicate(self[*b])) { + Some(0) => Err(Err::Error(E::from_error_kind(self, e))), + Some(i) => Ok((&self[i..], &self[..i])), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position_complete<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> + where P: Fn(Self::Item) -> bool { + match (0..self.len()).find(|b| predicate(self[*b])) { + Some(i) => Ok((&self[i..], &self[..i])), + None => Ok(self.take_split(self.input_len())), + } + } + + fn split_at_position1_complete<P, E: ParseError<Self>>(&self, predicate: P, e: ErrorKind) -> IResult<Self, Self, E> + where P: Fn(Self::Item) -> bool { + match (0..self.len()).find(|b| predicate(self[*b])) { + Some(0) => Err(Err::Error(E::from_error_kind(self, e))), + Some(i) => Ok((&self[i..], &self[..i])), + None => { + if self.len() == 0 { + Err(Err::Error(E::from_error_kind(self, e))) + } else { + Ok(self.take_split(self.input_len())) + } + }, + } + } +} + +impl<'a> InputTakeAtPosition for &'a str { + type Item = char; + + fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> + where + P: Fn(Self::Item) -> bool, + { + match self.find(predicate) { + Some(i) => Ok((&self[i..], &self[..i])), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position1<P, E: ParseError<Self>>(&self, predicate: P, e: ErrorKind) -> IResult<Self, Self, E> + where + P: Fn(Self::Item) -> bool, + { + match self.find(predicate) { + Some(0) => Err(Err::Error(E::from_error_kind(self, e))), + Some(i) => Ok((&self[i..], &self[..i])), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position_complete<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> + where P: Fn(Self::Item) -> bool { + match self.find(predicate) { + Some(i) => Ok((&self[i..], &self[..i])), + None => Ok(self.take_split(self.input_len())) + } + } + + fn split_at_position1_complete<P, E: ParseError<Self>>(&self, predicate: P, e: ErrorKind) -> IResult<Self, Self, E> + where P: Fn(Self::Item) -> bool { + match self.find(predicate) { + Some(0) => Err(Err::Error(E::from_error_kind(self, e))), + Some(i) => Ok((&self[i..], &self[..i])), + None => { + if self.len() == 0 { + Err(Err::Error(E::from_error_kind(self, e))) + } else { + Ok(self.take_split(self.input_len())) + } + }, + } + } +} + +/// indicates wether a comparison was successful, an error, or +/// if more data was needed +#[derive(Debug, PartialEq)] +pub enum CompareResult { + /// comparison was successful + Ok, + /// we need more data to be sure + Incomplete, + /// comparison failed + Error, +} + +/// abstracts comparison operations +pub trait Compare<T> { + /// compares self to another value for equality + fn compare(&self, t: T) -> CompareResult; + /// compares self to another value for equality + /// independently of the case. + /// + /// warning: for `&str`, the comparison is done + /// by lowercasing both strings and comparing + /// the result. This is a temporary solution until + /// a better one appears + fn compare_no_case(&self, t: T) -> CompareResult; +} + +impl<'a, 'b> Compare<&'b [u8]> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: &'b [u8]) -> CompareResult { + let pos = self.iter().zip(t.iter()).position(|(a, b)| a != b); + + match pos { + Some(_) => CompareResult::Error, + None => { + if self.len() >= t.len() { + CompareResult::Ok + } else { + CompareResult::Incomplete + } + } + } + + /* + let len = self.len(); + let blen = t.len(); + let m = if len < blen { len } else { blen }; + let reduced = &self[..m]; + let b = &t[..m]; + + if reduced != b { + CompareResult::Error + } else if m < blen { + CompareResult::Incomplete + } else { + CompareResult::Ok + } + */ + } + + #[inline(always)] + fn compare_no_case(&self, t: &'b [u8]) -> CompareResult { + let len = self.len(); + let blen = t.len(); + let m = if len < blen { len } else { blen }; + let reduced = &self[..m]; + let other = &t[..m]; + + if !reduced.iter().zip(other).all(|(a, b)| match (*a, *b) { + (0..=64, 0..=64) | (91..=96, 91..=96) | (123..=255, 123..=255) => a == b, + (65..=90, 65..=90) | (97..=122, 97..=122) | (65..=90, 97..=122) | (97..=122, 65..=90) => *a | 0b00_10_00_00 == *b | 0b00_10_00_00, + _ => false, + }) { + CompareResult::Error + } else if m < blen { + CompareResult::Incomplete + } else { + CompareResult::Ok + } + } +} + +impl<'a, 'b> Compare<&'b str> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: &'b str) -> CompareResult { + self.compare(AsBytes::as_bytes(t)) + } + #[inline(always)] + fn compare_no_case(&self, t: &'b str) -> CompareResult { + self.compare_no_case(AsBytes::as_bytes(t)) + } +} + +impl<'a, 'b> Compare<&'b str> for &'a str { + #[inline(always)] + fn compare(&self, t: &'b str) -> CompareResult { + let pos = self.chars().zip(t.chars()).position(|(a, b)| a != b); + + match pos { + Some(_) => CompareResult::Error, + None => { + if self.len() >= t.len() { + CompareResult::Ok + } else { + CompareResult::Incomplete + } + } + } + } + + //FIXME: this version is too simple and does not use the current locale + #[inline(always)] + fn compare_no_case(&self, t: &'b str) -> CompareResult { + let pos = self + .chars() + .zip(t.chars()) + .position(|(a, b)| a.to_lowercase().zip(b.to_lowercase()).any(|(a, b)| a != b)); + + match pos { + Some(_) => CompareResult::Error, + None => { + if self.len() >= t.len() { + CompareResult::Ok + } else { + CompareResult::Incomplete + } + } + } + } +} + +/// look for a token in self +pub trait FindToken<T> { + /// returns true if self contains the token + fn find_token(&self, token: T) -> bool; +} + +impl<'a> FindToken<u8> for &'a [u8] { + fn find_token(&self, token: u8) -> bool { + memchr::memchr(token, self).is_some() + } +} + +impl<'a> FindToken<u8> for &'a str { + fn find_token(&self, token: u8) -> bool { + self.as_bytes().find_token(token) + } +} + +impl<'a, 'b> FindToken<&'a u8> for &'b [u8] { + fn find_token(&self, token: &u8) -> bool { + memchr::memchr(*token, self).is_some() + } +} + +impl<'a, 'b> FindToken<&'a u8> for &'b str { + fn find_token(&self, token: &u8) -> bool { + self.as_bytes().find_token(token) + } +} + +impl<'a> FindToken<char> for &'a [u8] { + fn find_token(&self, token: char) -> bool { + for i in self.iter() { + if token as u8 == *i { + return true; + } + } + false + } +} + +impl<'a> FindToken<char> for &'a str { + fn find_token(&self, token: char) -> bool { + for i in self.chars() { + if token == i { + return true; + } + } + false + } +} + +/// look for a substring in self +pub trait FindSubstring<T> { + /// returns the byte position of the substring if it is found + fn find_substring(&self, substr: T) -> Option<usize>; +} + +impl<'a, 'b> FindSubstring<&'b [u8]> for &'a [u8] { + fn find_substring(&self, substr: &'b [u8]) -> Option<usize> { + let substr_len = substr.len(); + + if substr_len == 0 { + // an empty substring is found at position 0 + // This matches the behavior of str.find(""). + Some(0) + } else if substr_len == 1 { + memchr::memchr(substr[0], self) + } else if substr_len > self.len() { + None + } else { + let max = self.len() - substr_len; + let mut offset = 0; + let mut haystack = &self[..]; + + while let Some(position) = memchr::memchr(substr[0], haystack) { + offset += position; + + if offset > max { + return None; + } + + if &haystack[position..position + substr_len] == substr { + return Some(offset); + } + + haystack = &haystack[position + 1..]; + offset += 1; + } + + None + } + } +} + +impl<'a, 'b> FindSubstring<&'b str> for &'a [u8] { + fn find_substring(&self, substr: &'b str) -> Option<usize> { + self.find_substring(AsBytes::as_bytes(substr)) + } +} + +impl<'a, 'b> FindSubstring<&'b str> for &'a str { + //returns byte index + fn find_substring(&self, substr: &'b str) -> Option<usize> { + self.find(substr) + } +} + +/// used to integrate str's parse() method +pub trait ParseTo<R> { + /// succeeds if `parse()` succeeded. The byte slice implementation + /// will first convert it to a &str, then apply the `parse()` function + fn parse_to(&self) -> Option<R>; +} + +impl<'a, R: FromStr> ParseTo<R> for &'a [u8] { + fn parse_to(&self) -> Option<R> { + from_utf8(self).ok().and_then(|s| s.parse().ok()) + } +} + +impl<'a, R: FromStr> ParseTo<R> for &'a str { + fn parse_to(&self) -> Option<R> { + self.parse().ok() + } +} + +/// slicing operations using ranges +/// +/// this trait is loosely based on +/// `Index`, but can actually return +/// something else than a `&[T]` or `&str` +pub trait Slice<R> { + /// slices self according to the range argument + fn slice(&self, range: R) -> Self; +} + +macro_rules! impl_fn_slice { + ( $ty:ty ) => { + fn slice(&self, range:$ty) -> Self { + &self[range] + } + } +} + +macro_rules! slice_range_impl { + ( [ $for_type:ident ], $ty:ty ) => { + impl<'a, $for_type> Slice<$ty> for &'a [$for_type] { + impl_fn_slice!( $ty ); + } + }; + ( $for_type:ty, $ty:ty ) => { + impl<'a> Slice<$ty> for &'a $for_type { + impl_fn_slice!( $ty ); + } + } +} + +macro_rules! slice_ranges_impl { + ( [ $for_type:ident ] ) => { + slice_range_impl! {[$for_type], Range<usize>} + slice_range_impl! {[$for_type], RangeTo<usize>} + slice_range_impl! {[$for_type], RangeFrom<usize>} + slice_range_impl! {[$for_type], RangeFull} + }; + ( $for_type:ty ) => { + slice_range_impl! {$for_type, Range<usize>} + slice_range_impl! {$for_type, RangeTo<usize>} + slice_range_impl! {$for_type, RangeFrom<usize>} + slice_range_impl! {$for_type, RangeFull} + } +} + +slice_ranges_impl! {str} +slice_ranges_impl! {[T]} + +macro_rules! array_impls { + ($($N:expr)+) => { + $( + impl InputLength for [u8; $N] { + #[inline] + fn input_len(&self) -> usize { + self.len() + } + } + + impl<'a> InputLength for &'a [u8; $N] { + #[inline] + fn input_len(&self) -> usize { + self.len() + } + } + + impl<'a> Compare<[u8; $N]> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: [u8; $N]) -> CompareResult { + self.compare(&t[..]) + } + + #[inline(always)] + fn compare_no_case(&self, t: [u8;$N]) -> CompareResult { + self.compare_no_case(&t[..]) + } + } + + impl<'a,'b> Compare<&'b [u8; $N]> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: &'b [u8; $N]) -> CompareResult { + self.compare(&t[..]) + } + + #[inline(always)] + fn compare_no_case(&self, t: &'b [u8;$N]) -> CompareResult { + self.compare_no_case(&t[..]) + } + } + + impl FindToken<u8> for [u8; $N] { + fn find_token(&self, token: u8) -> bool { + memchr::memchr(token, &self[..]).is_some() + } + } + + impl<'a> FindToken<&'a u8> for [u8; $N] { + fn find_token(&self, token: &u8) -> bool { + memchr::memchr(*token, &self[..]).is_some() + } + } + )+ + }; +} + +array_impls! { + 0 1 2 3 4 5 6 7 8 9 + 10 11 12 13 14 15 16 17 18 19 + 20 21 22 23 24 25 26 27 28 29 + 30 31 32 +} + +/// abstracts something which can extend an `Extend` +/// used to build modified input slices in `escaped_transform` +pub trait ExtendInto { + + /// the current input type is a sequence of that `Item` type. + /// + /// example: `u8` for `&[u8]` or `char` for &str` + type Item; + + /// the type that will be produced + type Extender: Extend<Self::Item>; + + /// create a new `Extend` of the correct type + fn new_builder(&self) -> Self::Extender; + /// accumulate the input into an accumulator + fn extend_into(&self, acc: &mut Self::Extender); +} + +#[cfg(feature = "alloc")] +impl ExtendInto for [u8] { + type Item = u8; + type Extender = Vec<u8>; + + #[inline] + fn new_builder(&self) -> Vec<u8> { + Vec::new() + } + #[inline] + fn extend_into(&self, acc: &mut Vec<u8>) { + acc.extend(self.iter().cloned()); + } +} + +#[cfg(feature = "alloc")] +impl ExtendInto for &[u8] { + type Item = u8; + type Extender = Vec<u8>; + + #[inline] + fn new_builder(&self) -> Vec<u8> { + Vec::new() + } + #[inline] + fn extend_into(&self, acc: &mut Vec<u8>) { + acc.extend(self.iter().cloned()); + } +} + + +#[cfg(feature = "alloc")] +impl ExtendInto for str { + type Item = char; + type Extender = String; + + #[inline] + fn new_builder(&self) -> String { + String::new() + } + #[inline] + fn extend_into(&self, acc: &mut String) { + acc.push_str(self); + } +} + +#[cfg(feature = "alloc")] +impl ExtendInto for &str { + type Item = char; + type Extender = String; + + #[inline] + fn new_builder(&self) -> String { + String::new() + } + #[inline] + fn extend_into(&self, acc: &mut String) { + acc.push_str(self); + } +} + +#[cfg(feature = "alloc")] +impl ExtendInto for char { + type Item = char; + type Extender = String; + + #[inline] + fn new_builder(&self) -> String { + String::new() + } + #[inline] + fn extend_into(&self, acc: &mut String) { + acc.push(*self); + } +} + +/// Helper trait to convert numbers to usize +/// +/// by default, usize implements `From<u8>` and `From<u16>` but not +/// `From<u32>` and `From<u64>` because that would be invalid on some +/// platforms. This trait implements the conversion for platforms +/// with 32 and 64 bits pointer platforms +pub trait ToUsize { + /// converts self to usize + fn to_usize(&self) -> usize; +} + +impl ToUsize for u8 { + #[inline] + fn to_usize(&self) -> usize { + *self as usize + } +} + +impl ToUsize for u16 { + #[inline] + fn to_usize(&self) -> usize { + *self as usize + } +} + +impl ToUsize for usize { + #[inline] + fn to_usize(&self) -> usize { + *self + } +} + +#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] +impl ToUsize for u32 { + #[inline] + fn to_usize(&self) -> usize { + *self as usize + } +} + +#[cfg(target_pointer_width = "64")] +impl ToUsize for u64 { + #[inline] + fn to_usize(&self) -> usize { + *self as usize + } +} + +/// equivalent From implementation to avoid orphan rules in bits parsers +pub trait ErrorConvert<E> { + /// transform to another error type + fn convert(self) -> E; +} + +impl<I> ErrorConvert<(I, ErrorKind)> for ((I, usize), ErrorKind) { + fn convert(self) -> (I, ErrorKind) { + ((self.0).0, self.1) + } +} + +impl<I> ErrorConvert<((I, usize), ErrorKind)> for (I, ErrorKind) { + fn convert(self) -> ((I, usize), ErrorKind) { + ((self.0, 0), self.1) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_offset_u8() { + let s = b"abcd123"; + let a = &s[..]; + let b = &a[2..]; + let c = &a[..4]; + let d = &a[3..5]; + assert_eq!(a.offset(b), 2); + assert_eq!(a.offset(c), 0); + assert_eq!(a.offset(d), 3); + } + + #[test] + fn test_offset_str() { + let s = "abcřèÂßÇd123"; + let a = &s[..]; + let b = &a[7..]; + let c = &a[..5]; + let d = &a[5..9]; + assert_eq!(a.offset(b), 7); + assert_eq!(a.offset(c), 0); + assert_eq!(a.offset(d), 5); + } +} diff --git a/third_party/rust/nom/src/util.rs b/third_party/rust/nom/src/util.rs new file mode 100644 index 0000000000..488bbfb7d1 --- /dev/null +++ b/third_party/rust/nom/src/util.rs @@ -0,0 +1,214 @@ +#[cfg(feature = "std")] +use crate::internal::IResult; +#[cfg(feature = "std")] +use std::fmt::Debug; + +#[cfg(feature = "std")] +/// Helper trait to show a byte slice as a hex dump +pub trait HexDisplay { + /// Converts the value of `self` to a hex dump, returning the owned + /// string. + fn to_hex(&self, chunk_size: usize) -> String; + + /// Converts the value of `self` to a hex dump beginning at `from` address, returning the owned + /// string. + fn to_hex_from(&self, chunk_size: usize, from: usize) -> String; +} + +#[cfg(feature = "std")] +static CHARS: &'static [u8] = b"0123456789abcdef"; + +#[cfg(feature = "std")] +impl HexDisplay for [u8] { + #[allow(unused_variables)] + fn to_hex(&self, chunk_size: usize) -> String { + self.to_hex_from(chunk_size, 0) + } + + #[allow(unused_variables)] + fn to_hex_from(&self, chunk_size: usize, from: usize) -> String { + let mut v = Vec::with_capacity(self.len() * 3); + let mut i = from; + for chunk in self.chunks(chunk_size) { + let s = format!("{:08x}", i); + for &ch in s.as_bytes().iter() { + v.push(ch); + } + v.push(b'\t'); + + i += chunk_size; + + for &byte in chunk { + v.push(CHARS[(byte >> 4) as usize]); + v.push(CHARS[(byte & 0xf) as usize]); + v.push(b' '); + } + if chunk_size > chunk.len() { + for j in 0..(chunk_size - chunk.len()) { + v.push(b' '); + v.push(b' '); + v.push(b' '); + } + } + v.push(b'\t'); + + for &byte in chunk { + if (byte >= 32 && byte <= 126) || byte >= 128 { + v.push(byte); + } else { + v.push(b'.'); + } + } + v.push(b'\n'); + } + + String::from_utf8_lossy(&v[..]).into_owned() + } +} + +#[cfg(feature = "std")] +impl HexDisplay for str { + #[allow(unused_variables)] + fn to_hex(&self, chunk_size: usize) -> String { + self.to_hex_from(chunk_size, 0) + } + + #[allow(unused_variables)] + fn to_hex_from(&self, chunk_size: usize, from: usize) -> String { + self.as_bytes().to_hex_from(chunk_size, from) + } +} + +#[doc(hidden)] +#[macro_export] +macro_rules! nom_line ( + () => (line!()); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! nom_println ( + ($($args:tt)*) => (println!($($args)*)); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! nom_stringify ( + ($($args:tt)*) => (stringify!($($args)*)); +); + + +/// Prints a message if the parser fails +/// +/// The message prints the `Error` or `Incomplete` +/// and the parser's calling code +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(f, dbg!( tag!( "abcd" ) ) ); +/// +/// let a = &b"efgh"[..]; +/// +/// // Will print the following message: +/// // Error(Position(0, [101, 102, 103, 104])) at l.5 by ' tag ! ( "abcd" ) ' +/// f(a); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! dbg ( + ($i: expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + let l = nom_line!(); + match $submac!($i, $($args)*) { + Err(e) => { + nom_println!("Err({:?}) at l.{} by ' {} '", e, l, nom_stringify!($submac!($($args)*))); + Err(e) + }, + a => a, + } + } + ); + + ($i:expr, $f:ident) => ( + dbg!($i, call!($f)); + ); +); + +/// Prints a message and the input if the parser fails +/// +/// The message prints the `Error` or `Incomplete` +/// and the parser's calling code. +/// +/// It also displays the input in hexdump format +/// +/// ```rust +/// use nom::{IResult, dbg_dmp, bytes::complete::tag}; +/// +/// fn f(i: &[u8]) -> IResult<&[u8], &[u8]> { +/// dbg_dmp(tag("abcd"), "tag")(i) +/// } +/// +/// let a = &b"efghijkl"[..]; +/// +/// // Will print the following message: +/// // Error(Position(0, [101, 102, 103, 104, 105, 106, 107, 108])) at l.5 by ' tag ! ( "abcd" ) ' +/// // 00000000 65 66 67 68 69 6a 6b 6c efghijkl +/// f(a); +/// ``` +#[cfg(feature = "std")] +pub fn dbg_dmp<'a, F, O, E: Debug>(f: F, context: &'static str) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], O, E> + where F: Fn(&'a [u8]) -> IResult<&'a [u8], O, E> { + move |i: &'a [u8]| { + match f(i) { + Err(e) => { + println!("{}: Error({:?}) at:\n{}", context, e, i.to_hex(8)); + Err(e) + }, + a => a, + } + } +} + +/// Prints a message and the input if the parser fails +/// +/// The message prints the `Error` or `Incomplete` +/// and the parser's calling code. +/// +/// It also displays the input in hexdump format +/// +/// ```ignore +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(f, dbg_dmp!( tag!( "abcd" ) ) ); +/// +/// let a = &b"efghijkl"[..]; +/// +/// // Will print the following message: +/// // Error(Position(0, [101, 102, 103, 104, 105, 106, 107, 108])) at l.5 by ' tag ! ( "abcd" ) ' +/// // 00000000 65 66 67 68 69 6a 6b 6c efghijkl +/// f(a); +/// # } +#[macro_export(local_inner_macros)] +#[cfg(feature = "std")] +macro_rules! dbg_dmp ( + ($i: expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::HexDisplay; + let l = nom_line!(); + match $submac!($i, $($args)*) { + Err(e) => { + nom_println!("Error({:?}) at l.{} by ' {} '\n{}", e, l, nom_stringify!($submac!($($args)*)), $i.to_hex(8)); + Err(e) + }, + a => a, + } + } + ); + + ($i:expr, $f:ident) => ( + dbg_dmp!($i, call!($f)); + ); +); + diff --git a/third_party/rust/nom/src/whitespace.rs b/third_party/rust/nom/src/whitespace.rs new file mode 100644 index 0000000000..9ed3f55aad --- /dev/null +++ b/third_party/rust/nom/src/whitespace.rs @@ -0,0 +1,1077 @@ +//! Support for whitespace delimited formats +//! +//! a lot of textual formats allows spaces and other +//! types of separators between tokens. Handling it +//! manually with nom means wrapping all parsers +//! like this: +//! +//! ```ignore +//! named!(token, delimited!(space, tk, space)); +//! ``` +//! +//! To ease the development of such parsers, you +//! can use the whitespace parsing facility, which works +//! as follows: +//! +//! ``` +//! # #[macro_use] extern crate nom; +//! # fn main() { +//! named!(tuple<&[u8], (&[u8], &[u8]) >, +//! ws!(tuple!( take!(3), tag!("de") )) +//! ); +//! +//! assert_eq!( +//! tuple(&b" \t abc de fg"[..]), +//! Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))) +//! ); +//! # } +//! ``` +//! +//! The `ws!` combinator will modify the parser to +//! intersperse space parsers everywhere. By default, +//! it will consume the following characters: `" \t\r\n"`. +//! +//! If you want to modify that behaviour, you can make +//! your own whitespace wrapper. As an example, if +//! you don't want to consume ends of lines, only +//! spaces and tabs, you can do it like this: +//! +//! ``` +//! # #[macro_use] extern crate nom; +//! named!(pub space, eat_separator!(&b" \t"[..])); +//! +//! #[macro_export] +//! macro_rules! sp ( +//! ($i:expr, $($args:tt)*) => ( +//! { +//! use nom::Err; +//! +//! match sep!($i, space, $($args)*) { +//! Err(e) => Err(e), +//! Ok((i1,o)) => { +//! match space(i1) { +//! Err(e) => Err(Err::convert(e)), +//! Ok((i2,_)) => Ok((i2, o)) +//! } +//! } +//! } +//! } +//! ) +//! ); +//! +//! # fn main() { +//! named!(tuple<&[u8], (&[u8], &[u8]) >, +//! sp!(tuple!( take!(3), tag!("de") )) +//! ); +//! +//! assert_eq!( +//! tuple(&b" \t abc de fg"[..]), +//! Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))) +//! ); +//! # } +//! ``` +//! +//! This combinator works by replacing each combinator with +//! a version that supports wrapping with separator parsers. +//! It will not support the combinators you wrote in your +//! own code. You can still manually wrap them with the separator +//! you want, or you can copy the macros defined in src/whitespace.rs +//! and modify them to support a new combinator: +//! +//! * copy the combinator's code here, add the _sep suffix +//! * add the `$separator:expr` as second argument +//! * wrap any sub parsers with sep!($separator, $submac!($($args)*)) +//! * reference it in the definition of `sep!` as follows: +//! +//! ```ignore +//! ($i:expr, $separator:path, my_combinator ! ($($rest:tt)*) ) => { +//! wrap_sep!($i, +//! $separator, +//! my_combinator_sep!($separator, $($rest)*) +//! ) +//! }; +//! ``` +//! + +/// applies the separator parser before the other parser +#[macro_export(local_inner_macros)] +macro_rules! wrap_sep ( + ($i:expr, $separator:expr, $submac:ident!( $($args:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + use $crate::{Err,IResult}; + + fn unify_types<I,O,P,E>(_: &IResult<I,O,E>, _: &IResult<I,P,E>) {} + + let sep_res = ($separator)($i); + match sep_res { + Ok((i1,_)) => { + let res = $submac!(i1, $($args)*); + unify_types(&sep_res, &res); + res + }, + Err(e) => Err(Err::convert(e)), + } + }); + ($i:expr, $separator:expr, $f:expr) => ( + wrap_sep!($i, $separator, call!($f)) + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! pair_sep ( + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + tuple!( + $i, + sep!($separator, $submac!($($args)*)), + sep!($separator, $submac2!($($args2)*)) + ) + ); + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => ( + pair_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => ( + pair_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:path, $f:expr, $g:expr) => ( + pair_sep!($i, $separator, call!($f), call!($g)); + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! delimited_sep ( + ($i:expr, $separator:path, $submac1:ident!( $($args1:tt)* ), $($rest:tt)+) => ({ + use $crate::lib::std::result::Result::*; + + match tuple_sep!($i, $separator, (), $submac1!($($args1)*), $($rest)+) { + Err(e) => Err(e), + Ok((remaining, (_,o,_))) => { + Ok((remaining, o)) + } + } + }); + ($i:expr, $separator:path, $f:expr, $($rest:tt)+) => ( + delimited_sep!($i, $separator, call!($f), $($rest)+); + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! separated_pair_sep ( + ($i:expr, $separator:path, $submac1:ident!( $($args1:tt)* ), $($rest:tt)+) => ({ + use $crate::lib::std::result::Result::*; + + match tuple_sep!($i, $separator, (), $submac1!($($args1)*), $($rest)+) { + Err(e) => Err(e), + Ok((remaining, (o1,_,o2))) => { + Ok((remaining, (o1,o2))) + } + } + }); + ($i:expr, $separator:path, $f:expr, $($rest:tt)+) => ( + separated_pair_sep!($i, $separator, call!($f), $($rest)+); + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! preceded_sep ( + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match pair_sep!($i, $separator, $submac!($($args)*), $submac2!($($args2)*)) { + Err(e) => Err(e), + Ok((remaining, (_,o))) => { + Ok((remaining, o)) + } + } + }); + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => ( + preceded_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => ( + preceded_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:path, $f:expr, $g:expr) => ( + preceded_sep!($i, $separator, call!($f), call!($g)); + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! terminated_sep ( + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match pair_sep!($i, $separator, $submac!($($args)*), $submac2!($($args2)*)) { + Err(e) => Err(e), + Ok((remaining, (o,_))) => { + Ok((remaining, o)) + } + } + }); + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => ( + terminated_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => ( + terminated_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:path, $f:expr, $g:expr) => ( + terminated_sep!($i, $separator, call!($f), call!($g)); + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! tuple_sep ( + ($i:expr, $separator:path, ($($parsed:tt),*), $e:path, $($rest:tt)*) => ( + tuple_sep!($i, $separator, ($($parsed),*), call!($e), $($rest)*); + ); + ($i:expr, $separator:path, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + tuple_sep!(i, $separator, (o), $($rest)*) + } + } + } + ); + ($i:expr, $separator:path, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + tuple_sep!(i, $separator, ($($parsed)* , o), $($rest)*) + } + } + } + ); + ($i:expr, $separator:path, ($($parsed:tt),*), $e:path) => ( + tuple_sep!($i, $separator, ($($parsed),*), call!($e)); + ); + ($i:expr, $separator:path, (), $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + Ok((i, (o))) + } + } + } + ); + ($i:expr, $separator:path, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + Ok((i, ($($parsed),* , o))) + } + } + } + ); + ($i:expr, $separator:path, ($($parsed:expr),*)) => ( + { + ::sts::result::Result::Ok(($i, ($($parsed),*))) + } + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! do_parse_sep ( + (__impl $i:expr, $separator:path, ( $($rest:expr),* )) => ( + $crate::lib::std::result::Result::Ok(($i, ( $($rest),* ))) + ); + + (__impl $i:expr, $separator:path, $e:ident >> $($rest:tt)*) => ( + do_parse_sep!(__impl $i, $separator, call!($e) >> $($rest)*); + ); + (__impl $i:expr, $separator:path, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,_)) => { + do_parse_sep!(__impl i, $separator, $($rest)*) + }, + } + } + ); + + (__impl $i:expr, $separator:path, $field:ident : $e:ident >> $($rest:tt)*) => ( + do_parse_sep!(__impl $i, $separator, $field: call!($e) >> $($rest)*); + ); + + (__impl $i:expr, $separator:path, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + let $field = o; + do_parse_sep!(__impl i, $separator, $($rest)*) + }, + } + } + ); + + // ending the chain + (__impl $i:expr, $separator:path, $e:ident >> ( $($rest:tt)* )) => ( + do_parse_sep!(__impl $i, $separator, call!($e) >> ( $($rest)* )); + ); + + (__impl $i:expr, $separator:path, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,_)) => { + Ok((i, ( $($rest)* ))) + }, + } + }); + + (__impl $i:expr, $separator:path, $field:ident : $e:ident >> ( $($rest:tt)* )) => ( + do_parse_sep!(__impl $i, $separator, $field: call!($e) >> ( $($rest)* ) ); + ); + + (__impl $i:expr, $separator:path, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + let $field = o; + Ok((i, ( $($rest)* ))) + }, + } + }); + + ($i:expr, $separator:path, $($rest:tt)*) => ( + { + do_parse_sep!(__impl $i, $separator, $($rest)*) + } + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! permutation_sep ( + ($i:expr, $separator:path, $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::{Err,error::ErrorKind}; + + let mut res = permutation_init!((), $($rest)*); + let mut input = $i; + let mut error = None; + let mut needed = None; + + loop { + let mut all_done = true; + permutation_iterator_sep!(0, input, $separator, all_done, needed, res, $($rest)*); + + //if we reach that part, it means none of the parsers were able to read anything + if !all_done { + //FIXME: should wrap the error returned by the child parser + error = Option::Some(error_position!(input, ErrorKind::Permutation)); + } + break; + } + + if let Some(need) = needed { + Err(Err::convert(need)) + } else { + if let Some(unwrapped_res) = { permutation_unwrap!(0, (), res, $($rest)*) } { + Ok((input, unwrapped_res)) + } else { + if let Some(e) = error { + Err(Err::Error(error_node_position!($i, ErrorKind::Permutation, e))) + } else { + Err(Err::Error(error_position!($i, ErrorKind::Permutation))) + } + } + } + } + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! permutation_iterator_sep ( + ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident?, $($rest:tt)*) => ( + permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, call!($e), $($rest)*); + ); + ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident, $($rest:tt)*) => ( + permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, call!($e), $($rest)*); + ); + + ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ({ + permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, $submac!($($args)*), $($rest)*); + }); + ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ({ + use $crate::lib::std::result::Result::*; + use $crate::Err; + + if $res.$it == $crate::lib::std::option::Option::None { + match {sep!($i, $separator, $submac!($($args)*))} { + Ok((i,o)) => { + $i = i; + $res.$it = $crate::lib::std::option::Option::Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + $needed = $crate::lib::std::option::Option::Some(e); + break; + } + }; + } + succ!($it, permutation_iterator_sep!($i, $separator, $all_done, $needed, $res, $($rest)*)); + }); + + ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident?) => ( + permutation_iterator_sep!($it, $i, $separator, $all_done, $res, call!($e)); + ); + ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident) => ( + permutation_iterator_sep!($it, $i, $separator, $all_done, $res, call!($e)); + ); + + ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )?) => ({ + permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, $submac!($($args)*)); + }); + ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + use $crate::Err; + + if $res.$it == $crate::lib::std::option::Option::None { + match sep!($i, $separator, $submac!($($args)*)) { + Ok((i,o)) => { + $i = i; + $res.$it = $crate::lib::std::option::Option::Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + $needed = $crate::lib::std::option::Option::Some(e); + break; + } + }; + } + }); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! alt_sep ( + (__impl $i:expr, $separator:path, $e:path | $($rest:tt)*) => ( + alt_sep!(__impl $i, $separator, call!($e) | $($rest)*); + ); + + (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let res = sep!($i, $separator, $subrule!($($args)*)); + match res { + Ok((_,_)) => res, + Err(Err::Error(_)) => alt_sep!(__impl $i, $separator, $($rest)*), + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match sep!($i, $separator, $subrule!( $($args)* )) { + Ok((i,o)) => Ok((i,$gen(o))), + Err(Err::Error(_)) => { + alt_sep!(__impl $i, $separator, $($rest)+) + }, + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr, $separator:path, $e:path => { $gen:expr } | $($rest:tt)*) => ( + alt_sep!(__impl $i, $separator, call!($e) => { $gen } | $($rest)*); + ); + + (__impl $i:expr, $separator:path, $e:path => { $gen:expr }) => ( + alt_sep!(__impl $i, $separator, call!($e) => { $gen }); + ); + + (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match sep!($i, $separator, $subrule!( $($args)* )) { + Ok((i,o)) => Ok((i,$gen(o))), + Err(Err::Error(e)) => { + fn unify_types<T>(_: &T, _: &T) {} + let e2 = error_position!($i, $crate::error::ErrorKind::Alt); + unify_types(&e, &e2); + Err(Err::Error(e2)) + }, + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr, $separator:path, $e:path) => ( + alt_sep!(__impl $i, $separator, call!($e)); + ); + + (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)*)) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match sep!($i, $separator, $subrule!( $($args)* )) { + Ok((i,o)) => Ok((i,o)), + Err(Err::Error(e)) => { + fn unify_types<T>(_: &T, _: &T) {} + let e2 = error_position!($i, $crate::error::ErrorKind::Alt); + unify_types(&e, &e2); + Err(Err::Error(e2)) + }, + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr) => ({ + use $crate::lib::std::result::Result::*; + use $crate::{Err,Needed,IResult}; + + Err(Err::Error(error_position!($i, $crate::error::ErrorKind::Alt))) + }); + + (__impl $i:expr, $separator:path) => ({ + use $crate::lib::std::result::Result::*; + use $crate::{Err,Needed,IResult}; + + Err(Err::Error(error_position!($i, $crate::error::ErrorKind::Alt))) + }); + + ($i:expr, $separator:path, $($rest:tt)*) => ( + { + alt_sep!(__impl $i, $separator, $($rest)*) + } + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! switch_sep ( + (__impl $i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(Err::Error(e)) => Err(Err::Error(error_node_position!( + $i, $crate::error::ErrorKind::Switch, e + ))), + Err(Err::Failure(e)) => Err(Err::Failure( + error_node_position!($i, $crate::error::ErrorKind::Switch, e))), + Err(e) => Err(e), + Ok((i, o)) => { + match o { + $($p => match sep!(i, $separator, $subrule!($($args2)*)) { + Err(Err::Error(e)) => Err(Err::Error(error_node_position!( + $i, $crate::error::ErrorKind::Switch, e + ))), + Err(Err::Failure(e)) => Err(Err::Failure( + error_node_position!($i, $crate::error::ErrorKind::Switch, e))), + a => a, + }),*, + _ => Err(Err::Error(error_position!($i, $crate::error::ErrorKind::Switch))) + } + } + } + } + ); + ($i:expr, $separator:path, $submac:ident!( $($args:tt)*), $($rest:tt)*) => ( + { + switch_sep!(__impl $i, $separator, $submac!($($args)*), $($rest)*) + } + ); + ($i:expr, $separator:path, $e:path, $($rest:tt)*) => ( + { + switch_sep!(__impl $i, $separator, call!($e), $($rest)*) + } + ); +); + +#[doc(hidden)] +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! separated_list_sep ( + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + separated_list!( + $i, + sep!($separator, $submac!($($args)*)), + sep!($separator, $submac2!($($args2)*)) + ) + ); + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_list_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_list_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:path, $f:expr, $g:expr) => ( + separated_list_sep!($i, $separator, call!($f), call!($g)); + ); +); + +/// helper macros to build a separator parser +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// named!(pub space, eat_separator!(&b" \t"[..])); +/// # fn main() {} +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! eat_separator ( + ($i:expr, $arr:expr) => ( + { + use $crate::{FindToken, InputTakeAtPosition}; + let input = $i; + input.split_at_position(|c| !$arr.find_token(c)) + } + ); +); + +/// sep is the parser rewriting macro for whitespace separated formats +/// +/// it takes as argument a space eating function and a parser tree, +/// and will intersperse the space parser everywhere +/// +/// ```ignore +/// #[macro_export(local_inner_macros)] +/// macro_rules! ws ( +/// ($i:expr, $($args:tt)*) => ( +/// { +/// use sp; +/// sep!($i, sp, $($args)*) +/// } +/// ) +/// ); +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! sep ( + ($i:expr, $separator:path, tuple ! ($($rest:tt)*) ) => { + tuple_sep!($i, $separator, (), $($rest)*) + }; + ($i:expr, $separator:path, pair ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + pair_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, delimited ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + delimited_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, separated_pair ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + separated_pair_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, preceded ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + preceded_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, terminated ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + terminated_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, do_parse ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + do_parse_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, permutation ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + permutation_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, alt ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + alt_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, switch ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + switch_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, separated_list ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + separated_list_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, many0 ! ($($rest:tt)*) ) => { + many0!($i, wrap_sep!($separator, $($rest)*)) + }; + ($i:expr, $separator:path, many1 ! ($($rest:tt)*) ) => { + many1!($i, wrap_sep!($separator, $($rest)*)) + }; + ($i:expr, $separator:path, return_error!( $($args:tt)* )) => { + return_error!($i, wrap_sep!($separator, $($args)*)) + }; +//FIXME: missing separated_nonempty_list, +// many_till, many_m_n, count, count_fixed, fold_many0, fold_many1, +// fold_many_m_n + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* )) => { + wrap_sep!($i, $separator, $submac!($($args)*)) + }; + ($i:expr, $separator:path, $f:expr) => { + wrap_sep!($i, $separator, call!($f)) + }; +); + +/// `ws!(I -> IResult<I,O>) => I -> IResult<I, O>` +/// +/// transforms a parser to automatically consume +/// whitespace between each token. By default, +/// it takes the following characters: `" \t\r\n"`. +/// +/// If you need a whitespace parser consuming a +/// different set of characters, you can make +/// your own by reusing the `sep!` combinator. +/// +/// To use `ws!`, pass your parser as argument: +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(tuple<&[u8], (&[u8], &[u8]) >, +/// ws!(tuple!( take!(3), tag!("de") )) +/// ); +/// +/// assert_eq!( +/// tuple(&b" \t abc de fg"[..]), +/// Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))) +/// ); +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +#[deprecated(since = "5.0.0", note = "whitespace parsing only works with macros and will not be updated anymore")] +macro_rules! ws ( + ($i:expr, $($args:tt)*) => ( + { + use $crate::Err; + use $crate::lib::std::result::Result::*; + use $crate::character::complete::multispace0; + + match sep!($i, multispace0, $($args)*) { + Err(e) => Err(e), + Ok((i1,o)) => { + match (multispace0)(i1) { + Err(e) => Err(Err::convert(e)), + Ok((i2,_)) => Ok((i2, o)) + } + } + } + } + ) +); + +#[cfg(test)] +#[allow(dead_code)] +mod tests { + #[cfg(feature = "alloc")] + use crate::{ + error::ParseError, + lib::std::{ + string::{String, ToString}, + fmt::Debug + } + }; + use crate::internal::{Err, IResult, Needed}; + use crate::character::complete::multispace0 as sp; + use crate::error::ErrorKind; + + #[test] + fn spaaaaace() { + assert_eq!(sp::<_,(_,ErrorKind)>(&b" \t abc "[..]), Ok((&b"abc "[..], &b" \t "[..]))); + } + + #[test] + fn tag() { + named!(abc, ws!(tag!("abc"))); + + assert_eq!(abc(&b" \t abc def"[..]), Ok((&b"def"[..], &b"abc"[..]))); + } + + #[test] + fn pair() { + named!(pair_2<&[u8], (&[u8], &[u8]) >, + ws!(pair!( take!(3), tag!("de") )) + ); + + assert_eq!( + pair_2(&b" \t abc de fg"[..]), + Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))) + ); + } + + #[test] + fn preceded() { + named!(prec<&[u8], &[u8] >, + ws!(preceded!( take!(3), tag!("de") )) + ); + + assert_eq!(prec(&b" \t abc de fg"[..]), Ok((&b"fg"[..], &b"de"[..]))); + } + + #[test] + fn terminated() { + named!(term<&[u8], &[u8] >, + ws!(terminated!( take!(3), tag!("de") )) + ); + + assert_eq!(term(&b" \t abc de fg"[..]), Ok((&b"fg"[..], &b"abc"[..]))); + } + + #[test] + fn tuple() { + //trace_macros!(true); + named!(tuple_2<&[u8], (&[u8], &[u8]) >, + ws!(tuple!( take!(3), tag!("de") )) + ); + //trace_macros!(false); + + assert_eq!( + tuple_2(&b" \t abc de fg"[..]), + Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))) + ); + } + + #[test] + fn levels() { + //trace_macros!(true); + named!(level_2<&[u8], (&[u8], (&[u8], &[u8])) >, + ws!(pair!(take!(3), tuple!( tag!("de"), tag!("fg ") ))) + ); + //trace_macros!(false); + + assert_eq!( + level_2(&b" \t abc de fg \t hi "[..]), + Ok((&b"hi "[..], (&b"abc"[..], (&b"de"[..], &b"fg "[..])))) + ); + } + + #[test] + fn do_parse() { + fn ret_int1(i: &[u8]) -> IResult<&[u8], u8> { + Ok((i, 1)) + }; + fn ret_int2(i: &[u8]) -> IResult<&[u8], u8> { + Ok((i, 2)) + }; + + //trace_macros!(true); + named!(do_parser<&[u8], (u8, u8)>, + ws!(do_parse!( + tag!("abcd") >> + opt!(tag!("abcd")) >> + aa: ret_int1 >> + tag!("efgh") >> + bb: ret_int2 >> + tag!("efgh") >> + (aa, bb) + )) + ); + + //trace_macros!(false); + + assert_eq!( + do_parser(&b"abcd abcd\tefghefghX"[..]), + Ok((&b"X"[..], (1, 2))) + ); + assert_eq!( + do_parser(&b"abcd\tefgh efgh X"[..]), + Ok((&b"X"[..], (1, 2))) + ); + assert_eq!( + do_parser(&b"abcd ab"[..]), + Err(Err::Incomplete(Needed::Size(4))) + ); + assert_eq!( + do_parser(&b" abcd\tefgh\tef"[..]), + Err(Err::Incomplete(Needed::Size(4))) + ); + } + + #[test] + fn permutation() { + //trace_macros!(true); + named!( + perm<(&[u8], &[u8], &[u8])>, + ws!(permutation!(tag!("abcd"), tag!("efg"), tag!("hi"))) + ); + //trace_macros!(false); + + let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); + + let a = &b"abcd\tefg \thijk"[..]; + assert_eq!(perm(a), Ok((&b"jk"[..], expected))); + let b = &b" efg \tabcdhi jk"[..]; + assert_eq!(perm(b), Ok((&b"jk"[..], expected))); + let c = &b" hi efg\tabcdjk"[..]; + assert_eq!(perm(c), Ok((&b"jk"[..], expected))); + + let d = &b"efg xyzabcdefghi"[..]; + assert_eq!( + perm(d), + Err(Err::Error(error_node_position!( + &b"efg xyzabcdefghi"[..], + ErrorKind::Permutation, + error_position!(&b" xyzabcdefghi"[..], ErrorKind::Permutation) + ))) + ); + + let e = &b" efg \tabc"[..]; + assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[cfg(feature = "alloc")] + #[derive(Debug, Clone, PartialEq)] + pub struct ErrorStr(String); + + #[cfg(feature = "alloc")] + impl<'a> From<(&'a[u8], ErrorKind)> for ErrorStr { + fn from(i: (&'a[u8], ErrorKind)) -> Self { + ErrorStr(format!("custom error code: {:?}", i)) + } + } + + #[cfg(feature = "alloc")] + impl<'a> From<(&'a str, ErrorKind)> for ErrorStr { + fn from(i: (&'a str, ErrorKind)) -> Self { + ErrorStr(format!("custom error message: {:?}", i)) + } + } + + #[cfg(feature = "alloc")] + impl<I: Debug> ParseError<I> for ErrorStr { + fn from_error_kind(input: I, kind: ErrorKind) -> Self { + ErrorStr(format!("custom error message: ({:?}, {:?})", input, kind)) + } + + fn append(input: I, kind: ErrorKind, other: Self) -> Self { + ErrorStr(format!("custom error message: ({:?}, {:?}) - {:?}", input, kind, other)) + } + } + + #[cfg(feature = "alloc")] + #[test] + fn alt() { + fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Ok((&b""[..], input)) + } + + #[allow(unused_variables)] + fn dont_work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Err(Err::Error(ErrorStr("abcd".to_string()))) + } + + fn work2(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Ok((input, &b""[..])) + } + + fn alt1(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | dont_work) + } + fn alt2(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | work) + } + fn alt3(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | dont_work | work2 | dont_work) + } + + let a = &b"\tabcd"[..]; + assert_eq!( + alt1(a), + Err(Err::Error(error_position!(a, ErrorKind::Alt))) + ); + assert_eq!(alt2(a), Ok((&b""[..], a))); + assert_eq!(alt3(a), Ok((a, &b""[..]))); + + } + + named!(str_parse(&str) -> &str, ws!(tag!("test"))); + #[allow(unused_variables)] + #[test] + fn str_test() { + assert_eq!(str_parse(" \n test\t a\nb"), Ok(("a\nb", "test"))); + } + + // test whitespace parser generation for alt + named!(space, tag!(" ")); + #[cfg(feature = "alloc")] + named!(pipeline_statement<&[u8], ()>, + ws!( + do_parse!( + tag!("pipeline") >> + attributes: delimited!(char!('{'), + separated_list!(char!(','), alt!( + space | + space + )), + char!('}')) >> + + ({ + let _ = attributes; + () + }) + ) + ) + ); + + #[cfg(feature = "alloc")] + named!( + fail<&[u8]>, + map!(many_till!(take!(1), ws!(tag!("."))), |(r, _)| r[0]) + ); +} diff --git a/third_party/rust/nom/tests/arithmetic.rs b/third_party/rust/nom/tests/arithmetic.rs new file mode 100644 index 0000000000..b3c38931df --- /dev/null +++ b/third_party/rust/nom/tests/arithmetic.rs @@ -0,0 +1,109 @@ +extern crate nom; + + +use nom::{ + IResult, + branch::alt, + combinator::map_res, + character::complete::char, + bytes::complete::tag, + character::complete::{digit1 as digit, space0 as space}, + multi::fold_many0, + sequence::{delimited, pair} +}; + +// Parser definition + +use std::str::FromStr; + +// We parse any expr surrounded by parens, ignoring all whitespaces around those +fn parens(i: &str) -> IResult<&str, i64> { + delimited( + space, + delimited( + tag("("), + expr, + tag(")") + ), + space + )(i) +} + +// We transform an integer string into a i64, ignoring surrounding whitespaces +// We look for a digit suite, and try to convert it. +// If either str::from_utf8 or FromStr::from_str fail, +// we fallback to the parens parser defined above +fn factor(i: &str) -> IResult<&str, i64> { + alt(( + map_res(delimited(space, digit, space), FromStr::from_str), + parens + ))(i) +} + +// We read an initial factor and for each time we find +// a * or / operator followed by another factor, we do +// the math by folding everything +fn term(i: &str) -> IResult<&str, i64> { + let (i, init) = factor(i)?; + + fold_many0( + pair(alt((char('*'), char('/'))), factor), + init, + |acc, (op, val): (char, i64)| { + if op == '*' { acc * val } else { acc / val } + } + )(i) +} + +fn expr(i: &str) -> IResult<&str, i64> { + let (i, init) = term(i)?; + + fold_many0( + pair(alt((char('+'), char('-'))), term), + init, + |acc, (op, val): (char, i64)| { + if op == '+' { acc + val } else { acc - val } + } + )(i) +} + +#[test] +fn factor_test() { + assert_eq!(factor("3"), Ok(("", 3))); + assert_eq!(factor(" 12"), Ok(("", 12))); + assert_eq!(factor("537 "), Ok(("", 537))); + assert_eq!(factor(" 24 "), Ok(("", 24))); +} + +#[test] +fn term_test() { + assert_eq!(term(" 12 *2 / 3"), Ok(("", 8))); + assert_eq!( + term(" 2* 3 *2 *2 / 3"), + Ok(("", 8)) + ); + assert_eq!(term(" 48 / 3/2"), Ok(("", 8))); +} + +#[test] +fn expr_test() { + assert_eq!(expr(" 1 + 2 "), Ok(("", 3))); + assert_eq!( + expr(" 12 + 6 - 4+ 3"), + Ok(("", 17)) + ); + assert_eq!(expr(" 1 + 2*3 + 4"), Ok(("", 11))); +} + +#[test] +fn parens_test() { + assert_eq!(expr(" ( 2 )"), Ok(("", 2))); + assert_eq!( + expr(" 2* ( 3 + 4 ) "), + Ok(("", 14)) + ); + assert_eq!( + expr(" 2*2 / ( 5 - 1) + 3"), + Ok(("", 4)) + ); +} diff --git a/third_party/rust/nom/tests/arithmetic_ast.rs b/third_party/rust/nom/tests/arithmetic_ast.rs new file mode 100644 index 0000000000..ecb8d40641 --- /dev/null +++ b/third_party/rust/nom/tests/arithmetic_ast.rs @@ -0,0 +1,172 @@ +extern crate nom; + +use std::fmt; +use std::fmt::{Debug, Display, Formatter}; + +use std::str::FromStr; + +use nom::{ + IResult, + character::complete::{digit1 as digit, multispace0 as multispace}, + sequence::{preceded, delimited}, + combinator::{map, map_res}, + multi::many0, + branch::alt, + bytes::complete::tag, +}; + +pub enum Expr { + Value(i64), + Add(Box<Expr>, Box<Expr>), + Sub(Box<Expr>, Box<Expr>), + Mul(Box<Expr>, Box<Expr>), + Div(Box<Expr>, Box<Expr>), + Paren(Box<Expr>), +} + +#[derive(Debug)] +pub enum Oper { + Add, + Sub, + Mul, + Div, +} + +impl Display for Expr { + fn fmt(&self, format: &mut Formatter) -> fmt::Result { + use self::Expr::*; + match *self { + Value(val) => write!(format, "{}", val), + Add(ref left, ref right) => write!(format, "{} + {}", left, right), + Sub(ref left, ref right) => write!(format, "{} - {}", left, right), + Mul(ref left, ref right) => write!(format, "{} * {}", left, right), + Div(ref left, ref right) => write!(format, "{} / {}", left, right), + Paren(ref expr) => write!(format, "({})", expr), + } + } +} + +impl Debug for Expr { + fn fmt(&self, format: &mut Formatter) -> fmt::Result { + use self::Expr::*; + match *self { + Value(val) => write!(format, "{}", val), + Add(ref left, ref right) => write!(format, "({:?} + {:?})", left, right), + Sub(ref left, ref right) => write!(format, "({:?} - {:?})", left, right), + Mul(ref left, ref right) => write!(format, "({:?} * {:?})", left, right), + Div(ref left, ref right) => write!(format, "({:?} / {:?})", left, right), + Paren(ref expr) => write!(format, "[{:?}]", expr), + } + } +} + +fn parens(i: &str) -> IResult<&str, Expr> { + delimited( + multispace, + delimited( + tag("("), + map(expr, |e| Expr::Paren(Box::new(e))), + tag(")") + ), + multispace + )(i) +} + +fn factor(i: &str) -> IResult<&str, Expr> { + alt(( + map( + map_res( + delimited(multispace, digit, multispace), + FromStr::from_str + ), + Expr::Value + ), + parens + ))(i) +} + +fn fold_exprs(initial: Expr, remainder: Vec<(Oper, Expr)>) -> Expr { + remainder.into_iter().fold(initial, |acc, pair| { + let (oper, expr) = pair; + match oper { + Oper::Add => Expr::Add(Box::new(acc), Box::new(expr)), + Oper::Sub => Expr::Sub(Box::new(acc), Box::new(expr)), + Oper::Mul => Expr::Mul(Box::new(acc), Box::new(expr)), + Oper::Div => Expr::Div(Box::new(acc), Box::new(expr)), + } + }) +} + +fn term(i: &str) -> IResult<&str, Expr> { + let (i, initial) = factor(i)?; + let (i, remainder) = many0(alt(( + |i| { + let(i, mul) = preceded(tag("*"), factor)(i)?; + Ok((i,(Oper::Mul, mul))) + }, + |i| { + let(i, div) = preceded(tag("/"), factor)(i)?; + Ok((i, (Oper::Div, div))) + }, + + )))(i)?; + + Ok((i, fold_exprs(initial, remainder))) +} + +fn expr(i: &str) -> IResult<&str, Expr> { + let (i, initial) = term(i)?; + let (i, remainder) = many0(alt(( + |i| { + let(i, add) = preceded(tag("+"), term)(i)?; + Ok((i,(Oper::Add, add))) + }, + |i| { + let(i, sub) = preceded(tag("-"), term)(i)?; + Ok((i, (Oper::Sub, sub))) + }, + + )))(i)?; + + Ok((i, fold_exprs(initial, remainder))) +} + +#[test] +fn factor_test() { + assert_eq!( + factor(" 3 ").map(|(i, x)| (i, format!("{:?}", x))), + Ok(("", String::from("3"))) + ); +} + +#[test] +fn term_test() { + assert_eq!( + term(" 3 * 5 ").map(|(i, x)| (i, format!("{:?}", x))), + Ok(("", String::from("(3 * 5)"))) + ); +} + +#[test] +fn expr_test() { + assert_eq!( + expr(" 1 + 2 * 3 ").map(|(i, x)| (i, format!("{:?}", x))), + Ok(("", String::from("(1 + (2 * 3))"))) + ); + assert_eq!( + expr(" 1 + 2 * 3 / 4 - 5 ").map(|(i, x)| (i, format!("{:?}", x))), + Ok(("", String::from("((1 + ((2 * 3) / 4)) - 5)"))) + ); + assert_eq!( + expr(" 72 / 2 / 3 ").map(|(i, x)| (i, format!("{:?}", x))), + Ok(("", String::from("((72 / 2) / 3)"))) + ); +} + +#[test] +fn parens_test() { + assert_eq!( + expr(" ( 1 + 2 ) * 3 ").map(|(i, x)| (i, format!("{:?}", x))), + Ok(("", String::from("([(1 + 2)] * 3)"))) + ); +} diff --git a/third_party/rust/nom/tests/blockbuf-arithmetic.rs b/third_party/rust/nom/tests/blockbuf-arithmetic.rs new file mode 100644 index 0000000000..27a295fc77 --- /dev/null +++ b/third_party/rust/nom/tests/blockbuf-arithmetic.rs @@ -0,0 +1,321 @@ +/* +#[macro_use] +extern crate nom; +extern crate bytes; + +use nom::{Compare,CompareResult,InputLength,InputIter,Slice,HexDisplay}; + +use std::str; +use std::str::FromStr; +use bytes::{Buf,MutBuf}; +use bytes::buf::{BlockBuf,BlockBufCursor}; +use std::ops::{Range,RangeTo,RangeFrom,RangeFull}; +use std::iter::{Enumerate,Iterator}; +use std::fmt; +use std::cmp::{min,PartialEq}; + +#[derive(Clone,Copy)] +#[repr(C)] +pub struct BlockSlice<'a> { + buf: &'a BlockBuf, + start: usize, + end: usize, +} + +impl<'a> BlockSlice<'a> { + fn cursor(&self) -> WrapCursor<'a> { + let mut cur = self.buf.buf(); + cur.advance(self.start); + WrapCursor { + cursor: cur, + length: self.end - self.start, + } + } +} + +impl<'a> fmt::Debug for BlockSlice<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "BlockSlice {{ start: {}, end: {}, data:\n{}\n}}", self.start, self.end, self.buf.bytes().unwrap_or(&b""[..]).to_hex(16)) + } +} + +impl<'a> PartialEq for BlockSlice<'a> { + fn eq(&self, other: &BlockSlice<'a>) -> bool { + let bufs = (self.buf as *const BlockBuf) == (other.buf as *const BlockBuf); + self.start == other.start && self.end == other.end && bufs + } +} + +impl<'a> Slice<Range<usize>> for BlockSlice<'a> { + fn slice(&self, range:Range<usize>) -> Self { + BlockSlice { + buf: self.buf, + start: self.start + range.start, + //FIXME: check for valid end here + end: self.start + range.end, + } + } +} + +impl<'a> Slice<RangeTo<usize>> for BlockSlice<'a> { + fn slice(&self, range:RangeTo<usize>) -> Self { + self.slice(0..range.end) + } +} + +impl<'a> Slice<RangeFrom<usize>> for BlockSlice<'a> { + fn slice(&self, range:RangeFrom<usize>) -> Self { + self.slice(range.start..self.end - self.start) + } +} + +impl<'a> Slice<RangeFull> for BlockSlice<'a> { + fn slice(&self, _:RangeFull) -> Self { + BlockSlice { + buf: self.buf, + start: self.start, + end: self.end, + } + } +} + + +impl<'a> InputIter for BlockSlice<'a> { + type Item = u8; + type RawItem = u8; + type Iter = Enumerate<WrapCursor<'a>>; + type IterElem = WrapCursor<'a>; + + fn iter_indices(&self) -> Self::Iter { + self.cursor().enumerate() + } + fn iter_elements(&self) -> Self::IterElem { + self.cursor() + } + fn position<P>(&self, predicate: P) -> Option<usize> where P: Fn(Self::RawItem) -> bool { + self.cursor().position(|b| predicate(b)) + } + fn slice_index(&self, count:usize) -> Option<usize> { + if self.end - self.start >= count { + Some(count) + } else { + None + } + } +} + + +impl<'a> InputLength for BlockSlice<'a> { + fn input_len(&self) -> usize { + self.end - self.start + } +} + +impl<'a,'b> Compare<&'b[u8]> for BlockSlice<'a> { + fn compare(&self, t: &'b[u8]) -> CompareResult { + let len = self.end - self.start; + let blen = t.len(); + let m = if len < blen { len } else { blen }; + let reduced = self.slice(..m); + let b = &t[..m]; + + for (a,b) in reduced.cursor().zip(b.iter()) { + if a != *b { + return CompareResult::Error; + } + } + if m < blen { + CompareResult::Incomplete + } else { + CompareResult::Ok + } + } + + + #[inline(always)] + fn compare_no_case(&self, t: &'b[u8]) -> CompareResult { + let len = self.end - self.start; + let blen = t.len(); + let m = if len < blen { len } else { blen }; + let reduced = self.slice(..m); + let other = &t[..m]; + + if !reduced.cursor().zip(other).all(|(a, b)| { + match (a,*b) { + (0...64, 0...64) | (91...96, 91...96) | (123...255, 123...255) => a == *b, + (65...90, 65...90) | (97...122, 97...122) | (65...90, 97...122 ) |(97...122, 65...90) => { + a & 0b01000000 == *b & 0b01000000 + } + _ => false + } + }) { + CompareResult::Error + } else if m < blen { + CompareResult::Incomplete + } else { + CompareResult::Ok + } + } +} + +impl<'a,'b> Compare<&'b str> for BlockSlice<'a> { + fn compare(&self, t: &'b str) -> CompareResult { + self.compare(str::as_bytes(t)) + } + fn compare_no_case(&self, t: &'b str) -> CompareResult { + self.compare_no_case(str::as_bytes(t)) + } +} + +//Wrapper to implement Iterator on BlockBufCursor +pub struct WrapCursor<'a> { + pub cursor: BlockBufCursor<'a>, + pub length: usize, +} + +impl<'a> Iterator for WrapCursor<'a> { + type Item = u8; + fn next(&mut self) -> Option<u8> { + //println!("NEXT: length={}, remaining={}", self.length, self.cursor.remaining()); + if min(self.length, self.cursor.remaining()) > 0 { + self.length -=1; + Some(self.cursor.read_u8()) + } else { + None + } + } +} + +//Reimplement eat_separator instead of fixing iterators +#[macro_export] +macro_rules! block_eat_separator ( + ($i:expr, $arr:expr) => ( + { + use nom::{InputLength,InputIter,Slice}; + if ($i).input_len() == 0 { + Ok(($i, ($i).slice(0..0))) + } else { + match ($i).iter_indices().position(|(_, item)| { + for (_,c) in ($arr).iter_indices() { + if *c == item { return false; } + } + true + }) { + Some(index) => { + Ok((($i).slice(index..), ($i).slice(..index))) + }, + None => { + Ok((($i).slice(($i).input_len()..), $i)) + } + } + } + } + ) +); + +#[macro_export] +macro_rules! block_named ( + ($name:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name<'a>( i: BlockSlice<'a> ) -> nom::IResult<BlockSlice<'a>, BlockSlice<'a>, u32> { + $submac!(i, $($args)*) + } + ); + ($name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + fn $name<'a>( i: BlockSlice<'a> ) -> nom::IResult<BlockSlice<'a>, $o, u32> { + $submac!(i, $($args)*) + } + ); +); + +block_named!(sp, block_eat_separator!(&b" \t\r\n"[..])); + +macro_rules! block_ws ( + ($i:expr, $($args:tt)*) => ( + { + sep!($i, sp, $($args)*) + } + ) +); + +block_named!(digit, is_a!("0123456789")); + +block_named!(parens<i64>, block_ws!(delimited!( tag!("("), expr, tag!(")") )) ); + + +block_named!(factor<i64>, alt!( + map_res!( + block_ws!(digit), + to_i64 + ) + | parens + ) +); + +block_named!(term <i64>, do_parse!( + init: factor >> + res: fold_many0!( + pair!(alt!(tag!("*") | tag!("/")), factor), + init, + |acc, (op, val): (BlockSlice, i64)| { + if (op.cursor().next().unwrap() as char) == '*' { acc * val } else { acc / val } + } + ) >> + (res) + ) +); + +block_named!(expr <i64>, do_parse!( + init: term >> + res: fold_many0!( + pair!(alt!(tag!("+") | tag!("-")), term), + init, + |acc, (op, val): (BlockSlice, i64)| { + if (op.cursor().next().unwrap() as char) == '+' { acc + val } else { acc - val } + } + ) >> + (res) + ) +); + + +fn blockbuf_from(input: &[u8]) -> BlockBuf { + let mut b = BlockBuf::new(2, 100); + b.copy_from(input); + b +} + + +fn sl<'a>(input: &'a BlockBuf) -> BlockSlice<'a> { + BlockSlice { + buf: input, + start: 0, + end: input.len(), + } +} + +fn to_i64<'a>(input: BlockSlice<'a>) -> Result<i64, ()> { + let v: Vec<u8> = input.cursor().collect(); + + match str::from_utf8(&v) { + Err(_) => Err(()), + Ok(s) => match FromStr::from_str(s) { + Err(_) => Err(()), + Ok(i) => Ok(i) + } + } +} + +#[test] +fn factor_test() { + let a = blockbuf_from(&b"3"[..]); + println!("calculated: {:?}", factor(sl(&a))); +} + +#[test] +fn parens_test() { + let input1 = blockbuf_from(&b" 2* ( 3 + 4 ) "[..]); + println!("calculated 1: {:?}", expr(sl(&input1))); + let input2 = blockbuf_from(&b" 2*2 / ( 5 - 1) + 3"[..]); + println!("calculated 2: {:?}", expr(sl(&input2))); +} +*/ diff --git a/third_party/rust/nom/tests/css.rs b/third_party/rust/nom/tests/css.rs new file mode 100644 index 0000000000..0ce7e77b05 --- /dev/null +++ b/third_party/rust/nom/tests/css.rs @@ -0,0 +1,51 @@ +extern crate nom; + +use nom::IResult; +use nom::bytes::complete::{tag, take_while_m_n}; +use nom::combinator::map_res; +use nom::sequence::tuple; + +#[derive(Debug, PartialEq)] +pub struct Color { + pub red: u8, + pub green: u8, + pub blue: u8, +} + +fn from_hex(input: &str) -> Result<u8, std::num::ParseIntError> { + u8::from_str_radix(input, 16) +} + +fn is_hex_digit(c: char) -> bool { + c.is_digit(16) +} + +fn hex_primary(input: &str) -> IResult<&str, u8> { + map_res( + take_while_m_n(2, 2, is_hex_digit), + from_hex + )(input) +} + +fn hex_color(input: &str) -> IResult<&str, Color> { + let (input, _) = tag("#")(input)?; + let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?; + + Ok((input, Color { red, green, blue })) +} + + +#[test] +fn parse_color() { + assert_eq!( + hex_color("#2F14DF"), + Ok(( + "", + Color { + red: 47, + green: 20, + blue: 223, + } + )) + ); +} diff --git a/third_party/rust/nom/tests/custom_errors.rs b/third_party/rust/nom/tests/custom_errors.rs new file mode 100644 index 0000000000..89366dc9e1 --- /dev/null +++ b/third_party/rust/nom/tests/custom_errors.rs @@ -0,0 +1,53 @@ +#![allow(dead_code)] +#![cfg_attr(feature = "cargo-clippy", allow(block_in_if_condition_stmt))] + +#[macro_use] +extern crate nom; + +use nom::IResult; +use nom::error::{ErrorKind,ParseError}; +use nom::character::streaming::digit1 as digit; + +use std::convert::From; + +#[derive(Debug)] +pub struct CustomError(String); + +impl<'a> From<(&'a str, ErrorKind)> for CustomError { + fn from(error: (&'a str, ErrorKind)) -> Self { + CustomError(format!("error code was: {:?}", error)) + } +} + +impl<'a> ParseError<&'a str> for CustomError { + fn from_error_kind(_: &'a str, kind: ErrorKind) -> Self { + CustomError(format!("error code was: {:?}", kind)) + } + + fn append(_: &'a str, kind: ErrorKind, other: CustomError) -> Self { + CustomError(format!("{:?}\nerror code was: {:?}", other, kind)) + + } + +} + +fn test1(input: &str) -> IResult<&str, &str, CustomError> { + //fix_error!(input, CustomError, tag!("abcd")) + tag!(input, "abcd") +} + +fn test2(input: &str) -> IResult<&str, &str, CustomError> { + //terminated!(input, test1, fix_error!(CustomError, digit)) + terminated!(input, test1, digit) +} + +fn test3(input: &str) -> IResult<&str, &str, CustomError> { + verify!(input, test1, |s: &str| { + s.starts_with("abcd") + }) +} + +#[cfg(feature = "alloc")] +fn test4(input: &str) -> IResult<&str, Vec<&str>, CustomError> { + count!(input, test1, 4) +} diff --git a/third_party/rust/nom/tests/escaped.rs b/third_party/rust/nom/tests/escaped.rs new file mode 100644 index 0000000000..19c1ed70a4 --- /dev/null +++ b/third_party/rust/nom/tests/escaped.rs @@ -0,0 +1,25 @@ +use nom::{Err, error::ErrorKind, IResult}; +use nom::character::complete::digit1; +use nom::bytes::complete::escaped; +use nom::character::complete::one_of; + +fn esc(s: &str) -> IResult<&str, &str> { + escaped(digit1, '\\', one_of("\"n\\"))(s) +} + +#[cfg(feature="alloc")] +fn esc_trans(s: &str) -> IResult<&str, String> { + use nom::bytes::complete::{escaped_transform, tag}; + escaped_transform(digit1, '\\', |i: &str| tag("n")(i))(s) +} + +#[test] +fn test_escaped() { + assert_eq!(esc("abcd"), Err(Err::Error(("abcd", ErrorKind::Escaped)))); +} + +#[test] +#[cfg(feature="alloc")] +fn test_escaped_transform() { + assert_eq!(esc_trans("abcd"), Err(Err::Error(("abcd", ErrorKind::EscapedTransform)))); +} diff --git a/third_party/rust/nom/tests/float.rs b/third_party/rust/nom/tests/float.rs new file mode 100644 index 0000000000..eb82804f29 --- /dev/null +++ b/third_party/rust/nom/tests/float.rs @@ -0,0 +1,46 @@ +#[macro_use] +extern crate nom; + +use nom::character::streaming::digit1 as digit; + +use std::str; +use std::str::FromStr; + +named!( + unsigned_float<f32>, + map_res!( + map_res!( + recognize!(alt!( + delimited!(digit, tag!("."), opt!(digit)) | delimited!(opt!(digit), tag!("."), digit) + )), + str::from_utf8 + ), + FromStr::from_str + ) +); + +named!( + float<f32>, + map!( + pair!(opt!(alt!(tag!("+") | tag!("-"))), unsigned_float), + |(sign, value): (Option<&[u8]>, f32)| sign + .and_then(|s| if s[0] == b'-' { Some(-1f32) } else { None }) + .unwrap_or(1f32) * value + ) +); + +#[test] +fn unsigned_float_test() { + assert_eq!(unsigned_float(&b"123.456;"[..]), Ok((&b";"[..], 123.456))); + assert_eq!(unsigned_float(&b"0.123;"[..]), Ok((&b";"[..], 0.123))); + assert_eq!(unsigned_float(&b"123.0;"[..]), Ok((&b";"[..], 123.0))); + assert_eq!(unsigned_float(&b"123.;"[..]), Ok((&b";"[..], 123.0))); + assert_eq!(unsigned_float(&b".123;"[..]), Ok((&b";"[..], 0.123))); +} + +#[test] +fn float_test() { + assert_eq!(float(&b"123.456;"[..]), Ok((&b";"[..], 123.456))); + assert_eq!(float(&b"+123.456;"[..]), Ok((&b";"[..], 123.456))); + assert_eq!(float(&b"-123.456;"[..]), Ok((&b";"[..], -123.456))); +} diff --git a/third_party/rust/nom/tests/inference.rs b/third_party/rust/nom/tests/inference.rs new file mode 100644 index 0000000000..851c1382c9 --- /dev/null +++ b/third_party/rust/nom/tests/inference.rs @@ -0,0 +1,53 @@ +//! test type inference issues in parsee compilation +//#![feature(trace_macros)] +#![allow(dead_code)] +#![allow(unused_comparisons)] +#![allow(unused_variables)] +#![allow(unused_imports)] + +#[macro_use] +extern crate nom; + +use std::str; +use nom::character::{streaming::alpha1 as alpha, is_digit}; + +// issue #617 +named!(multi<&[u8], () >, fold_many0!( take_while1!( is_digit ), (), |_, _| {})); + +// issue #561 +#[cfg(feature = "alloc")] +named!( + value<Vec<Vec<&str>>>, + do_parse!( + first_line: map_res!(is_not!("\n"), std::str::from_utf8) + >> rest: + many_m_n!( + 0, + 1, + separated_list!( + tag!("\n\t"), + map_res!(take_while!(call!(|c| c != b'\n')), std::str::from_utf8) + ) + ) >> (rest) + ) +); + +// issue #534 +#[cfg(feature = "alloc")] +fn wrap_suffix(input: &Option<Vec<&[u8]>>) -> Option<String> { + if input.is_some() { + // I've tried both of the lines below individually and get the same error. + Some("hello".to_string()) + //Some(str::from_utf8(u).expect("Found invalid UTF-8").to_string()) + } else { + None + } +} + +#[cfg(feature = "alloc")] +named!(parse_suffix<&[u8],Option<String>>,do_parse!( + u: opt!(many1!(alt!( + complete!(tag!("%")) | complete!(tag!("#")) | complete!(tag!("@")) | complete!(alpha) + ))) >> + (wrap_suffix(&u)) +)); diff --git a/third_party/rust/nom/tests/ini.rs b/third_party/rust/nom/tests/ini.rs new file mode 100644 index 0000000000..c3f9b750d7 --- /dev/null +++ b/third_party/rust/nom/tests/ini.rs @@ -0,0 +1,221 @@ +#[macro_use] +extern crate nom; + +use nom::{ + IResult, + bytes::complete::take_while, + sequence::delimited, + combinator::map_res, + character::complete::{char, alphanumeric1 as alphanumeric, multispace0 as multispace, space0 as space} +}; + +use std::str; +use std::collections::HashMap; + +fn category(i: &[u8]) -> IResult<&[u8], &str> { + map_res(delimited(char('['), take_while(|c| c != b']'), char(']')), str::from_utf8)(i) +} + +fn complete_byte_slice_to_str<'a>(s: &'a[u8]) -> Result<&'a str, str::Utf8Error> { + str::from_utf8(s) +} + +named!(key_value <&[u8],(&str,&str)>, + do_parse!( + key: map_res!(alphanumeric, complete_byte_slice_to_str) + >> opt!(space) + >> char!('=') + >> opt!(space) + >> val: map_res!( + take_while!(call!(|c| c != b'\n' && c != b';')), + complete_byte_slice_to_str + ) + >> opt!(pair!(char!(';'), take_while!(call!(|c| c != b'\n')))) + >> (key, val) + ) +); + +named!(keys_and_values<&[u8], HashMap<&str, &str> >, + map!( + many0!(terminated!(key_value, opt!(multispace))), + |vec: Vec<_>| vec.into_iter().collect() + ) +); + +named!(category_and_keys<&[u8],(&str,HashMap<&str,&str>)>, + do_parse!( + category: category >> + opt!(multispace) >> + keys: keys_and_values >> + (category, keys) + ) +); + +named!(categories<&[u8], HashMap<&str, HashMap<&str,&str> > >, + map!( + many0!( + separated_pair!( + category, + opt!(multispace), + map!( + many0!(terminated!(key_value, opt!(multispace))), + |vec: Vec<_>| vec.into_iter().collect() + ) + ) + ), + |vec: Vec<_>| vec.into_iter().collect() + ) +); + +#[test] +fn parse_category_test() { + let ini_file = &b"[category] + +parameter=value +key = value2"[..]; + + let ini_without_category = &b"\n\nparameter=value +key = value2"[..]; + + let res = category(ini_file); + println!("{:?}", res); + match res { + Ok((i, o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o), + _ => println!("error"), + } + + assert_eq!(res, Ok((ini_without_category, "category"))); +} + +#[test] +fn parse_key_value_test() { + let ini_file = &b"parameter=value +key = value2"[..]; + + let ini_without_key_value = &b"\nkey = value2"[..]; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2), + _ => println!("error"), + } + + assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value")))); +} + +#[test] +fn parse_key_value_with_space_test() { + let ini_file = &b"parameter = value +key = value2"[..]; + + let ini_without_key_value = &b"\nkey = value2"[..]; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2), + _ => println!("error"), + } + + assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value")))); +} + +#[test] +fn parse_key_value_with_comment_test() { + let ini_file = &b"parameter=value;abc +key = value2"[..]; + + let ini_without_key_value = &b"\nkey = value2"[..]; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2), + _ => println!("error"), + } + + assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value")))); +} + +#[test] +fn parse_multiple_keys_and_values_test() { + let ini_file = &b"parameter=value;abc + +key = value2 + +[category]"[..]; + + let ini_without_key_value = &b"[category]"[..]; + + let res = keys_and_values(ini_file); + println!("{:?}", res); + match res { + Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o), + _ => println!("error"), + } + + let mut expected: HashMap<&str, &str> = HashMap::new(); + expected.insert("parameter", "value"); + expected.insert("key", "value2"); + assert_eq!(res, Ok((ini_without_key_value, expected))); +} + +#[test] +fn parse_category_then_multiple_keys_and_values_test() { + //FIXME: there can be an empty line or a comment line after a category + let ini_file = &b"[abcd] +parameter=value;abc + +key = value2 + +[category]"[..]; + + let ini_after_parser = &b"[category]"[..]; + + let res = category_and_keys(ini_file); + println!("{:?}", res); + match res { + Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o), + _ => println!("error"), + } + + let mut expected_h: HashMap<&str, &str> = HashMap::new(); + expected_h.insert("parameter", "value"); + expected_h.insert("key", "value2"); + assert_eq!(res, Ok((ini_after_parser, ("abcd", expected_h)))); +} + +#[test] +fn parse_multiple_categories_test() { + let ini_file = &b"[abcd] + +parameter=value;abc + +key = value2 + +[category] +parameter3=value3 +key4 = value4 +"[..]; + + let ini_after_parser = &b""[..]; + + let res = categories(ini_file); + //println!("{:?}", res); + match res { + Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o), + _ => println!("error"), + } + + let mut expected_1: HashMap<&str, &str> = HashMap::new(); + expected_1.insert("parameter", "value"); + expected_1.insert("key", "value2"); + let mut expected_2: HashMap<&str, &str> = HashMap::new(); + expected_2.insert("parameter3", "value3"); + expected_2.insert("key4", "value4"); + let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new(); + expected_h.insert("abcd", expected_1); + expected_h.insert("category", expected_2); + assert_eq!(res, Ok((ini_after_parser, expected_h))); +} diff --git a/third_party/rust/nom/tests/ini_str.rs b/third_party/rust/nom/tests/ini_str.rs new file mode 100644 index 0000000000..d778918c40 --- /dev/null +++ b/third_party/rust/nom/tests/ini_str.rs @@ -0,0 +1,215 @@ +#[macro_use] +extern crate nom; + +use nom::{ + IResult, + combinator::opt, + bytes::complete::{take_while, is_a}, + sequence::{delimited, terminated}, + character::complete::{char, alphanumeric1 as alphanumeric, space0 as space} +}; + +use std::collections::HashMap; + +fn is_line_ending_or_comment(chr: char) -> bool { + chr == ';' || chr == '\n' +} + +fn not_line_ending(i: &str) -> IResult<&str, &str> { + take_while(|c| c != '\r' && c != '\n')(i) +} + +fn space_or_line_ending(i: &str) -> IResult<&str, &str> { + is_a(" \r\n")(i) +} + +fn category(i: &str) -> IResult<&str, &str> { + terminated(delimited(char('['), take_while(|c| c != ']'), char(']')), opt(is_a(" \r\n")))(i) +} + +named!(key_value <&str,(&str,&str)>, + do_parse!( + key: alphanumeric >> + opt!(space) >> + tag!("=") >> + opt!(space) >> + val: take_till!(is_line_ending_or_comment) >> + opt!(space) >> + opt!(pair!(tag!(";"), not_line_ending)) >> + opt!(space_or_line_ending) >> + (key, val) + ) +); + +named!(keys_and_values_aggregator<&str, Vec<(&str, &str)> >, many0!(key_value)); + +fn keys_and_values(input: &str) -> IResult<&str, HashMap<&str, &str>> { + match keys_and_values_aggregator(input) { + Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())), + Err(e) => Err(e), + } +} + +named!(category_and_keys<&str,(&str,HashMap<&str,&str>)>, + pair!(category, keys_and_values) +); + +named!(categories_aggregator<&str, Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys)); + +fn categories(input: &str) -> IResult<&str, HashMap<&str, HashMap<&str, &str>>> { + match categories_aggregator(input) { + Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())), + Err(e) => Err(e), + } +} + +#[test] +fn parse_category_test() { + let ini_file = "[category] + +parameter=value +key = value2"; + + let ini_without_category = "parameter=value +key = value2"; + + let res = category(ini_file); + println!("{:?}", res); + match res { + Ok((i, o)) => println!("i: {} | o: {:?}", i, o), + _ => println!("error"), + } + + assert_eq!(res, Ok((ini_without_category, "category"))); +} + +#[test] +fn parse_key_value_test() { + let ini_file = "parameter=value +key = value2"; + + let ini_without_key_value = "key = value2"; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2), + _ => println!("error"), + } + + assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value")))); +} + +#[test] +fn parse_key_value_with_space_test() { + let ini_file = "parameter = value +key = value2"; + + let ini_without_key_value = "key = value2"; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2), + _ => println!("error"), + } + + assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value")))); +} + +#[test] +fn parse_key_value_with_comment_test() { + let ini_file = "parameter=value;abc +key = value2"; + + let ini_without_key_value = "key = value2"; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2), + _ => println!("error"), + } + + assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value")))); +} + +#[test] +fn parse_multiple_keys_and_values_test() { + let ini_file = "parameter=value;abc + +key = value2 + +[category]"; + + let ini_without_key_value = "[category]"; + + let res = keys_and_values(ini_file); + println!("{:?}", res); + match res { + Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o), + _ => println!("error"), + } + + let mut expected: HashMap<&str, &str> = HashMap::new(); + expected.insert("parameter", "value"); + expected.insert("key", "value2"); + assert_eq!(res, Ok((ini_without_key_value, expected))); +} + +#[test] +fn parse_category_then_multiple_keys_and_values_test() { + //FIXME: there can be an empty line or a comment line after a category + let ini_file = "[abcd] +parameter=value;abc + +key = value2 + +[category]"; + + let ini_after_parser = "[category]"; + + let res = category_and_keys(ini_file); + println!("{:?}", res); + match res { + Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o), + _ => println!("error"), + } + + let mut expected_h: HashMap<&str, &str> = HashMap::new(); + expected_h.insert("parameter", "value"); + expected_h.insert("key", "value2"); + assert_eq!(res, Ok((ini_after_parser, ("abcd", expected_h)))); +} + +#[test] +fn parse_multiple_categories_test() { + let ini_file = "[abcd] + +parameter=value;abc + +key = value2 + +[category] +parameter3=value3 +key4 = value4 +"; + + let res = categories(ini_file); + //println!("{:?}", res); + match res { + Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o), + _ => println!("error"), + } + + let mut expected_1: HashMap<&str, &str> = HashMap::new(); + expected_1.insert("parameter", "value"); + expected_1.insert("key", "value2"); + let mut expected_2: HashMap<&str, &str> = HashMap::new(); + expected_2.insert("parameter3", "value3"); + expected_2.insert("key4", "value4"); + let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new(); + expected_h.insert("abcd", expected_1); + expected_h.insert("category", expected_2); + assert_eq!(res, Ok(("", expected_h))); +} diff --git a/third_party/rust/nom/tests/issues.rs b/third_party/rust/nom/tests/issues.rs new file mode 100644 index 0000000000..62a1a01bc5 --- /dev/null +++ b/third_party/rust/nom/tests/issues.rs @@ -0,0 +1,318 @@ +//#![feature(trace_macros)] +#![allow(dead_code)] +#![cfg_attr(feature = "cargo-clippy", allow(redundant_closure))] + +#[macro_use] +extern crate nom; + +use nom::{character::{is_digit, streaming::space1 as space}, Err, IResult, Needed, error::ErrorKind, number::streaming::le_u64}; + +#[allow(dead_code)] +struct Range { + start: char, + end: char, +} + +pub fn take_char(input: &[u8]) -> IResult<&[u8], char> { + if !input.is_empty() { + Ok((&input[1..], input[0] as char)) + } else { + Err(Err::Incomplete(Needed::Size(1))) + } +} + +//trace_macros!(true); + +#[allow(dead_code)] +named!(range<&[u8], Range>, + alt!( + do_parse!( + start: take_char >> + tag!("-") >> + end: take_char >> + (Range { + start: start, + end: end, + }) + ) | + map!( + take_char, + |c| { + Range { + start: c, + end: c, + } + } + ) + ) +); + +#[allow(dead_code)] +named!(literal<&[u8], Vec<char> >, + map!( + many1!(take_char), + |cs| { + cs + } + ) +); + +#[test] +fn issue_58() { + let _ = range(&b"abcd"[..]); + let _ = literal(&b"abcd"[..]); +} + +//trace_macros!(false); + +#[cfg(feature = "std")] +mod parse_int { + use nom::HexDisplay; + use nom::{IResult, character::streaming::{digit1 as digit, space1 as space}}; + use std::str; + + named!(parse_ints<Vec<i32>>, many0!(spaces_or_int)); + + fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32> { + println!("{}", input.to_hex(8)); + do_parse!( + input, + opt!(complete!(space)) >> res: map!(complete!(digit), |x| { + println!("x: {:?}", x); + let result = str::from_utf8(x).unwrap(); + println!("Result: {}", result); + println!("int is empty?: {}", x.is_empty()); + match result.parse() { + Ok(i) => i, + Err(e) => panic!("UH OH! NOT A DIGIT! {:?}", e), + } + }) >> (res) + ) + } + + #[test] + fn issue_142() { + let subject = parse_ints(&b"12 34 5689a"[..]); + let expected = Ok((&b"a"[..], vec![12, 34, 5689])); + assert_eq!(subject, expected); + + let subject = parse_ints(&b"12 34 5689 "[..]); + let expected = Ok((&b" "[..], vec![12, 34, 5689])); + assert_eq!(subject, expected) + } +} + +#[test] +fn usize_length_bytes_issue() { + use nom::number::streaming::be_u16; + let _: IResult<&[u8], &[u8], (&[u8], ErrorKind)> = length_data!(b"012346", be_u16); +} + +/* + DOES NOT COMPILE +#[test] +fn issue_152() { + named!(take4, take!(4)); + named!(xyz, tag!("XYZ")); + named!(abc, tag!("abc")); + + + named!(sw, + switch!(take4, + b"abcd" => xyz | + b"efgh" => abc + ) + ); +} +*/ + +#[test] +fn take_till_issue() { + named!(nothing, take_till!(call!(|_| true))); + + assert_eq!(nothing(b""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(nothing(b"abc"), Ok((&b"abc"[..], &b""[..]))); +} + +named!( + issue_498<Vec<&[u8]>>, + separated_nonempty_list!(opt!(space), tag!("abcd")) +); + +named!(issue_308(&str) -> bool, + do_parse! ( + tag! ("foo") >> + b: alt! ( + complete!(map! (tag! ("1"), |_: &str|->bool {true})) | + value! (false) + ) >> + (b) )); + +#[cfg(feature = "alloc")] +fn issue_302(input: &[u8]) -> IResult<&[u8], Option<Vec<u64>>> { + do_parse!(input, entries: cond!(true, count!(le_u64, 3)) >> (entries)) +} + +#[test] +fn issue_655() { + use nom::character::streaming::{line_ending, not_line_ending}; + named!(twolines(&str) -> (&str, &str), + do_parse!( + l1 : not_line_ending >> + line_ending >> + l2 : not_line_ending >> + line_ending >> + ((l1, l2)) + ) + ); + + assert_eq!(twolines("foo\nbar\n"), Ok(("", ("foo", "bar")))); + assert_eq!(twolines("féo\nbar\n"), Ok(("", ("féo", "bar")))); + assert_eq!(twolines("foé\nbar\n"), Ok(("", ("foé", "bar")))); + assert_eq!(twolines("foé\r\nbar\n"), Ok(("", ("foé", "bar")))); +} + +#[test] +fn issue_721() { + named!(f1<&str, u16>, parse_to!(u16)); + named!(f2<&str, String>, parse_to!(String)); + assert_eq!(f1("1234"), Ok(("", 1234))); + assert_eq!(f2("foo"), Ok(("", "foo".to_string()))); + //assert_eq!(parse_to!("1234", u16), Ok(("", 1234))); + //assert_eq!(parse_to!("foo", String), Ok(("", "foo".to_string()))); +} + +#[cfg(feature = "alloc")] +named!(issue_717<&[u8], Vec<&[u8]> >, + separated_list!(tag!([0x0]), is_not!([0x0u8])) +); + +struct NoPartialEq { + value: i32, +} + +named!(issue_724<&str, i32>, + do_parse!( + metadata: permutation!( + map!(tag!("hello"), |_| NoPartialEq { value: 1 }), + map!(tag!("world"), |_| NoPartialEq { value: 2 }) + ) >> + (metadata.0.value + metadata.1.value) + ) +); + +#[test] +fn issue_752() { + assert_eq!( + Err::Error(("ab", nom::error::ErrorKind::ParseTo)), + parse_to!("ab", usize).unwrap_err() + ) +} + +fn atom_specials(c: u8) -> bool { + c == b'q' +} + +named!( + capability<&str>, + do_parse!(tag!(" ") >> _atom: map_res!(take_till1!(atom_specials), std::str::from_utf8) >> ("a")) +); + +#[test] +fn issue_759() { + assert_eq!(capability(b" abcqd"), Ok((&b"qd"[..], "a"))); +} + +named_args!(issue_771(count: usize)<Vec<u32>>, + length_count!(value!(count), call!(nom::number::streaming::be_u32)) +); + +/// This test is in a separate module to check that all required symbols are imported in +/// `escaped_transform!()`. Without the module, the `use`-es of the current module would +/// mask the error ('"Use of undeclared type or module `Needed`" in escaped_transform!'). +mod issue_780 { + named!(issue_780<&str, String>, + escaped_transform!(call!(::nom::character::streaming::alpha1), '\\', tag!("n")) + ); +} + +// issue 617 +named!(digits, take_while1!( is_digit )); +named!(multi_617<&[u8], () >, fold_many0!( digits, (), |_, _| {})); + +// Sad :( +named!(multi_617_fails<&[u8], () >, fold_many0!( take_while1!( is_digit ), (), |_, _| {})); + +mod issue_647 { + use nom::{Err, number::streaming::be_f64, error::ErrorKind}; + pub type Input<'a> = &'a [u8]; + + #[derive(PartialEq, Debug, Clone)] + struct Data { + c: f64, + v: Vec<f64> + } + + fn list<'a,'b>(input: Input<'a>, _cs: &'b f64) -> Result<(Input<'a>,Vec<f64>), Err<(&'a [u8], ErrorKind)>> { + separated_list!(input, complete!(tag!(",")), complete!(be_f64)) + } + + named!(data<Input,Data>, map!( + do_parse!( + c: be_f64 >> + tag!("\n") >> + v: call!(list,&c) >> + (c,v) + ), |(c,v)| { + Data { + c: c, + v: v + } + } + )); +} + +named!(issue_775, take_till1!(|_| true)); + +#[test] +fn issue_848_overflow_incomplete_bits_to_bytes() { + named!(take, take!(0x2000000000000000)); + named!(parser<&[u8], &[u8]>, bits!(bytes!(take))); + assert_eq!(parser(&b""[..]), Err(Err::Failure(error_position!(&b""[..], ErrorKind::TooLarge)))); +} + +#[test] +fn issue_942() { + use nom::error::ParseError; + pub fn parser<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, usize, E> { + use nom::{character::complete::char, error::context, multi::many0_count}; + many0_count(context("char_a", char('a')))(i) + } + assert_eq!(parser::<()>("aaa"), Ok(("", 3))); +} + +#[test] +fn issue_many_m_n_with_zeros() { + use nom::multi::many_m_n; + use nom::character::complete::char; + let parser = many_m_n::<_, _, (), _>(0, 0, char('a')); + assert_eq!(parser("aaa"), Ok(("aaa", vec!()))); +} + +#[test] +fn issue_1027_convert_error_panic_nonempty() { + use nom::error::{VerboseError, convert_error}; + use nom::sequence::pair; + use nom::character::complete::char; + + let input = "a"; + + let result: IResult<_, _, VerboseError<&str>> = pair(char('a'), char('b'))(input); + let err = match result.unwrap_err() { + Err::Error(e) => e, + _ => unreachable!(), + }; + + let msg = convert_error(&input, err); + assert_eq!(msg, "0: at line 1:\na\n ^\nexpected \'b\', got end of input\n\n"); +} diff --git a/third_party/rust/nom/tests/json.rs b/third_party/rust/nom/tests/json.rs new file mode 100644 index 0000000000..14329a16ae --- /dev/null +++ b/third_party/rust/nom/tests/json.rs @@ -0,0 +1,105 @@ +#![cfg(feature = "alloc")] +//#![feature(trace_macros)] + +#[macro_use] +extern crate nom; + +use nom::{character::is_alphanumeric, number::complete::recognize_float}; + +use std::str; +use std::collections::HashMap; + +#[derive(Debug, PartialEq)] +pub enum JsonValue { + Str(String), + Num(f32), + Array(Vec<JsonValue>), + Object(HashMap<String, JsonValue>), +} + +named!(float<f32>, flat_map!(recognize_float, parse_to!(f32))); + +//FIXME: verify how json strings are formatted +named!( + string<&str>, + delimited!( + char!('"'), + //map_res!(escaped!(call!(alphanumeric), '\\', is_a!("\"n\\")), str::from_utf8), + map_res!( + escaped!(take_while1!(is_alphanumeric), '\\', one_of!("\"n\\")), + str::from_utf8 + ), + char!('"') + ) +); + +named!( + array<Vec<JsonValue>>, + ws!(delimited!( + char!('['), + separated_list!(char!(','), value), + char!(']') + )) +); + +named!( + key_value<(&str, JsonValue)>, + ws!(separated_pair!(string, char!(':'), value)) +); + +named!( + hash<HashMap<String, JsonValue>>, + ws!(map!( + delimited!(char!('{'), separated_list!(char!(','), key_value), char!('}')), + |tuple_vec| { + let mut h: HashMap<String, JsonValue> = HashMap::new(); + for (k, v) in tuple_vec { + h.insert(String::from(k), v); + } + h + } + )) +); + +named!( + value<JsonValue>, + ws!(alt!( + hash => { |h| JsonValue::Object(h) } | + array => { |v| JsonValue::Array(v) } | + string => { |s| JsonValue::Str(String::from(s)) } | + float => { |num| JsonValue::Num(num) } + )) +); + +#[test] +fn json_object() { + let input = + r#"{ + "a": 42, + "b": "x" + }\0"#; + + let mut expected_map = HashMap::new(); + expected_map.insert(String::from("a"), JsonValue::Num(42f32)); + expected_map.insert(String::from("b"), JsonValue::Str(String::from("x"))); + let expected = JsonValue::Object(expected_map); + + assert_eq!(expected, value(input.as_bytes()).unwrap().1); +} + +#[test] +fn json_array() { + let input = + r#"[ + 42, + "x" + ]\0"#; + + let expected_vec = vec![ + JsonValue::Num(42f32), + JsonValue::Str(String::from("x")) + ]; + let expected = JsonValue::Array(expected_vec); + + assert_eq!(expected, value(input.as_bytes()).unwrap().1); +} diff --git a/third_party/rust/nom/tests/mp4.rs b/third_party/rust/nom/tests/mp4.rs new file mode 100644 index 0000000000..1432621611 --- /dev/null +++ b/third_party/rust/nom/tests/mp4.rs @@ -0,0 +1,319 @@ +#![allow(dead_code)] + +#[macro_use] +extern crate nom; + +use nom::{ + IResult, Needed, Err, + error::ErrorKind, + number::streaming::{be_u16, be_u32, be_u64, be_f32} +}; + +use std::str; + +fn mp4_box(input: &[u8]) -> IResult<&[u8], &[u8]> { + match be_u32(input) { + Ok((i, offset)) => { + let sz: usize = offset as usize; + if i.len() >= sz - 4 { + Ok((&i[(sz - 4)..], &i[0..(sz - 4)])) + } else { + Err(Err::Incomplete(Needed::Size(offset as usize + 4))) + } + } + Err(e) => Err(e), + } +} + +#[cfg_attr(rustfmt, rustfmt_skip)] +#[derive(PartialEq,Eq,Debug)] +struct FileType<'a> { + major_brand: &'a str, + major_brand_version: &'a [u8], + compatible_brands: Vec<&'a str> +} + +#[cfg_attr(rustfmt, rustfmt_skip)] +#[allow(non_snake_case)] +#[derive(Debug,Clone)] +pub struct Mvhd32 { + version_flags: u32, // actually: + // version: u8, + // flags: u24 // 3 bytes + created_date: u32, + modified_date: u32, + scale: u32, + duration: u32, + speed: f32, + volume: u16, // actually a 2 bytes decimal + /* 10 bytes reserved */ + scaleA: f32, + rotateB: f32, + angleU: f32, + rotateC: f32, + scaleD: f32, + angleV: f32, + positionX: f32, + positionY: f32, + scaleW: f32, + preview: u64, + poster: u32, + selection: u64, + current_time: u32, + track_id: u32 +} + +#[cfg_attr(rustfmt, rustfmt_skip)] +#[allow(non_snake_case)] +#[derive(Debug,Clone)] +pub struct Mvhd64 { + version_flags: u32, // actually: + // version: u8, + // flags: u24 // 3 bytes + created_date: u64, + modified_date: u64, + scale: u32, + duration: u64, + speed: f32, + volume: u16, // actually a 2 bytes decimal + /* 10 bytes reserved */ + scaleA: f32, + rotateB: f32, + angleU: f32, + rotateC: f32, + scaleD: f32, + angleV: f32, + positionX: f32, + positionY: f32, + scaleW: f32, + preview: u64, + poster: u32, + selection: u64, + current_time: u32, + track_id: u32 +} + +#[allow(non_snake_case)] +named!(mvhd32 <&[u8], MvhdBox>, + do_parse!( + version_flags: be_u32 >> + created_date: be_u32 >> + modified_date: be_u32 >> + scale: be_u32 >> + duration: be_u32 >> + speed: be_f32 >> + volume: be_u16 >> // actually a 2 bytes decimal + take!(10) >> + scale_a: be_f32 >> + rotate_b: be_f32 >> + angle_u: be_f32 >> + rotate_c: be_f32 >> + scale_d: be_f32 >> + angle_v: be_f32 >> + position_x: be_f32 >> + position_y: be_f32 >> + scale_w: be_f32 >> + preview: be_u64 >> + poster: be_u32 >> + selection: be_u64 >> + current_time: be_u32 >> + track_id: be_u32 >> + ( + MvhdBox::M32(Mvhd32 { + version_flags: version_flags, + created_date: created_date, + modified_date: modified_date, + scale: scale, + duration: duration, + speed: speed, + volume: volume, + scaleA: scale_a, + rotateB: rotate_b, + angleU: angle_u, + rotateC: rotate_c, + scaleD: scale_d, + angleV: angle_v, + positionX: position_x, + positionY: position_y, + scaleW: scale_w, + preview: preview, + poster: poster, + selection: selection, + current_time: current_time, + track_id: track_id + }) + )) +); + +#[allow(non_snake_case)] +named!(mvhd64 <&[u8], MvhdBox>, + do_parse!( + version_flags: be_u32 >> + created_date: be_u64 >> + modified_date: be_u64 >> + scale: be_u32 >> + duration: be_u64 >> + speed: be_f32 >> + volume: be_u16 >> // actually a 2 bytes decimal + take!(10) >> + scale_a: be_f32 >> + rotate_b: be_f32 >> + angle_u: be_f32 >> + rotate_c: be_f32 >> + scale_d: be_f32 >> + angle_v: be_f32 >> + position_x: be_f32 >> + position_y: be_f32 >> + scale_w: be_f32 >> + preview: be_u64 >> + poster: be_u32 >> + selection: be_u64 >> + current_time: be_u32 >> + track_id: be_u32 >> + ( + MvhdBox::M64(Mvhd64 { + version_flags: version_flags, + created_date: created_date, + modified_date: modified_date, + scale: scale, + duration: duration, + speed: speed, + volume: volume, + scaleA: scale_a, + rotateB: rotate_b, + angleU: angle_u, + rotateC: rotate_c, + scaleD: scale_d, + angleV: angle_v, + positionX: position_x, + positionY: position_y, + scaleW: scale_w, + preview: preview, + poster: poster, + selection: selection, + current_time: current_time, + track_id: track_id + }) + )) +); + +#[derive(Debug, Clone)] +pub enum MvhdBox { + M32(Mvhd32), + M64(Mvhd64), +} + +#[derive(Debug, Clone)] +pub enum MoovBox { + Mdra, + Dref, + Cmov, + Rmra, + Iods, + Mvhd(MvhdBox), + Clip, + Trak, + Udta, +} + +#[derive(Debug)] +enum MP4BoxType { + Ftyp, + Moov, + Mdat, + Free, + Skip, + Wide, + Mdra, + Dref, + Cmov, + Rmra, + Iods, + Mvhd, + Clip, + Trak, + Udta, + Unknown, +} + +#[derive(Debug)] +struct MP4BoxHeader { + length: u32, + tag: MP4BoxType, +} + +named!(brand_name<&[u8],&str>, map_res!(take!(4), str::from_utf8)); + +named!(filetype_parser<&[u8], FileType>, + do_parse!( + m: brand_name >> + v: take!(4) >> + c: many0!(brand_name) >> + (FileType{ major_brand: m, major_brand_version:v, compatible_brands: c }) + ) +); + +fn mvhd_box(input: &[u8]) -> IResult<&[u8], MvhdBox> { + let res = if input.len() < 100 { + Err(Err::Incomplete(Needed::Size(100))) + } else if input.len() == 100 { + mvhd32(input) + } else if input.len() == 112 { + mvhd64(input) + } else { + Err(Err::Error(error_position!(input, ErrorKind::TooLarge))) + }; + println!("res: {:?}", res); + res +} + +fn unknown_box_type(input: &[u8]) -> IResult<&[u8], MP4BoxType> { + Ok((input, MP4BoxType::Unknown)) +} + +//named!(box_type<&[u8], MP4BoxType>, +fn box_type(input: &[u8]) -> IResult<&[u8], MP4BoxType> { + alt!(input, + tag!("ftyp") => { |_| MP4BoxType::Ftyp } | + tag!("moov") => { |_| MP4BoxType::Moov } | + tag!("mdat") => { |_| MP4BoxType::Mdat } | + tag!("free") => { |_| MP4BoxType::Free } | + tag!("skip") => { |_| MP4BoxType::Skip } | + tag!("wide") => { |_| MP4BoxType::Wide } | + unknown_box_type + ) +} + +// warning, an alt combinator with 9 branches containing a tag combinator +// can make the compilation very slow. Use functions as sub parsers, +// or split into multiple alt! parsers if it gets slow +named!(moov_type<&[u8], MP4BoxType>, + alt!( + tag!("mdra") => { |_| MP4BoxType::Mdra } | + tag!("dref") => { |_| MP4BoxType::Dref } | + tag!("cmov") => { |_| MP4BoxType::Cmov } | + tag!("rmra") => { |_| MP4BoxType::Rmra } | + tag!("iods") => { |_| MP4BoxType::Iods } | + tag!("mvhd") => { |_| MP4BoxType::Mvhd } | + tag!("clip") => { |_| MP4BoxType::Clip } | + tag!("trak") => { |_| MP4BoxType::Trak } | + tag!("udta") => { |_| MP4BoxType::Udta } + ) +); + +named!(box_header<&[u8],MP4BoxHeader>, + do_parse!( + length: be_u32 >> + tag: box_type >> + (MP4BoxHeader{ length: length, tag: tag}) + ) +); + +named!(moov_header<&[u8],MP4BoxHeader>, + do_parse!( + length: be_u32 >> + tag: moov_type >> + (MP4BoxHeader{ length: length, tag: tag}) + ) +); + diff --git a/third_party/rust/nom/tests/multiline.rs b/third_party/rust/nom/tests/multiline.rs new file mode 100644 index 0000000000..cca19ecbad --- /dev/null +++ b/third_party/rust/nom/tests/multiline.rs @@ -0,0 +1,36 @@ +extern crate nom; + +use nom::{ + IResult, + multi::many0, + sequence::terminated, + character::complete::{alphanumeric1 as alphanumeric, line_ending as eol} +}; + +pub fn end_of_line(input: &str) -> IResult<&str, &str> { + if input.is_empty() { + Ok((input, input)) + } else { + eol(input) + } +} + +pub fn read_line(input: &str) -> IResult<&str, &str> { + terminated(alphanumeric, end_of_line)(input) +} + +pub fn read_lines(input: &str) -> IResult<&str, Vec<&str>> { + many0(read_line)(input) +} + +#[cfg(feature = "alloc")] +#[test] +fn read_lines_test() { + let res = Ok(( + "", + vec!["Duck", "Dog", "Cow"], + )); + + assert_eq!(read_lines("Duck\nDog\nCow\n"), res); + assert_eq!(read_lines("Duck\nDog\nCow"), res); +} diff --git a/third_party/rust/nom/tests/named_args.rs b/third_party/rust/nom/tests/named_args.rs new file mode 100644 index 0000000000..811b0a0dce --- /dev/null +++ b/third_party/rust/nom/tests/named_args.rs @@ -0,0 +1,161 @@ +#[macro_use] +extern crate nom; + +use nom::{ + branch::alt, + sequence::{delimited, pair, preceded}, + character::complete::{digit1 as digit, space0 as space}, + bytes::complete::tag +}; + +// Parser definition + +use std::str; +use std::str::FromStr; + +use self::Operator::*; + +enum Operator { + Slash, + Star, +} + +impl Operator { + fn to_str(&self) -> &'static str { + match *self { + Slash => "/", + Star => "*", + } + } +} + +// Parse the specified `Operator`. +named_args!(operator(op: Operator) <&[u8], &[u8]>, + call!(tag(op.to_str())) +); + +// We parse any expr surrounded by the tags `open_tag` and `close_tag`, ignoring all whitespaces around those +named_args!(brackets<'a>(open_tag: &str, close_tag: &str) <&'a[u8], i64>, + call!(delimited( + space, + delimited(tag(open_tag), preceded(space, expr), preceded(space, tag(close_tag))), + space + )) +); + +fn byte_slice_to_str<'a>(s: &'a[u8]) -> Result<&'a str, str::Utf8Error> { + str::from_utf8(s) +} + +// We transform an integer string into a i64, ignoring surrounding whitespaces +// We look for a digit suite, and try to convert it. +// If either str::from_utf8 or FromStr::from_str fail, +// we fallback to the brackets parser defined above +named!(factor<&[u8], i64>, alt!( + map_res!( + map_res!( + call!(delimited(space, digit, space)), + byte_slice_to_str + ), + FromStr::from_str + ) + | call!(brackets, "(", ")") + ) +); + +// We read an initial factor and for each time we find +// a * or / operator followed by another factor, we do +// the math by folding everything +named!(term <&[u8], i64>, do_parse!( + init: factor >> + res: fold_many0!( + pair!(alt!(call!(operator, Star) | call!(operator, Slash)), factor), + init, + |acc, (op, val): (&[u8], i64)| { + if (op[0] as char) == '*' { acc * val } else { acc / val } + } + ) >> + (res) + ) +); + +named!(expr <&[u8], i64>, do_parse!( + init: term >> + res: fold_many0!( + call!(pair(alt((tag("+"), tag("-"))), term)), + init, + |acc, (op, val): (&[u8], i64)| { + if (op[0] as char) == '+' { acc + val } else { acc - val } + } + ) >> + (res) + ) +); + +#[test] +fn factor_test() { + assert_eq!( + factor(&b"3"[..]), + Ok((&b""[..], 3)) + ); + assert_eq!( + factor(&b" 12"[..]), + Ok((&b""[..], 12)) + ); + assert_eq!( + factor(&b"537 "[..]), + Ok((&b""[..], 537)) + ); + assert_eq!( + factor(&b" 24 "[..]), + Ok((&b""[..], 24)) + ); +} + +#[test] +fn term_test() { + assert_eq!( + term(&b" 12 *2 / 3"[..]), + Ok((&b""[..], 8)) + ); + assert_eq!( + term(&b" 2* 3 *2 *2 / 3"[..]), + Ok((&b""[..], 8)) + ); + assert_eq!( + term(&b" 48 / 3/2"[..]), + Ok((&b""[..], 8)) + ); +} + +#[test] +fn expr_test() { + assert_eq!( + expr(&b" 1 + 2 "[..]), + Ok((&b""[..], 3)) + ); + assert_eq!( + expr(&b" 12 + 6 - 4+ 3"[..]), + Ok((&b""[..], 17)) + ); + assert_eq!( + expr(&b" 1 + 2*3 + 4"[..]), + Ok((&b""[..], 11)) + ); +} + +#[test] +fn parens_test() { + assert_eq!( + expr(&b" ( 2 )"[..]), + Ok((&b""[..], 2)) + ); + assert_eq!( + expr(&b" 2* ( 3 + 4 ) "[..]), + Ok((&b""[..], 14)) + ); + assert_eq!( + expr(&b" 2*2 / ( 5 - 1) + 3"[..]), + Ok((&b""[..], 4)) + ); +} diff --git a/third_party/rust/nom/tests/overflow.rs b/third_party/rust/nom/tests/overflow.rs new file mode 100644 index 0000000000..2f4a87f34a --- /dev/null +++ b/third_party/rust/nom/tests/overflow.rs @@ -0,0 +1,139 @@ +#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))] +#![cfg(target_pointer_width = "64")] + +#[macro_use] +extern crate nom; + +use nom::{Err, Needed}; +#[cfg(feature = "alloc")] +use nom::number::streaming::be_u64; + +// Parser definition + +// We request a length that would trigger an overflow if computing consumed + requested +named!(parser01<&[u8],()>, + do_parse!( + hdr: take!(1) >> + data: take!(18446744073709551615) >> + ({ + let _ = hdr; + let _ = data; + () + }) + ) +); + +// We request a length that would trigger an overflow if computing consumed + requested +named!(parser02<&[u8],(&[u8],&[u8])>, + tuple!(take!(1),take!(18446744073709551615)) +); + +#[test] +fn overflow_incomplete_do_parse() { + assert_eq!( + parser01(&b"3"[..]), + Err(Err::Incomplete(Needed::Size(18446744073709551615))) + ); +} + +#[test] +fn overflow_incomplete_tuple() { + assert_eq!( + parser02(&b"3"[..]), + Err(Err::Incomplete(Needed::Size(18446744073709551615))) + ); +} + +#[test] +#[cfg(feature = "alloc")] +fn overflow_incomplete_length_bytes() { + named!(multi<&[u8], Vec<&[u8]> >, many0!( length_data!(be_u64) ) ); + + // Trigger an overflow in length_data + assert_eq!( + multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]), + Err(Err::Incomplete(Needed::Size(18446744073709551615))) + ); +} + +#[test] +#[cfg(feature = "alloc")] +fn overflow_incomplete_many0() { + named!(multi<&[u8], Vec<&[u8]> >, many0!( length_data!(be_u64) ) ); + + // Trigger an overflow in many0 + assert_eq!( + multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), + Err(Err::Incomplete(Needed::Size(18446744073709551599))) + ); +} + +#[test] +#[cfg(feature = "alloc")] +fn overflow_incomplete_many1() { + named!(multi<&[u8], Vec<&[u8]> >, many1!( length_data!(be_u64) ) ); + + // Trigger an overflow in many1 + assert_eq!( + multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), + Err(Err::Incomplete(Needed::Size(18446744073709551599))) + ); +} + +#[test] +#[cfg(feature = "alloc")] +fn overflow_incomplete_many_till() { + named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( length_data!(be_u64), tag!("abc") ) ); + + // Trigger an overflow in many_till + assert_eq!( + multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), + Err(Err::Incomplete(Needed::Size(18446744073709551599))) + ); +} + +#[test] +#[cfg(feature = "alloc")] +fn overflow_incomplete_many_m_n() { + named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, length_data!(be_u64) ) ); + + // Trigger an overflow in many_m_n + assert_eq!( + multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), + Err(Err::Incomplete(Needed::Size(18446744073709551599))) + ); +} + +#[test] +#[cfg(feature = "alloc")] +fn overflow_incomplete_count() { + named!(counter<&[u8], Vec<&[u8]> >, count!( length_data!(be_u64), 2 ) ); + + assert_eq!( + counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), + Err(Err::Incomplete(Needed::Size(18446744073709551599))) + ); +} + +#[test] +#[cfg(feature = "alloc")] +fn overflow_incomplete_length_count() { + use nom::number::streaming::be_u8; + named!(multi<&[u8], Vec<&[u8]> >, length_count!( be_u8, length_data!(be_u64) ) ); + + assert_eq!( + multi(&b"\x04\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xee\xaa"[..]), + Err(Err::Incomplete(Needed::Size(18446744073709551598))) + ); +} + +#[test] +#[cfg(feature = "alloc")] +fn overflow_incomplete_length_data() { + named!(multi<&[u8], Vec<&[u8]> >, many0!( length_data!(be_u64) ) ); + + assert_eq!( + multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]), + Err(Err::Incomplete(Needed::Size(18446744073709551615))) + ); +} diff --git a/third_party/rust/nom/tests/reborrow_fold.rs b/third_party/rust/nom/tests/reborrow_fold.rs new file mode 100644 index 0000000000..b53555b074 --- /dev/null +++ b/third_party/rust/nom/tests/reborrow_fold.rs @@ -0,0 +1,18 @@ +#![allow(dead_code)] +#![allow(unused_variables)] + +#[macro_use] +extern crate nom; + +use std::str; + +named_args!(atom<'a>(tomb: &'a mut ())<String>, + map!(map_res!(is_not!(" \t\r\n()"), str::from_utf8), ToString::to_string)); + +/*FIXME: should we support the use case of borrowing data mutably in a parser? +named_args!(list<'a>(tomb: &'a mut ())<String>, + delimited!( + char!('('), + fold_many0!(call!(atom, tomb), "".to_string(), |acc: String, next: String| acc + next.as_str()), + char!(')'))); +*/ diff --git a/third_party/rust/nom/tests/test1.rs b/third_party/rust/nom/tests/test1.rs new file mode 100644 index 0000000000..85c442fe84 --- /dev/null +++ b/third_party/rust/nom/tests/test1.rs @@ -0,0 +1,44 @@ +#![cfg(feature = "stream")] + +#[macro_use] +extern crate nom; + +use nom::{not_line_ending, IResult}; + +use std::fmt::Debug; + +/* +#[test] +#[allow(unused_must_use)] +fn tag() { + FileProducer::new("assets/links.txt", 20).map(|producer: FileProducer| { + let mut p = producer; + p.refill(); + + consumer_from_parser!(PrintConsumer<()>, flat_map!(map_res!(tag!("https!"), str::from_utf8), print)); + let mut cs = PrintConsumer::new(); + for _ in 1..4 { + p.apply(&mut cs); + } + }); +} +*/ + +pub fn print<T: Debug>(input: T) -> IResult<T, ()> { + println!("{:?}", input); + Ok((input, ())) +} + +#[test] +fn is_not() { + //is_not!(foo b"\r\n"); + named!(foo<&[u8],&[u8]>, is_not!(&b"\r\n"[..])); + let a = &b"ab12cd\nefgh"[..]; + assert_eq!(foo(a), Ok((&b"\nefgh"[..], &b"ab12cd"[..]))); +} + +#[test] +fn exported_public_method_defined_by_macro() { + let a = &b"ab12cd\nefgh"[..]; + assert_eq!(not_line_ending(a), Ok((&b"\nefgh"[..], &b"ab12cd"[..]))); +} |