r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Parse] Discussion of PARSE dialect

BrianH
7-Jun-2007
[1909x4]
Don't forget continue.
First tip: You don't have to bind the body - it just uses the existing 
binding of the word.
Netx tip: What native does forskip decompose down to? Try using that 
first.
; Try against this, the forskip code with the skip part taken out
forall: func [
    "Evaluates a block for every value in a series."
    [catch throw]

    'word [word!] {Word set to each position in series and changed as 
    a result}
    body [block!] "Block to evaluate each time"
    /local orig result
][
    if not any [
        series? get word
        port? get word

    ] [throw make error! {forall expected word argument to refer to a 
    series or port!}]
    orig: get word
    while [any [not tail? get word (set word orig false)]] [
        set/any 'result do body
        set word next get word
        get/any 'result
    ]
]
Rebolek
7-Jun-2007
[1913x2]
BrianH: I know it can be done using while loop in forskip, it was 
more an experiment on using parse as foreach/forall kind of function.
to see how fast it can be
BrianH
7-Jun-2007
[1915x3]
Well, if you can figure out how to make it handle break and continue, 
let us know.
Or for that matter, ports.
If you want to test the speed of parse, replace the any-type! with 
a skip - the forall you are comparing it to doesn't do that test.
Chris
7-Jun-2007
[1918]
What is 'continue?
BrianH
7-Jun-2007
[1919]
Sort of like the opposite of break. It breaks one iteration of a 
loop and continues at the top of the next iteration. Many languages 
have it, as does REBOL 3.
Gabriele
8-Jun-2007
[1920]
notice that, forall was initially done that way, and converted to 
use forskip for simplicity (ie. avoiding having the same code twice 
in the source)
Rebolek
8-Jun-2007
[1921]
I tried to enclose parse in loop 1 [] and it seems to handle break. 
I guess you'll probably prove me wrong, Brian :)

parall: func [
	'word body
	/loc data
][
	loop 1 [
		data: get :word
		parse data compose/deep [
			some [(to set-word! word) skip (to paren! [do body])]
		]
	]
]

re: continue - this is not r3 ;)


>> n: [1 2 3 4 5] parall n [if n/1 = 4 [break/return "break"] if 
n/1 > 4 [print "bad"]]
== "break"
Gabriele
8-Jun-2007
[1922x4]
good :) now... if Ladislav was here he'd probably find at least ten 
wrong things in that function. ;)
you need to add [throw] for example, and return the last return value 
of the body
but... as long as we're not replacing forall... this could be a very 
fast alternative to use in cases when you don't need all the features...
and actually... i'd call parse directly in that case ;)
Rebolek
8-Jun-2007
[1926]
forall replacement not...an experiment in using parse for maybe not 
obvious purposes - yes :)
Volker
8-Jun-2007
[1927x2]
You could make something new. like this:
 parall [p: set x integer! set y integer!] [?? p ?? x ?? y]
parall [p: set x integer! set y integer!]  DATA [?? p ?? x ?? y] 
;..
Oldes
10-Jun-2007
[1929]
To be able escape from infinite loops use () somewhere in the parse 
rules as for example:

 >> parse "abc" [any [()]]
 *** YOU CAN PRESS [ESC] NOW TO STOP THE LOOP ***

>> parse "abc" [any []]
*** HANGS REBOL ***
BrianH
11-Jun-2007
[1930]
Cool!
PeterWood
11-Jun-2007
[1931]
Oldes: does using () inside a parse loop slow things down? I'm wary 
of () as they do seem to slow the interpreter down in many cases.
Rebolek
11-Jun-2007
[1932]
Peter it does slow interpreter down, it's better to use this trick 
when designing your rules. If you're happy with them, remove the 
parens to make your rule faster.
Steeve
27-Jun-2007
[1933x3]
What about a new word “all” which would allow  to parse several successives 
conditions whatever their order  ?
parse [a: 1 b: 2] [all ['a: integer! | 'b: integer!]] would be true 
only if it exists successivly [a: integer!] and [b: integer! ] whatever 
their order
need it
btiffin
27-Jun-2007
[1936]
Umm...I'm not really a parser but


parse [a: 1 b: 2] [set-word! integer! set-word! integer!] returns 
true.
Steeve
27-Jun-2007
[1937x5]
yeah but what ? if the entry is [b:2 a:  1] instead
hum sorry, it works too
but if the entry is more complex like [a: 1 huge tight  b: 1] and 
that i want parse all this values in any order ?
and in your example , you don't test that we must encounter 'a and 
'b
i give another one example , more simple

i want to parse the serie "ABC" but it could be the serie "BCA" , 
"CBA" , "ACB" ... any combination

so if i could write parse "ABC" [all ["A" | "B" [ "C" ]]  , it should 
be more simple
btiffin
27-Jun-2007
[1942]
Again, I'm not a parser, but parse's support of alternates with some 
and any covers these cases no?
Steeve
27-Jun-2007
[1943x2]
no
if you write  parse "AB" [some ["A" |  "B" | "C"]]
btiffin
27-Jun-2007
[1945]
parse "ABC" [ 0 1 "A" | 0 1 "B" | 0 1 "C"] ?
Steeve
27-Jun-2007
[1946]
you get a true answer, but that not the case
btiffin
27-Jun-2007
[1947x2]
I think I'm clue'in to what you mean.  I'll leave this to the pros... 
 :)
like my use of 0 1 can be opt...  :)
Steeve
27-Jun-2007
[1949x2]
anyway, you're example just don't work ^^
*anyway your example just doesn't work (fewwww)
btiffin
27-Jun-2007
[1951]
Yep.  I'm out.  Not experienced enough to have piped up...  ;)
Steeve
27-Jun-2007
[1952x2]
and what is your mater ? ;-)
we can talk of rebcode if you want :-)
btiffin
27-Jun-2007
[1954]
Thinking about this one, you'd have to repeat all the alternatives. 
 Defeating the purpose...but I'll bet Chris or Geomol or Anton or... 
will have a workable solution posted before ya know it.
Steeve
27-Jun-2007
[1955]
i h'ave many alternatives (all comsuming lot of code)  that's not 
the point, i just think it could be more elegant to have such a command
btiffin
27-Jun-2007
[1956]
Maybe we could hold side bets on which rebol will come up with the 
solution first...I'd put money down on Oldes or Volker giving you 
a workable  :)
Steeve
27-Jun-2007
[1957]
if you say so, i  tried many alternatives, but they are all uggly
Brock
27-Jun-2007
[1958]
try this... parse "AB" [some ["A" |  "B" | "C"] | none 
]