From: Ivan Bella
Subject: LISP SLOC counter
Date: 
Message-ID: <3246F5F5.41C6@lmco.com>
Hello out there,
	Does anybody know of a Source Lines Of Code counter (SLOC) for
LISP?  I am not all to sure what consitutes a "line" in this context.
What I need is something that I can track over time including logical
lines of code, physical lines of code, lines of comments, and anything
else of interest.  Any help would be appreciated.  Thanks in advance.
-------------------------------------------------------------------------------
Ivan Bella; ··········@lmco.com / BELLAI at WMAVM7; 301-240-6097; Fax:
240-6187
Lockheed Martin Federal Systems;  700 N. Frederick Rd. Gaithersburg, MD
20882

From: Francis Leboutte
Subject: Re: LISP SLOC counter
Date: 
Message-ID: <324776c2.2318583@news.glo.be>
Ivan Bella <·········@lmco.com> wrote:

>Hello out there,
>	Does anybody know of a Source Lines Of Code counter (SLOC) for
>LISP?  I am not all to sure what consitutes a "line" in this context.
>What I need is something that I can track over time including logical
>lines of code, physical lines of code, lines of comments, and anything
>else of interest.  Any help would be appreciated.  Thanks in advance.

You should have a look in the CMU repository
(ftp://ftp.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/lang/lisp/).
If you don't find there what you want, you may also look at my
Define-System facility which include a simple function to count code,
comment and obsolete code lines (obsolete forms are forms which are
read-time conditionalized with some particular features e.g. #+old form).
See http://user.glo.be/~algo.

Francis
--
Francis Leboutte, Algorithme, Rue de la Charrette 141, 4130 Tilff, Belgium
 ····@glo.be
 http://user.glo.be/~algo 
 t&fax: +32-(0)4-3883528
From: Donald H. Mitchell
Subject: Re: LISP SLOC counter - count-lines.lsp [01/01]
Date: 
Message-ID: <52hd1l$4ib@dropit.pgh.net>
In article <·············@lmco.com>, Ivan Bella <·········@lmco.com> wrote:
>Hello out there,
>        Does anybody know of a Source Lines Of Code counter (SLOC) for
>LISP? ... including logical
>lines of code, physical lines of code, lines of comments, and anything
>else of interest. ...

Here's a simple starting point.  I'm not certain what's a logical v. physical 
line of code; so, this may or may not adapt to your needs.

BEGIN -- Cut Here -- cut here
(in-package :cl-user)

;;; Call like (source-lines-of-code "f:\\sources\\source.lsp")
;;; To do multiple files, merely catch and add the returned values
;;;  (let ((code 0) (comments 0))
;;;     (mapc #'(lambda (file)
;;;         (multiple-value-bind (file-code file-comments)
;;;              (source-lines-of-code file)
;;;           (incf code file-code)
;;;           (incf comments file-comments)))
;;;       (get-all-our-files)))


(defun source-lines-of-code (file)
  (with-open-file (in file :direction :input)
    (let ((code 0) (comments 0))
      (loop with eof = (gensym)
	    for line = (read-line in nil eof)
	    until (eq line eof)
	    do (case (line-syntax line)
		 (whitespace)
		 (comment (incf comments))
		 (comment-block
		  (incf comments
			(1+ (check-comment-block in eof))))
		 (code (incf code)))
	    finally (return (values code comments)))))) 

(defun line-syntax (line &optional start)
  (loop for char across (if start (subseq line start) line)
	as index from (or start 0)
	thereis (cond
		  ((whitespacep char) nil)
		  ((char= #\; char)
		   'comment)
		  ((char= #\# char)
		   (pound-reader line (1+ index)))
		  (t 'code))
	finally (return 'whitespace)))


(defun check-comment-block (in eof)
  ;;return the number of lines in the comment block
  (loop for line = (read-line in nil eof)
	until (eq line eof)
	count t
	until (comment-block-done-p line)))  

;;; only use if there's no accessible function in your lisp to do the same
(defun whitespacep (char)
  (member char '(#\space #\newline #\page #\tab #\return #\linefeed)))

(defun pound-reader (line index)
  "Return 'comment if there's a self-contained comment block. Return
'comment-block if this begins a comment block that does not end on
this line.  Return 'code if this appears to be the start of code.
Return 'whitespace if there's no real code or comment here."
  (case (char line index)
    (#\| (classify-block-comment line (1+ index)))
    ((#\+ #\-) (classify-feature-line line (1+ index)))
    ;;if you define any reader macros, interpret them here.
    (otherwise 'code))) 

(defun classify-block-comment (line index)
  ;;it's a comment if there's an end to the block on this line
  (if (comment-block-done-p (subseq line index))
      'comment
      'comment-block)) 

(defun classify-feature-line (line index)
  ;;does this line contain anything following the feature?
  (line-syntax line
	       (nth-value 1 (read-from-string line nil nil :start index))))
 

(defun comment-block-done-p (line)
  (let ((index (position #\| line)))
    (and index (char= (char line (1+ index)) #\#)))) 
END -- Cut Here -- cut here

Donald H. Mitchell              ···@pgh.net
Proactive Solutions, Inc.       412.835.2410
5858 Horseshoe Dr.              412.835.2411 (fax)
Bethel Park, PA 15102