on master help sign in
Merge pull request #2759 from jeremy-w/jeremy-w/improve-filename-error-messages

PParser.hs: filename: Report the specific failed check on error

commit9963bbee58765bdae91584f8b4aad4e5917216dd authorNiklas Larsson <metaniklas@gmail.com> dateFri 30 Oct 2015, 21:34 (11 years ago) parent10e0a06429db44
4 files changed+33−15
CHANGELOG+4 src/Pkg/PParser.hs+24−9 test/pkg003/run+1−1 test/pkg004/expected+4−5
CHANGELOG+4
@@ -45,6 +45,10 @@ Tool updates
4545* iPKG files have a new option `pkgs` which takes a comma-separated list
4646 of package names that the idris project depends on. This reduces bloat
4747 in the `opts` option with multiple package declarations.
48+* iPKG files now allow `executable = "your filename here"` in addition to
49+ the existing `executable = yourFilenameHere` style. While the unquoted
50+ version is limited to filenames that look like namespaced Idris identifiers
51+ (`your.filename.here`), the quoted version accepts any valid filename.
4852* Add definition command (\d in Vim, Ctrl-Alt-A in Atom, C-c C-s in Emacs) now
4953 adds missing clauses if there is already a definition.
5054
src/Pkg/PParser.hs+24−9
@@ -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 ()
test/pkg003/run+1−1
@@ -3,4 +3,4 @@
33### containing a valid filename.
44idris $@ --build test.ipkg
55rm -f *.ibc
6−rm -f 'quoting-allows-hyphens and spaces? and fun stuff!'
6+rm -f 'quoting-allows-hyphens and spaces and fun stuff!'
test/pkg004/expected+4−5
@@ -1,6 +1,5 @@
1−Uncaught error: user error (test.ipkg:10:1: error: a
2− filename must be non-empty and
3− have no directory
1+Uncaught error: user error (test.ipkg:10:1: error: filename
2+ must contain no directory
43 component, expected: space
5−<EOF>
6−^ )
4+<EOF>
5+^ )