Here is a little Lisp puzzle for you:
I want to read a Lisp file and produce a list of the top-level forms in the file, but I want to preserve comments. Any comment that appears on a line within a top-level form should be associated with that form. Comments that appear by themselves at top level should be associated with the following top-level form. For example, if the file contains:
;;; -*- Lisp -*- ;; This file is a test (in-package "CL-USER") ;; This is the test function: (defun foo (arg) ;; This function does nothing (declare (ignore arg)) nil) ;; Return nil (defparameter *x* 42) (defparameter *y* 99) ;; This is y ;; The BAR function doesn't do anything either (defun bar () nil) ;; Trailing comment in file.
Then I want to produce the following list:
'(";;; -*- Lisp -*-
;; This file is a test
(in-package \"CL-USER\")
"
"
;; This is the test function:
(defun foo (arg)
;; This function does nothing
(declare (ignore arg))
nil) ;; Return nil
"
"
(defparameter *x* 42)
"
"(defparameter *y* 99) ;; This is y
"
"
;; The BAR function doesn't do anything either
(defun bar ()
nil)
"
"
;; Trailing comment in file.
")
This puzzle is not difficult, but it is tricky to get the edge cases right.
No comments:
Post a Comment