diff --git a/compiler/noirc_frontend/src/lexer/errors.rs b/compiler/noirc_frontend/src/lexer/errors.rs index 8b5aac8c787..9bc9a820239 100644 --- a/compiler/noirc_frontend/src/lexer/errors.rs +++ b/compiler/noirc_frontend/src/lexer/errors.rs @@ -1,3 +1,5 @@ +use crate::parser::ParserError; +use crate::parser::ParserErrorReason; use crate::token::SpannedToken; use super::token::Token; @@ -29,6 +31,13 @@ pub enum LexerErrorKind { InvalidEscape { escaped: char, span: Span }, } +impl From 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 { diff --git a/compiler/noirc_frontend/src/parser/errors.rs b/compiler/noirc_frontend/src/parser/errors.rs index fea0e906895..0c0d4e7645c 100644 --- a/compiler/noirc_frontend/src/parser/errors.rs +++ b/compiler/noirc_frontend/src/parser/errors.rs @@ -1,3 +1,4 @@ +use crate::lexer::errors::LexerErrorKind; use crate::lexer::token::Token; use crate::Expression; use small_ord_set::SmallOrdSet; @@ -39,6 +40,8 @@ pub enum ParserErrorReason { NoFunctionAttributesAllowedOnStruct, #[error("Assert statements can only accept string literals")] AssertMessageNotString, + #[error("{0}")] + Lexer(LexerErrorKind), } /// Represents a parsing error, or a parsing error in the making. diff --git a/compiler/noirc_frontend/src/parser/parser.rs b/compiler/noirc_frontend/src/parser/parser.rs index 6b43bd003f3..89cc792c4f0 100644 --- a/compiler/noirc_frontend/src/parser/parser.rs +++ b/compiler/noirc_frontend/src/parser/parser.rs @@ -55,8 +55,11 @@ use noirc_errors::{Span, Spanned}; /// Vec is non-empty, there may be Error nodes in the Ast to fill in the gaps that /// failed to parse. Otherwise the Ast is guaranteed to have 0 Error nodes. pub fn parse_program(source_program: &str) -> (ParsedModule, Vec) { - let (tokens, _lexing_errors) = Lexer::lex(source_program); - let (module, parsing_errors) = program().parse_recovery_verbose(tokens); + let (tokens, lexing_errors) = Lexer::lex(source_program); + let (module, mut parsing_errors) = program().parse_recovery_verbose(tokens); + + parsing_errors.extend(lexing_errors.into_iter().map(Into::into)); + (module.unwrap(), parsing_errors) }