Skip to content

Commit 214e914

Browse files
junlarsenMish Jude
authored andcommitted
Parse trailing commas in Cedar policy files (cedar-policy#1606)
Signed-off-by: Mats Jun Larsen <[email protected]> Signed-off-by: Mish Jude <[email protected]>
1 parent 3641867 commit 214e914

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

cedar-policy-core/src/parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,20 +1190,20 @@ mod tests {
11901190
"permit(principal,",
11911191
"unexpected end of input",
11921192
"",
1193-
"expected identifier",
1193+
"expected `)` or identifier",
11941194
);
11951195
assert_labeled_span(
11961196
"permit(principal,action,",
11971197
"unexpected end of input",
11981198
"",
1199-
"expected identifier",
1199+
"expected `)` or identifier",
12001200
);
12011201
// Nothing will actually convert to an AST here.
12021202
assert_labeled_span(
12031203
"permit(principal,action,resource,",
12041204
"unexpected end of input",
12051205
"",
1206-
"expected identifier",
1206+
"expected `)` or identifier",
12071207
);
12081208
// We still list out `if` as an expected token because it doesn't get
12091209
// parsed as an ident in this position.

cedar-policy-core/src/parser/grammar.lalrpop

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ match {
8585
}
8686

8787
Comma<E>: Vec<E> = {
88-
<e:E?> => e.into_iter().collect(),
89-
<mut es:(<E> ",")+> <e:E> => {
90-
es.push(e);
91-
es
92-
},
88+
<mut es:(<E> ",")*> <e:E?> => match e {
89+
None => es,
90+
Some(e) => {
91+
es.push(e);
92+
es
93+
}
94+
}
9395
}
9496

9597
// Policies := {Policy}

cedar-policy-core/src/parser/text_to_cst.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ mod tests {
12551255
src,
12561256
&errs,
12571257
&ExpectedErrorMessageBuilder::error("unexpected token `!`")
1258-
.exactly_one_underline_with_label("!", "expected identifier")
1258+
.exactly_one_underline_with_label("!", "expected `)` or identifier")
12591259
.build(),
12601260
);
12611261

@@ -1269,14 +1269,14 @@ mod tests {
12691269
src,
12701270
&errs,
12711271
&ExpectedErrorMessageBuilder::error("unexpected token `!`")
1272-
.exactly_one_underline_with_label("!", "expected identifier")
1272+
.exactly_one_underline_with_label("!", "expected `)` or identifier")
12731273
.build(),
12741274
);
12751275
expect_some_error_matches(
12761276
src,
12771277
&errs,
12781278
&ExpectedErrorMessageBuilder::error("unexpected token `+`")
1279-
.exactly_one_underline_with_label("+", "expected identifier")
1279+
.exactly_one_underline_with_label("+", "expected `)` or identifier")
12801280
.build(),
12811281
);
12821282
expect_n_errors(src, &errs, 2);
@@ -1290,7 +1290,7 @@ mod tests {
12901290
src,
12911291
&errs,
12921292
&ExpectedErrorMessageBuilder::error("unexpected token `!`")
1293-
.exactly_one_underline_with_label("!", "expected identifier")
1293+
.exactly_one_underline_with_label("!", "expected `)` or identifier")
12941294
.build(),
12951295
);
12961296
}
@@ -1721,6 +1721,20 @@ mod tests {
17211721
}
17221722
}
17231723

1724+
#[test]
1725+
fn trailing_comma() {
1726+
assert_parse_succeeds(
1727+
parse_policy,
1728+
r#"
1729+
permit(principal, action, resource,);
1730+
"#,
1731+
);
1732+
assert_parse_succeeds(parse_expr, r#"foo(a, b, c,)"#);
1733+
assert_parse_succeeds(parse_expr, r#"[A, B, C,]"#);
1734+
assert_parse_succeeds(parse_expr, r#"{ A: B, C: D, }"#);
1735+
assert_parse_succeeds(parse_ref, r#"Principal::{uid: "123", role: "admin",}"#);
1736+
}
1737+
17241738
#[test]
17251739
#[cfg(feature = "tolerant-ast")]
17261740
fn policies_tolerant_success() {

cedar-policy/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Cedar Language Version: TBD
2828
`PolicySet`, `Policy`, and `Template`. These APIs don't retain source code information, trading detailed error
2929
reporting for reduced memory usage and faster parsing. The feature is only intended for use with raw parsing
3030
APIs, as regular parsing performance is degraded when the `raw-parsing` feature is enabled.
31+
- Implemented [RFC 71 (trailing commas)](https://github.com/cedar-policy/rfcs/blob/main/text/0071-trailing-commas.md)
32+
for Cedar policy files. (#1606)
3133

3234
### Changed
3335

0 commit comments

Comments
 (0)