库来解析和检查Haskell代码? [英] Library to parse and check Haskell code?

查看:141
本文介绍了库来解析和检查Haskell代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何hackage库可以解析haskell代码并检查它是否是有效代码?

我愿意玩一点进化论模型,我想检查生成的代码串是否会编译而不必将它们写入磁盘并运行编译器。

理想情况下,能够在字符串中运行代码会很好,但只能检查代码的有效性。

如果您了解用于检查其他语言(lisp,C,...)的解析器库(在haskell中),它也会很好。

解决方案

对于解析Haskell代码,您可以使用



后者处理所有的GHC扩展(然后是一些),而前者仅解析Haskell 98.下面是一个用法示例:

 前奏> import Language.Haskell.Exts.Parser 

Prelude Language.Haskell.Exts.Parser> parseModulemain = putStrLn \Hello \
ParseOk(Module(SrcLoc {srcFilename =< unknown> .hs,srcLine = 1,srcColumn = 1))(ModuleNameMain)[ ]没有(只是[EVar(UnQual(Identmain))])[] [PatBind(SrcLoc {srcFilename =< unknown> .hs,srcLine = 1,srcColumn = 1))(PVar )))))(BDecls [])])

Prelude Language.Haskell(App(Var(UnQual(IdentputStrLn .Exts.Parser> parseModulemain == putStrLn \Hello \
ParseFailed(SrcLoc {srcFilename =< unknown> .hs,srcLine = 1,srcColumn = 25))TemplateHaskell未启用

请注意,即使代码解析正确,也并不意味着它会检查:

  Prelude Language.Haskell.Exts.Parser> parseModulemain = putStrLn2 \Hello \
ParseOk(Module(SrcLoc {srcFilename =< unknown> .hs,srcLine = 1,srcColumn = 1))(ModuleNameMain)[ ]没有(只是[EVar(UnQual(Identmain))])[] [PatBind(SrcLoc {srcFilename =< unknown> .hs,srcLine = 1,srcColumn = 1))(PVar ))Nothing(UnGuardedRhs(App(Var(UnQual(IdentputStrLn2)))(Lit(StringHello))))(BDecls [])])

因此,对于您的特定用例,最好使用 GHC API ,它还可以让你检查分析的代码,或者只需在你的文件中运行 ghc -c



解析C代码时,有 language-c



如果您需要解析某种其他语言,请查看关于Hackage的类别。例如,这里是 S表达式的解析器


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 check if produced strings of code will compile without having to write them to the disk and run the compiler.

Ideally it would be nice to be able to run the code in the strings too, but only checking the validity of the code is ok.

If you know about parser libraries (in haskell) that check for other languages (lisp, C,...)it would be nice too.

解决方案

For parsing Haskell code, you can use either

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.

这篇关于库来解析和检查Haskell代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆