kodept_parse/
parser.rs

1use cfg_if::cfg_if;
2
3use kodept_core::structure::rlt::RLT;
4
5use crate::common::{ErrorAdapter, RLTProducer};
6use crate::error::{Original, ParseErrors};
7use crate::lexer::Token;
8use crate::token_stream::TokenStream;
9
10cfg_if! {
11    if #[cfg(feature = "peg")] {
12        pub type DefaultParser = PegParser<false>;
13    } else if #[cfg(feature = "nom")] {
14        pub type DefaultParser = NomParser;
15    } else {
16        compile_error!("Either feature `peg` or `nom` must be enabled for this crate");
17    }
18}
19
20#[cfg(feature = "nom")]
21pub type NomParser = crate::nom::Parser;
22#[cfg(feature = "peg")]
23pub type PegParser<const TRACE: bool> = crate::peg::Parser<TRACE>;
24
25pub fn parse_from_top<'t, A, E, P>(input: TokenStream<'t>, parser: P) -> Result<RLT, ParseErrors<A>>
26where
27    P: RLTProducer<Error<'t> = E>,
28    E: ErrorAdapter<A, TokenStream<'t>>,
29    TokenStream<'t>: Original<A>,
30{
31    match parser.parse_rlt(input) {
32        Ok(x) => Ok(x),
33        Err(e) => Err(e.adapt(input, 0)),
34    }
35}
36
37pub fn default_parse_from_top(input: TokenStream) -> Result<RLT, ParseErrors<Token>>
38{
39    parse_from_top(input, DefaultParser::new())
40}