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

World: r3wp

[Parse] Discussion of PARSE dialect

BrianH
30-Sep-2009
[4290x2]
>> parse "(()" [(p: 0) any [#"(" (++ p) | #")" if (1 <= -- p) then 
none | break] if (p = 0)]
== false

>> parse "(())" [(p: 0) any [#"(" (++ p) | #")" if (1 <= -- p) then 
none | break] if (p = 0)]
== true

>> parse "())" [(p: 0) any [#"(" (++ p) | #")" if (1 <= -- p) then 
none | break] if (p = 0)]
== false

>> parse ")(" [(p: 0) any [#"(" (++ p) | #")" if (1 <= -- p) then 
none | break] if (p = 0)]
== false
This kind of thing is why I suggested IF (as CHECK) 5 years ago :)
Steeve
30-Sep-2009
[4292x2]
to optimize a little, i would prevent from useless entering in the 
any loop:

[#"(" (p: 1) some  [#"(" (++ p) | #")" if (1 <= -- p) then none | 
break] if (p = 0)]
but now, the  (1 <= -- p) condition is false i mean
BrianH
30-Sep-2009
[4294x2]
[(p: 0) some [#"(" (++ p) | #")" if (1 <= -- p) then none | break] 
if (p = 0)]

Handles the empty string. Given the | break alternate, ANY and SOME 
are equivalent.
Yours would work, but wouldn't recognize an empty string.
Steeve
30-Sep-2009
[4296x2]
still, aren't the condition (1 <= -- p) wrong ?
-- decrements but returns previous value
BrianH
30-Sep-2009
[4298]
You want the condition to be false, so the break is chosen.
Steeve
30-Sep-2009
[4299]
if p = 1 then it must break
BrianH
30-Sep-2009
[4300x2]
No, you want to break when p is 0 or less.
Even in your version.
Steeve
30-Sep-2009
[4302]
wait...
BrianH
30-Sep-2009
[4303x2]
The | break is not only the else for the if (), it is also the alternate 
chosen if #"(" or #")" are not matched.
That is what THEN gives us: the ability to overload alternates.
Steeve
30-Sep-2009
[4305]
oh ok, it's not exiting the loop for such case [()()()()]
BrianH
30-Sep-2009
[4306]
Right :)
Steeve
30-Sep-2009
[4307]
Am ok now
BrianH
30-Sep-2009
[4308]
But we *want* to exit the loop in this case: ")("
Steeve
30-Sep-2009
[4309]
yep
BrianH
30-Sep-2009
[4310]
I find myself liking THEN quite a bit :)
Steeve
30-Sep-2009
[4311x3]
But we still have a problem with break not being able encapable in 
a block.
We can't construct reusable rules with such logic.
i mean we can't push the part [if (1 <= -- p) then none | break] 
in a separated rule
we need a parameter for break
BrianH
30-Sep-2009
[4314]
It's tricky, true. There was either a proposal or a bug report to 
make BREAK break out of the closest enclosing ANY or SOME...
Steeve
30-Sep-2009
[4315x3]
break level
break 0
break -1
BrianH
30-Sep-2009
[4318]
Counted back, not negative.
Steeve
30-Sep-2009
[4319x2]
if you prefer
time for a new proposal Brian
Maxim
30-Sep-2009
[4321]
'ROLLBACK anyone  ;-)
BrianH
30-Sep-2009
[4322]
Done, as n BREAK :)
Maxim
30-Sep-2009
[4323]
funny I suggested it 2 hours ago, and you guys end up with a path 
with a use case implementing a simple rule  :-)
BrianH
30-Sep-2009
[4324]
I had to figure out how to make it fit in. Done. Off to dinner.
Pekr
1-Oct-2009
[4325x6]
guys, what have you created for the simple recursive replacement 
of paren: [#"(" paren #")"] looks like regexp hell in comparison 
...
Not having proper recursion support is big downside of REBOL.
>> parse d: "abc" [change skip 123]
>> d
== "123bc"
Isn't it weird? I would expect a123
hmm, maybe it is correct, just need a bit more of thought. Skip is 
not skipping "a", it is "matching" "a", hence defining, what should 
be replaced ...
The stack limit is so lame, almost unusable:

REBOL level recursion:

>> cnt: 0 recursion: does [++ cnt recursion] recursion
** Internal error: stack overflow

>> cnt
== 4004

Parse level recursion:

>> cnt: 0 rule: [(++ cnt) rule] parse "123" [some rule]
** Internal error: internal limit reached: parse

>> cnt
== 512
Henrik
1-Oct-2009
[4331]
I never bumped into the stack limit in R2 with parse. Is it smaller 
in R3?
Pekr
1-Oct-2009
[4332x3]
will try ...
no, it is exactly the same ...
But function example gives me == 14265 .... so - is function recursion 
stack lower in R3?
Henrik
1-Oct-2009
[4335]
I think it varies with the OS platform used. I get 1335 in OSX.
Graham
1-Oct-2009
[4336]
sounds plenty for parse
Ladislav
1-Oct-2009
[4337]
Graham: your "plenty" and my "plenty" are not the same, as it looks 
;-)
Henrik
1-Oct-2009
[4338]
But can it be controlled? I figured this is something controlled 
by the OS.
Ladislav
1-Oct-2009
[4339]
Pekr: the original rule was a little bit more complicated: paren: 
[any [#"(" paren #")"]], but still much simpler, than the above hell. 
Which is nothing compared to a trial to do the same in the case of:

bracket: [any [#"(" bracket #")" | #"[" bracket #"]"]]