clojure代码修改保存阅读器宏 [英] clojure code modification preserving reader macros

查看:156
本文介绍了clojure代码修改保存阅读器宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在clojure中,read-string后跟str不会返回原始字符串,而是读取宏已扩展的字符串:

In clojure, read-string followed by str will not return the original string, but the string for which the reader macros have been expanded:

(str (read-string "(def foo [] #(bar))"))
;"(def foo [] (fn* [] (bar)))"

这是有问题的,如果我们想操纵一小部分代码,远离任何读取器宏,返回保留读取器宏的字符串表示形式。是否有工作?

This is problematic if we want to manipulate a small part of the code, far away from any reader macros, and get back a string representation that preserves the reader macros. Is there a work around?

推荐答案

read 的目的是构建代码的AST,因此函数不保留原始文本的所有属性。否则,它应该跟踪原始代码布局(例如括号,换行符,缩进(制表符/空格),注释等的位置)。如果您查看 LispReader.java a>,您可以看到读取器宏是无条件应用的( * read-eval * 不会影响所有的读取器宏)。

The purpose of read is to build an AST of the code and as such the function does not preserve all the properties of the original text. Otherwise, it should keep track of the original code-layout (e.g. location of parenthesis, newlines, indentation (tabs/spaces), comments, and so on). If you have a look at LispReader.java, you can see that the reader macros are unconditionally applied (*read-eval* does not influence all reader macros).

这里是我的建议:


  • 您可以从现有的 code>并实现自己的读者。也许足以更改分派表,以便宏字符指向你自己的读者。您还需要构建引用表单的运行时表示,并为这些对象提供足够的打印机函数。

  • 您可以使用Emacs lisp处理原始文件,

备注:您必须知道您要达成的目标气味腥味。你可能有一个很好的理由想这样做,但这看起来不必要的复杂,不知道为什么你想在语法级别工作。如果您可以提供更多详情,这将有所帮助。

Remark: you must know that what you are trying to achieve smells fishy. You might have a good reason to want to do this, but this looks needlessly complex without knowing why you want to work at the syntax level. It would help if you could provide more details.

这篇关于clojure代码修改保存阅读器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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