pegtastic_runtime/lib.rs
1use std::fmt::Display;
2
3pub mod str;
4mod slice;
5pub mod error;
6
7/// The result type used internally in the parser.
8///
9/// You'll only need this if implementing the `Parse*` traits for a custom input
10/// type. The public API of a parser adapts errors to `std::result::Result`.
11#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
12pub enum RuleResult<T> {
13 Matched(usize, T),
14 Failed,
15}
16
17/// A type that can be used as input to a parser.
18pub trait Parse {
19 type PositionRepr: Display;
20 fn start<'input>(&'input self) -> usize;
21 fn position_repr<'input>(&'input self, p: usize) -> Self::PositionRepr;
22}
23
24/// A parser input type supporting the `[...]` syntax.
25pub trait ParseElem: Parse {
26 /// Type of a single atomic element of the input, for example a character or token
27 type Element;
28
29 /// Get the element at `pos`, or `Failed` if past end of input.
30 fn parse_elem(&self, pos: usize) -> RuleResult<Self::Element>;
31}
32
33/// A parser input type supporting the `"literal"` syntax.
34pub trait ParseLiteral: Parse {
35 /// Attempt to match the `literal` string at `pos`, returning whether it
36 /// matched or failed.
37 fn parse_string_literal(&self, pos: usize, literal: &str) -> RuleResult<()>;
38}
39
40/// A parser input type supporting the `$()` syntax.
41pub trait ParseSlice<'input>: Parse {
42 /// Type of a slice of the input.
43 type Slice;
44
45 /// Get a slice of input.
46 fn parse_slice(&'input self, p1: usize, p2: usize) -> Self::Slice;
47}
48