[REBOL] Re: Strange parse "bug"/behavior?
From: pwawood:gm:ail at: 17-Mar-2007 22:55
Hi Petr
I'm no expert but perhaps the following examples will help a little
> 1)
> rule: [any [copy char skip 1 (prin char)]]
> parse/all "this is a test string" rule
>
> result:
> this is a test string== true
>> alphaorspace: charset [#"a" - #"z" #"A" - #"Z" #" "]
== make bitset! #{
0000000001000000FEFFFF07FEFFFF0700000000000000000000000000000000
}
>> rule: [any [copy char alphaorspace (prin char) skip]]
== [any [copy char alphaorspace (prin char) skip]]
>> parse/all "this is a test string" rule
ti sats tig== false
My understanding of this rule is:
1. any tells parse to look for zero or more occurrences of the pattern
in the following block
2. the pattern in the block is a single alphaorspace followed by a skip.
3. if the pattern is found, parse will copy the character that matches
alphaorspace into the "variable" char and execute the rebol code
enclosed in parentheses.
4. It will then look for the next pattern to match that in the block
until it doesn't find one of it reaches the end of the string.
If we change the rule slightly to put the code after the skip, we get :
>> rule: [any [copy char alphaorspace skip (prin char)]]
== [any [copy char alphaorspace skip (prin char)]]
>> parse/all "this is a test string" rule
ti sats ti== false
My understanding of the subtle change, the "g" of string is not printed
is that with this rule parse reached the end of the string after the
g
so the skip didn't "match" so the code in parentheses was not
executed.
Putting skip before the copy char alphaorspace gives:
>> rule: [any [skip copy char alphaorspace (prin char)]]
== [any [skip copy char alphaorspace (prin char)]]
>> parse/all "this is a test string" rule
hsi etsrn== false
Probably what we expected.
If we only look for alpha characters instead of alpha or spaces, we get
the following result:
>> alpha: charset [#"a" - #"z" #"A" - #"Z"]
== make bitset! #{
0000000000000000FEFFFF07FEFFFF0700000000000000000000000000000000
}
>> rule: [any [copy char alpha (prin char) skip]]
== [any [copy char alpha (prin char) skip]]
>> parse/all "this is a test string" rule
ti== false
As I understand:
1. parse matches "th" and prints "t"
2. parse matches "is" and prints "i"
3. parse cannot match the space so stops.
> And my question is - what is going on here? Should not case 1) fail
> too?
> How is that string is properly parsed, just because there is "(prin
> char)" added, if integer 1 contained in rule is not correct?
I'm afraid I couldn't work out why case 1 worked either.
Regards
Peter