1
1
mirror of https://github.com/nmattia/snack.git synced 2024-09-11 11:55:36 +03:00

imports: filter out BOM

This commit is contained in:
Nicolas Mattia 2018-10-03 21:34:59 +03:00
parent d733661f31
commit 1ee0684116
2 changed files with 12 additions and 1 deletions

View File

@ -16,5 +16,6 @@ Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
### Fixed
- The module import parsing when the CPP extension is enabled.
- The module import parsing when a BOM is present
[Unreleased]: https://github.com/nmattia/snack/compare/51987daf76cffc31289e6913174dfb46b93df36b...HEAD

View File

@ -4,6 +4,7 @@
module Main (main) where
import Control.Monad.IO.Class
import Data.List (stripPrefix)
import Data.Semigroup
import System.Environment
import Control.Exception
@ -57,7 +58,7 @@ main = do
_ <- GHC.setSessionDynFlags dflags
-- Read the file that we want to parse
str <- liftIO $ readFile fp2
str <- liftIO $ filterBOM <$> readFile fp2
runParser fp2 str Parser.parseModule >>= \case
Lexer.POk _ (SrcLoc.L _ res) -> pure res
@ -82,6 +83,15 @@ main = do
-- here we pretend that @show :: [String] -> String@ outputs JSON
print imports'
-- | Filter out the Byte Order Mark to avoid the following error:
-- lexical error at character '\65279'
filterBOM :: String -> String
filterBOM = \case
[] -> []
str@(x:xs) -> case stripPrefix "\65279" str of
Just str' -> filterBOM str'
Nothing -> x : filterBOM xs
runParser :: FilePath -> String -> Lexer.P a -> GHC.Ghc (Lexer.ParseResult a)
runParser filename str parser = do
dynFlags <- DynFlags.getDynFlags