unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* grep and sed in guile
@ 2013-08-25 21:32 Stefan Israelsson Tampe
  2013-08-27 13:27 ` Ludovic Courtès
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Israelsson Tampe @ 2013-08-25 21:32 UTC (permalink / raw)
  To: guile-devel, guile-user

Hi all,

This small discussion refers to the current head of the guile-log
branch. When the parser framework is rich enough I will issue a new 
release of it.

As you know I'm currently working with guile-log to attach decent
parser tools in for anyone wanting to do parsing in a logic
programming environment. My next task have been to develop grep and
sed like tools using the parser framework. This is now successful and
I would ask for what features will be needed for it to be good enough.
This will be a kind of testbed to develop the parser further.

So, the background is that we designed a regular expressions like 
functionality via e.g. 

    (s-and m1 ... mn)  m1 ... and mn must both match mn is used for 
consuming
                       the stream
    s-and!             the same as s-and but only successes one time
    (s-seq m1 m2 ...)  m1 match then m2 match ...
    (s-or  m1 m2 ...)  m1 match else m2 match else
    s-or!              the same as s-or but successes only one time

    (s-not m)          if m does not match then consume one character
     s-not!            the same as s-not but will store the character
     pr-not            the same as s-not but prints the character to
                       stdout

    (s-tag tag)        (s-tag a)  matches a, (s-tag "a") matches a
     s-tag!            the same as s-tag, but stores the character
    pr-tag             the sa,e as s-tag, but prints the character to
     stdout

    (s-reg pat)        (s-ref ".") matches any character the pattern
                        should be a regular expression matching one
		        character
   
     s-reg! pr-reg     as above
  
With this basic regular expressions can be built.

Currently the grep and sed facilities comes in

1) (grep m expr #:key (a 0) (b 0) (c 0))

the standard output of the expr is captured by grep and if the matcher
m matches starting at line i, then lines:

   (- i (max a c)) -> (+ i (max b c))

will be printed

2) (par-grep m expr #:key (a 0) (b 0) (c 0) (d 0))

This grepper knows about scheme code and will work basically the same
but print the lines containing the form including the matched sentence
and d enclosing forms above. a b c is as before but now defines the
extra lines printed appart from the target form.

Example:

scheme@(guile-user)> (par-grep (s-tag x) (f1) #:d 0)
(a (b c "x  
     ")

scheme@(guile-user)> (par-grep (s-tag x) (f1) #:d 1)
(a (b c "x  
     ")
   (d e)
   (b (f g h)))

3) (sed m expr)

This will print out on std output all lines not matched by m, if m is
matched then that expr will make sure to print the correct way

An example matcher is 
  m := (s-tr a b) 

which will replace a by b

4) (par-sed m expr)
This is the same as sed but knows about scheme. There is nothing 
interesting
with this right know.

Have fun!




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: grep and sed in guile
  2013-08-25 21:32 grep and sed in guile Stefan Israelsson Tampe
@ 2013-08-27 13:27 ` Ludovic Courtès
  2013-08-27 15:24   ` Stefan Israelsson Tampe
  0 siblings, 1 reply; 3+ messages in thread
From: Ludovic Courtès @ 2013-08-27 13:27 UTC (permalink / raw)
  To: guile-user

> 3) (sed m expr)
> 
> This will print out on std output all lines not matched by m, if m is
> matched then that expr will make sure to print the correct way
> 
> An example matcher is 
>   m := (s-tr a b) 
> 
> which will replace a by b

FWIW Guix has a sed-like macro, called ‘substitute*’ [0].  Example:

  (substitute* "my-file.txt"
    (("^(.*) world$" _ first-word)
     (string-append "Hello (was: " first-word ") world\n")))

Ludo’.

[0] http://git.savannah.gnu.org/cgit/guix.git/tree/guix/build/utils.scm#n387




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: grep and sed in guile
  2013-08-27 13:27 ` Ludovic Courtès
@ 2013-08-27 15:24   ` Stefan Israelsson Tampe
  0 siblings, 0 replies; 3+ messages in thread
From: Stefan Israelsson Tampe @ 2013-08-27 15:24 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 1133 bytes --]

Nice!

Also scsh for guile can be used to get grep and sed functionality. The
interesting thing is to try to
design sed/grep like functionality that knows about scheme or C or whatever
format in some clever way.
Usually sad and grep is good enough, but it is an interesting task never
the less. My plan for know is to be able
to stream scheme source file and be able to sed them with an interface that
is close to syntax-parse. That would be
really cool.

Cheers


On Tue, Aug 27, 2013 at 3:27 PM, Ludovic Courtès <ludo@gnu.org> wrote:

> > 3) (sed m expr)
> >
> > This will print out on std output all lines not matched by m, if m is
> > matched then that expr will make sure to print the correct way
> >
> > An example matcher is
> >   m := (s-tr a b)
> >
> > which will replace a by b
>
> FWIW Guix has a sed-like macro, called ‘substitute*’ [0].  Example:
>
>   (substitute* "my-file.txt"
>     (("^(.*) world$" _ first-word)
>      (string-append "Hello (was: " first-word ") world\n")))
>
> Ludo’.
>
> [0]
> http://git.savannah.gnu.org/cgit/guix.git/tree/guix/build/utils.scm#n387
>
>
>

[-- Attachment #2: Type: text/html, Size: 1777 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-08-27 15:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-25 21:32 grep and sed in guile Stefan Israelsson Tampe
2013-08-27 13:27 ` Ludovic Courtès
2013-08-27 15:24   ` Stefan Israelsson Tampe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).