[REBOL] Re: Another question
From: joel:neely:fedex at: 15-Feb-2002 7:18
Hi, Andrew, Alex, and all,
I made the assumption that the list of tokens might be largish and
unsuitable for explicit inclusion in the parse rule itself...
Andrew Martin wrote:
> Alex wrote:
> > the original goal was to replace [red] with an escape character
> > followed by "[31m" for example), but I was hoping to go through
> > the various color tags ([red], [green], [blue] etc.) and search
> > through the remainder of the text after finding a tag regexp-style
> > for the next tag. That way, no time would be wasted re-scanning
> > through the entire string to find new codes
>
Here's my attempt...
8<------------------------------------------------------------
srcrep: make object! [
pairs: [
"red" "pink"
"purple" "lavendar"
"brown" "yellow"
"navy" "periwinkle"
"black" "gray"
]
token: other: ""
rule: [
any [[
began: "[" copy token to "]" "]" ended: (
if found? other: select/skip pairs token 2 [
change/part began first other ended
:ended
]
)]
|
skip
]
]
run: func [s [string!]] [
parse/all s rule
]
]
8<------------------------------------------------------------
So that (obviously) one could create more of these by saying
something like:
color-fixer: make srcrep [
pairs: reduce [
"red" "^[[31m"
...
]
]
It does this (with my original demo pairs):
>> foo: {
{ Once upon a time, I had:
{ some [black] pants,
{ some [brown] shoes,
{ a [navy] coat,
{ a [purple] shirt, and
{ a [red] tie.
{ Then I left them
{ too close to the window
{ on a sunny day!
{ }
== {
Once upon a time, I had:
some [black] pants,
some [brown] shoes,
a [navy] coat,
a [purple] shirt, and
a [red] tie.
Then I left...
>> srcrep/run foo
== true
>> print foo
Once upon a time, I had:
some gray pants,
some yellow shoes,
a periwinkle coat,
a lavendar shirt, and
a pink tie.
Then I left them
too close to the window
on a sunny day!
But there's still one puzzle I haven't figured out (probably I'm
just overlooking something silly, as I've only had one cup of
coffee so far this morning!)
>> baz: {[red] [purple] [brown] [navy] [black] [this is a block]
[red] [23] [navy]}
== {[red] [purple] [brown] [navy] [black] [this is a block] [red]
[23] [navy]}
Just a string with a mixture of expected and non-expected values inside
the
square brackets...
>> mw: make srcrep [pairs: ["this is a block" "gone!" "23" "42"]]
>> mw/run baz
== true
>> baz
== {[red] [purple] [brown] [navy] [black] gone! [red] [23] [navy]}
>> mw/run baz
== true
>> baz
== {[red] [purple] [brown] [navy] [black] gone! [red] 42 [navy]}
Notice that I had to apply the modified wordlist twice to get all of
the tokens found and replaced!?!? I expected it to do all of them, as
in:
>> srcrep/run baz
== true
>> baz
== {pink lavendar yellow periwinkle gray gone! pink 42 periwinkle}
with the remainder, or
>> baz: {[red] [purple] [brown] [navy] [black] [this is a block]
[red] [23] [navy]}
== {[red] [purple] [brown] [navy] [black] [this is a block] [red]
[23] [navy]}
>> srcrep/run baz
== true
>> baz
== {pink lavendar yellow periwinkle gray [this is a block] pink [23]
periwinkle}
with the original mixed list.
Suggestions anyone? What am I overlooking?
-jn-
--
; sub REBOL {}; sub head ($) {@_[0]}
REBOL []
# despam: func [e] [replace replace/all e ":" "." "#" "@"]
; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"}
print head reverse despam "moc:xedef#yleen:leoj" ;