Let's consider the following method
SomeClass >> #msg: arg
"This is a comment"
^self doThisTo: arg
It is tempting to implement a service for extracting comments such as:
commentOf: aCompiledMethod
^method sourceCode readStream upTo: $"; upTo: $"
In fact, in the example above, we would get the string 'This is a comment'
. The problem, however, is that the double quote character is not always a comment delimiter. Consider for instance the following method
String >> #doubleQuoted
^'"', self , '"'
if we tried to use our method #commentOf:
above for extracting comments from this method, we would get
' , self, '
which doesn't make any sense.
This means that our parsing should be less naïve. Therefore, the question that we should ask ourselves is whether it is not better to use the Smalltalk Parser of our environment. I don't know how to do this in gnu-smalltalk, so let me show here (as an example) how could we proceed in Pharo:
aCompiledMethod ast comments
The #ast
message answers with the Abstract Syntactic Tree of the the method, also known as the Parse Tree, which exposes the syntactic structure of the method's source code. In particular, it detects all comments in the form of CommentNode
objects, and that is why the #comments
method simply consists in enumerating all parse nodes while collecting the comments.