parse problem
[1/10] 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
[2/10] from: tomc:darkwing:uoregon at: 16-Nov-2003 21:08
Hi Patrick,
Is this more or less what you are loking for?
start-char: charset join {"} "{"
not-start-char: complement start-char
rule: [
some[
any not-start-char
[
["{" copy string to "}" "}"]
|
[{"} copy string to {"} {"}]
(print string)
]
]
]
parse mold code rule
Hello World!
test
test 2
alone in the line
Salut {you}
problem?
On Sun, 16 Nov 2003 [Patrick--Philipot--laposte--net] wrote:
[3/10] from: patrick:philipot:laposte at: 17-Nov-2003 9:25
Thanks Tom,
This is more than I had expected. Your code is much more simple and it works
!
Regards
Patrick
[4/10] from: g:santilli:tiscalinet:it at: 17-Nov-2003 10:27
Hi Patrick,
On Sunday, November 16, 2003, 10:39:36 PM, you wrote:
PPln> I have being struggling all day with a parse problem:
PPln> extracting all strings from a Rebol script.
Is there any reason why you need to parse the script as a string?
That is, instead of loading it and parse it as a block.
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amiga Group Italia sez. L'Aquila --- SOON: http://www.rebol.it/
[5/10] from: patrick:philipot:laposte at: 17-Nov-2003 12:02
Hi Gabriele,
No reason at all, and you are asking because it would be more simple ?
Regards
Patrick
[6/10] from: g:santilli:tiscalinet:it at: 17-Nov-2003 12:36
Hi Patrick,
On Monday, November 17, 2003, 12:02:47 PM, you wrote:
ppln> Hi Gabriele,
ppln> No reason at all, and you are asking because it would be more simple ?
Indeed. Let REBOL do the hard work for you.
Then, you can do something like (not tested):
parse script rule: [
any [
set str string! (probe str)
| into rule
| skip
]
]
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amiga Group Italia sez. L'Aquila --- SOON: http://www.rebol.it/
[7/10] from: nitsch-lists:netcologne at: 17-Nov-2003 13:41
Am Montag, 17. November 2003 12:36 schrieb Gabriele Santilli:
> Hi Patrick,
> On Monday, November 17, 2003, 12:02:47 PM, you wrote:
<<quoted lines omitted: 10>>
> ]
> ]
And have a look at load/next. You can position at the start of a string,
load/next and get [first-value rest-of-string]. so you can pick strings out
of a bigger string without loading all (which may fail because rest of the
bigger string is not rebol.
>> load/next find {Hey "jippy" hoo} {"}
== ["jippy" " hoo"]
> Regards,
> Gabriele.
-Volker
[8/10] from: patrick:philipot:laposte at: 17-Nov-2003 14:02
Thanks Gabriele
I tested it and it works.
However I don't understand the "| into rule" line which can be removed
without effect (in this example).
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: load code
parse script rule: [
any [
set str string! (probe str)
| into rule
| skip
]
]
Hello World!
test
{Hello "vous"}
test 2
alone in the line
{"strange" thing}
Salut {you}
problem?
Thanks again
Regards
Patrick
[9/10] from: g:santilli:tiscalinet:it at: 17-Nov-2003 14:34
Hi Patrick,
On Monday, November 17, 2003, 2:02:44 PM, you wrote:
ppln> However I don't understand the "| into rule" line which can be removed
ppln> without effect (in this example).
That's needed to recurse into nested blocks.
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amiga Group Italia sez. L'Aquila --- SOON: http://www.rebol.it/
[10/10] from: patrick:philipot:laposte at: 17-Nov-2003 15:58
Hi Volker,
This List is amazing !
I started this post with "I was struggling all day".
This "struggle" was indeed fun and I have enjoyed it a lot.
My code was working and I hesitated a long time before posting it.
I don't regret it.
I have learned a lot with you guys.
Regards
Patrick
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted