-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy patherrors.rs
More file actions
121 lines (113 loc) · 5.11 KB
/
Copy patherrors.rs
File metadata and controls
121 lines (113 loc) · 5.11 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::parser::ParserError;
use crate::parser::ParserErrorReason;
use crate::token::SpannedToken;
use super::token::Token;
use noirc_errors::CustomDiagnostic as Diagnostic;
use noirc_errors::Span;
use thiserror::Error;
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum LexerErrorKind {
#[error("An unexpected character {:?} was found.", found)]
UnexpectedCharacter { span: Span, expected: String, found: Option<char> },
#[error("Internal error: Tried to lex {:?} as a double char token", found)]
NotADoubleChar { span: Span, found: Token },
#[error("Invalid integer literal, {:?} is not a integer", found)]
InvalidIntegerLiteral { span: Span, found: String },
#[error("{:?} is not a valid attribute", found)]
MalformedFuncAttribute { span: Span, found: String },
#[error("Integer type is larger than the maximum supported size of u128")]
TooManyBits { span: Span, max: u32, got: u32 },
#[error("Logical and used instead of bitwise and")]
LogicalAnd { span: Span },
#[error("Unterminated block comment")]
UnterminatedBlockComment { span: Span },
#[error("Unterminated string literal")]
UnterminatedStringLiteral { span: Span },
#[error(
"'\\{escaped}' is not a valid escape sequence. Use '\\' for a literal backslash character."
)]
InvalidEscape { escaped: char, span: Span },
}
impl From<LexerErrorKind> for ParserError {
fn from(value: LexerErrorKind) -> Self {
let span = value.span();
ParserError::with_reason(ParserErrorReason::Lexer(value), span)
}
}
impl LexerErrorKind {
pub fn span(&self) -> Span {
match self {
LexerErrorKind::UnexpectedCharacter { span, .. } => *span,
LexerErrorKind::NotADoubleChar { span, .. } => *span,
LexerErrorKind::InvalidIntegerLiteral { span, .. } => *span,
LexerErrorKind::MalformedFuncAttribute { span, .. } => *span,
LexerErrorKind::TooManyBits { span, .. } => *span,
LexerErrorKind::LogicalAnd { span } => *span,
LexerErrorKind::UnterminatedBlockComment { span } => *span,
LexerErrorKind::UnterminatedStringLiteral { span } => *span,
LexerErrorKind::InvalidEscape { span, .. } => *span,
}
}
fn parts(&self) -> (String, String, Span) {
match self {
LexerErrorKind::UnexpectedCharacter {
span,
expected,
found,
} => {
let found: String = found.map(Into::into).unwrap_or_else(|| "<eof>".into());
(
"An unexpected character was found".to_string(),
format!("Expected {expected}, but found {found}"),
*span,
)
},
LexerErrorKind::NotADoubleChar { span, found } => (
format!("Tried to parse {found} as double char"),
format!(
" {found:?} is not a double char, this is an internal error"
),
*span,
),
LexerErrorKind::InvalidIntegerLiteral { span, found } => (
"Invalid integer literal".to_string(),
format!(" {found} is not an integer"),
*span,
),
LexerErrorKind::MalformedFuncAttribute { span, found } => (
"Malformed function attribute".to_string(),
format!(" {found} is not a valid attribute"),
*span,
),
LexerErrorKind::TooManyBits { span, max, got } => (
"Integer literal too large".to_string(),
format!(
"The maximum number of bits needed to represent a field is {max}, This integer type needs {got} bits"
),
*span,
),
LexerErrorKind::LogicalAnd { span } => (
"Noir has no logical-and (&&) operator since short-circuiting is much less efficient when compiling to circuits".to_string(),
"Try `&` instead, or use `if` only if you require short-circuiting".to_string(),
*span,
),
LexerErrorKind::UnterminatedBlockComment { span } => ("Unterminated block comment".to_string(), "Unterminated block comment".to_string(), *span),
LexerErrorKind::UnterminatedStringLiteral { span } =>
("Unterminated string literal".to_string(), "Unterminated string literal".to_string(), *span),
LexerErrorKind::InvalidEscape { escaped, span } =>
(format!("'\\{escaped}' is not a valid escape sequence. Use '\\' for a literal backslash character."), "Invalid escape sequence".to_string(), *span),
}
}
}
impl From<LexerErrorKind> for Diagnostic {
fn from(error: LexerErrorKind) -> Diagnostic {
let (primary, secondary, span) = error.parts();
Diagnostic::simple_error(primary, secondary, span)
}
}
impl From<LexerErrorKind> for chumsky::error::Simple<SpannedToken, Span> {
fn from(error: LexerErrorKind) -> Self {
let (_, message, span) = error.parts();
chumsky::error::Simple::custom(span, message)
}
}