2017-02-01 08:45:55 +00:00
|
|
|
// Copyright 2016 Mozilla
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
|
|
// this file except in compliance with the License. You may obtain a copy of the
|
|
|
|
// License at http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
|
// specific language governing permissions and limitations under the License.
|
|
|
|
|
2017-02-02 02:01:50 +00:00
|
|
|
extern crate combine;
|
|
|
|
|
|
|
|
use combine::ParseResult;
|
|
|
|
use combine::combinator::{Expected, FnParser};
|
|
|
|
|
2017-02-02 23:11:55 +00:00
|
|
|
/// A type definition for a function parser that either parses an `O` from an input stream of type
|
|
|
|
/// `I`, or fails with an "expected" failure.
|
|
|
|
/// See <https://docs.rs/combine/2.2.1/combine/trait.Parser.html#method.expected> for more
|
|
|
|
/// illumination.
|
|
|
|
/// Nothing about this is specific to the result type of the parser.
|
2017-02-02 02:01:50 +00:00
|
|
|
pub type ResultParser<O, I> = Expected<FnParser<I, fn(I) -> ParseResult<O, I>>>;
|
|
|
|
|
2017-02-01 08:45:55 +00:00
|
|
|
/// `assert_parses_to!` simplifies some of the boilerplate around running a
|
|
|
|
/// parser function against input and expecting a certain result.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert_parses_to {
|
|
|
|
( $parser: path, $input: expr, $expected: expr ) => {{
|
|
|
|
let mut par = $parser();
|
|
|
|
let result = par.parse(&$input[..]);
|
|
|
|
assert_eq!(result, Ok(($expected, &[][..])));
|
|
|
|
}}
|
2017-02-02 02:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// `satisfy_unwrap!` makes it a little easier to implement a `satisfy_map`
|
|
|
|
/// body that matches a particular `Value` enum case, otherwise returning `None`.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! satisfy_unwrap {
|
|
|
|
( $cas: path, $var: ident, $body: block ) => {
|
|
|
|
satisfy_map(|x: edn::Value| if let $cas($var) = x $body else { None })
|
|
|
|
}
|
|
|
|
}
|