Skip to content

Commit 3598ec2

Browse files
committed
Ignore UTF-8 BOM while parsing
See #3
1 parent 3ce3437 commit 3598ec2

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/StylishHaskell/Parse.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ unCpp = unlines . map unCpp' . lines
2424
unCpp' xs = xs
2525

2626

27+
--------------------------------------------------------------------------------
28+
-- | If the given string is prefixed with an UTF-8 Byte Order Mark, drop it
29+
-- because haskell-src-exts can't handle it.
30+
dropBom :: String -> String
31+
dropBom ('\xfeff' : str) = str
32+
dropBom str = str
33+
34+
2735
--------------------------------------------------------------------------------
2836
-- | Read an extension name from a string
2937
parseExtension :: String -> Either String H.Extension
@@ -46,8 +54,8 @@ parseModule extraExts mfp string = do
4654
mode = H.defaultParseMode
4755
{H.extensions = exts, H.fixities = Nothing}
4856

49-
-- Special handling for CPP, haskell-src-exts can't deal with it
50-
string' = if H.CPP `elem` exts then unCpp string else string
57+
-- Preprocessing
58+
string' = dropBom $ (if H.CPP `elem` exts then unCpp else id) $ string
5159

5260
case H.parseModuleWithComments mode string' of
5361
H.ParseOk md -> return md

0 commit comments

Comments
 (0)