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

chore: create parser specifically for function visibility #4425

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Changes from all 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
51 changes: 22 additions & 29 deletions compiler/noirc_frontend/src/parser/parser/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ pub(super) fn function_definition(allow_self: bool) -> impl NoirParser<NoirFunct
is_unconstrained: modifiers.0,
is_open: modifiers.2,
is_internal: modifiers.3,
visibility: if modifiers.1 {
FunctionVisibility::PublicCrate
} else if modifiers.4 {
FunctionVisibility::Public
} else {
FunctionVisibility::Private
},
visibility: modifiers.1,
generics,
parameters,
body,
Expand All @@ -57,36 +51,35 @@ pub(super) fn function_definition(allow_self: bool) -> impl NoirParser<NoirFunct
})
}

/// function_modifiers: 'unconstrained'? 'pub(crate)'? 'pub'? 'open'? 'internal'?
/// visibility_modifier: 'pub(crate)'? 'pub'? ''
fn visibility_modifier() -> impl NoirParser<FunctionVisibility> {
let is_pub_crate = (keyword(Keyword::Pub)
.then_ignore(just(Token::LeftParen))
.then_ignore(keyword(Keyword::Crate))
.then_ignore(just(Token::RightParen)))
.map(|_| FunctionVisibility::PublicCrate);

let is_pub = keyword(Keyword::Pub).map(|_| FunctionVisibility::Public);

let is_private = empty().map(|_| FunctionVisibility::Private);

choice((is_pub_crate, is_pub, is_private))
}

/// function_modifiers: 'unconstrained'? (visibility)? 'open'? 'internal'?
///
/// returns (is_unconstrained, is_pub_crate, is_open, is_internal, is_pub) for whether each keyword was present
fn function_modifiers() -> impl NoirParser<(bool, bool, bool, bool, bool)> {
/// returns (is_unconstrained, visibility, is_open, is_internal) for whether each keyword was present
fn function_modifiers() -> impl NoirParser<(bool, FunctionVisibility, bool, bool)> {
keyword(Keyword::Unconstrained)
.or_not()
.then(is_pub_crate())
.then(keyword(Keyword::Pub).or_not())
.then(visibility_modifier())
.then(keyword(Keyword::Open).or_not())
.then(keyword(Keyword::Internal).or_not())
.map(|((((unconstrained, pub_crate), public), open), internal)| {
(
unconstrained.is_some(),
pub_crate,
open.is_some(),
internal.is_some(),
public.is_some(),
)
.map(|(((unconstrained, visibility), open), internal)| {
(unconstrained.is_some(), visibility, open.is_some(), internal.is_some())
})
}

fn is_pub_crate() -> impl NoirParser<bool> {
(keyword(Keyword::Pub)
.then_ignore(just(Token::LeftParen))
.then_ignore(keyword(Keyword::Crate))
.then_ignore(just(Token::RightParen)))
.or_not()
.map(|a| a.is_some())
}

/// non_empty_ident_list: ident ',' non_empty_ident_list
/// | ident
///
Expand Down
Loading