Skip to content

Commit

Permalink
chore: transforming lexical errors into parser errors (#3163)
Browse files Browse the repository at this point in the history
Co-authored-by: jfecher <[email protected]>
  • Loading branch information
2 people authored and signorecello committed Oct 19, 2023
1 parent c660bfc commit 8cd69b0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
9 changes: 9 additions & 0 deletions compiler/noirc_frontend/src/lexer/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::parser::ParserError;
use crate::parser::ParserErrorReason;
use crate::token::SpannedToken;

use super::token::Token;
Expand Down Expand Up @@ -29,6 +31,13 @@ pub enum LexerErrorKind {
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 {
Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/parser/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::lexer::errors::LexerErrorKind;
use crate::lexer::token::Token;
use crate::Expression;
use small_ord_set::SmallOrdSet;
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParserError>) {
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)
}

Expand Down

0 comments on commit 8cd69b0

Please sign in to comment.