factor/basis/interpolate/interpolate.factor

41 lines
1.1 KiB
Factor
Raw Normal View History

2008-10-01 19:15:41 -04:00
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: io kernel macros make multiline namespaces parser
present sequences strings splitting fry accessors ;
2008-10-01 19:15:41 -04:00
IN: interpolate
TUPLE: interpolate-var name ;
: (parse-interpolate) ( string -- )
[
"${" split1-slice [ >string , ] [
[
"}" split1-slice
[ >string interpolate-var boa , ]
[ (parse-interpolate) ] bi*
] when*
] bi*
] unless-empty ;
: parse-interpolate ( string -- seq )
[ (parse-interpolate) ] { } make ;
2008-10-01 19:15:41 -04:00
MACRO: interpolate ( string -- )
parse-interpolate [
dup interpolate-var?
[ name>> '[ _ get present write ] ]
[ '[ _ write ] ]
if
] map [ ] join ;
2008-10-01 19:15:41 -04:00
: interpolate-locals ( string -- quot )
parse-interpolate [
dup interpolate-var?
[ name>> search '[ _ present write ] ]
[ '[ _ write ] ]
if
] map [ ] join ;
2008-10-01 19:15:41 -04:00
: I[ "]I" parse-multiline-string
interpolate-locals parsed \ call parsed ; parsing