读取合格的符号 [英] Reading qualified symbols

查看:84
本文介绍了读取合格的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Lisp开发一个代码格式化程序,该程序正在使用阅读器将代码读取为S表达式格式。

I'm working on a code formatter for Lisp, which is using the reader to read code into S-expression format.

这对于普通符号来说效果很好。

This works fine for plain symbols.

对于合格的符号来说效果不佳。 foo:bar 仅在定义了包 foo 时才可读,但是就格式化程序而言,它没有,因为与编译器不同,格式化程序仅读取代码,而不执行代码。

It doesn't work so well for qualified symbols. foo:bar is only readable if the package foo has been defined, but of course as far as the formatter is concerned, it has not, because unlike the compiler, the formatter is only reading the code, not executing it.

我如何告诉读者继续进行并自动创建一个运行中的 foo 包,否则,不要大汗淋漓,只需阅读 foo:bar ,而不是本身就是一个符号,但是我可以以某种明确的格式将其作为特殊情况处理?

How can I tell the reader to either go ahead and automatically create a package foo on the fly, or failing that, don't sweat it, just read foo:bar, not as a symbol per se, but in some unambiguous format I can deal with as a special case?

推荐答案

我认为您应该不要使用读取器,因为那样会造成损失(您会丢失评论,以及通过读取器宏更改的所有内容,例如读取时间值,读取时间引用等)。

I believe that you should not use the reader for that, because that is lossy (you lose comments, and anything that gets changed through reader macros, e. g. read-time-values, read-time references etc.).

但是,如果需要,您可以自动创建包,也可以通过处理错误e导出符号。 G。在SBCL上:

But if you want, you can automatically create the package and maybe also export the symbol by handling the error, e. g. on SBCL:

(handler-bind ((sb-int:simple-reader-package-error
                (lambda (e)
                  (let ((p (sb-int::package-error-package e)))
                    (ctypecase p
                      (string
                       (make-package p)
                       (invoke-restart 'retry))
                      (package
                       (export (intern (first (simple-condition-format-arguments e)) p) p)
                       (invoke-restart 'retry)))))))
  (with-simple-restart (retry "Retry")
    (read-from-string "foo:bar")))

这有点hacky,我们不能保证条件的格式保持这样。

This is a bit hacky, and we have no guarantee that the format of the condition stays like that.

这篇关于读取合格的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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