[REBOL] Re: Proper usage of "thru" in parse
From: edoconnor:gmai:l at: 20-Jul-2006 12:00
The parse keywords to and thru are a bit tricky -- they zoom through
the string until they find a match or fail.
What you need to do is step through the input one character at a time,
testing for a match at each step. In this case, avoid using to and
thru -- use skip instead.
parse/all text [any [
<normal> copy label to <end> (print label) |
<highlight> copy label to <end> (print label) |
skip
]
to end
]
The english statement of the above would be:
Go through all of the text, including spaces.
Repeat the following rule exhaustively through the text. At each character:
1. if match the tag! value <normal> copy the text until the <end>
tag and print it
2. if match the tag! value <highlight> copy the text until the <end>
tag and print it
3. no match, so skip to the next character in text string and return to step 1
Go to the end of the text string, which returns "true" to the general
parse statement.
Be careful about the mistake of surrounding sub-rules for parse in
curly braces. They are sub-blocks and need to be enclosed in [].
I usually find it best to keep the parse rules as basic as possible,
and then refine/condense them if I'm confident they work as intended.
My slightly bloated example above could be streamlined to:
end-tag: [to <end>]
rule: [any [
<normal> copy i end-tag (print i) |
<highlight> copy i end-tag (print i) |
skip
]
to end
]
>> parse/all text rule
item1
item2
item3
item4
== true
Regards,
Ed
On 7/20/06, Mark Chang <mediquip8-yahoo.com> wrote: