nameless_peg_parser/peg/
parsing.rs

1use crate::peg::expression::Expression;
2use std::fmt::{Display, Formatter};
3
4/// Capture(a, b) is an index into the source string x representing the slice x\[a..b]
5#[derive(Clone, Debug)]
6pub struct Capture(pub usize, pub usize);
7
8#[derive(Clone, Debug)]
9pub enum Token {
10    Terminal(Capture),
11    NonTerminal(String, Capture, Vec<Token>),
12}
13
14#[derive(Clone, Debug)]
15pub struct ParserOutput(pub u32, pub usize, pub Result<Vec<Token>, ParserError>);
16
17#[derive(Clone, Debug)]
18#[allow(unused)]
19pub enum ParserErrorCode {
20    UnexpectedEndOfInput,
21    ExpressionDoesNotMatch,
22    NotDidMatch(Vec<Token>),
23    NonTerminalDoesNotMatch,
24}
25
26#[derive(Clone, Debug)]
27#[allow(unused)]
28pub struct ParserError {
29    pub position: usize,
30    pub expression: Expression,
31    pub error: ParserErrorCode,
32    pub cause: Option<Box<ParserError>>,
33}
34
35impl Display for ParserError {
36    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
37        match &self.cause {
38            None => write!(
39                f,
40                "Encountered {} @ {} for '{}'",
41                self.error, self.position, self.expression
42            ),
43            Some(inner) => write!(
44                f,
45                "Encountered {} @ {} for '{}'\n\tCaused by: {}",
46                self.error, self.position, self.expression, inner
47            ),
48        }
49    }
50}
51
52impl Display for ParserErrorCode {
53    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54        write!(f, "{:?}", self)
55    }
56}