Paste: interpolate

Author: slava
Mode: factor
Date: Tue, 30 Sep 2008 00:16:55
Plain Text |
MACRO: interpolate ( string -- )
[EBNF
var = "${" [^}]+ "}" => [[ second >string [ get present write ] curry ]]
text = [^$]+ => [[ >string [ write ] curry ]]
interpolate = (var|text)* => [[ [ ] join ]]
EBNF] ;

! Example

( scratchpad ) "a" "x" set
( scratchpad ) "hello" "y" set
( scratchpad ) "test ${x} and ${y}\n" interpolate 
test a and hello

Annotation: locals

Author: slava
Mode: factor
Date: Tue, 30 Sep 2008 00:23:02
Plain Text |
EBNF: interpolate
var = "${" [^}]+ "}" => [[ [ second >string search , [ present write ] % ] [ ] make ]]
text = [^$]+ => [[ [ >string , [ write ] % ] [ ] make ]]
interpolate = (var|text)* => [[ [ ] join ]]
;EBNF

: I[ "]I" parse-multiline-string interpolate parsed \ call parsed ; parsing

! Example
:: foo ( a b -- ) I[ Hello ${a} and ${b}]I ;

"foo" "bar" foo

Annotation: test

Author: test
Mode: factor
Date: Tue, 30 Sep 2008 04:17:36
Plain Text |
test

New Annotation

Summary:
Author:
Mode:
Body: