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

落花浮王杯 提交于 2019-12-07 09:51:25

问题


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:


Steeve over on the REBOL3 forum suggests this:

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


来源:https://stackoverflow.com/questions/6392533/how-to-mix-together-string-parsing-and-block-parsing-in-the-same-rule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!