[REBOL] parse problem
From: Patrick::Philipot::laposte::net at: 16-Nov-2003 22:39
Hi List
I have being struggling all day with a parse problem: extracting all strings from a Rebol
script.
It may seem trivial at the first look.
However as you know strings are delimited by a pair of (") or by ({) and (}) and inclusions
are allowed.
{Hello "vous"} and "Salut {you}" are valid strings.
I have produced several solutions that were ok with my test data by failed with a real
script.
Here is my final draft which passed all my tests.
However it may not be perfect or simply wrong.
Here is how it is supposed to work:
rule1 is for "string" delimited by (")
rule2 is for {string} delimited by ({) and (})
rule1 AND rule2 are applied.
If both passed, I keep the one with the nearest match and the parsing position is adjusted.
otherwise rule1 is applied
otherwise rule2 is applied
I will be glad if some parse gurus may have a critical look...
8<-----------------------------------------------------
code: [
Rebol []
; This is a program
str: "Hello World!"
print "test"
str2: {Hello "vous"}
print {test 2}
print
{alone in the line}
print {"strange" thing}
str3: "Salut {you}"
print {problem?}
]
script: mold code
strings: copy []
rule1: [thru #"^"" mark1: copy text1 to #"^"" skip end1:]
rule2: [thru #"{" mark2: copy text2 to #"}" skip end2:]
store: [either (index? mark1) > (index? mark2)
[
append strings text2
restart: end2
][
append strings text1
restart: end1
]
] ; store
parse script [ any [start: rule1 :start rule2 (do store) :restart |
rule1 (append strings text1)|
rule2 (append strings text2)] to end]
foreach s strings [print s]
8<-----------------------------------------------------
Regards
Patrick