-- Execute a given IO action with the AST corresponding to the given opts.
-- Die with the returned error message if any.
withAST :: Options -> (FileSystem -> IO ()) -> IO ()
withAST opts successFn =
  case parseExp expr srcs root sep token start of
    Right tree  -> do
      errors <- if shouldCheck
                   then checkIntegrity True (templates opts) tree
                   else return []
      if null errors
         then successFn tree
         else fatal $ formatErrors errors
    Left errMsg -> fatal errMsg
  where
    (Just expr) = input opts
    srcs        = sources opts
    root        = rootDir opts
    sep         = delimiter opts
    token       = sigil opts
    start       = startAt opts
    shouldCheck = not . noCheck $ opts

-- Recursively copy a directory.
-- No existence checks are done.
withAST :: Options -> (FileSystem -> IO ()) -> IO ()
withAST opts successFn =
  case parseExp expr srcs root sep token start of
    Right tree  -> do
      errors <- if shouldCheck
                   then checkIntegrity True (templates opts) tree
                   else return []
      if null errors
         then successFn tree
         else fatal $ formatErrors errors
    Left errMsg -> fatal errMsg
  where
    (Just expr) = input opts
    srcs        = sources opts
    root        = rootDir opts
    sep         = delimiter opts
    token       = sigil opts
    start       = startAt opts
    shouldCheck = not . noCheck $ opts

-- Recursively copy a directory.
-- No existence checks are done.
withAST opts successFn =
  case parseExp expr srcs root sep token start of
    Right tree  -> do
      errors <- if shouldCheck
                   then checkIntegrity True (templates opts) tree
                   else return []
      if null errors
         then successFn tree
         else fatal $ formatErrors errors
    Left errMsg -> fatal errMsg
  where
    (Just expr) = input opts
    srcs        = sources opts
    root        = rootDir opts
    sep         = delimiter opts
    token       = sigil opts
    start       = startAt opts
    shouldCheck = not . noCheck $ opts

-- Recursively copy a directory.
-- No existence checks are done.