Function parse

Source
pub fn parse<In: Input, Reason>(
    pat: impl Pattern,
) -> impl Parser<In, In, Reason>
Expand description

Parses 1 instance of pattern pat.

§Errors

The returned parser returns a recoverable error if the pattern didn’t match at the beginning of the input.

Examples found in repository?
examples/xml.rs (line 56)
55fn parse_string<In: Input>(input: In) -> ParsingResult<In, In, Error> {
56    parse('"')
57        .then(parse_until_ex(NotEscaped('\\', '"')).or_reason(Error::UnclosedString))
58        .parse(input)
59}
60
61fn parse_attr<In: Input>(input: In) -> ParsingResult<In, Attr<In>, Error> {
62    parse_ident
63        .skip(parse_whitespace)
64        .and(
65            parse('=')
66                .skip(parse_whitespace)
67                .then(parse_string.or_reason(Error::NoAttrValue))
68                .skip(parse_whitespace)
69                .maybe(),
70        )
71        .map_out(from_tuple!(Attr { name, value }))
72        .parse(input)
73}
74
75fn parse_tag<In: Input>(input: In) -> ParsingResult<In, Fragment<In>, Error> {
76    parse_whitespace::<In, Error>
77        .then(parse('<'))
78        .then(parse_whitespace)
79        .then(parse('/').ok())
80        .skip(parse_whitespace)
81        .and(parse_ident)
82        .skip(parse_whitespace)
83        .map(match_out! {
84            (true, name) => ready(Fragment::ClosingTag { name }),
85            (false, name) => parse_attr
86                .collect()
87                .and(parse('/').ok())
88                .skip(parse_whitespace)
89                .map_out(|(attrs, self_closing)| Fragment::Tag { self_closing, name: name.clone(), attrs })
90        })
91        .skip(parse_whitespace)
92        .skip(parse('>').or_reason(Error::TagUnclosed))
93        .parse(input)
94}