mykhal
mykhal

Reputation: 19895

rebol parse problem

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

Upvotes: 3

Views: 267

Answers (3)

sqlab
sqlab

Reputation: 6436

What you were really looking is probably

>> parse "aaa" [(ab: charset "ab")  some ab]
== true

define a word at beginning inside parse expression and use that word as part of rule later

Upvotes: 0

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...)

Upvotes: 2

Graham Chiu
Graham Chiu

Reputation: 4886

You're looking for 'compose

>> parse "aaa" compose [ some (charset [#"a" #"b"] ) ]
== true

Upvotes: 2

Related Questions