Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 585
Michael Snoyman Avatar asked Apr 03 '13 09:04

Michael Snoyman


1 Answers

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.

like image 90
Anupam Jain Avatar answered Sep 19 '22 13:09

Anupam Jain