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: allow trailing comma when parsing where clauses #5594

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
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
42 changes: 3 additions & 39 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
BinaryOp, BinaryOpKind, BlockExpression, ForLoopStatement, ForRange, Ident, IfExpression,
InfixExpression, LValue, Literal, ModuleDeclaration, NoirTypeAlias, Param, Path, Pattern,
Recoverable, Statement, TraitBound, TypeImpl, UnaryRhsMemberAccess, UnaryRhsMethodCall,
UnresolvedTraitConstraint, UseTree, UseTreeKind, Visibility,
UseTree, UseTreeKind, Visibility,
};
use crate::ast::{
Expression, ExpressionKind, LetStatement, StatementKind, UnresolvedType, UnresolvedTypeData,
Expand All @@ -48,7 +48,7 @@

use chumsky::prelude::*;
use iter_extended::vecmap;
use lalrpop_util::lalrpop_mod;

Check warning on line 51 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lalrpop)

Check warning on line 51 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lalrpop)
use noirc_errors::{Span, Spanned};

mod assertion;
Expand All @@ -62,8 +62,8 @@
mod traits;
mod types;

// synthesized by LALRPOP

Check warning on line 65 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (LALRPOP)
lalrpop_mod!(pub noir_parser);

Check warning on line 66 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lalrpop)

#[cfg(test)]
mod test_helpers;
Expand All @@ -71,6 +71,7 @@
use literals::literal;
use path::{maybe_empty_path, path};
use primitives::{dereference, ident, negation, not, nothing, right_shift_operator, token_kind};
use traits::where_clause;

/// Entry function for the parser - also handles lexing internally.
///
Expand All @@ -87,12 +88,12 @@

if cfg!(feature = "experimental_parser") {
for parsed_item in &parsed_module.items {
if lalrpop_parser_supports_kind(&parsed_item.kind) {

Check warning on line 91 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lalrpop)
match &parsed_item.kind {
ItemKind::Import(parsed_use_tree) => {
prototype_parse_use_tree(Some(parsed_use_tree), source_program);
}
// other kinds prevented by lalrpop_parser_supports_kind

Check warning on line 96 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lalrpop)
_ => unreachable!(),
}
}
Expand All @@ -109,7 +110,7 @@
}

let mut lexer = Lexer::new(input);
lexer = lexer.skip_whitespaces(false);

Check warning on line 113 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (whitespaces)
let mut errors = Vec::new();

// NOTE: this is a hack to get the references working
Expand Down Expand Up @@ -365,45 +366,8 @@
.labelled(ParsingRuleLabel::Parameter)
}

fn where_clause() -> impl NoirParser<Vec<UnresolvedTraitConstraint>> {
struct MultiTraitConstraint {
typ: UnresolvedType,
trait_bounds: Vec<TraitBound>,
}

let constraints = parse_type()
.then_ignore(just(Token::Colon))
.then(trait_bounds())
.map(|(typ, trait_bounds)| MultiTraitConstraint { typ, trait_bounds });

keyword(Keyword::Where)
.ignore_then(constraints.separated_by(just(Token::Comma)))
.or_not()
.map(|option| option.unwrap_or_default())
.map(|x: Vec<MultiTraitConstraint>| {
let mut result: Vec<UnresolvedTraitConstraint> = Vec::new();
for constraint in x {
for bound in constraint.trait_bounds {
result.push(UnresolvedTraitConstraint {
typ: constraint.typ.clone(),
trait_bound: bound,
});
}
}
result
})
}

fn trait_bounds() -> impl NoirParser<Vec<TraitBound>> {
trait_bound().separated_by(just(Token::Plus)).at_least(1).allow_trailing()
}

pub fn trait_bound() -> impl NoirParser<TraitBound> {
path().then(generic_type_args(parse_type())).map(|(trait_path, trait_generics)| TraitBound {
trait_path,
trait_generics,
trait_id: None,
})
traits::trait_bound()
}

fn block_expr<'a>(
Expand Down
7 changes: 4 additions & 3 deletions compiler/noirc_frontend/src/parser/parser/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn trait_implementation_body() -> impl NoirParser<Vec<TraitImplItem>> {
function.or(alias).repeated()
}

fn where_clause() -> impl NoirParser<Vec<UnresolvedTraitConstraint>> {
pub(super) fn where_clause() -> impl NoirParser<Vec<UnresolvedTraitConstraint>> {
struct MultiTraitConstraint {
typ: UnresolvedType,
trait_bounds: Vec<TraitBound>,
Expand All @@ -163,7 +163,7 @@ fn where_clause() -> impl NoirParser<Vec<UnresolvedTraitConstraint>> {
.map(|(typ, trait_bounds)| MultiTraitConstraint { typ, trait_bounds });

keyword(Keyword::Where)
.ignore_then(constraints.separated_by(just(Token::Comma)))
.ignore_then(constraints.separated_by(just(Token::Comma)).allow_trailing())
.or_not()
.map(|option| option.unwrap_or_default())
.map(|x: Vec<MultiTraitConstraint>| {
Expand All @@ -184,7 +184,7 @@ fn trait_bounds() -> impl NoirParser<Vec<TraitBound>> {
trait_bound().separated_by(just(Token::Plus)).at_least(1).allow_trailing()
}

fn trait_bound() -> impl NoirParser<TraitBound> {
pub(super) fn trait_bound() -> impl NoirParser<TraitBound> {
path().then(generic_type_args(parse_type())).map(|(trait_path, trait_generics)| TraitBound {
trait_path,
trait_generics,
Expand Down Expand Up @@ -215,6 +215,7 @@ mod test {
"trait GenericTrait<T> { fn elem(&mut self, index: Field) -> T; }",
"trait GenericTraitWithConstraints<T> where T: SomeTrait { fn elem(self, index: Field) -> T; }",
"trait TraitWithMultipleGenericParams<A, B, C> where A: SomeTrait, B: AnotherTrait<C> { let Size: Field; fn zero() -> Self; }",
"trait TraitWithMultipleGenericParams<A, B, C> where A: SomeTrait, B: AnotherTrait<C>, { let Size: Field; fn zero() -> Self; }",
],
);

Expand Down
Loading