@@ -308,24 +308,24 @@ directivep = (do
308
308
-- optionally with a file type prefix. Relative paths are relative to the current file.
309
309
includedirectivep :: MonadIO m => ErroringJournalParser m ()
310
310
includedirectivep = do
311
- -- save the position
312
- off <- getOffset
313
- pos <- getSourcePos
311
+ -- save the position at start of include directive, for error messages
312
+ eoff <- getOffset
313
+ -- and the parent file's path, for error messages and debug output
314
+ parentf <- getSourcePos >>= sourcePosFilePath
314
315
315
316
-- parse the directive
316
317
string " include"
317
318
lift skipNonNewlineSpaces1
318
319
prefixedglob <- rstrip . T. unpack <$> takeWhileP Nothing (`notElem` [' ;' ,' \n ' ])
319
320
lift followingcommentp
320
321
let (mprefix,glb) = splitReaderPrefix prefixedglob
321
- f <- sourcePosFilePath pos
322
- when (null $ dbg6 (f <> " include: glob pattern" ) glb) $
323
- customFailure $ parseErrorAt off $ " include needs a file path or glob pattern argument"
322
+ when (null $ dbg6 (parentf <> " include: glob pattern" ) glb) $
323
+ customFailure $ parseErrorAt eoff $ " include needs a file path or glob pattern argument"
324
324
325
325
-- Find the file or glob-matched files (just the ones from this include directive), with some IO error checking.
326
326
-- Also report whether a glob pattern was used, and not just a literal file path.
327
327
-- (paths, isglob) <- findMatchedFiles off pos glb
328
- paths <- findMatchedFiles off pos glb
328
+ paths <- findMatchedFiles eoff parentf glb
329
329
330
330
-- XXX worth the trouble ? no
331
331
-- Comprehensively exclude files already processed. Some complexities here:
@@ -343,9 +343,7 @@ includedirectivep = do
343
343
Just fmt -> map ((show fmt++ " :" )++ ) paths
344
344
345
345
-- Parse each one, as if inlined here.
346
- -- Reset the position to the `include` line, for error messages.
347
- setOffset off
348
- forM_ prefixedpaths $ parseIncludedFile off pos
346
+ forM_ prefixedpaths $ parseIncludedFile eoff
349
347
350
348
where
351
349
@@ -364,8 +362,8 @@ includedirectivep = do
364
362
-- (detected with unsafePerformIO; it's not worth a ton of boilerplate).
365
363
-- In that case, be aware ** recursive globs will search intermediate dot directories.
366
364
367
- findMatchedFiles :: (MonadIO m ) => Int -> SourcePos -> FilePath -> JournalParser m [FilePath ]
368
- findMatchedFiles off pos globpattern = do
365
+ findMatchedFiles :: (MonadIO m ) => Int -> FilePath -> FilePath -> JournalParser m [FilePath ]
366
+ findMatchedFiles off parentf globpattern = do
369
367
370
368
-- Some notes about the Glob library that we use (related: https://github.com/Deewiant/glob/issues/49):
371
369
-- It does not expand tilde.
@@ -385,8 +383,7 @@ includedirectivep = do
385
383
expandedglob <- lift $ expandHomePath globpattern `orRethrowIOError` " failed to expand ~"
386
384
387
385
-- get the directory of the including file
388
- parentfile <- sourcePosFilePath pos
389
- let cwd = takeDirectory parentfile
386
+ let cwd = takeDirectory parentf
390
387
391
388
-- Don't allow 3 or more stars.
392
389
when (" ***" `isInfixOf` expandedglob) $
@@ -436,23 +433,22 @@ includedirectivep = do
436
433
-- If a glob was used, exclude the current file, for convenience.
437
434
let
438
435
files3 =
439
- dbg6 (parentfile <> " include: matched files" <> if isglob then " (excluding current file)" else " " ) $
440
- (if isglob then filter (/= parentfile ) else id ) files2
436
+ dbg6 (parentf <> " include: matched files" <> if isglob then " (excluding current file)" else " " ) $
437
+ (if isglob then filter (/= parentf ) else id ) files2
441
438
442
439
return files3
443
440
444
- -- Parse the given included file (and any deeper includes, recursively)
445
- -- as if it was inlined in the current (parent) file.
446
- -- The position in the parent file is provided for error messages.
447
- parseIncludedFile :: MonadIO m => Int -> SourcePos -> PrefixedFilePath -> ErroringJournalParser m ()
448
- parseIncludedFile off _pos prefixedpath = do
441
+ -- Parse the given included file (and any deeper includes, recursively) as if it was inlined in the current (parent) file.
442
+ -- The offset of the start of the include directive in the parent file is provided for error messages.
443
+ parseIncludedFile :: MonadIO m => Int -> PrefixedFilePath -> ErroringJournalParser m ()
444
+ parseIncludedFile eoff prefixedpath = do
449
445
let (_mprefix,filepath) = splitReaderPrefix prefixedpath
450
446
451
447
-- Throw an error if a cycle is detected
452
448
parentj <- get
453
449
let parentfilestack = jincludefilestack parentj
454
450
when (dbg7 " parseIncludedFile: reading" filepath `elem` parentfilestack) $
455
- customFailure $ parseErrorAt off $ " This included file forms a cycle: " ++ filepath
451
+ customFailure $ parseErrorAt eoff $ " This included file forms a cycle: " ++ filepath
456
452
457
453
-- Read the file's content, or throw an error
458
454
childInput <- lift $ readFilePortably filepath `orRethrowIOError` " failed to read a file"
0 commit comments