on master help sign in
PParser.hs: filename: Verify it is a non-empty filename

Using |guard| after the parser has done its work leads to unfortunate error messages on parse failure, though: *Pkg.PParser> execout <$> runparser pPkg defaultPkg "foo.ipkg" "package bar\n\nexecutable = \"invalid/filename\"" Failure foo.ipkg:3:32: error: expected: space executable = "invalid/filename"<EOF> ^

commit3ccdc4b7c73ddacc53eca82ca816282dbc57ebb4 authorJeremy W. Sherman <jeremyw.sherman@gmail.com> dateMon 12 Oct 2015, 23:11 (11 years ago) parent44b87ec
1 file changed+20−10
src/Pkg/PParser.hs+20−10
src/Pkg/PParser.hs+20−10
@@ -14,6 +14,7 @@ import Idris.CmdOptions
1414
1515import Control.Monad.State.Strict
1616import Control.Applicative
17+import System.FilePath (takeFileName)
1718
1819import Util.System
1920
@@ -67,17 +68,26 @@ pPkg = do
6768-- |
6869-- | Treated for now as an identifier or a double-quoted string.
6970filename :: (MonadicParsing m, HasLastTokenSpan m) => m String
70−filename = token $
71− -- Treat a double-quoted string as a filename to support spaces.
72− -- This also moves away from tying filenames to identifiers, so
73− -- it will also accept hyphens
74− -- (https://github.com/idris-lang/Idris-dev/issues/2721)
75− stringLiteral
76− <|>
77− -- Through at least version 0.9.19.1, IPKG executable values were
78− -- possibly namespaced identifiers, like foo.bar.baz.
79− show <$> fst <$> iName []
71+filename = (do
72+ filename <- token $
73+ -- Treat a double-quoted string as a filename to support spaces.
74+ -- This also moves away from tying filenames to identifiers, so
75+ -- it will also accept hyphens
76+ -- (https://github.com/idris-lang/Idris-dev/issues/2721)
77+ stringLiteral
78+ <|>
79+ -- Through at least version 0.9.19.1, IPKG executable values were
80+ -- possibly namespaced identifiers, like foo.bar.baz.
81+ show <$> fst <$> iName []
82+ guard $ isValidFilename filename
83+ return filename)
8084 <?> "filename"
85+ where
86+ isValidFilename :: FilePath -> Bool
87+ isValidFilename path = isNonEmpty && hasNoDirectoryComponent
88+ where
89+ isNonEmpty = path /= ""
90+ hasNoDirectoryComponent = path == takeFileName path
8191
8292
8393pClause :: PParser ()