4

It's cool that Rebol's PARSE dialect is generalized enough that it can do pattern matching and extraction on symbolic structures as well as on strings. Like this:

; match a single "a" character, followed by any number of "b" chars
>> string-rule: ["a" some "b"]

>> parse "abb" string-rule
== true
>> parse "aab" string-rule
== false

; look for a single apple symbol, followed by any number of bananas
>> block-rule: ['apple some 'banana]

>> parse [apple banana banana] block-rule
== true
>> parse [apple apple banana] block-rule
== false

But let's say I'm looking for a block containing an apple symbol, and then any number of character strings matching the string-rule:

; test 1
>> parse [apple "ab" "abbbbb"] mixed-rule
== true

; test 2
>> parse [apple "aaaa" "abb"] mixed-rule
== false

; test 3
>> parse [banana "abb" "abbb"] mixed-rule
== false

How would I formulate such a mixed-rule? Looking at the documentation it suggests that one can use INTO:

http://www.rebol.net/wiki/Parse_Project#INTO

The seemingly natural answer doesn't seem to work:

>> mixed-rule: ['apple some [string! into ["a" some "b"]]]

While it passes test 1 and correctly returns false for test 3, it incorrectly returns true in test 2. Is this my mistake or a bug in Rebol (I'm using r3 A111)?

1 Answers1

3

Steeve over on the REBOL3 forum suggests this:

only the second string is checked.
Should be:
    ['apple some [and string! into ["a" some "b" ]]]
Sunanda
  • 1,555
  • 7
  • 9
  • Hmmm... yes, this indeed works once you throw in an AND. Do you know if it's a workaround for a bug, or if there is some coherent reasoning for why you would need it there? – HostileFork says dont trust SE Jun 18 '11 at 16:23
  • Coherent reasoning: the general rule is that a successful match advances the matching position (to the next string in the above case). If you want to match the element twice (using STRING! and INTO), you need to use the AND keyword. – Ladislav Jan 25 '13 at 17:57