#parser

parsr

a libary for simple parsing

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

#780 in #parser

Download history 2/week @ 2024-07-02 125/week @ 2024-08-20 19/week @ 2024-08-27 913/week @ 2024-09-10 378/week @ 2024-09-17 80/week @ 2024-09-24 44/week @ 2024-10-01

896 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