regex_anre/
lib.rs

1// Copyright (c) 2024 Hemashushu <hippospark@gmail.com>, All rights reserved.
2//
3// This Source Code Form is subject to the terms of
4// the Mozilla Public License version 2.0 and additional exceptions,
5// more details in file LICENSE, LICENSE.additional and CONTRIBUTING.
6
7mod anre;
8mod ast;
9mod charwithposition;
10mod compiler;
11mod errorprinter;
12mod instance;
13mod location;
14mod peekableiter;
15mod printer;
16mod rulechecker;
17mod tradition;
18mod transition;
19mod utf8reader;
20
21pub mod process;
22pub mod route;
23
24pub use process::Regex;
25
26use std::fmt::{self, Display};
27
28use crate::location::Location;
29
30#[derive(Debug, PartialEq, Clone)]
31pub enum AnreError {
32    SyntaxIncorrect(String),
33    UnexpectedEndOfDocument(String),
34
35    // note that the "index" (and the result of "index+length") may exceed
36    // the last index of string, for example, the "char incomplete" error raised by a string `'a`,
37    // which index is 2.
38    MessageWithLocation(String, Location),
39}
40
41impl Display for AnreError {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        match self {
44            AnreError::SyntaxIncorrect(msg) => f.write_str(msg),
45            AnreError::UnexpectedEndOfDocument(detail) => {
46                writeln!(f, "Unexpected to reach the end of document.")?;
47                write!(f, "{}", detail)
48            }
49            AnreError::MessageWithLocation(detail, location) => {
50                writeln!(
51                    f,
52                    "Error at line: {}, column: {}",
53                    location.line + 1,
54                    location.column + 1
55                )?;
56                write!(f, "{}", detail)
57            }
58        }
59    }
60}
61
62impl std::error::Error for AnreError {}