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

Increase flexibility of union literals #129

Merged
merged 1 commit into from Sep 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 36 additions & 23 deletions src/Dhall/Parser.hs
Expand Up @@ -837,51 +837,64 @@ unionTypeOrLiteral :: Show a => Parser a -> Parser (Expr Src a)
unionTypeOrLiteral embedded = do
symbol "<"

let emptyUnionTypeOrLiteral = do
let emptyUnionType = do
symbol ">"
return (Union Data.Map.empty)

let nonEmptyUnionTypeOrLiteral = do
let withLabel = do
a <- label

let unionType = do
let withColon = do
symbol ":"
b <- exprA embedded

let unionTypeWithoutAlternatives = do
let withClose = do
symbol ">"
return (Union (Data.Map.singleton a b))
return (Union, [(a, b)])

let unionTypeWithAlternatives = do
let withBar = do
symbol "|"
c <- alternativeTypes embedded
symbol ">"
d <- toMap ((a, b):c)
return (Union d)

unionTypeWithoutAlternatives <|> unionTypeWithAlternatives
let continue = do
(c, d) <- withLabel
return (c, (a, b):d)

withClose <|> continue

withBar <|> withClose

let unionLiteral = do
symbol "="
b <- exprA embedded
let unionLitWithoutAlternatives = do

let emptyUnionLiteral = do
symbol ">"
return (UnionLit a b Data.Map.empty)
return (UnionLit a b, [])

let unionLitWithAlternatives = do
let nonEmptyUnionLiteral = do
symbol "|"
c <- alternativeTypes embedded
d <- toMap c
symbol ">"
return (UnionLit a b d)
unionLitWithoutAlternatives <|> unionLitWithAlternatives

unionType <|> unionLiteral
let stop = do
symbol ">"
return (UnionLit a b, [])

let continue = do
c <- Text.Parser.Combinators.sepEndBy (alternativeType embedded) (symbol "|")
symbol ">"
return (UnionLit a b, c)

stop <|> continue

emptyUnionTypeOrLiteral <|> nonEmptyUnionTypeOrLiteral
emptyUnionLiteral <|> nonEmptyUnionLiteral

withColon <|> unionLiteral

let nonEmptyUnionTypeOrLiteral = do
(a, b) <- withLabel
c <- toMap b
return (a c)

alternativeTypes :: Show a => Parser a -> Parser [(Text, Expr Src a)]
alternativeTypes embedded = sepBy (alternativeType embedded) (symbol "|")
emptyUnionType <|> nonEmptyUnionTypeOrLiteral

alternativeType :: Show a => Parser a -> Parser (Text, Expr Src a)
alternativeType embedded = do
Expand Down