Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better errors for missing fn keyword #4154

Merged
merged 6 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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/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 {}",
"Expected a{} {} but found {}.",
michaeljklein marked this conversation as resolved.
Show resolved Hide resolved
if vowel { "n" } else { "" },
first,
self.found
Expand Down
32 changes: 12 additions & 20 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)
jfecher marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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,6 @@ 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()
.then(function_modifiers())
.then_ignore(keyword(Keyword::Fn))
.then(ident())
Expand Down Expand Up @@ -224,6 +223,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 +260,9 @@ 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()
.then_ignore(keyword(Struct))
.then(ident())
.then(generics())
Expand Down Expand Up @@ -448,7 +447,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 All @@ -463,20 +462,14 @@ fn trait_function_declaration() -> impl NoirParser<TraitItem> {
}

fn validate_attributes(
attributes: Option<Vec<Attribute>>,
attributes: Vec<Attribute>,
span: Span,
emit: &mut dyn FnMut(ParserError),
) -> Attributes {
if attributes.is_none() {
return Attributes::empty();
}

let attrs = attributes.unwrap();

let mut primary = None;
let mut secondary = Vec::new();

for attribute in attrs {
for attribute in attributes {
match attribute {
Attribute::Function(attr) => {
if primary.is_some() {
Expand All @@ -495,14 +488,13 @@ fn validate_attributes(
}

fn validate_struct_attributes(
attributes: Option<Vec<Attribute>>,
attributes: Vec<Attribute>,
span: Span,
emit: &mut dyn FnMut(ParserError),
) -> Vec<SecondaryAttribute> {
let attrs = attributes.unwrap_or_default();
let mut struct_attributes = vec![];

for attribute in attrs {
for attribute in attributes {
match attribute {
Attribute::Function(..) => {
emit(ParserError::with_reason(
Expand Down Expand Up @@ -976,7 +968,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 +1329,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
Loading