sed like utility for s-expr

Discussion of Common Lisp

sed like utility for s-expr

Postby puoi » Thu Jul 04, 2013 7:11 am

hi.

What is the best way to substitute s-exprs in a file?
For example: (a b c) -> (b (a a) something c), no matter how the expression is formatted.
puoi
 
Posts: 1
Joined: Thu Jul 04, 2013 7:04 am

Re: sed like utility for s-expr

Postby aidlt » Thu Jul 04, 2013 11:30 pm

Why not just READ s-expressions in a loop and process them? A simple example:
Code: Select all
(defun foo (in out process-sexpr)
  "Read s-expressions from the stream IN, process them one by one using the function PROCESS-SEXPR and print the results to the steram OUT."
  (do ((sexpr (read in nil :eof) (read in nil :eof)))
    ((eq sexpr :eof))
    (print (funcall process-sexpr sexpr) out)))

;;; Example: send all the s-exprs from a file to stdout.
(with-open-file (in "test.txt")
  (foo in t #'identity))
aidlt
 
Posts: 13
Joined: Thu May 16, 2013 1:42 pm

Re: sed like utility for s-expr

Postby pjstirling » Fri Jul 05, 2013 6:30 am

Won't that a) lose comments and extra whitespace, b) print everything upper-case?
pjstirling
 
Posts: 79
Joined: Sun Nov 28, 2010 4:21 pm

Re: sed like utility for s-expr

Postby Goheeca » Fri Jul 05, 2013 6:42 am

The case can handled by readtable-case.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 213
Joined: Thu May 10, 2012 12:54 pm

Re: sed like utility for s-expr

Postby aidlt » Fri Jul 05, 2013 3:07 pm

pjstirling wrote:Won't that a) lose comments and extra whitespace... ?


Surely that will! They are virtually non-existent. :)
aidlt
 
Posts: 13
Joined: Thu May 16, 2013 1:42 pm

Re: sed like utility for s-expr

Postby findinglisp » Fri Jul 12, 2013 7:40 pm

You could write your own basic reader. If the files you're processing are just basic sexprs, without fancy read-table stuff going on, then a basic reader is really pretty simple to build. You could do it such that you parse the file while retaining the comments. Not ideal, surely, but once you are able to get the file contents into memory as lists, it will be much easier to fiddle with them.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
findinglisp
 
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 1 guest