factor/basis/peg/search/search.factor

30 lines
799 B
Factor
Raw Normal View History

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