Haskell 中的 CPP 扩展和多行文字
是否可以在包含多行字符串文字的 Haskell 代码上使用 CPP 扩展? Haskell 还有其他条件编译技术吗?
例如,让我们看一下这段代码:
-- If the next line is uncommented, the program does not compile.
-- {-# LANGUAGE CPP #-}
msg = "Hello\
\ Wor\
\ld!"
main = putStrLn msg
如果我取消注释 {-# LANGUAGE CPP #-}
,那么 GHC 就会用词法错误反驳这段代码:
[1 of 1] Compiling Main ( cpp-multiline.hs, cpp-multiline.o )
cpp-multiline.hs:4:17:
lexical error in string/character literal at character 'o'
Using GHC 6.12.1, cpphs is available。
我确认使用 cpphs.compat 包装器和-pgmP cpphs.compat
选项有帮助,但我希望有一个不依赖于自定义 shell 脚本的解决方案。 -pgmP cpphs
不起作用。
PS 我需要对 GHC < 使用不同的代码6.12 且 GHC >= 6.12,是否可以不用预处理器?
UPD。除了 Ganesh 接受的答案之外,我还发现另一种解决方法是将所有条件声明放在带有 {-# LANGUAGE CPP #-}
的单独模块中,从而避免在具有多行的模块中使用 CPP字符串。
Is it possible to use CPP extension on Haskell code which contains multiline string literals? Are there other conditional compilation techniques for Haskell?
For example, let's take this code:
-- If the next line is uncommented, the program does not compile.
-- {-# LANGUAGE CPP #-}
msg = "Hello\
\ Wor\
\ld!"
main = putStrLn msg
If I uncomment {-# LANGUAGE CPP #-}
, then GHC refutes this code with a lexical error:
[1 of 1] Compiling Main ( cpp-multiline.hs, cpp-multiline.o )
cpp-multiline.hs:4:17:
lexical error in string/character literal at character 'o'
Using GHC 6.12.1, cpphs is available.
I confirm that using cpphs.compat wrapper and -pgmP cpphs.compat
option helps, but I'd like to have a solution which does not depend on custom shell scripts. -pgmP cpphs
does not work.
P.S. I need to use different code for GHC < 6.12 and GHC >= 6.12, is it possible without preprocessor?
UPD. In addition to the accepted answer of Ganesh, I also found that another workaround is to put all conditional declarations in a separate module with {-# LANGUAGE CPP #-}
and thus avoid CPP in the modules with multiline strings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
cpphs 现在本身有一个 --cpp 选项,我认为这使得 compat 脚本变得不必要:请参阅 http:// /haskell.org/cpphs/
我认为您需要将
-optP --cpp
传递给 GHC (以及-pgmP cpphs
)才能启用这种行为。cpphs now has a --cpp option itself, which I think makes the compat script unnecessary: see the cpphs 1.3 entry at http://haskell.org/cpphs/
I think you'd need to pass
-optP --cpp
to GHC (as well as-pgmP cpphs
) to enable this behaviour.GHC 用户手册似乎解决了这个问题: 第 4.10.3.1 节 内容如下
It seems the GHC user manual addresses this: Section 4.10.3.1 reads
决定是
反斜杠后不需要空格,并且行开头不需要反斜杠。
The decision is
No spaces after backslash needed plus no backslash needed at the line begin.