Skip to content

Commit

Permalink
chore: move attributes to a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Feb 24, 2024
1 parent 2b5df09 commit 7774b50
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
40 changes: 2 additions & 38 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::ast::{
};
use crate::lexer::Lexer;
use crate::parser::{force, ignore_then_commit, statement_recovery};
use crate::token::{Attribute, Attributes, Keyword, Token, TokenKind};
use crate::token::{Keyword, Token, TokenKind};
use crate::{
BinaryOp, BinaryOpKind, BlockExpression, Distinctness, ForLoopStatement, ForRange,
FunctionReturnType, Ident, IfExpression, InfixExpression, LValue, Lambda, Literal,
Expand All @@ -50,6 +50,7 @@ use iter_extended::vecmap;
use noirc_errors::{Span, Spanned};

mod assertion;
mod attributes;
mod function;
mod literals;
mod path;
Expand Down Expand Up @@ -225,17 +226,6 @@ fn function_return_type() -> impl NoirParser<((Distinctness, Visibility), Functi
})
}

fn attribute() -> impl NoirParser<Attribute> {
token_kind(TokenKind::Attribute).map(|token| match token {
Token::Attribute(attribute) => attribute,
_ => unreachable!("Parser should have already errored due to token not being an attribute"),
})
}

fn attributes() -> impl NoirParser<Vec<Attribute>> {
attribute().repeated()
}

fn lambda_parameters() -> impl NoirParser<Vec<(Pattern, UnresolvedType)>> {
let typ = parse_type().recover_via(parameter_recovery());
let typ = just(Token::Colon).ignore_then(typ);
Expand Down Expand Up @@ -283,32 +273,6 @@ fn self_parameter() -> impl NoirParser<Param> {
})
}

fn validate_attributes(
attributes: Vec<Attribute>,
span: Span,
emit: &mut dyn FnMut(ParserError),
) -> Attributes {
let mut primary = None;
let mut secondary = Vec::new();

for attribute in attributes {
match attribute {
Attribute::Function(attr) => {
if primary.is_some() {
emit(ParserError::with_reason(
ParserErrorReason::MultipleFunctionAttributesFound,
span,
));
}
primary = Some(attr);
}
Attribute::Secondary(attr) => secondary.push(attr),
}
}

Attributes { function: primary, secondary }
}

/// Function declaration parameters differ from other parameters in that parameter
/// patterns are not allowed in declarations. All parameters must be identifiers.
fn function_declaration_parameters() -> impl NoirParser<Vec<(Ident, UnresolvedType)>> {
Expand Down
46 changes: 46 additions & 0 deletions compiler/noirc_frontend/src/parser/parser/attributes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use chumsky::Parser;
use noirc_errors::Span;

use crate::{
parser::{NoirParser, ParserError, ParserErrorReason},
token::{Attribute, Attributes, Token, TokenKind},
};

use super::primitives::token_kind;

fn attribute() -> impl NoirParser<Attribute> {
token_kind(TokenKind::Attribute).map(|token| match token {
Token::Attribute(attribute) => attribute,
_ => unreachable!("Parser should have already errored due to token not being an attribute"),
})
}

pub(super) fn attributes() -> impl NoirParser<Vec<Attribute>> {
attribute().repeated()
}

pub(super) fn validate_attributes(
attributes: Vec<Attribute>,
span: Span,
emit: &mut dyn FnMut(ParserError),
) -> Attributes {
let mut primary = None;
let mut secondary = Vec::new();

for attribute in attributes {
match attribute {
Attribute::Function(attr) => {
if primary.is_some() {
emit(ParserError::with_reason(
ParserErrorReason::MultipleFunctionAttributesFound,
span,
));
}
primary = Some(attr);
}
Attribute::Secondary(attr) => secondary.push(attr),
}
}

Attributes { function: primary, secondary }
}
7 changes: 4 additions & 3 deletions compiler/noirc_frontend/src/parser/parser/function.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::{
attributes, block, fresh_statement, ident, keyword, nothing, optional_distinctness,
optional_visibility, parameter_name_recovery, parameter_recovery, parenthesized, parse_type,
pattern, self_parameter, validate_attributes, where_clause, NoirParser,
attributes::{attributes, validate_attributes},
block, fresh_statement, ident, keyword, nothing, optional_distinctness, optional_visibility,
parameter_name_recovery, parameter_recovery, parenthesized, parse_type, pattern,
self_parameter, where_clause, NoirParser,
};
use crate::parser::labels::ParsingRuleLabel;
use crate::parser::spanned;
Expand Down
5 changes: 2 additions & 3 deletions compiler/noirc_frontend/src/parser/parser/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::{
macros_api::SecondaryAttribute,
parser::{
parser::{
attributes, function,
attributes::attributes,
function, parse_type,
primitives::{ident, keyword},
},
NoirParser, ParserError, ParserErrorReason, TopLevelStatement,
Expand All @@ -14,8 +15,6 @@ use crate::{
Ident, NoirStruct, UnresolvedType,
};

use super::parse_type;

pub(super) fn struct_definition() -> impl NoirParser<TopLevelStatement> {
use self::Keyword::Struct;
use Token::*;
Expand Down

0 comments on commit 7774b50

Please sign in to comment.