Rebol 3がいつでもオープンソースになることを記念して-今(?)、私はそれをいじりに戻っています。演習として、PARSE方言で独自のJSONパーサーを作成しようとしています。
Douglas Crockfordは、JSONの発見に対するRebolの影響を認めているので、簡単だと思いました。中かっこを角かっこに置き換えてすべてのコンマを取り除く以外に、文字列で単に使用することの障壁の1つは、Rebolのトークナイザーの文字列のように見えるものを使用するLOAD
という事実です。SET-WORD!
その後の違法な漂遊コロン:
{
"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
ですか?