为了纪念 Rebol 3 随时开放源代码(?),我又开始搞砸它了。作为一个练习,我正在尝试用 PARSE 方言编写我自己的 JSON 解析器。
由于 Douglas Crockford将 Rebol 的影响归功于他对 JSON 的发现,我认为这很容易。除了用括号替换大括号并去掉所有这些逗号之外,仅LOAD
在字符串上使用的障碍之一是,当他们想要做等效的 aSET-WORD!
时,他们使用看起来像 Rebol 标记器的字符串的东西,后面有一个非法的冒号:
{
"key one": {
"summary": "This is the string content for key one's summary",
"value": 7
},
"key two": {
"summary": "Another actually string, not supposed to be a 'symbol'",
"value": 100
}
}
基本上我想找到所有类似的情况"foo bar":
并将它们转换为foo-bar:
同时留下不单独冒号的匹配引号对。
当我在 PARSE 中解决这个问题时(我在原则上理解得很好,但仍然没有使用太多)出现了几个问题。但主要是,当您可以转入代码并从解析器下修改系列时的承诺条件是什么……特别是在 Rebol 3 中?更一般地说,它是“适合工作的工具”吗?
这是我尝试过的规则,似乎适用于这部分任务:
any [
; require a matched pair of quotes & capture series positions before
; and after the first quote, and before the last quote
to {"} beforePos: skip startPos: to {"} endPos: skip
; optional colon next (if not there the rest of the next rule is skipped)
opt [
{:}
; if we got to this part of the optional match rule, there was a colon.
; we escape to code changing spaces to dashes in the range we captured
(
setWordString: copy/part startPos endPos
replace/all setWordString space "-"
change startPos setWordString
)
; break back out into the parse dialect, and instead of changing the
; series length out from under the parser we jump it back to the position
; before that first quote that we saw
:beforePos
; Now do the removals through a match rule. We know they are there and
; this will not cause this "colon-case" match rule to fail...because we
; saw those two quotes on the first time through!
remove [{"}] to {"} remove [{"}]
]
]
可以吗?change startPos setWordString
开放代码中是否有可能破坏外部解析......如果不是在这种情况下,那么在一些微妙的不同?
与往常一样,任何说教的“它更干净/更短/更好”的建议都会受到赞赏。
PS为什么没有replace/all/part
?