项目作者: d-plaindoux

项目描述 :
Rust Parser Combinators
高级语言: Rust
项目地址: git://github.com/d-plaindoux/parsec.rust.git
创建时间: 2018-07-28T05:33:46Z
项目社区:https://github.com/d-plaindoux/parsec.rust

开源协议:Apache License 2.0

下载


Parser Combinator in Rust

Build Status
unstable

Status

This library is no more maintained. Please see Celma generalized parsec instead!

Objective

A parser combinator library
implementation from scratch in Rust.

Parsers

Core definition

A parser is specified by the following Trait.

  1. pub trait Parser<A> {}

Since the Parser size is not known Rust does not allow the Trait type to be returned and used as is. For this reason each parser is denoted by a specific
structure (struct) and the corresponding Parser trait implementation.

Basic parsers

module parsecute::parsers::core

  1. returns :: A -> Parser<A> where A: Copy
  2. fail :: () -> Parser<A>
  3. any :: () -> Parser<u8>
  4. eos :: () -> Parser<()>
  1. satisfy :: self:Parser<A> -> Box<(Fn(&A) -> bool)> -> Parser<A>
  2. do_try :: Parser<A> -> Parser<A>
  3. lookahead :: Parser<A> -> Parser<A>

Monadic

module parsecute::parsers::monadics

  1. fmap :: self:Parser<A> -> (Fn(A) -> B) -> Parser<B>
  2. bind :: self:Parser<A> -> (Fn(A) -> Parser<B>) -> Parser<B>

Flow

module parsecute::parsers::flow

  1. then :: self:Parser<A> -> Parser<B> -> Parser<(A,B)>
  2. or :: self:Parser<A> -> Parser<A> -> Parser<A>
  3. opt :: self:Parser<A> -> Parser<Option<A>>
  4. optrep :: self:Parser<A> -> Parser<Vec<A>>
  5. rep :: self:Parser<A> -> Parser<Vec<A>>
  6. take_while :: (Fn(&u8) -> bool) -> Parser<Vec<u8>>
  7. take_one :: (Fn(&u8) -> bool) -> Parser<Option<u8>>

Literals

module parsecute::parsers::literals

char and string data types implement the do_parse method.

  1. digit :: () -> Parser<char>
  2. letter :: () -> Parser<char>
  3. float :: () -> Parser<FloatLiteral>
  4. string_delim :: () -> Parser<StringLiteral>
  5. char_delim :: () -> Parser<char>

Example

  1. // item ::= [^,]*
  2. // line ::= item (',' item)*
  3. let atom = || take_while(|c| *c != ',' as u8);
  4. let line = atom().then(','.then(atom()).fmap(|(_,b)| b).optrep());

Benchmarks

The benchmarks were run on a 2016 Macbook Pro, quad core 2,7 GHz Intel Core i7.

Brute force tests on basic parsers execution

  1. test basic_and ... bench: 7,971,699 ns/iter (+/- 1,005,128) = 131 MB/s
  2. test basic_any ... bench: 990,096 ns/iter (+/- 550,996) = 1059 MB/s
  3. test basic_do_try ... bench: 911,593 ns/iter (+/- 58,521) = 1150 MB/s
  4. test basic_fmap ... bench: 9,200,929 ns/iter (+/- 880,518) = 113 MB/s
  5. test basic_or ... bench: 11,342,151 ns/iter (+/- 2,780,405) = 92 MB/s
  6. test basic_skip ... bench: 11,097,176 ns/iter (+/- 650,653) = 188 MB/s
  7. test literal_delimited_string ... bench: 10,365 ns/iter (+/- 1,332) = 790 MB/s
  8. test literal_float ... bench: 15,928 ns/iter (+/- 3,338) = 771 MB/s

Json benches

The Parser

  1. fn json_parser<'a>() -> Parsec<'a, JsonValue<'a>> {
  2. #[inline]
  3. fn spaces<E, A>(p: E) -> FMap<And<Skip, (), E, A>, ((), A), A> where E: Parser<A> {
  4. seq!((skip(" \n\r\t".to_string())) ~> (p))
  5. }
  6. fn to_str(s: &[u8]) -> &str {
  7. std::str::from_utf8(s).unwrap()
  8. }
  9. #[inline]
  10. fn object<'a>() -> Parsec<'a, JsonValue<'a>> {
  11. let attribute = || seq!((seq!((spaces(delimited_string())) <~ (spaces(':')))) ~ (json::<'a>()));
  12. let attributes = seq!((attribute()) ~ (seq!((spaces(',')) ~> (attribute())).optrep())).opt();
  13. let parser = seq!(('{') ~> (attributes) <~ (spaces('}'))).fmap(|v| {
  14. let mut r = HashMap::default();
  15. if let Some(((k, e), v)) = v {
  16. r.insert(to_str(k), e);
  17. for (k, e) in v {
  18. r.insert(to_str(k), e);
  19. }
  20. }
  21. JsonValue::Object(r)
  22. });
  23. parsec!('a, parser)
  24. }
  25. #[inline]
  26. fn array<'a>() -> Parsec<'a, JsonValue<'a>> {
  27. let elements = seq!((json::<'a>()) ~ (seq!((spaces(',')) ~> (json::<'a>())).optrep())).opt();
  28. let parser = seq!(('[') ~> (elements) <~ (spaces(']'))).fmap(|v| {
  29. if let Some((e, v)) = v {
  30. let mut r = v;
  31. r.insert(0, e);
  32. JsonValue::Array(r)
  33. } else {
  34. JsonValue::Array(Vec::default())
  35. }
  36. });
  37. parsec!('a, parser)
  38. }
  39. #[inline]
  40. fn json<'a>() -> Parsec<'a, JsonValue<'a>> {
  41. let parser = lazy!(
  42. // This trigger should be done automatically in the next version hiding this ugly parse type impersonation
  43. spaces(lookahead(any()).bind(|c| {
  44. match c as char {
  45. '{' => object::<'a>(),
  46. '[' => array::<'a>(),
  47. '"' => parsec!('a, delimited_string().fmap(|v| JsonValue::Str(to_str(v)))),
  48. 'f' => parsec!('a, "false".fmap(|_| JsonValue::Boolean(false))),
  49. 't' => parsec!('a, "true".fmap(|_| JsonValue::Boolean(true))),
  50. 'n' => parsec!('a, "null".fmap(|_| JsonValue::Null())),
  51. _ => parsec!('a, float().fmap(|v| JsonValue::Num(v.to_f64()))),
  52. }
  53. }))
  54. );
  55. parsec!('a, parser)
  56. }
  57. parsec!('a,json::<'a>().then_left(spaces(eos())))
  58. }

JSon benches based on Nom data set

Reference: Nom & al. Benchmarks

  1. test json_apache ... bench: 2,434,180 ns/iter (+/- 192,180) = 51 MB/s
  2. test json_basic ... bench: 3,960 ns/iter (+/- 374) = 20 MB/s
  3. test json_canada_nom ... bench: 135,969 ns/iter (+/- 23,376) = 75 MB/s
  4. test json_data ... bench: 170,537 ns/iter (+/- 103,725) = 74 MB/s

JSon benches based on Pest data set

Reference: Pest & al. Benchmarks

  1. test json_canada_pest ... bench: 97,576,631 ns/iter (+/- 41,502,590) = 23 MB/s

Based on the throughput the referenced Json file is processed building the corresponding
AST in 86ms.

License

Copyright 2018 D. Plaindoux.

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

  1. 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.