如何将变量设置为匹配Rebol PARSE中多个元素的规则的结果?

时间:2014-03-01 00:52:36

标签: parsing rebol rebol3

想象一个简单的例子,我们希望将字符串"0-5"转换为"012345"

这有效:

>> parse "0-5" [
    set a char!
    "-"
    set b char!

    (
        while [a <= b] [
            prin a
            a: a + 1
        ]
    )
]

结果是:

012345
true

但是,如果我想要更通用的内容,可以将"10-12"变成"101112"或更远?这不起作用:

>> parse "0-5" [
    set a [some char!]
    "-"
    set b [some char!]

    (
        a-int: to integer! a
        b-int: to integer! b
        while [a-int <= b-int] [
            prin to string! a-int
            a-int: a-int + 1
        ]
    )
]

它不起作用的原因是因为它不是set a [some char!]捕获一串字符而是错误:

>> parse "10" [set a [some char!] (print a)]
** Script error: PARSE - invalid rule or usage of rule: char
** Where: parse
** Near: parse "10" [set a [some char] (print a)]

作为奖励问题,为什么会失败并且使用skip仅返回第一个数字?

>> parse "10" [set a [some skip] (print a)]
1
== true

...当some skipsome char!在字符串上等效匹配时,因为字符串中唯一要跳过的是字符......?

2 个答案:

答案 0 :(得分:2)

在Parse方言中,SET仅捕获由子规则匹配的输入的第一个元素,而COPY捕获所有这些元素。因此,当您想从任意字符串中提取多个字符时,需要使用COPY!系列。

答案 1 :(得分:1)

这有效

digits: charset "0123456789"
parse "0-5" [
  copy a [some digits]
  "-"
  copy b [some digits]
  (
    a-int: to integer! a
    b-int: to integer! b
    while [a-int <= b-int] [
        prin to string! a-int
        a-int: a-int + 1
    ]
  )
]

012345 == true

类型? #“ - ”也给了char!

 parse "10" [some skip (print a)]

在新的控制台会话中不起作用,因为它没有价值,但是

parse "10" [copy a some skip (print a)]

作品