dev: include: revert wrong error position fix; refactor
Errors in the main file are being reported a few lines too high, due to the setOffset in includedirectivep. It seems reverting this should have restored the original bug with wrong line number in certain include error messages, but I can't find that right now.
This commit is contained in:
parent
6521b8dfdb
commit
e69c72a6c7
@ -308,9 +308,10 @@ directivep = (do
|
|||||||
-- optionally with a file type prefix. Relative paths are relative to the current file.
|
-- optionally with a file type prefix. Relative paths are relative to the current file.
|
||||||
includedirectivep :: MonadIO m => ErroringJournalParser m ()
|
includedirectivep :: MonadIO m => ErroringJournalParser m ()
|
||||||
includedirectivep = do
|
includedirectivep = do
|
||||||
-- save the position
|
-- save the position at start of include directive, for error messages
|
||||||
off <- getOffset
|
eoff <- getOffset
|
||||||
pos <- getSourcePos
|
-- and the parent file's path, for error messages and debug output
|
||||||
|
parentf <- getSourcePos >>= sourcePosFilePath
|
||||||
|
|
||||||
-- parse the directive
|
-- parse the directive
|
||||||
string "include"
|
string "include"
|
||||||
@ -318,14 +319,13 @@ includedirectivep = do
|
|||||||
prefixedglob <- rstrip . T.unpack <$> takeWhileP Nothing (`notElem` [';','\n'])
|
prefixedglob <- rstrip . T.unpack <$> takeWhileP Nothing (`notElem` [';','\n'])
|
||||||
lift followingcommentp
|
lift followingcommentp
|
||||||
let (mprefix,glb) = splitReaderPrefix prefixedglob
|
let (mprefix,glb) = splitReaderPrefix prefixedglob
|
||||||
f <- sourcePosFilePath pos
|
when (null $ dbg6 (parentf <> " include: glob pattern") glb) $
|
||||||
when (null $ dbg6 (f <> " include: glob pattern") glb) $
|
customFailure $ parseErrorAt eoff $ "include needs a file path or glob pattern argument"
|
||||||
customFailure $ parseErrorAt off $ "include needs a file path or glob pattern argument"
|
|
||||||
|
|
||||||
-- Find the file or glob-matched files (just the ones from this include directive), with some IO error checking.
|
-- Find the file or glob-matched files (just the ones from this include directive), with some IO error checking.
|
||||||
-- Also report whether a glob pattern was used, and not just a literal file path.
|
-- Also report whether a glob pattern was used, and not just a literal file path.
|
||||||
-- (paths, isglob) <- findMatchedFiles off pos glb
|
-- (paths, isglob) <- findMatchedFiles off pos glb
|
||||||
paths <- findMatchedFiles off pos glb
|
paths <- findMatchedFiles eoff parentf glb
|
||||||
|
|
||||||
-- XXX worth the trouble ? no
|
-- XXX worth the trouble ? no
|
||||||
-- Comprehensively exclude files already processed. Some complexities here:
|
-- Comprehensively exclude files already processed. Some complexities here:
|
||||||
@ -343,9 +343,7 @@ includedirectivep = do
|
|||||||
Just fmt -> map ((show fmt++":")++) paths
|
Just fmt -> map ((show fmt++":")++) paths
|
||||||
|
|
||||||
-- Parse each one, as if inlined here.
|
-- Parse each one, as if inlined here.
|
||||||
-- Reset the position to the `include` line, for error messages.
|
forM_ prefixedpaths $ parseIncludedFile eoff
|
||||||
setOffset off
|
|
||||||
forM_ prefixedpaths $ parseIncludedFile off pos
|
|
||||||
|
|
||||||
where
|
where
|
||||||
|
|
||||||
@ -364,8 +362,8 @@ includedirectivep = do
|
|||||||
-- (detected with unsafePerformIO; it's not worth a ton of boilerplate).
|
-- (detected with unsafePerformIO; it's not worth a ton of boilerplate).
|
||||||
-- In that case, be aware ** recursive globs will search intermediate dot directories.
|
-- In that case, be aware ** recursive globs will search intermediate dot directories.
|
||||||
|
|
||||||
findMatchedFiles :: (MonadIO m) => Int -> SourcePos -> FilePath -> JournalParser m [FilePath]
|
findMatchedFiles :: (MonadIO m) => Int -> FilePath -> FilePath -> JournalParser m [FilePath]
|
||||||
findMatchedFiles off pos globpattern = do
|
findMatchedFiles off parentf globpattern = do
|
||||||
|
|
||||||
-- Some notes about the Glob library that we use (related: https://github.com/Deewiant/glob/issues/49):
|
-- Some notes about the Glob library that we use (related: https://github.com/Deewiant/glob/issues/49):
|
||||||
-- It does not expand tilde.
|
-- It does not expand tilde.
|
||||||
@ -385,8 +383,7 @@ includedirectivep = do
|
|||||||
expandedglob <- lift $ expandHomePath globpattern `orRethrowIOError` "failed to expand ~"
|
expandedglob <- lift $ expandHomePath globpattern `orRethrowIOError` "failed to expand ~"
|
||||||
|
|
||||||
-- get the directory of the including file
|
-- get the directory of the including file
|
||||||
parentfile <- sourcePosFilePath pos
|
let cwd = takeDirectory parentf
|
||||||
let cwd = takeDirectory parentfile
|
|
||||||
|
|
||||||
-- Don't allow 3 or more stars.
|
-- Don't allow 3 or more stars.
|
||||||
when ("***" `isInfixOf` expandedglob) $
|
when ("***" `isInfixOf` expandedglob) $
|
||||||
@ -436,23 +433,22 @@ includedirectivep = do
|
|||||||
-- If a glob was used, exclude the current file, for convenience.
|
-- If a glob was used, exclude the current file, for convenience.
|
||||||
let
|
let
|
||||||
files3 =
|
files3 =
|
||||||
dbg6 (parentfile <> " include: matched files" <> if isglob then " (excluding current file)" else "") $
|
dbg6 (parentf <> " include: matched files" <> if isglob then " (excluding current file)" else "") $
|
||||||
(if isglob then filter (/= parentfile) else id) files2
|
(if isglob then filter (/= parentf) else id) files2
|
||||||
|
|
||||||
return files3
|
return files3
|
||||||
|
|
||||||
-- Parse the given included file (and any deeper includes, recursively)
|
-- Parse the given included file (and any deeper includes, recursively) as if it was inlined in the current (parent) file.
|
||||||
-- as if it was inlined in the current (parent) file.
|
-- The offset of the start of the include directive in the parent file is provided for error messages.
|
||||||
-- The position in the parent file is provided for error messages.
|
parseIncludedFile :: MonadIO m => Int -> PrefixedFilePath -> ErroringJournalParser m ()
|
||||||
parseIncludedFile :: MonadIO m => Int -> SourcePos -> PrefixedFilePath -> ErroringJournalParser m ()
|
parseIncludedFile eoff prefixedpath = do
|
||||||
parseIncludedFile off _pos prefixedpath = do
|
|
||||||
let (_mprefix,filepath) = splitReaderPrefix prefixedpath
|
let (_mprefix,filepath) = splitReaderPrefix prefixedpath
|
||||||
|
|
||||||
-- Throw an error if a cycle is detected
|
-- Throw an error if a cycle is detected
|
||||||
parentj <- get
|
parentj <- get
|
||||||
let parentfilestack = jincludefilestack parentj
|
let parentfilestack = jincludefilestack parentj
|
||||||
when (dbg7 "parseIncludedFile: reading" filepath `elem` parentfilestack) $
|
when (dbg7 "parseIncludedFile: reading" filepath `elem` parentfilestack) $
|
||||||
customFailure $ parseErrorAt off $ "This included file forms a cycle: " ++ filepath
|
customFailure $ parseErrorAt eoff $ "This included file forms a cycle: " ++ filepath
|
||||||
|
|
||||||
-- Read the file's content, or throw an error
|
-- Read the file's content, or throw an error
|
||||||
childInput <- lift $ readFilePortably filepath `orRethrowIOError` "failed to read a file"
|
childInput <- lift $ readFilePortably filepath `orRethrowIOError` "failed to read a file"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user