2007-09-20 18:09:08 -04:00
|
|
|
! Copyright (C) 2006 Chris Double.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2013-03-24 20:29:48 -04:00
|
|
|
USING: combinators continuations io io.streams.string kernel
|
|
|
|
math memoize namespaces peg sequences strings ;
|
2008-01-06 12:30:23 -05:00
|
|
|
IN: peg.search
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2013-03-24 20:29:48 -04:00
|
|
|
: stream-tree-write ( object stream -- )
|
2013-01-03 21:33:20 -05:00
|
|
|
{
|
2013-03-24 20:29:48 -04:00
|
|
|
{ [ over number? ] [ stream-write1 ] }
|
|
|
|
{ [ over string? ] [ stream-write ] }
|
|
|
|
{ [ over sequence? ] [ [ stream-tree-write ] curry each ] }
|
2016-11-27 01:01:03 -05:00
|
|
|
[ stream-write ]
|
2013-01-03 21:33:20 -05:00
|
|
|
} cond ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2013-03-24 20:29:48 -04:00
|
|
|
: tree-write ( object -- )
|
|
|
|
output-stream get stream-tree-write ;
|
|
|
|
|
2007-12-19 23:06:21 -05:00
|
|
|
MEMO: any-char-parser ( -- parser )
|
2013-01-03 21:33:20 -05:00
|
|
|
[ drop t ] satisfy ;
|
2007-12-19 23:06:21 -05:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: search ( string parser -- seq )
|
2013-01-03 21:33:20 -05:00
|
|
|
any-char-parser [ drop f ] action 2choice repeat0
|
|
|
|
[ parse sift ] [ 3drop { } ] recover ;
|
2007-12-19 23:06:21 -05:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: (replace) ( string parser -- seq )
|
2013-01-03 21:33:20 -05:00
|
|
|
any-char-parser 2choice repeat0 parse sift ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: replace ( string parser -- result )
|
2013-03-24 20:29:48 -04:00
|
|
|
[ (replace) tree-write ] with-string-writer ;
|