[racket] Modifying source code, keeping comments and formatting intact

From: Jukka Tuominen (jukka.tuominen at finndesign.fi)
Date: Mon Apr 4 05:11:16 EDT 2011


I seem to be coming back to this problem repeatedly. I need to modify source
code by code so that the outcome is the modified  source code with the
original formatting and comments. For example...

";; comment1
(list 'a ;comment2
      'b ;comment3 mentions 'a

>> (a b)

~TWEAK~ (add/remove/replace)

";; comment1
(list 'a ;comment2
      'b ;comment3 mentions 'a

>> (a b c)

So far I haven't been able to find any clever way to do this or seen how
others have done it. I was thinking about starting to solve the problem by
locating the position of interest with 'regexp-match-positions' or similar,
devide the source code into three parts: 'beginning, 'old-target, 'end and
after modifications reconstruct: 'beginning, 'modified-target, 'end.

Does this seem like a rational way to go forward, or is there a better way
to do it? I wonder how the DrRackect's 'rename' functionality is
implemented, for example (target inside correct scope, not part of comment)?

It would be great to have generic functions like source-car, source-cdr and
source-cons that would work as expected, but preserving the comments and
formatting :)

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.