Skip to content

Commit

Permalink
Make import names can have a trailing comma and be multiline
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Dec 10, 2017
1 parent c5c3c0e commit bbe0b3e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/Nirum/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import Data.Map.Strict as Map hiding (foldl)
import Data.Set hiding (empty, foldl, fromList, map)
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import Text.Megaparsec hiding (ParseError, label, parse)
import Text.Megaparsec hiding (ParseError, parse)
import Text.Megaparsec.Char ( char
, eol
, noneOf
Expand Down Expand Up @@ -298,13 +298,13 @@ handleNameDuplication :: Declaration a
=> String -> [a]
-> (DeclarationSet a -> Parser b)
-> Parser b
handleNameDuplication label declarations cont =
handleNameDuplication label' declarations cont =
case DeclarationSet.fromList declarations of
Left (BehindNameDuplication (Name _ bname)) ->
fail ("the behind " ++ label ++ " name `" ++ toString bname ++
fail ("the behind " ++ label' ++ " name `" ++ toString bname ++
"` is duplicated")
Left (FacialNameDuplication (Name fname _)) ->
fail ("the facial " ++ label ++ " name `" ++ toString fname ++
fail ("the facial " ++ label' ++ " name `" ++ toString fname ++
"` is duplicated")
Right set -> cont set

Expand Down Expand Up @@ -346,32 +346,32 @@ enumTypeDeclaration = do
fieldsOrParameters :: forall a . (String, String)
-> (Name -> TypeExpression -> A.AnnotationSet -> a)
-> Parser [a]
fieldsOrParameters (label, pluralLabel) make = do
annotationSet' <- annotationSet <?> (label ++ " annotations")
fieldsOrParameters (label', pluralLabel) make = do
annotationSet' <- annotationSet <?> (label' ++ " annotations")
spaces
type' <- typeExpression <?> (label ++ " type")
type' <- typeExpression <?> (label' ++ " type")
spaces1
name' <- name <?> (label ++ " name")
name' <- name <?> (label' ++ " name")
spaces
let makeWithDocs = make name' type' . A.union annotationSet'
. annotationsFromDocs
followedByComma makeWithDocs <|> do
d <- optional docs' <?> (label ++ " docs")
d <- optional docs' <?> (label' ++ " docs")
return [makeWithDocs d]

where
recur :: Parser [a]
recur = fieldsOrParameters (label, pluralLabel) make
recur = fieldsOrParameters (label', pluralLabel) make
followedByComma :: (Maybe Docs -> a) -> Parser [a]
followedByComma makeWithDocs = do
char ','
spaces
d <- optional docs' <?> (label ++ " docs")
d <- optional docs' <?> (label' ++ " docs")
rest <- option [] recur <?> ("rest of " ++ pluralLabel)
return $ makeWithDocs d : rest
docs' :: Parser Docs
docs' = do
d <- docs <?> (label ++ " docs")
d <- docs <?> (label' ++ " docs")
spaces
return d
annotationsFromDocs :: Maybe Docs -> A.AnnotationSet
Expand Down Expand Up @@ -583,9 +583,11 @@ imports = do
spaces
char '('
spaces
idents <- sepBy1 importName
(spaces >> char ',' >> spaces)
<?> "names to import"
idents <- label "names to import" $ (`sepEndBy1` char ',') $ do
spaces
importName' <- importName
spaces
return importName'
spaces
char ')'
spaces
Expand Down
16 changes: 16 additions & 0 deletions test/Nirum/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,22 @@ service method-dups (
union fooAnnotationSet bazAnnotationSet
, Import ["foo", "bar"] "b" empty
]
specify "import names can have a trailing comma" $
parse' "import foo.bar (a, b,);" `shouldBeRight`
[ Import ["foo", "bar"] "a" empty
, Import ["foo", "bar"] "b" empty
]
specify "import names in parentheses can be multiline" $ do
-- without a trailing comma
parse' "import foo.bar (\n a,\n b\n);" `shouldBeRight`
[ Import ["foo", "bar"] "a" empty
, Import ["foo", "bar"] "b" empty
]
-- with a trailing comma
parse' "import foo.bar (\n c,\n d,\n);" `shouldBeRight`
[ Import ["foo", "bar"] "c" empty
, Import ["foo", "bar"] "d" empty
]
it "errors if parentheses have nothing" $
expectError "import foo.bar ();" 1 17

Expand Down

0 comments on commit bbe0b3e

Please sign in to comment.