28 releases (7 breaking)

0.8.5 Sep 18, 2024
0.7.2 Sep 13, 2024
0.6.1 Jun 6, 2024
0.2.5 Mar 26, 2024

#1487 in Rust patterns

Download history 131/week @ 2024-08-22 13/week @ 2024-08-29 1058/week @ 2024-09-12 279/week @ 2024-09-19 61/week @ 2024-09-26 17/week @ 2024-10-03

1,496 downloads per month
Used in roped

MIT/Apache

27KB
705 lines

Parsr

A simple libary for creating new parsing systems as well as a few simple parsers for common use cases with support for custom contexts (such as trimming by arbitrary characters).

Usage

Trimming:

use parsr::prelude::*;

use trim_parser::TrimParser;
use white_space::WhiteSpace;

let input = " hello";

let trimmed = input
    .parse_to_with::<TrimParser>(&WhiteSpace)
    .map(|t| t.input()); // the 'into_output()' is the unit struct '()'

assert_eq!(trimmed, Ok(Some("hello")));

Segmenting:

use parsr::*;

use segment_parser::SegmentParser;
use white_space::WhiteSpace;

let input = " \n hello world \t !!! \r \n";

let vec = input
    .parse_iter_with::<SegmentParser>(&WhiteSpace)
    .map(|t| t.get())
    .collect::<Vec<_>>();

assert_eq!(vec, vec!["hello", "world", "!!!"]);

Functionality

Core traits:

  • ParseFrom: parsing an object from an input
  • ParseFromWith: parsing an object from an input and a reference to context
  • ParseFromWithMut: parsing an object from an input and a mutable reference to context

Iterators:

  • ParseIter: iterator that calls ParseFrom until there is an error or the input is empty
  • ParseIterWith: iterator that calls ParseFromWith until there is an error or the input is empty
  • ParseIterWithMut: iterator that calls ParseFromWithMut until there is an error or the input is empty

Parsers:

  • TrimParser: trims an input
  • UntilParser: reads to the output until a match is found (trim but it saves the trimmed part as an output)
  • SegmentParser: an application of TrimParser followed by UntilParser
  • ExpectParser: like trim except with more versatile auto derives and containers

Matchers:

  • MatchOver: a trait that allows you to match over an input for 'TrimParser'
  • FindMatch: a trait that allows you to find a match in an input for 'UntilParser'
  • Combined they allow a matcher to be used in a 'SegmentParser'

Dependencies

~7KB