如何在运行时编译QuasiQuoter



我有一个"QuasiQuoter",它在Haskell的源代码中很有用,但也是一个独立的应用程序。所以,我需要能够运行QuasiQuoter

  1. 在Haskell-[myGrammar|someCommand|]中编译期间
  2. shell中的运行时(运行时编译(-mygrammar 'someCommand'

第一部分很容易,但如果解决为使用运行时生成的一些代码调用编译器,第二部分可能会有点笨拙。

我想在Haskell中使用一些不错的方法来解决问题的第二部分,该方法不只接受源代码,而是接受QuasyQuoter数据类型,这样代码就不那么笨拙了。但我找不到这样的编译方法。

你知道吗?谢谢

用法示例

哈斯克尔

该函数接受tuple[(a,b,c,d,e(]并返回一个包含乘积的字符串列表。

function = [lsql| {1..5}, r=[ a.* |> (*) ], "Product of a.1 * a.2 * ... * a.5 is &a.r"|]

Bash

该命令从stdin-csv读取,至少有5个数字列,并返回其产品列表(每行一个(。

lsql-csv '-, r=[ a.* |> (*) ], "Product of a.1 * a.2 * ... * a.5 is &a.r"'

I认为问题是如何在准引号和其他代码块之间以统一的方式解析和处理字符串。如果这个解释是正确的,那么你只是。。。这样做。例如:

-- implementation of these is left to the reader, but can use standard Haskell
-- programming techniques and libraries, like parsec and ADTs and stuff
command :: Parser Command
interpret :: Command -> IO ()
jit :: Command -> Exp -- or Q Exp

然后,在你的lsql-csv.hs中,你会写一些类似的东西

main = do
[s] <- getArgs
case parse command s of
Left err -> die (show err)
Right com -> interpret com

在你的LSql/CSV/QQ.hs中,你会写一些类似的东西

lsql = QuasiQuoter { quoteExp = s -> case parse command s of
Left err -> qReport True (show err) >> fail ""
Right com -> return (jit com) -- or just jit com if that's already a Q Exp
}

最新更新