Library to parse and check Haskell code?

后端 未结 1 1800
抹茶落季
抹茶落季 2021-02-19 07:33

Is there any library in hackage that can parse haskell code and check if it is valid code or not?

I\'m willing to play a bit with an evolutionary model and I want to che

相关标签:
1条回答
  • 2021-02-19 08:07

    For parsing Haskell code, you can use either

    • haskell-src
    • or haskell-src-exts

    The latter handles all of the GHC extensions (and then some), while the former parses only Haskell 98. Here's an example of usage:

    Prelude> import Language.Haskell.Exts.Parser
    
    Prelude Language.Haskell.Exts.Parser> parseModule "main = putStrLn \"Hello\""
    ParseOk (Module (SrcLoc {srcFilename = "<unknown>.hs", srcLine = 1, srcColumn = 1}) (ModuleName "Main") [] Nothing (Just [EVar (UnQual (Ident "main"))]) [] [PatBind (SrcLoc {srcFilename = "<unknown>.hs", srcLine = 1, srcColumn = 1}) (PVar (Ident "main")) Nothing (UnGuardedRhs (App (Var (UnQual (Ident "putStrLn"))) (Lit (String "Hello")))) (BDecls [])])
    
    Prelude Language.Haskell.Exts.Parser> parseModule "main == putStrLn \"Hello\""
    ParseFailed (SrcLoc {srcFilename = "<unknown>.hs", srcLine = 1, srcColumn = 25}) "TemplateHaskell is not enabled"
    

    Note that even if the code parses correctly, it doesn't mean it will typecheck:

    Prelude Language.Haskell.Exts.Parser> parseModule "main = putStrLn2 \"Hello\""
    ParseOk (Module (SrcLoc {srcFilename = "<unknown>.hs", srcLine = 1, srcColumn = 1}) (ModuleName "Main") [] Nothing (Just [EVar (UnQual (Ident "main"))]) [] [PatBind (SrcLoc {srcFilename = "<unknown>.hs", srcLine = 1, srcColumn = 1}) (PVar (Ident "main")) Nothing (UnGuardedRhs (App (Var (UnQual (Ident "putStrLn2"))) (Lit (String "Hello")))) (BDecls [])])
    

    So for your specific use case, it is probably better to use GHC API which also lets you typecheck parsed code, or just run ghc -c on your file.

    For parsing C code, there is language-c.

    If you need to parse some other language, take a look at this category on Hackage. For example, here's a parser for S-expressions.

    0 讨论(0)
提交回复
热议问题