Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/lexer/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl LexerErrorKind {

(
"An unexpected character was found".to_string(),
format!("Expected {expected}, but found {found}"),
format!("Lexer: expected {expected}, but found {found}"),
*span,
)
},
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl std::fmt::Display for ParserError {
let vowel = "aeiou".contains(first.chars().next().unwrap());
write!(
f,
"Expected a{} {} but found {}",
"Parser: expected a{} {} but found {}",
if vowel { "n" } else { "" },
first,
self.found
Expand Down
19 changes: 10 additions & 9 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ pub fn parse_program(source_program: &str) -> (ParsedModule, Vec<ParserError>) {

parsing_errors.extend(lexing_errors.into_iter().map(Into::into));

(module.unwrap(), parsing_errors)
(module.unwrap_or(ParsedModule { items: vec![] }), parsing_errors)
}

/// program: module EOF
fn program() -> impl NoirParser<ParsedModule> {
module().then_ignore(force(just(Token::EOF)))
module().then_ignore(just(Token::EOF))
}

/// module: top_level_statement module
/// | %empty
fn module() -> impl NoirParser<ParsedModule> {
recursive(|module_parser| {
empty()
.map(|_| ParsedModule::default())
.to(ParsedModule::default())
.then(spanned(top_level_statement(module_parser)).repeated())
.foldl(|mut program, (statement, span)| {
let mut push_item = |kind| program.items.push(Item { kind, span });
Expand Down Expand Up @@ -164,7 +164,7 @@ fn contract(module_parser: impl NoirParser<ParsedModule>) -> impl NoirParser<Top
/// function_modifiers 'fn' ident generics '(' function_parameters ')' function_return_type block
fn function_definition(allow_self: bool) -> impl NoirParser<NoirFunction> {
attributes()
.or_not()
.map(Option::Some)
.then(function_modifiers())
.then_ignore(keyword(Keyword::Fn))
.then(ident())
Expand Down Expand Up @@ -224,6 +224,7 @@ fn function_modifiers() -> impl NoirParser<(bool, bool, bool, bool, bool)> {
)
})
}

fn is_pub_crate() -> impl NoirParser<bool> {
(keyword(Keyword::Pub)
.then_ignore(just(Token::LeftParen))
Expand Down Expand Up @@ -260,10 +261,10 @@ fn struct_definition() -> impl NoirParser<TopLevelStatement> {
[(LeftParen, RightParen), (LeftBracket, RightBracket)],
|_| vec![],
))
.or(just(Semicolon).map(|_| Vec::new()));
.or(just(Semicolon).to(Vec::new()));

attributes()
.or_not()
.map(Option::Some)
.then_ignore(keyword(Struct))
.then(ident())
.then(generics())
Expand Down Expand Up @@ -448,7 +449,7 @@ fn trait_constant_declaration() -> impl NoirParser<TraitItem> {
/// trait_function_declaration: 'fn' ident generics '(' declaration_parameters ')' function_return_type
fn trait_function_declaration() -> impl NoirParser<TraitItem> {
let trait_function_body_or_semicolon =
block(fresh_statement()).map(Option::from).or(just(Token::Semicolon).map(|_| Option::None));
block(fresh_statement()).map(Option::from).or(just(Token::Semicolon).to(Option::None));

keyword(Keyword::Fn)
.ignore_then(ident())
Expand Down Expand Up @@ -976,7 +977,7 @@ fn assign_operator() -> impl NoirParser<Token> {
// Since >> is lexed as two separate "greater-than"s, >>= is lexed as > >=, so
// we need to account for that case here as well.
let right_shift_fix =
just(Token::Greater).then(just(Token::GreaterEqual)).map(|_| Token::ShiftRight);
just(Token::Greater).then(just(Token::GreaterEqual)).to(Token::ShiftRight);

let shorthand_syntax = shorthand_syntax.or(right_shift_fix);
just(Token::Assign).or(shorthand_syntax)
Expand Down Expand Up @@ -1337,7 +1338,7 @@ fn create_infix_expression(lhs: Expression, (operator, rhs): (BinaryOp, Expressi
// to parse nested generic types. For normal expressions however, it means we have to manually
// parse two greater-than tokens as a single right-shift here.
fn right_shift_operator() -> impl NoirParser<Token> {
just(Token::Greater).then(just(Token::Greater)).map(|_| Token::ShiftRight)
just(Token::Greater).then(just(Token::Greater)).to(Token::ShiftRight)
}

fn operator_with_precedence(precedence: Precedence) -> impl NoirParser<Spanned<BinaryOpKind>> {
Expand Down