[racket] How to keep comments when reading a file?

From: Jukka Tuominen (jukka.tuominen at finndesign.fi)
Date: Tue Jan 11 02:53:02 EST 2011

Hi,

some time ago I asked about a somewhat related problem (see the original
message at the bottom). Shortly, in my case I want to _modify_ the source
code and have the output include both the modified code and all comments in
their original places. Unfortunately it is still to be solved, and I have
put it aside for the time being, but just thought maybe there is a more
generic solution for the both. 'Check Syntax/ Rename' propably eat these
things for breakfast :)

br, jukka

> -----Original Message-----
> From: users-bounces at racket-lang.org
> [mailto:users-bounces at racket-lang.org]On Behalf Of John Clements
> Sent: 11 January 2011 03:52
> To: Todd O'Bryan
> Cc: PLT-Scheme Mailing List
> Subject: Re: [racket] How to keep comments when reading a file?
>
>
>
> On Jan 10, 2011, at 10:51 AM, Todd O'Bryan wrote:
>
> > Thanks for the pointer. I had hoped to avoid making two passes with
> > different readers.
> >
> > Can anyone give me a hint? It looks like the pieces are there, but
> > there are no examples in the documentation that explain how to get at
> > the pieces I need and how to plug them in.
>
> It looks to me like you want to create an editor% and load the
> file into it[*], then examine it using the editor's methods. No?
>
> Apologies if I'm misunderstanding your problem.
>
> John Clements
>
> [*] IIRC, this method is called 'load-file'.

-----


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


_________________________________________________
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users




Posted on the users mailing list.