user310291
user310291

Reputation: 38238

compose and compose/deep for parse rule in red?

How to keep (print text) as it disappears when using compose ?

s: {hello test1 parse test2}
t1: "test1"
t2: "test2"

rule: compose [ thru (t1) mark: copy text to (t2) (print text)]
parse s rule

same question for compose/deep if it's different answer:

s: {hello test1 parse test2. hello second test for test1 parse test2.}
t1: "test1"
t2: "test2"

rule: compose/deep [ any [thru (t1) mark: copy text to (t2) (print text)]]
parse s rule

Upvotes: 2

Views: 99

Answers (3)

sqlab
sqlab

Reputation: 6436

As blocks shield their content from evaluation of compose you can also use

>> rule: compose [ thru (t1) mark: copy text to (t2) (first [(print text)] )]
== [thru "test1" mark: copy text to "test2" (print text)]

Upvotes: 1

You aren't limited to what's in the box, so you could make your own COMPOSE-like construct that does other patterns.

For instance, here's a composeII which only substitutes for cases where there are nested parentheses. So it will leave (x) alone but evaluate ((x)).

composeII: function [block /only /deep] [
    collect [
        foreach item block [
            case [
                all [
                    paren? :item
                    1 = length? item
                    paren? first item
                ][
                    either only [
                        keep/only do first item
                    ][
                        keep do first item
                    ]
                ]

                all [
                    deep
                    block? :item
                ][
                    either only [
                        keep/only composeII/deep/only item
                    ][
                        keep/only composeII/deep item
                    ]
                ]

                true [
                    keep/only :item
                ]
            ]
        ]
    ]
]

So for your case:

>> t1: "test1"
>> t2: "test2"

>> composeII/deep [
    any [thru ((t1)) mark: copy text to ((t2)) (print text)]
]

== [
    any [thru "test1" mark: copy text to "test2" (print text)]
]

Upvotes: 6

rebolek
rebolek

Reputation: 1301

You can either convert block to paren, or quote paren:

>> compose [something here (to paren! [print text])]
== [something here (print text)]
>> compose [something here (quote (print text))]
== [something here (print text)]

It's the same for compose/deep .

Upvotes: 4

Related Questions