|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rule split ( string separator ) |
|
{ |
|
local result ; |
|
local s = $(string) ; |
|
|
|
|
|
local match = 1 ; |
|
while $(match) |
|
{ |
|
match = [ MATCH ^(.*)($(separator))(.*) : $(s) ] ; |
|
if $(match) |
|
{ |
|
match += "" ; |
|
result = $(match[3]) $(result) ; |
|
s = $(match[1]) ; |
|
} |
|
} |
|
|
|
|
|
|
|
return $(s) $(result) ; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
rule split-list ( list * : separator ) |
|
{ |
|
local result ; |
|
for s in $(list) |
|
{ |
|
result += [ split $(s) $(separator) ] ; |
|
} |
|
return $(result) ; |
|
} |
|
|
|
|
|
|
|
|
|
rule match ( pattern : string : indices * ) |
|
{ |
|
indices ?= 1 2 3 4 5 6 7 8 9 ; |
|
local x = [ MATCH $(pattern) : $(string) ] ; |
|
return $(x[$(indices)]) ; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rule transform ( list * : pattern : indices * ) |
|
{ |
|
indices ?= 1 ; |
|
local result ; |
|
for local e in $(list) |
|
{ |
|
local m = [ MATCH $(pattern) : $(e) ] ; |
|
if $(m) |
|
{ |
|
result += $(m[$(indices)]) ; |
|
} |
|
} |
|
return $(result) ; |
|
} |
|
|
|
NATIVE_RULE regex : transform ; |
|
|
|
|
|
|
|
|
|
|
|
rule escape ( string : symbols : escape-symbol ) |
|
{ |
|
local result = "" ; |
|
local m = 1 ; |
|
while $(m) |
|
{ |
|
m = [ MATCH ^([^$(symbols)]*)([$(symbols)])(.*) : $(string) ] ; |
|
if $(m) |
|
{ |
|
m += "" ; |
|
result = "$(result)$(m[1])$(escape-symbol)$(m[2])" ; |
|
string = $(m[3]) ; |
|
} |
|
} |
|
string ?= "" ; |
|
result = "$(result)$(string)" ; |
|
return $(result) ; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
rule replace ( |
|
string |
|
match |
|
replacement |
|
) |
|
{ |
|
local result = "" ; |
|
local parts = 1 ; |
|
while $(parts) |
|
{ |
|
parts = [ MATCH ^(.*)($(match))(.*) : $(string) ] ; |
|
if $(parts) |
|
{ |
|
parts += "" ; |
|
result = "$(replacement)$(parts[3])$(result)" ; |
|
string = $(parts[1]) ; |
|
} |
|
} |
|
string ?= "" ; |
|
result = "$(string)$(result)" ; |
|
return $(result) ; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rule replace-list ( list * : match : replacement ) |
|
{ |
|
local result ; |
|
for local e in $(list) |
|
{ |
|
result += [ replace $(e) $(match) $(replacement) ] ; |
|
} |
|
return $(result) ; |
|
} |
|
|
|
|
|
rule __test__ ( ) |
|
{ |
|
import assert ; |
|
|
|
assert.result a b c : split "a/b/c" / ; |
|
assert.result "" a b c : split "/a/b/c" / ; |
|
assert.result "" "" a b c : split "//a/b/c" / ; |
|
assert.result "" a "" b c : split "/a//b/c" / ; |
|
assert.result "" a "" b c "" : split "/a//b/c/" / ; |
|
assert.result "" a "" b c "" "" : split "/a//b/c//" / ; |
|
|
|
assert.result a c b d |
|
: match (.)(.)(.)(.) : abcd : 1 3 2 4 ; |
|
|
|
assert.result a b c d |
|
: match (.)(.)(.)(.) : abcd ; |
|
|
|
assert.result ababab cddc |
|
: match ((ab)*)([cd]+) : abababcddc : 1 3 ; |
|
|
|
assert.result a.h c.h |
|
: transform <a.h> \"b.h\" <c.h> : <(.*)> ; |
|
|
|
assert.result a.h b.h c.h |
|
: transform <a.h> \"b.h\" <c.h> : <([^>]*)>|\"([^\"]*)\" : 1 2 ; |
|
|
|
assert.result "^<?xml version=\"1.0\"^>" |
|
: escape "<?xml version=\"1.0\">" : "&|()<>^" : "^" ; |
|
|
|
assert.result "<?xml version=\\\"1.0\\\">" |
|
: escape "<?xml version=\"1.0\">" : "\\\"" : "\\" ; |
|
|
|
assert.result "string string " : replace "string string " " " " " ; |
|
assert.result " string string" : replace " string string" " " " " ; |
|
assert.result "string string" : replace "string string" " " " " ; |
|
assert.result "-" : replace "&" "&" "-" ; |
|
|
|
assert.result "-" "a-b" : replace-list "&" "a&b" : "&" : "-" ; |
|
} |
|
|