当可能存在嵌入块时如何使用load / next / error?

时间:2013-07-05 13:23:35

标签: rebol rebol3

transcode / next / error返回第一个加载的值,以及值后面的位置,这样你就可以去加载下一个值:

>> transcode/next/error to binary! " a b c "
== [a #{2062206320}]

或者您收到错误,错误值后面的位置:

>> transcode/next/error to binary! " 1a b c " 
== [make error! [                             
    code: 200                             
    type: 'Syntax                         
    id: 'invalid                          
    arg1: "integer"                       
    arg2: "1a"                            
    arg3: none                            
    near: "(line 1) 1a b c "              
    where: [transcode]                    
] #{2062206320}]                          

但是如果要加载的值是块,并且块内有错误,那么

  • 你收到错误!
  • 错误值后的位置,
  • 块内的好值被丢弃:

喜欢这里

>> transcode/next/error to binary! "[ a b 1c ]"
== [make error! [
    code: 200
    type: 'Syntax
    id: 'invalid
    arg1: "integer"
    arg2: "1c"
    arg3: none
    near: "(line 1) [ a b 1c ]"
    where: [transcode]
] #{205D}]

我当前的[*]解决方案是,纠正输入字符串,并从最后一个位置重新启动。这样我就可以重新加载整个块,一次性完成它。

有没有更好的方法来解决这个问题?

[*]见https://github.com/IngoHohmann/rebol3-tools/blob/master/load-all.r3

1 个答案:

答案 0 :(得分:1)

假设您有一个调用'transcode的每个实例的循环,您可以在转码前使用自己的机制来处理块字符[]()。然后你负责仲裁有效的块,但由于你的目标是加载任何数据,你可能需要处理不平衡的块分隔符。

你所需要的只是一种像转码一样工作的机制,但是对于块分隔符:

block-transcode: func [source [binary!] /local symbol][
    if parse source [
        any space  ; space should be defined
        copy symbol [#"[" | #"]" | #"(" | #")"]
        source: to end
    ][
        reduce [symbol source]
    ]
]

当然,这不会检查路径中的parens中的块,但它是一个开始......