haskell-suite / haskell-src-exts

Manipulating Haskell source: abstract syntax, lexer, parser, and pretty-printer
Other
193 stars 94 forks source link

Parse failed with LANGUAGE pragma #424

Closed Magicloud closed 5 years ago

Magicloud commented 5 years ago

I have checked prompted issues, do not seem like the same issue.

I have a module using GADTs extension enabled by LANGUAGE pragma. And LambdaCase enabled by Cabal default-extensions.

{-# LANGUAGE GADTs #-}
module Test where

data R m a where
  R :: MonadIO m => ReaderT Context m a -> R m a
-- other codes

Then following parsing code gave me error.

  let parseMode = defaultParseMode
        { parseFilename = input
        , extensions    = exts -- [LambdaCase]
        }
  in parseModuleWithComments parseMode <$> readFile input
    ParseFailed l s -> fail (s ++ ": " ++ input ++ "\n" ++ show l)
setup: GADTs language extension is not enabled. Please add {-# LANGUAGE GADTs
#-} pragma at the top of your module.: Test.hs
SrcLoc "Test.hs" 6 1

Test module works with GHC 8.6.3. haskell-src-exts is 1.20.3.

miyamoto1340 commented 5 years ago

What is inside exts? There should be the Language Pragmas you use. In your case there should be GADTs inside.

Magicloud commented 5 years ago

I am not sure if I understand correctly. Since the error message prompted the Language Pragma, I think the line above "module Test" should work?

exts contains the "default-extensions" from .cabal file, which does not contain GADTs since GADTs only being used in this module.

miyamoto1340 commented 5 years ago

Okay so you have to include GADTs. With extensions = exts you tell haskell-src-exts which Language Pragmas it should parse. So in your case exts should be [LambdaCase, GADTs].

Magicloud commented 5 years ago

I see. So if the input (module Test) is unknown when I write the parser code, I have to enable all extensions?

miyamoto1340 commented 5 years ago

Yes you can enable all via extensions = [EnableExtension x | x <- [minBound..maxBound]] if you import Language.Haskell.Exts.Extension

Magicloud commented 5 years ago

OK. Thank you. Did not know Language Pragma does not work for HSE.