shex_compact/
shex_parser_error.rs

1use iri_s::IriSError;
2use prefixmap::{DerefError, PrefixMapError};
3use std::{
4    io,
5    num::{ParseFloatError, ParseIntError},
6};
7use thiserror::Error;
8
9use crate::{LocatedParseError, Span};
10
11#[derive(Error, Debug)]
12pub enum ParseError {
13    #[error("Parsing error: {err}")]
14    NomError { err: Box<LocatedParseError> },
15
16    #[error("Parsing node selector for string: {str}: {err}")]
17    NodeSelectorNomError {
18        err: Box<LocatedParseError>,
19        str: String,
20    },
21
22    #[error(transparent)]
23    IOError {
24        #[from]
25        err: io::Error,
26    },
27
28    #[error(transparent)]
29    PrefixMapError {
30        #[from]
31        err: PrefixMapError,
32    },
33
34    #[error("{msg}")]
35    Custom { msg: String },
36
37    #[error(transparent)]
38    IRISError {
39        #[from]
40        err: IriSError,
41    },
42
43    #[error(transparent)]
44    DerefError {
45        #[from]
46        err: DerefError,
47    },
48
49    #[error("Syntax error: {0}")]
50    SyntaxError(String),
51
52    #[error("Expected further input: {0}")]
53    MissingInput(String),
54
55    #[error(r#"Expected "{0}""#)]
56    ExpectedToken(String),
57
58    #[error("Expected shape definition of shape reference")]
59    ExpectedShapeOrRef,
60
61    #[error("Expected shape expression declaration")]
62    ExpectedShapeExprDecl,
63
64    #[error("Expected exclusion that starts by .")]
65    ExclusionPlus,
66
67    #[error("Expected exclusion")]
68    Exclusion,
69
70    #[error("Expected set of values between [ and ]")]
71    ValueSet,
72
73    #[error("Expected value set value")]
74    ValueSetValue,
75
76    #[error("Expected value set")]
77    ValueSetFacets,
78
79    #[error("Expected literal node constraint")]
80    LitNodeConstraint,
81
82    #[error("Expected shape expression definition or external ")]
83    ShapeExprOrExternal,
84
85    #[error("Expected non literal node constraint followed by optional shape or shape reference")]
86    NonLitNodeConstraintOptShapeOrRef,
87
88    #[error(
89        "Expected non literal inline node constraint followed by optional shape or shape reference"
90    )]
91    NonLitInlineNodeConstraintOptShapeOrRef,
92
93    #[error("Expected inline shape atom")]
94    ExpectedInlineShapeAtom,
95
96    #[error("Expected datatype with optional xs_facets")]
97    DatatypeFacets,
98
99    #[error("Expected prefixed name")]
100    ExpectedPrefixedName,
101
102    #[error("Expected extends followed by shape references")]
103    Extension,
104
105    #[error("Expected Start declaration")]
106    ExpectedStart,
107
108    #[error("Expected cardinality")]
109    ExpectedCardinality,
110
111    #[error("Expected triple constraint")]
112    ExpectedTripleConstraint,
113
114    #[error("Expected literal range")]
115    ExpectedLiteralRange,
116
117    #[error("Expected prefix declaration")]
118    ExpectedPrefixDecl,
119
120    #[error("Expected cardinality declaration starting by {{")]
121    ExpectedRepeatRange,
122
123    #[error("Expected rest of cardinality declaration after comma")]
124    ExpectedRestRepeatRange,
125
126    #[error("Expected shape expr")]
127    ExpectedShapeExpr,
128
129    #[error("Expected inline shape expr")]
130    ExpectedInlineShapeExpr,
131
132    #[error("Expected IRI or Literal")]
133    ExpectedIriOrLiteral,
134
135    #[error("Expected language range")]
136    LanguageRange,
137
138    #[error("Expected Literal")]
139    Literal,
140
141    #[error("Expected Shape Atom")]
142    ShapeAtom,
143
144    #[error("Expected annotation")]
145    ExpectedAnnotation,
146
147    #[error("Expected triple expression")]
148    TripleExpression,
149
150    #[error("Expected string literal between single quotes")]
151    StringLiteralQuote,
152
153    #[error("Expected RDF Literal")]
154    RDFLiteral,
155
156    #[error("Expected triple expression between parenthesis")]
157    BracketedTripleExpr,
158
159    #[error("Expected OneOf triple expression")]
160    OneOfTripleExpr,
161
162    #[error("Expected code in semantic action")]
163    Code,
164
165    #[error("Expected code declaration")]
166    CodeDeclaration,
167
168    #[error("Expected unary triple expression")]
169    UnaryTripleExpr,
170
171    #[error("Expected include")]
172    Include,
173
174    #[error("Expected base declaration")]
175    ExpectedBaseDecl,
176
177    #[error("Expected import declaration")]
178    ExpectedImportDecl,
179
180    #[error("Expected string literal")]
181    ExpectedStringLiteral,
182
183    #[error("Expected shape definition")]
184    ExpectedShapeDefinition,
185
186    #[error("Expected EXTRA property set")]
187    ExpectedEXTRAPropertySet,
188
189    #[error("Expected CLOSED")]
190    ExpectedClosed,
191
192    #[error("Expected CLOSED or EXTRA followed by list of predicates")]
193    ExpectedQualifier,
194
195    #[error("Expected list of CLOSED or EXTRA followed by list of predicates")]
196    ExpectedQualifiers,
197
198    #[error("Parse int error for str {str}: {err} ")]
199    ParseIntError { str: String, err: ParseIntError },
200
201    #[error("Parse f64 error for str {str}: {err}")]
202    ParseFloatError { str: String, err: ParseFloatError },
203
204    #[error("Expected numeric literal")]
205    NumericLiteral,
206
207    #[error("Expected integer literal")]
208    IntegerLiteral,
209
210    #[error("Expected integer")]
211    Integer,
212
213    #[error("Expected ShapeSpec: IRIREF, BNode or START")]
214    ExpectedShapeSpec,
215
216    #[error("Expected ShEx statement")]
217    ExpectedStatement,
218
219    #[error("Expected ShapeMap association")]
220    ExpectedShapeMapAssociation,
221
222    #[error("Expected node selector specification")]
223    ExpectedNodeSpec,
224
225    #[error("Failed regular expression, str: {str} doesn't match: {re}")]
226    RegexFailed { re: String, str: String },
227
228    #[error("Utf8 error: {error}")]
229    Utf8Error { error: String },
230}
231
232impl ParseError {
233    /// Locate this error by adding a position.
234    pub fn at(self, position: Span) -> LocatedParseError {
235        // miri doesn't like nom_locate, cf. https://github.com/fflorent/nom_locate/issues/88
236        let column = if cfg!(not(miri)) {
237            position.naive_get_utf8_column()
238        } else {
239            0
240        };
241        let fragment = if position.is_empty() {
242            String::new()
243        } else {
244            let line = if cfg!(not(miri)) {
245                String::from_utf8(position.get_line_beginning().to_vec())
246                    .expect("input is valid UTF-8")
247            } else {
248                String::new()
249            };
250            format!("\"{line}\"\n{}^", "-".repeat(3 + column))
251        };
252
253        LocatedParseError {
254            source: self,
255            line: position.location_line(),
256            column,
257            fragment,
258            context: Vec::new(),
259        }
260    }
261}