Skip to content

Commit

Permalink
chore: fix lexer errors for unexpected characters (#1955)
Browse files Browse the repository at this point in the history
fix lexer errors for unexpected characters and attribute parsing
  • Loading branch information
kek kek kek authored Jul 18, 2023
1 parent 211e251 commit f62ace9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
16 changes: 10 additions & 6 deletions crates/noirc_frontend/src/lexer/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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: char },
UnexpectedCharacter { span: Span, expected: String, found: Option<char> },
#[error("NotADoubleChar : {:?} is not a double char token", found)]
NotADoubleChar { span: Span, found: Token },
#[error("InvalidIntegerLiteral : {:?} is not a integer", found)]
Expand Down Expand Up @@ -39,11 +39,15 @@ impl LexerErrorKind {
span,
expected,
found,
} => (
"an unexpected character was found".to_string(),
format!(" expected {expected} , but got {found}"),
*span,
),
} => {
let found: String = found.map(Into::into).unwrap_or_else(|| "<eof>".into());

(
"an unexpected character was found".to_string(),
format!(" expected {expected} , but got {}", found),
*span,
)
},
LexerErrorKind::NotADoubleChar { span, found } => (
format!("tried to parse {found} as double char"),
format!(
Expand Down
15 changes: 12 additions & 3 deletions crates/noirc_frontend/src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl<'a> Lexer<'a> {
'0'..='9' => self.eat_digit(initial_char),
_ => Err(LexerErrorKind::UnexpectedCharacter {
span: Span::single_char(self.position),
found: initial_char,
found: initial_char.into(),
expected: "an alpha numeric character".to_owned(),
}),
}
Expand All @@ -254,7 +254,7 @@ impl<'a> Lexer<'a> {
if !self.peek_char_is('[') {
return Err(LexerErrorKind::UnexpectedCharacter {
span: Span::single_char(self.position),
found: self.next_char().unwrap(),
found: self.next_char(),
expected: "[".to_owned(),
});
}
Expand All @@ -269,7 +269,7 @@ impl<'a> Lexer<'a> {
return Err(LexerErrorKind::UnexpectedCharacter {
span: Span::single_char(self.position),
expected: "]".to_owned(),
found: self.next_char().unwrap(),
found: self.next_char(),
});
}
self.next_char();
Expand Down Expand Up @@ -427,6 +427,15 @@ fn test_single_double_char() {
}
}

#[test]
fn invalid_attribute() {
let input = "#";
let mut lexer = Lexer::new(input);

let token = lexer.next().unwrap();
assert!(token.is_err());
}

#[test]
fn test_custom_gate_syntax() {
let input = "#[foreign(sha256)]#[foreign(blake2s)]#[builtin(sum)]";
Expand Down

0 comments on commit f62ace9

Please sign in to comment.