Skip to content

Commit

Permalink
Allow trailing comma for function parameters. (FuelLabs#793)
Browse files Browse the repository at this point in the history
* Allow trailing comma for function parameters.

* Add tests.
  • Loading branch information
adlerjohn authored Feb 14, 2022
1 parent 96b5321 commit 7f0c825
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions sway-core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,34 @@ mod test {
);
parsed.unwrap();
}

#[test]
fn test_trailing_fn_param_comma_pass() {
let parsed = SwayParser::parse(
Rule::fn_decl,
r#"fn myfunc(x: u64, y: u64,) -> u64 {
69
}"#
.into(),
);
parsed.unwrap();
}

#[test]
fn test_trailing_fn_param_comma_fail() {
let parsed = SwayParser::parse(
Rule::fn_decl,
r#"fn myfunc(x: u64, y: u64,,) -> u64 {
69
}"#
.into(),
);
// this parse should fail since double comma
match parsed {
Err(_) => (),
Ok(o) => {
panic!("{:?}", o)
}
}
}
}
2 changes: 1 addition & 1 deletion sway-core/src/sway.pest
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ enum_field_name = {ident}
impl_self = {impl_keyword ~ type_params? ~ type_name ~ trait_bounds? ~ ("{" ~ fn_decl* ~ "}")}

// // fn declaration
fn_decl_params = {"(" ~ (fn_decl_param ~ ("," ~ fn_decl_param)*)? ~ ")"}
fn_decl_params = {"(" ~ (fn_decl_param ~ ("," ~ fn_decl_param)*)? ~ ","? ~ ")"}
type_params = {"<" ~ generic_type_param ~ (", " ~ generic_type_param)* ~ ">"}
fn_decl_param = {("self")|(fn_decl_param_name ~ ":" ~ type_name)}
fn_decl_param_name = {ident}
Expand Down

0 comments on commit 7f0c825

Please sign in to comment.