Skip to content

Commit

Permalink
refactor: cleanup union parsing (#4462)
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen committed May 11, 2024
1 parent 418afcc commit 5189ddb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
1 change: 1 addition & 0 deletions prqlc/prqlc-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub fn lex_token() -> impl Parser<char, Token, Error = Cheap<char>> {
just("internal"),
just("func"),
just("import"),
just("enum"),
))
.then_ignore(end_expr())
.map(|x| x.to_string())
Expand Down
47 changes: 27 additions & 20 deletions prqlc/prqlc-parser/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,32 @@ pub fn type_expr() -> impl Parser<TokenKind, Ty, Error = PError> {
.map(TyKind::Tuple)
.labelled("tuple");

let union_parenthesized = ident_part()
.then_ignore(ctrl('='))
.or_not()
.then(nested_type_expr.clone())
.padded_by(new_line().repeated())
.separated_by(just(TokenKind::Or))
.allow_trailing()
.then_ignore(new_line().repeated())
.delimited_by(ctrl('('), ctrl(')'))
.recover_with(nested_delimiters(
TokenKind::Control('('),
TokenKind::Control(')'),
[
(TokenKind::Control('{'), TokenKind::Control('}')),
(TokenKind::Control('('), TokenKind::Control(')')),
(TokenKind::Control('['), TokenKind::Control(']')),
],
|_| vec![],
))
let enum_ = keyword("enum")
.ignore_then(
ident_part()
.then(ctrl('=').ignore_then(nested_type_expr.clone()).or_not())
.map(|(name, ty)| {
(
Some(name),
ty.unwrap_or_else(|| Ty::new(TyKind::Tuple(vec![]))),
)
})
.padded_by(new_line().repeated())
.separated_by(ctrl(','))
.allow_trailing()
.then_ignore(new_line().repeated())
.delimited_by(ctrl('{'), ctrl('}'))
.recover_with(nested_delimiters(
TokenKind::Control('{'),
TokenKind::Control('}'),
[
(TokenKind::Control('{'), TokenKind::Control('}')),
(TokenKind::Control('('), TokenKind::Control(')')),
(TokenKind::Control('['), TokenKind::Control(']')),
],
|_| vec![],
)),
)
.map(TyKind::Union)
.labelled("union");

Expand All @@ -120,7 +127,7 @@ pub fn type_expr() -> impl Parser<TokenKind, Ty, Error = PError> {
.map(TyKind::Array)
.labelled("array");

let term = choice((basic, ident, func, tuple, array, union_parenthesized))
let term = choice((basic, ident, func, tuple, array, enum_))
.map_with_span(into_ty)
.boxed();

Expand Down
15 changes: 7 additions & 8 deletions prqlc/prqlc/tests/integration/resolving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ fn resolve_types_01() {
#[test]
fn resolve_types_02() {
assert_snapshot!(resolve(r#"
type A = int || ()
type A = int || {}
"#).unwrap(), @r###"
type A = int
type A = int || {}
"###)
}

Expand All @@ -78,16 +78,15 @@ fn resolve_types_03() {
fn resolve_types_04() {
assert_snapshot!(resolve(
r#"
type Status = (
Paid = () ||
Unpaid = float ||
Canceled = {reason = text, cancelled_at = timestamp} ||
)
type Status = enum {
Paid = {},
Unpaid = float,
Canceled = {reason = text, cancelled_at = timestamp},
}
"#,
)
.unwrap(), @r###"
type Status = (
Paid = () ||
Unpaid = float ||
{reason = text, cancelled_at = timestamp} ||
)
Expand Down

0 comments on commit 5189ddb

Please sign in to comment.