开发者

How to mix together string parsing and block parsing in the same rule?

开发者 https://www.devze.com 2023-03-13 15:53 出处:网络
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:

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 an开发者_如何转开发y 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)?


Steeve over on the REBOL3 forum suggests this:

only the second string is checked.
Should be:
    ['apple some [and string! into ["a" some "b" ]]]
0

精彩评论

暂无评论...
验证码 换一张
取 消