1use std::fmt;
3use thiserror::Error;
4
5#[derive(Error, Eq, PartialEq)]
7pub enum SyntaxError {
8 #[error("Syntax error, expecting identifier")]
10 ExpectedIdentifier(String),
11
12 #[error("Syntax error, expecting path")]
14 ExpectedPath(String),
15
16 #[error("Syntax error, block name must be an identifier")]
18 BlockName(String),
19
20 #[error("Syntax error, new lines in raw literals must be escaped (\\n)")]
22 LiteralNewline(String),
23
24 #[error("Syntax error, partial operator (>) must come first")]
26 PartialPosition(String),
27
28 #[error(
30 "Syntax error, got close sub-expression but no sub-expression is open"
31 )]
32 SubExprNotOpen(String),
33
34 #[error(
39 "Syntax error, sub-expression must use an identifier for the target"
40 )]
41 SubExprTargetNotAllowed(String),
42
43 #[error("Syntax error, path delimiter (.) not allowed here")]
45 PathDelimiterNotAllowed(String),
46
47 #[error("Syntax error, 'else' keyword is not allowed here")]
49 ElseNotAllowed(String),
50
51 #[error(
53 "Syntax error, explicit this reference must be at the start of a path"
54 )]
55 UnexpectedPathExplicitThis(String),
56
57 #[error("Syntax error, parent scopes must be at the start of a path")]
59 UnexpectedPathParent(String),
60
61 #[error(
63 "Syntax error, local scope identifiers must be at the start of a path"
64 )]
65 UnexpectedPathLocal(String),
66
67 #[error("Syntax error, expected identifier but got path delimiter")]
69 UnexpectedPathDelimiter(String),
70
71 #[error("Syntax error, parent scopes and local identifiers are mutually exclusive")]
73 UnexpectedPathParentWithLocal(String),
74
75 #[error(
77 "Syntax error, parent scopes and explicit this are mutually exclusive"
78 )]
79 UnexpectedPathParentWithExplicit(String),
80
81 #[error("Syntax error, expected path delimiter (.)")]
83 ExpectedPathDelimiter(String),
84
85 #[error("Syntax error, sub-expression not terminated")]
87 OpenSubExpression(String),
88
89 #[error("Syntax error, closing name does not match")]
91 TagNameMismatch(String),
92
93 #[error("Syntax error, got a closing tag but no block is open")]
95 BlockNotOpen(String),
96
97 #[error("Syntax error, sub-expression was not terminated")]
99 SubExpressionNotTerminated(String),
100 #[error("Syntax error, link was not terminated")]
102 LinkNotTerminated(String),
103
104 #[error("Syntax error, raw block open tag was not terminated")]
106 RawBlockOpenNotTerminated(String),
107
108 #[error("Syntax error, raw block was not terminated")]
110 RawBlockNotTerminated(String),
111 #[error("Syntax error, raw comment was not terminated")]
113 RawCommentNotTerminated(String),
114 #[error("Syntax error, raw statement was not terminated")]
116 RawStatementNotTerminated(String),
117 #[error("Syntax error, comment was not terminated")]
119 CommentNotTerminated(String),
120
121 #[error("Syntax error, block target sub expressions are only supported for partials")]
123 BlockTargetSubExpr(String),
124 #[error("Syntax error, path is empty")]
126 EmptyPath(String),
127 #[error("Syntax error, path component type could not be identified")]
129 ComponentType(String),
130 #[error("Syntax error, partials and conditionals may not be combined")]
132 MixedPartialConditional(String),
133
134 #[error("Syntax error, unexpected error token for context '{0}'")]
136 TokenError(String, String),
137 #[error("Syntax error, expecting path or sub-expression for call target")]
139 TokenCallTarget(String),
140 #[error("Syntax error, expecting JSON literal token")]
142 TokenJsonLiteral(String),
143 #[error("Syntax error, expecting parameter token")]
145 TokenParameter(String),
146 #[error("Syntax error, expecting key/value token")]
148 TokenHashKeyValue(String),
149 #[error("Syntax error, expecting raw literal token")]
151 TokenRawLiteral(String),
152 #[error("Syntax error, unexpected token parsing quoted literal (\"\")")]
154 TokenDoubleQuoteLiteral(String),
155 #[error("Syntax error, unexpected token parsing quoted literal ('')")]
157 TokenSingleQuoteLiteral(String),
158 #[error("Syntax error, unexpected token parsing quoted literal ([])")]
160 TokenArrayLiteral(String),
161 #[error("Syntax error, unexpected token parsing link")]
163 TokenLink(String),
164 #[error("Syntax error, unexpected token parsing path")]
166 TokenParameterPath(String),
167 #[error("Syntax error, unexpected token, expecting end of raw block")]
169 TokenEndRawBlock(String),
170}
171
172impl fmt::Debug for SyntaxError {
173 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174 write!(f, "{}\n", self.to_string())?;
175 match *self {
176 Self::ExpectedIdentifier(ref source)
177 | Self::ExpectedPath(ref source)
178 | Self::BlockName(ref source)
179 | Self::LiteralNewline(ref source)
180 | Self::PartialPosition(ref source)
181 | Self::SubExprNotOpen(ref source)
182 | Self::SubExprTargetNotAllowed(ref source)
183 | Self::PathDelimiterNotAllowed(ref source)
184 | Self::ElseNotAllowed(ref source)
185 | Self::UnexpectedPathExplicitThis(ref source)
186 | Self::UnexpectedPathParent(ref source)
187 | Self::UnexpectedPathLocal(ref source)
188 | Self::UnexpectedPathDelimiter(ref source)
189 | Self::UnexpectedPathParentWithLocal(ref source)
190 | Self::UnexpectedPathParentWithExplicit(ref source)
191 | Self::ExpectedPathDelimiter(ref source)
192 | Self::OpenSubExpression(ref source)
193 | Self::TagNameMismatch(ref source)
194 | Self::SubExpressionNotTerminated(ref source)
195 | Self::LinkNotTerminated(ref source)
196 | Self::RawBlockNotTerminated(ref source)
197 | Self::RawCommentNotTerminated(ref source)
198 | Self::RawStatementNotTerminated(ref source)
199 | Self::CommentNotTerminated(ref source)
200 | Self::BlockTargetSubExpr(ref source)
201 | Self::EmptyPath(ref source)
202 | Self::ComponentType(ref source)
203 | Self::MixedPartialConditional(ref source)
204 | Self::RawBlockOpenNotTerminated(ref source)
205 | Self::TokenError(_, ref source)
206 | Self::TokenCallTarget(ref source)
207 | Self::TokenJsonLiteral(ref source)
208 | Self::TokenParameter(ref source)
209 | Self::TokenHashKeyValue(ref source)
210 | Self::TokenRawLiteral(ref source)
211 | Self::TokenDoubleQuoteLiteral(ref source)
212 | Self::TokenSingleQuoteLiteral(ref source)
213 | Self::TokenArrayLiteral(ref source)
214 | Self::TokenLink(ref source)
215 | Self::TokenParameterPath(ref source)
216 | Self::TokenEndRawBlock(ref source)
217 | Self::BlockNotOpen(ref source) => write!(f, "{}", source)?,
218 }
219 Ok(())
220 }
221}