1#![warn(missing_docs)]
2use gss::{GSSNodeIndex, GSSNode};
10use petgraph::prelude::EdgeIndex;
11use thiserror::Error;
12
13use wagon_utils::{comma_separated_with_or_str, ErrorReport};
14use std::{hash::{Hash, Hasher}, rc::Rc, str::{from_utf8, Utf8Error}, collections::HashSet, mem::Discriminant};
15
16use sppf::{SPPFNodeIndex, SPPFNode};
17use value::{Value, ValueError, InnerValue, InnerValueError};
18use wagon_ident::Ident;
19
20pub mod sppf;
22pub mod gss;
24pub mod value;
26
27mod label;
28mod state;
30mod descriptor;
31mod slot;
32
33pub use label::{Label, RegexTerminal};
34pub use state::{GLLState, LabelMap, RuleMap, RegexMap};
35pub use slot::GrammarSlot;
36
37pub type TerminalBit<'a> = &'a u8;
39pub type Terminal<'a> = &'a[u8];
41
42pub const ROOT_UUID: &str = "S'";
46
47pub type GLLBlockLabel<'a> = Rc<dyn Label<'a>>;
49
50pub type AttributeMap<'a> = Vec<Value<'a>>;
52pub type ReturnMap<'a> = Vec<Option<Value<'a>>>;
54pub type AttributeKey = usize;
56
57pub type GLLResult<'a, T> = Result<T, GLLError<'a>>;
59
60pub type ParseResult<'a, T> = Result<T, GLLParseError<'a>>;
62
63pub type ImplementationResult<'a, T> = Result<T, GLLImplementationError<'a>>;
65
66#[derive(Debug, Error)]
67pub enum GLLError<'a> {
69 #[error(transparent)]
71 ImplementationError(GLLImplementationError<'a>),
72 #[error(transparent)]
74 ParseError(GLLParseError<'a>),
75 #[error("{0}")]
77 ProcessError(#[from] GLLProcessError)
78}
79
80#[derive(Debug, Error)]
81pub enum GLLImplementationError<'a> {
83 #[error("{0}")]
85 Utf8Error(#[from] Utf8Error),
86 #[error("{0}")]
88 ValueError(ValueError<'a>),
89 #[error("No rule with id {0} exists in the state object.")]
91 UnknownRule(&'a str),
92 #[error("No label with id {0} exists in the state object.")]
94 UnknownLabel(&'a str),
95 #[error("{ROOT_UUID} could not be found.")]
97 MissingRoot,
98 #[error("Expected to find SPPF node {0:?} in the graph, but it was not there.")]
100 MissingSPPFNode(SPPFNodeIndex),
101 #[error("Expected SPPFNode of type {}, got {1:?}", comma_separated_with_or_str(.0))]
103 IncorrectSPPFType(Vec<&'a str>, Discriminant<SPPFNode<'a>>),
104 #[error("Expected to find GSS node {0:?} in the graph, but it was not there.")]
106 MissingGSSNode(GSSNodeIndex),
107 #[error("Expected to find GSS edge {0:?} in the graph, but it was not there.")]
109 MissingGSSEdge(EdgeIndex),
110 #[error("The {0}th attribute is not at GSS node {1:?}")]
112 MissingAttribute(AttributeKey, Rc<GSSNode<'a>>),
113 #[error("The {0}th attribute is not in the context of GSS node {1:?}")]
115 MissingContext(AttributeKey, Rc<GSSNode<'a>>),
116 #[error("Tried to access completed slot {0} as if it were not completed.")]
118 CompletedSlot(String),
119 #[error("A fatal error occurred! {0}.")]
121 Fatal(&'a str),
122}
123
124#[derive(Debug, Error)]
125pub enum GLLParseError<'a> {
127 #[error("Encountered unexpected byte at {pointer}. Expected {expected} saw {offender}.")]
129 UnexpectedByte {
130 pointer: usize,
132 expected: u8,
134 offender: u8
136 },
137 #[error("Tried reading more than possible from input. Current pointer at {pointer}, tried reading {offender:?}.")]
139 TooLong {
140 pointer: usize,
142 offender: Terminal<'a>
144 },
145 #[error("No parse candidates were found for rule `{rule}` in context `{context}`")]
147 NoCandidates {
148 pointer: usize,
150 rule: String,
152 context: String
154 },
155 #[error("All weights for rule `{rule}` in context `{context}` were 0")]
157 ZeroWeights {
158 pointer: usize,
160 rule: String,
162 context: String
164 }
165}
166
167impl<'a> From<GLLImplementationError<'a>> for GLLError<'a> {
168 fn from(value: GLLImplementationError<'a>) -> Self {
169 Self::ImplementationError(value)
170 }
171}
172
173impl<'a> From<GLLParseError<'a>> for GLLError<'a> {
174 fn from(value: GLLParseError<'a>) -> Self {
175 Self::ParseError(value)
176 }
177}
178
179impl<'a> From<InnerValueError<Value<'a>>> for GLLImplementationError<'a> {
180 fn from(value: InnerValueError<Value<'a>>) -> Self {
181 Self::ValueError(ValueError::ValueError(value))
182 }
183}
184
185impl<'a> From<ValueError<'a>> for GLLImplementationError<'a> {
186 fn from(value: ValueError<'a>) -> Self {
187 Self::ValueError(value)
188 }
189}
190
191impl<'a> From<ValueError<'a>> for GLLError<'a> {
192 fn from(value: ValueError<'a>) -> Self {
193 Self::ImplementationError(GLLImplementationError::ValueError(value))
194 }
195}
196
197impl<'a> From<InnerValueError<Value<'a>>> for GLLError<'a> {
198 fn from(value: InnerValueError<Value<'a>>) -> Self {
199 Self::ImplementationError(GLLImplementationError::ValueError(ValueError::ValueError(value)))
200 }
201}
202
203impl<'a> From<InnerValueError<InnerValue<Value<'a>>>> for GLLImplementationError<'a> {
204 fn from(value: InnerValueError<InnerValue<Value<'a>>>) -> Self {
205 Self::ValueError(ValueError::ValueError(value.into()))
206 }
207}
208
209impl<'a> From<InnerValueError<InnerValue<Value<'a>>>> for GLLError<'a> {
210 fn from(value: InnerValueError<InnerValue<Value<'a>>>) -> Self {
211 Self::ImplementationError(GLLImplementationError::from(value))
212 }
213}
214
215impl ErrorReport for GLLError<'_> {
216 fn span(self) -> wagon_utils::Span {
217 match self {
218 GLLError::ParseError(e) => match e {
219 GLLParseError::UnexpectedByte { pointer, .. } | GLLParseError::TooLong { pointer, .. }
220 | GLLParseError::NoCandidates { pointer, .. } | GLLParseError::ZeroWeights { pointer, .. } => pointer..pointer,
221 },
222 _ => wagon_utils::Span::default(),
223 }
224 }
225
226 fn msg(&self) -> (String, String) {
227 match self {
228 GLLError::ImplementationError(e) => ("Fatal Implementation Error".to_string(), e.to_string()),
229 GLLError::ParseError(e) => ("Parse Error".to_string(), e.to_string()),
230 GLLError::ProcessError(e) => ("Post-Processing Error".to_string(), e.to_string()),
231 }
232 }
233}
234
235pub type ProcessResult<T> = Result<T, GLLProcessError>;
237
238#[derive(Debug, Error)]
240pub enum GLLProcessError {
241 #[error("Expected to find SPPF node {0:?} in the graph, but it was not there.")]
243 MissingSPPFNode(SPPFNodeIndex),
244}