[REBOL] Re: Big parse rules
From: nitsch-lists:netcologne at: 23-Jul-2003 21:13
Hi Andrew
Am Samstag, 19. Juli 2003 12:02 schrieb A J Martin:
> Volker wrote:
[snip]
> > without it, you could put everything in a context and export only 'Table^
>
> with 'set. or do
>
> > table^: ctx-table/table^
> > parse string [..]
> >
> > context[
> > Table_Printable^: exclude Printable charset #"|"
> > ;..
> > set 'Table^[
> > ; ..
> > ]
> > ]
>
> Thanks, Volker! That would work, except that I wanted it to eventually be
> recursive. With an object, I can't yet see how this could be done (perhaps
> copying to a stack/block?).
>
Played a bit with the stack-idea. Actually i played with it a while before,
coming up with a stack which took a block of words and saved the words and the
contents. so one could do
rule: [
(push [a b c])
rules rules rules
(pop)
]
lost it somewhere.
Started rewriting it, and wrote a bit more.
Here is the story:
First, a stack like described above.
stack: copy []
push: func [word-block] [
insert stack reduce ['set word-block reduce word-block]
]
pop: func [] [do/next stack remove/part stack 3]
then i realized i don't like typing, so the push-pop should be automatic,
and creating real locals to.
stack: copy []
rule-with-locals: func [locals rule] [
insert/only rule to-paren compose/only [insert stack reduce (locals)]
append/only rule to-paren compose/only [
set (locals) first stack remove stack]
;cute hack to create new context and bind block to it:
use locals reduce [ 'set locals none rule]
]
;specially constructed test, see console-session:
rule: [
here: copy part any ["(" rule | ")" break | skip] (? here)
]
string: "very-left ( left ( middle ) right ) very-right )"
{console:
>> parse string rule ;subrule overwrites superrules 'here
HERE is a string of value: "middle ) right ) very-right )"
HERE is a string of value: "middle ) right ) very-right )"
HERE is a string of value: "middle ) right ) very-right )"
== true
>> parse string rule-with-locals [here] rule ; this works :) <<<<<<<<<
HERE is a string of value: "middle ) right ) very-right )"
HERE is a string of value: "left ( middle ) right ) very-right )"
HERE is a string of value: "very-left ( left ( middle ) right ) very-right )"
== true
}
Then i thought, eventually the locals should be in a context, instead of in a
hidden use, played with contexts and discovered:
>> ctx: context [val: #original]
>> old: third ctx
== [val: #original]
>> ctx/val: #modified
== #modified
>> ctx/val
== #modified
>> do old
== #original
>> ctx/val
== #original
cute :) but does not work with
>> ctx: context [val: 'original]
>> ctx/val
== original
because it dequotes 'original
>> third ctx
== [val: original]
>> do third ctx
** Script Error: original has no value
** Near: val: original
End of part 1 / parse-with-locals.
Will the great coders extend this further?
Login when the great coders say: "Errrmmmm..."
So, what exactly do you want? ;)
[snip]
> Andrew J Martin
> ICQ: 26227169 http://www.rebol.it/Valley/ http://Valley.150m.com/
> -><-
Cheers,
-Volker