11

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?

duplode
  • 33,731
  • 7
  • 79
  • 150
Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77
  • 2
    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. – Anupam Jain Apr 03 '13 at 10:46
  • You're right, thanks! It looks like parseFileContents is the appropriate function for my use case. If you convert your comment into an answer, I'll mark it as the correct answer. – Michael Snoyman Apr 03 '13 at 11:18
  • Done! I wasn't absolutely sure this was the reason, but on more thinking, it definitely seems to be the case. – Anupam Jain Apr 03 '13 at 12:38

1 Answers1

7

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.

Anupam Jain
  • 7,851
  • 2
  • 39
  • 74