1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#![allow(clippy::new_without_default)]
#![allow(clippy::type_complexity)]
use std::{error::Error, fmt, hash::Hash};
use num_traits::{PrimInt, Unsigned};
use try_from::TryFrom;
mod builder;
mod lexer;
mod parser;
use crate::parser::parse_lex;
pub use crate::{
builder::LexerBuilder,
lexer::{LexerDef, Rule}
};
pub type LexBuildResult<T> = Result<T, LexBuildError>;
#[derive(Debug)]
pub struct LexBuildError {
pub kind: LexErrorKind,
line: usize,
col: usize
}
impl Error for LexBuildError {}
#[derive(Debug)]
pub enum LexErrorKind {
PrematureEnd,
RoutinesNotSupported,
UnknownDeclaration,
MissingSpace,
InvalidName,
DuplicateName,
RegexError
}
impl fmt::Display for LexBuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s;
match self.kind {
LexErrorKind::PrematureEnd => s = "File ends prematurely",
LexErrorKind::RoutinesNotSupported => s = "Routines not currently supported",
LexErrorKind::UnknownDeclaration => s = "Unknown declaration",
LexErrorKind::MissingSpace => s = "Rule is missing a space",
LexErrorKind::InvalidName => s = "Invalid rule name",
LexErrorKind::DuplicateName => s = "Rule name already exists",
LexErrorKind::RegexError => s = "Invalid regular expression"
}
write!(f, "{} at line {} column {}", s, self.line, self.col)
}
}
pub fn build_lex<StorageT: Copy + Eq + Hash + PrimInt + TryFrom<usize> + Unsigned>(
s: &str
) -> Result<LexerDef<StorageT>, LexBuildError> {
parse_lex(s)
}
#[macro_export]
macro_rules! lrlex_mod {
($path:expr) => {
include!(concat!(env!("OUT_DIR"), "/", $path, ".rs"));
};
}