views:

59

answers:

2

simple parse example:

ab: charset "ab"
parse "aaa" [some ab]  
; == true

if i wanted one-liner (define ab in place), how do i do it?

parse "aaa" [some [charset "ab"]]
; ** Script Error: Invalid argument: ?function?

parse "aaa" [some (charset "ab")]
; (INTERPRETER HANGS UP)

i use REBOL 2.7.7.4.2

UPDATE

in rebol 3:

parse "aaa" [some (charset "ab")]
; == false
+1  A: 

You're looking for 'compose

>> parse "aaa" compose [ some (charset [#"a" #"b"] ) ]
== true
Graham Chiu
Compose will work on this example. But he might be asking about the general ability to have a piece of a parse rule come from running an expression. That's what R3's DO purports to do ( http://www.rebol.net/wiki/Parse_Project#DO ), but I couldn't get it to work, see my answer...
Hostile Fork
thanks much, it took me a little while figuring out, how `compose` works. btw - are `charset "ab"` and `charset [#"a" #"b"]` equivalent? (i think the latter form is used for ranges, if we insert `-`)
mykhal
`>> ( charset "ab" ) = charset [ #"a" #"b" ]== true`
Graham Chiu
+1  A: 

In the parse dialect, parentheses evaluate expressions. Yet the result of the evaluation does not become part of the parse rule. That is by design, so you can work in this style:

>> count: 0
== 0

>> parse "aab" [while ["a" (print "Match a" ++ count)] "b"]
Match a
Match a
== true

>> count
== 2

Having the evaluation become part of the parse rule is a different use case. Some instances (like yours) are suited to COMPOSE because they evaluate an expression just once. But my match counting expression would go awry:

>> count: 0
== 0

>> rule: compose/deep [while ["a" (print "Match a" ++ count)] "b"]
Match a
== [while ["a" 0] "b"]

>> count
== 1

>> parse "aab" rule
== false

AFAIK, Rebol 2 has no general way to run "DO" expressions in the middle of a parse and have the expressions effectively merged into the rules. So you have to use parenthesized code to get and set words, and then use those words in the rules.

Rebol 3 supposedly has added a version of DO but I can't find any examples that work in the current alpha version. As described on the wiki, I'd expect this to return true and capture the result "abc":

>> result: none
== none

>> parse "abc" [copy result thru do [reverse "cba"]]
== false

>> result
== none

(It also doesn't work on simpler examples. But I tried that once due to the statement "A DO statement could be used as the rule argument to COPY, SET or RETURN operations. It didn't say it could not be used elsewhere, but it also didn't say it could be used elsewhere...)

Hostile Fork
thanks for a deep analysis, which however goes byeond current stable vesrion and my rebol knowledge (i'm seeing `++` for the first time)
mykhal