[racket] tweaking source code without loosing comments and formatting

From: Jukka Tuominen (jukka.tuominen at finndesign.fi)
Date: Sun Oct 31 08:58:14 EDT 2010

Hi all,

I'd like to do some tricks on scheme source code. The problem is that I
loose all comments and formatting in the process. Here is what I have now:

;; Helper functions
(define (number-of-arguments lambda-without-inputs)
  (length (cdadr lambda-without-inputs)))

(define (named-lambda-without-inputs? object)
  (if (and (list? object)
           (not (null? object))
           (equal? 'named-lambda (car object)))
      #t #f))

(define (generate-dummy-inputs num dummy-content)
  (if (= 0 num)
      '()
      (cons dummy-content (generate-dummy-inputs (- num 1) dummy-content))))

(define (add-inputs-to-named-lambda object)
  (if (named-lambda-without-inputs? object)
      (cons object (generate-dummy-inputs (number-of-arguments object)
'(void)))
      #f))

;; Some example inputs:

(define input1 '(named-lambda (function arg1 arg2 arg3) (list arg1 arg2
arg3)))

(define input2
  '(named-lambda (function arg1 arg2 arg3)
                 ;; important comment I don't want to loose
                 (list arg1 arg2 arg3))
  )


;; when you run the above functions with the two different inputs, you get
the following:

(add-inputs-to-named-lambda input1)
; >>> ((named-lambda (function arg1 arg2 arg3) (list arg1 arg2 arg3)) (void)
(void) (void))

(add-inputs-to-named-lambda input2)
;>>> ((named-lambda (function arg1 arg2 arg3) (list arg1 arg2 arg3)) (void)
(void) (void))


As you can see the comment and formatting is lost (which would be ok, if I
was a computer). Boxed comments don't work at all, BTW.

In normal case, the starting point as well the desired outcome is in string
format (just left out the conversion). The following is what I would want to
happen:

"
;;; This is an extremely great function
(named-lambda (function arg1 arg2 arg3); the name is given here
                 ;; important comment I don't want to loose
                 (list arg1 arg2 arg3)); this is what it does
;; great, wasn't it!
;; To do: more great things
"

>>>

"
;;; This is an extremely great function
((named-lambda (function arg1 arg2 arg3); the name is given here
                 ;; important comment I don't want to loose
                 (list arg1 arg2 arg3)(void) (void) (void)); this is what it
does
;; great, wasn't it!
;; To do: more great things
"

Any ideas how to solve this?


br, jukka


|  J U K K A   T U O M I N E N
|  m a n a g i n g   d i r e c t o r  M. A.
|
|  Finndesign  Kauppiaankatu 13, FI-00160 Helsinki, Finland
|  mobile +358 50 5666290
|  jukka.tuominen at finndesign.fi  www.finndesign.fi




Posted on the users mailing list.