mydoghasworms
mydoghasworms

Reputation: 18613

Rebol 2: Using a parse rule to check input without executing code

Let's suppose you have a rule like the one below from the Rebol docs on parsing.

If you wanted to check some input against a rule just to validate it, but without having the contents of the parentheses evaluated, how can you do it?

Is there a way that easily lets you just validate inputs against a rule without having the content of the parentheses being evaluated?

rule: [
    set action ['buy | 'sell]
    set number integer!
    'shares 'at
    set price money!
    (either action = 'sell [
            print ["income" price * number]
            total: total + (price * number)
        ] [
            print ["cost" price * number]
            total: total - (price * number)
        ]
    )
]

Upvotes: 3

Views: 210

Answers (2)

MarkI
MarkI

Reputation: 347

Well, you could just remove the parens from your rule:

unparen: func [b [block!]][forall b [case [
    paren! = type? b/1 [remove b] block! = type? b/1 [unparen b/1]]] head b]
new-rule: unparen copy/deep rule

You can then parse with new-rule.

I fear this still violates your 'easily' requirement, however!

And it doesn't handle nested rule references.

As an informative aside, there is theoretically no answer to your question. The code in the parentheses could be changing the rule, for example.

Upvotes: 3

rebolek
rebolek

Reputation: 1301

Depends if you mean parse input or parse rules.

For parse rules you need some special flag and function that will take care of it:

do?: true
parse-do: function [code] [if do? [do code]]
rule: ['a (parse-do [print "got it"])]
parse [a] rule
do?: false
parse [a] rule

For parse input use INTO:

>> parse [paren (1 + 1)]['paren into [integer! '+ integer!]]
== true

Upvotes: 2

Related Questions