I'm looking for some help in designing an algortihm to extract the comment part of a line of LISP code. In particular, I want to write a function "lisp-comment" that will take a line of LISP code in string format and return a list with the code part and comment part separated from each other. For example:
(lisp-comment "(setq a 3) ;we set a variable"))
would return
("(setq a 3)" ";we set a variable"))
At first it seems like you only have to look for the last semicolon and break from there but it is more complicated than that because a semicolon can be inside a string as well as being followed by quoted words. I'm having difficulty determining what the proper rule for finding the right semicolon is. Is there a regex that will do this?
Hi Jeremy,
This may help you get there:
(define (join-lines pat)
(join (clean empty? (clean nil?
(map (fn (e) (if (regex pat e) (trim $1) "")) lines))) "n"))
(define (lisp-comment txt)
(set 'lines (parse txt "n"))
(list
(join-lines "(;.*?)$")
(join-lines "([^;]+)(;?.*?)$")))
This also considers semicolons within strings to be comments, so it's not foolproof. For example, this code would think the last two lines contain comments ;-)
m i c h a e l
When I tried to write a newLISP formatter, I kept flags for 'inside a string', 'inside a comment', and so on, so that a semicolon was either to be interpreted as a string or a comment. It got messy - particularly with multi-line strings, if I remember.
Fanda //http://www.intricatevisions.com/index.cgi?page=nlcode has written some amazing code analysis tools, which I intend to study when I have the time.