Skip to content

Commit d52d2e8

Browse files
committed
Rename ParseErrors to ParseErrorKind
Signed-off-by: Jacinta Ferrant <jacinta.ferrant@gmail.com>
1 parent f183be7 commit d52d2e8

File tree

22 files changed

+683
-309
lines changed

22 files changed

+683
-309
lines changed

clarity-serialization/src/errors/ast.rs

Lines changed: 87 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::token::Token;
2525
pub type ParseResult<T> = Result<T, ParseError>;
2626

2727
#[derive(Debug, PartialEq)]
28-
pub enum ParseErrors {
28+
pub enum ParseErrorKind {
2929
CostOverflow,
3030
CostBalanceExceeded(ExecutionCost, ExecutionCost),
3131
MemoryBalanceExceeded(u64, u64),
@@ -97,13 +97,13 @@ pub enum ParseErrors {
9797

9898
#[derive(Debug, PartialEq)]
9999
pub struct ParseError {
100-
pub err: ParseErrors,
100+
pub err: ParseErrorKind,
101101
pub pre_expressions: Option<Vec<PreSymbolicExpression>>,
102102
pub diagnostic: Diagnostic,
103103
}
104104

105105
impl ParseError {
106-
pub fn new(err: ParseErrors) -> ParseError {
106+
pub fn new(err: ParseErrorKind) -> ParseError {
107107
let diagnostic = Diagnostic::err(&err);
108108
ParseError {
109109
err,
@@ -113,7 +113,7 @@ impl ParseError {
113113
}
114114

115115
pub fn rejectable(&self) -> bool {
116-
matches!(self.err, ParseErrors::InterpreterFailure)
116+
matches!(self.err, ParseErrorKind::InterpreterFailure)
117117
}
118118

119119
pub fn has_pre_expression(&self) -> bool {
@@ -149,154 +149,164 @@ impl error::Error for ParseError {
149149
}
150150
}
151151

152-
impl From<ParseErrors> for ParseError {
153-
fn from(err: ParseErrors) -> Self {
152+
impl From<ParseErrorKind> for ParseError {
153+
fn from(err: ParseErrorKind) -> Self {
154154
ParseError::new(err)
155155
}
156156
}
157157

158158
impl From<CostErrors> for ParseError {
159159
fn from(err: CostErrors) -> Self {
160160
match err {
161-
CostErrors::CostOverflow => ParseError::new(ParseErrors::CostOverflow),
161+
CostErrors::CostOverflow => ParseError::new(ParseErrorKind::CostOverflow),
162162
CostErrors::CostBalanceExceeded(a, b) => {
163-
ParseError::new(ParseErrors::CostBalanceExceeded(a, b))
163+
ParseError::new(ParseErrorKind::CostBalanceExceeded(a, b))
164164
}
165165
CostErrors::MemoryBalanceExceeded(a, b) => {
166-
ParseError::new(ParseErrors::MemoryBalanceExceeded(a, b))
166+
ParseError::new(ParseErrorKind::MemoryBalanceExceeded(a, b))
167167
}
168168
CostErrors::CostComputationFailed(s) => {
169-
ParseError::new(ParseErrors::CostComputationFailed(s))
169+
ParseError::new(ParseErrorKind::CostComputationFailed(s))
170170
}
171171
CostErrors::CostContractLoadFailure => ParseError::new(
172-
ParseErrors::CostComputationFailed("Failed to load cost contract".into()),
172+
ParseErrorKind::CostComputationFailed("Failed to load cost contract".into()),
173173
),
174174
CostErrors::InterpreterFailure | CostErrors::Expect(_) => {
175-
ParseError::new(ParseErrors::InterpreterFailure)
175+
ParseError::new(ParseErrorKind::InterpreterFailure)
176+
}
177+
CostErrors::ExecutionTimeExpired => {
178+
ParseError::new(ParseErrorKind::ExecutionTimeExpired)
176179
}
177-
CostErrors::ExecutionTimeExpired => ParseError::new(ParseErrors::ExecutionTimeExpired),
178180
}
179181
}
180182
}
181183

182-
impl DiagnosableError for ParseErrors {
184+
impl DiagnosableError for ParseErrorKind {
183185
fn message(&self) -> String {
184186
match &self {
185-
ParseErrors::CostOverflow => "Used up cost budget during the parse".into(),
186-
ParseErrors::CostBalanceExceeded(bal, used) => {
187+
ParseErrorKind::CostOverflow => "Used up cost budget during the parse".into(),
188+
ParseErrorKind::CostBalanceExceeded(bal, used) => {
187189
format!("Used up cost budget during the parse: {bal} balance, {used} used")
188190
}
189-
ParseErrors::MemoryBalanceExceeded(bal, used) => {
191+
ParseErrorKind::MemoryBalanceExceeded(bal, used) => {
190192
format!("Used up memory budget during the parse: {bal} balance, {used} used")
191193
}
192-
ParseErrors::TooManyExpressions => "Too many expressions".into(),
193-
ParseErrors::FailedCapturingInput => "Failed to capture value from input".into(),
194-
ParseErrors::SeparatorExpected(found) => {
194+
ParseErrorKind::TooManyExpressions => "Too many expressions".into(),
195+
ParseErrorKind::FailedCapturingInput => "Failed to capture value from input".into(),
196+
ParseErrorKind::SeparatorExpected(found) => {
195197
format!("Expected whitespace or a close parens. Found: '{found}'")
196198
}
197-
ParseErrors::SeparatorExpectedAfterColon(found) => {
199+
ParseErrorKind::SeparatorExpectedAfterColon(found) => {
198200
format!("Whitespace expected after colon (:), Found: '{found}'")
199201
}
200-
ParseErrors::ProgramTooLarge => "Program too large to parse".into(),
201-
ParseErrors::IllegalContractName(contract_name) => {
202+
ParseErrorKind::ProgramTooLarge => "Program too large to parse".into(),
203+
ParseErrorKind::IllegalContractName(contract_name) => {
202204
format!("Illegal contract name: '{contract_name}'")
203205
}
204-
ParseErrors::IllegalVariableName(var_name) => {
206+
ParseErrorKind::IllegalVariableName(var_name) => {
205207
format!("Illegal variable name: '{var_name}'")
206208
}
207-
ParseErrors::UnknownQuotedValue(value) => format!("Unknown 'quoted value '{value}'"),
208-
ParseErrors::FailedParsingIntValue(value) => {
209+
ParseErrorKind::UnknownQuotedValue(value) => format!("Unknown 'quoted value '{value}'"),
210+
ParseErrorKind::FailedParsingIntValue(value) => {
209211
format!("Failed to parse int literal '{value}'")
210212
}
211-
ParseErrors::FailedParsingUIntValue(value) => {
213+
ParseErrorKind::FailedParsingUIntValue(value) => {
212214
format!("Failed to parse uint literal 'u{value}'")
213215
}
214-
ParseErrors::FailedParsingHexValue(value, x) => {
216+
ParseErrorKind::FailedParsingHexValue(value, x) => {
215217
format!("Invalid hex-string literal {value}: {x}")
216218
}
217-
ParseErrors::FailedParsingPrincipal(value) => {
219+
ParseErrorKind::FailedParsingPrincipal(value) => {
218220
format!("Invalid principal literal: {value}")
219221
}
220-
ParseErrors::FailedParsingBuffer(value) => format!("Invalid buffer literal: {value}"),
221-
ParseErrors::FailedParsingField(value) => format!("Invalid field literal: {value}"),
222-
ParseErrors::FailedParsingRemainder(remainder) => {
222+
ParseErrorKind::FailedParsingBuffer(value) => {
223+
format!("Invalid buffer literal: {value}")
224+
}
225+
ParseErrorKind::FailedParsingField(value) => format!("Invalid field literal: {value}"),
226+
ParseErrorKind::FailedParsingRemainder(remainder) => {
223227
format!("Failed to lex input remainder: '{remainder}'")
224228
}
225-
ParseErrors::ClosingParenthesisUnexpected => {
229+
ParseErrorKind::ClosingParenthesisUnexpected => {
226230
"Tried to close list which isn't open.".into()
227231
}
228-
ParseErrors::ClosingParenthesisExpected => "List expressions (..) left opened.".into(),
229-
ParseErrors::ClosingTupleLiteralUnexpected => {
232+
ParseErrorKind::ClosingParenthesisExpected => {
233+
"List expressions (..) left opened.".into()
234+
}
235+
ParseErrorKind::ClosingTupleLiteralUnexpected => {
230236
"Tried to close tuple literal which isn't open.".into()
231237
}
232-
ParseErrors::ClosingTupleLiteralExpected => "Tuple literal {{..}} left opened.".into(),
233-
ParseErrors::ColonSeparatorUnexpected => "Misplaced colon.".into(),
234-
ParseErrors::CommaSeparatorUnexpected => "Misplaced comma.".into(),
235-
ParseErrors::TupleColonExpected(i) => {
238+
ParseErrorKind::ClosingTupleLiteralExpected => {
239+
"Tuple literal {{..}} left opened.".into()
240+
}
241+
ParseErrorKind::ColonSeparatorUnexpected => "Misplaced colon.".into(),
242+
ParseErrorKind::CommaSeparatorUnexpected => "Misplaced comma.".into(),
243+
ParseErrorKind::TupleColonExpected(i) => {
236244
format!("Tuple literal construction expects a colon at index {i}")
237245
}
238-
ParseErrors::TupleCommaExpected(i) => {
246+
ParseErrorKind::TupleCommaExpected(i) => {
239247
format!("Tuple literal construction expects a comma at index {i}")
240248
}
241-
ParseErrors::TupleItemExpected(i) => {
249+
ParseErrorKind::TupleItemExpected(i) => {
242250
format!("Tuple literal construction expects a key or value at index {i}")
243251
}
244-
ParseErrors::CircularReference(function_names) => format!(
252+
ParseErrorKind::CircularReference(function_names) => format!(
245253
"detected interdependent functions ({})",
246254
function_names.join(", ")
247255
),
248-
ParseErrors::NameAlreadyUsed(name) => {
256+
ParseErrorKind::NameAlreadyUsed(name) => {
249257
format!("defining '{name}' conflicts with previous value")
250258
}
251-
ParseErrors::ImportTraitBadSignature => {
259+
ParseErrorKind::ImportTraitBadSignature => {
252260
"(use-trait ...) expects a trait name and a trait identifier".into()
253261
}
254-
ParseErrors::DefineTraitBadSignature => {
262+
ParseErrorKind::DefineTraitBadSignature => {
255263
"(define-trait ...) expects a trait name and a trait definition".into()
256264
}
257-
ParseErrors::ImplTraitBadSignature => {
265+
ParseErrorKind::ImplTraitBadSignature => {
258266
"(impl-trait ...) expects a trait identifier".into()
259267
}
260-
ParseErrors::TraitReferenceNotAllowed => "trait references can not be stored".into(),
261-
ParseErrors::TraitReferenceUnknown(trait_name) => {
268+
ParseErrorKind::TraitReferenceNotAllowed => "trait references can not be stored".into(),
269+
ParseErrorKind::TraitReferenceUnknown(trait_name) => {
262270
format!("use of undeclared trait <{trait_name}>")
263271
}
264-
ParseErrors::ExpressionStackDepthTooDeep => format!(
272+
ParseErrorKind::ExpressionStackDepthTooDeep => format!(
265273
"AST has too deep of an expression nesting. The maximum stack depth is {MAX_CALL_STACK_DEPTH}"
266274
),
267-
ParseErrors::VaryExpressionStackDepthTooDeep => format!(
275+
ParseErrorKind::VaryExpressionStackDepthTooDeep => format!(
268276
"AST has too deep of an expression nesting. The maximum stack depth is {MAX_CALL_STACK_DEPTH}"
269277
),
270-
ParseErrors::InvalidCharactersDetected => "invalid characters detected".into(),
271-
ParseErrors::InvalidEscaping => "invalid escaping detected in string".into(),
272-
ParseErrors::CostComputationFailed(s) => format!("Cost computation failed: {s}"),
278+
ParseErrorKind::InvalidCharactersDetected => "invalid characters detected".into(),
279+
ParseErrorKind::InvalidEscaping => "invalid escaping detected in string".into(),
280+
ParseErrorKind::CostComputationFailed(s) => format!("Cost computation failed: {s}"),
273281

274282
// Parser v2 errors
275-
ParseErrors::Lexer(le) => le.message(),
276-
ParseErrors::ContractNameTooLong(name) => {
283+
ParseErrorKind::Lexer(le) => le.message(),
284+
ParseErrorKind::ContractNameTooLong(name) => {
277285
format!("contract name '{name}' is too long")
278286
}
279-
ParseErrors::ExpectedContractIdentifier => "expected contract identifier".into(),
280-
ParseErrors::ExpectedTraitIdentifier => "expected trait identifier".into(),
281-
ParseErrors::IllegalTraitName(name) => format!("illegal trait name, '{name}'"),
282-
ParseErrors::InvalidPrincipalLiteral => "invalid principal literal".into(),
283-
ParseErrors::InvalidBuffer => "invalid hex-string literal".into(),
284-
ParseErrors::NameTooLong(name) => format!("illegal name (too long), '{name}'"),
285-
ParseErrors::UnexpectedToken(token) => format!("unexpected '{token}'"),
286-
ParseErrors::ExpectedClosing(token) => format!("expected closing '{token}'"),
287-
ParseErrors::TupleColonExpectedv2 => "expected ':' after key in tuple".into(),
288-
ParseErrors::TupleCommaExpectedv2 => {
287+
ParseErrorKind::ExpectedContractIdentifier => "expected contract identifier".into(),
288+
ParseErrorKind::ExpectedTraitIdentifier => "expected trait identifier".into(),
289+
ParseErrorKind::IllegalTraitName(name) => format!("illegal trait name, '{name}'"),
290+
ParseErrorKind::InvalidPrincipalLiteral => "invalid principal literal".into(),
291+
ParseErrorKind::InvalidBuffer => "invalid hex-string literal".into(),
292+
ParseErrorKind::NameTooLong(name) => format!("illegal name (too long), '{name}'"),
293+
ParseErrorKind::UnexpectedToken(token) => format!("unexpected '{token}'"),
294+
ParseErrorKind::ExpectedClosing(token) => format!("expected closing '{token}'"),
295+
ParseErrorKind::TupleColonExpectedv2 => "expected ':' after key in tuple".into(),
296+
ParseErrorKind::TupleCommaExpectedv2 => {
289297
"expected ',' separating key-value pairs in tuple".into()
290298
}
291-
ParseErrors::TupleValueExpected => "expected value expression for tuple".into(),
292-
ParseErrors::IllegalClarityName(name) => format!("illegal clarity name, '{name}'"),
293-
ParseErrors::IllegalASCIIString(s) => format!("illegal ascii string \"{s}\""),
294-
ParseErrors::IllegalUtf8String(s) => format!("illegal UTF8 string \"{s}\""),
295-
ParseErrors::ExpectedWhitespace => "expected whitespace before expression".into(),
296-
ParseErrors::NoteToMatchThis(token) => format!("to match this '{token}'"),
297-
ParseErrors::UnexpectedParserFailure => "unexpected failure while parsing".to_string(),
298-
ParseErrors::InterpreterFailure => "unexpected failure while parsing".to_string(),
299-
ParseErrors::ExecutionTimeExpired => "max execution time expired".to_string(),
299+
ParseErrorKind::TupleValueExpected => "expected value expression for tuple".into(),
300+
ParseErrorKind::IllegalClarityName(name) => format!("illegal clarity name, '{name}'"),
301+
ParseErrorKind::IllegalASCIIString(s) => format!("illegal ascii string \"{s}\""),
302+
ParseErrorKind::IllegalUtf8String(s) => format!("illegal UTF8 string \"{s}\""),
303+
ParseErrorKind::ExpectedWhitespace => "expected whitespace before expression".into(),
304+
ParseErrorKind::NoteToMatchThis(token) => format!("to match this '{token}'"),
305+
ParseErrorKind::UnexpectedParserFailure => {
306+
"unexpected failure while parsing".to_string()
307+
}
308+
ParseErrorKind::InterpreterFailure => "unexpected failure while parsing".to_string(),
309+
ParseErrorKind::ExecutionTimeExpired => "max execution time expired".to_string(),
300310
}
301311
}
302312

@@ -306,14 +316,14 @@ impl DiagnosableError for ParseErrors {
306316

307317
fn level(&self) -> Level {
308318
match self {
309-
ParseErrors::NoteToMatchThis(_) => Level::Note,
310-
ParseErrors::Lexer(lexer_error) => lexer_error.level(),
319+
ParseErrorKind::NoteToMatchThis(_) => Level::Note,
320+
ParseErrorKind::Lexer(lexer_error) => lexer_error.level(),
311321
_ => Level::Error,
312322
}
313323
}
314324
}
315325

316326
pub struct PlacedError {
317-
pub e: ParseErrors,
327+
pub e: ParseErrorKind,
318328
pub span: Span,
319329
}

clarity-serialization/src/errors/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub mod lexer;
2121
use std::{error, fmt};
2222

2323
pub use analysis::{CheckError, CheckErrors, CheckResult};
24-
pub use ast::{ParseError, ParseErrors, ParseResult};
24+
pub use ast::{ParseError, ParseErrorKind, ParseResult};
2525
pub use cost::CostErrors;
2626
pub use lexer::LexerError;
2727
#[cfg(feature = "rusqlite")]
@@ -175,7 +175,7 @@ impl error::Error for RuntimeErrorType {
175175
impl From<ParseError> for Error {
176176
fn from(err: ParseError) -> Self {
177177
match &err.err {
178-
ParseErrors::InterpreterFailure => Error::from(InterpreterError::Expect(
178+
ParseErrorKind::InterpreterFailure => Error::from(InterpreterError::Expect(
179179
"Unexpected interpreter failure during parsing".into(),
180180
)),
181181
_ => Error::from(RuntimeErrorType::ASTError(err)),

0 commit comments

Comments
 (0)