Extracting comments

Started by Jeremy Dunn, July 07, 2007, 10:12:39 AM

Previous topic - Next topic

Jeremy Dunn

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?

m i c h a e l

#1
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

cormullion

#2
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">//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.