Extracting comments

For the Compleat Fan
Locked
Jeremy Dunn
Posts: 95
Joined: Wed Oct 13, 2004 8:02 pm
Location: Bellingham WA

Extracting comments

Post by 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
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

Hi Jeremy,

This may help you get there:

Code: Select all

(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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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.

Locked