当你在 Smalltalk 中定义一个类时,你可以像这样访问文档注释:
st> Integer comment
'I am the abstract integer class of the GNU Smalltalk system. My
subclasses'' instances can represent signed integers of various
sizes (a subclass is picked according to the size), with varying
efficiency.'
但也许,我是 Smalltalk 的新手,但我找不到如何访问方法/消息文档。即
最佳答案
让我们考虑以下方法
SomeClass >> #msg: arg
"This is a comment"
^self doThisTo: arg
实现提取评论的服务很诱人,例如:
commentOf: aCompiledMethod
^method sourceCode readStream upTo: $"; upTo: $"
事实上,在上面的例子中,我们会得到字符串'This is a comment'
。然而,问题在于双引号字符并不总是注释分隔符。例如考虑以下方法
String >> #doubleQuoted
^'"', self , '"'
如果我们尝试使用上面的方法 #commentOf:
从此方法中提取注释,我们会得到
' , self, '
这没有任何意义。
这意味着我们的解析应该不那么幼稚。因此,我们应该问自己的问题是,使用我们环境中的 Smalltalk Parser 是否更好。我不知道如何在 gnu-smalltalk 中执行此操作,所以让我在这里展示(作为示例)我们如何在 Pharo 中进行:
aCompiledMethod ast comments
#ast
消息使用该方法的抽象语法树进行应答,也称为解析树,它公开了语法该方法的源代码的结构。特别是,它会检测 CommentNode
对象形式的所有评论,这就是为什么 #comments
方法只是在收集评论时枚举所有解析节点。
关于smalltalk - 访问 Smalltalk 中的消息文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51933516/