@@ -15,6 +15,7 @@ import Idris.CmdOptions
1515import Control.Monad.State.Strict
1616import Control.Applicative
1717import System.FilePath (takeFileName, isValid)
18+import Data.Maybe (isNothing, fromJust)
1819
1920import Util.System
2021
@@ -79,18 +80,32 @@ filename = (do
7980 -- Through at least version 0.9.19.1, IPKG executable values were
8081 -- possibly namespaced identifiers, like foo.bar.baz.
8182 show <$> fst <$> iName []
82− if isValidFilename filename
83− then return filename
84− else fail "a filename must be non-empty and have no directory component")
83+ case filenameErrorMessage filename of
84+ Just errorMessage -> fail errorMessage
85+ Nothing -> return filename)
8586 <?> "filename"
8687 where
87− isValidFilename :: FilePath -> Bool
88− isValidFilename path =
89− and [isNonEmpty, isValidPath, hasNoDirectoryComponent]
88+ -- TODO: Report failing span better! We could lookAhead,
89+ -- or do something with DeltaParsing?
90+ filenameErrorMessage :: FilePath -> Maybe String
91+ filenameErrorMessage path = either Just (const Nothing) $ do
92+ checkEmpty path
93+ checkValid path
94+ checkNoDirectoryComponent path
9095 where
91− isNonEmpty = path /= ""
92− isValidPath = System.FilePath.isValid path
93− hasNoDirectoryComponent = path == takeFileName path
96+ checkThat ok message =
97+ if ok then Right () else Left message
98+
99+ checkEmpty path =
100+ checkThat (path /= "") "filename must not be empty"
101+
102+ checkValid path =
103+ checkThat (System.FilePath.isValid path)
104+ "filename must contain only valid characters"
105+
106+ checkNoDirectoryComponent path =
107+ checkThat (path == takeFileName path)
108+ "filename must contain no directory component"
94109
95110
96111pClause :: PParser ()