Search code examples
haskellhaskell-src-exts

Parsing UnicodeSyntax with haskell-src-exts


I have a Haskell source file which using Unicode syntax:

{-# LANGUAGE UnicodeSyntax #-}
succ' :: Int → Int
succ' = succ

main :: IO ()
main = print $ succ' 1

This parses and runs fine with GHC. Additionally, stylish-haskell and hlint (both based on haskell-src-exts) can read this file without any trouble. However, when I try to parse it myself using haskell-src-exts:

import Language.Haskell.Exts (parseModule)

main = do
    x <- readFile "test.hs"
    print $ parseModule x

I get the error message:

ParseFailed (SrcLoc {srcFilename = "<unknown>.hs", srcLine = 6, srcColumn = 1}) "TypeOperators is not enabled"

However, providing UnicodeSyntax explicitly in the extensions list or using parseFile works just fine:

import Language.Haskell.Exts

main = do
    x <- readFile "test.hs"
    print $ parseModuleWithMode defaultParseMode
        { extensions = [UnicodeSyntax]
        } x

    parseFile "test.hs" >>= print

Any idea why the first approach fails?


Solution

  • From a cursory glance at the source, it doesn't look like parseModule extracts language pragmas from the source before parsing (parseFile does do that by calling getExtensions). By the time parsing has begun, it is already too late to enable unicode syntax.