Skip to content

Commit

Permalink
Union tag docs now can be in parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jul 22, 2017
1 parent c59b7ff commit a23e5c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
10 changes: 7 additions & 3 deletions examples/shapes.nrm
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ record point (
# Record type definition.

offset left/x,
# for backward compatibility, you can specify *behind name*.
# For backward compatibility, you can specify *behind name*.

offset top,
# trailing comma is okay
# Trailing comma is okay.
);

union shape
# Type constructors in a sum type become translated to subtypes in OO
# languages, and datatypes in functional languages.
= rectangle (point upper-left, point lower-right)
= rectangle (
# Each tag can have zero or more fields like record types.
point upper-left,
point lower-right
)
| circle (point origin, offset radius)
;

Expand Down
15 changes: 11 additions & 4 deletions src/Nirum/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ tag = do
tagName <- name <?> "union tag name"
spaces
paren <- optional $ char '('
spaces
frontDocs <- optional $ do
d <- docs <?> "union tag docs"
spaces
return d
fields' <- case paren of
Just _ -> do
spaces
Expand All @@ -428,10 +433,12 @@ tag = do
return f
Nothing -> return empty
spaces
docs' <- optional $ do
d <- docs <?> "union tag docs"
spaces
return d
docs' <- case frontDocs of
d@(Just _) -> return d
Nothing -> optional $ do
d <- docs <?> "union tag docs"
spaces
return d
annotationSet'' <- annotationsWithDocs annotationSet' docs'
return $ Tag tagName fields' annotationSet''

Expand Down
11 changes: 9 additions & 2 deletions test/Nirum/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,21 @@ union shape
union shape
= circle (point origin, offset radius,)
# tag docs
| rectangle (point upper-left, point lower-right,)
| rectangle (
# front docs
point upper-left, point lower-right,
)
| none
;|] `shouldBeRight`
a { type' = union'
{ tags = [ circleTag
{ tagAnnotations = singleDocs "tag docs"
}
, rectTag, noneTag
, rectTag
{ tagAnnotations =
singleDocs "front docs"
}
, noneTag
]
}
}
Expand Down

0 comments on commit a23e5c2

Please sign in to comment.