如何分层“嵌套”使用 Haskell 漂亮打印进行缩进
我想使用 Haskell Pretty 包打印 AST。
一切都运行良好,但嵌套结构无法正确缩进。
我做了这样的事情:
draw :: Pretty a => a -> String
draw = render.pretty
pretty (Letin d c ) = text "let" <+> text (draw d) $$
nest 4 (text "in" <+> text (draw c))
但结果是这样的:
let Const x := 2
in let Var y := Int
in y = 3; let Var z := Int
in z = 0; z = z + 1
似乎嵌套级别不是继承的,所以所有的都是绝对的+4边距,而不是在每个级别连续缩进,即相对于其父级+4,当前缩进级别。
I want to print out an AST, using Haskell Pretty package.
It all works well, but nested constructs don't indent properly.
I do something like this:
draw :: Pretty a => a -> String
draw = render.pretty
pretty (Letin d c ) = text "let" <+> text (draw d) $
nest 4 (text "in" <+> text (draw c))
but the results are like this:
let Const x := 2
in let Var y := Int
in y = 3; let Var z := Int
in z = 0; z = z + 1
It seems that the nest levels are not inherited, so all are absolute on the +4 margin, instead of successively indented at each level, i.e. +4 relative to their parent, the current indent level.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的意思是递归调用
pretty
吗?从你的问题中我无法判断。尝试重现您所做的事情的快速测试:
结果如预期:
因此您可能需要列出更多代码。
Do you mean to call
pretty
recursively? I can't tell from your question.A quick test to try to reproduce what you've done:
Results in, as expected:
So you may have to list more code.