Parsing UnicodeSyntax with haskell-src-exts

混江龙づ霸主 提交于 2019-12-09 03:29:21

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15784076/parsing-unicodesyntax-with-haskell-src-exts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!