3

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)
        ]
    )
]
mydoghasworms
  • 18,233
  • 11
  • 61
  • 95

2 Answers2

3

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.

MarkI
  • 347
  • 2
  • 8
  • I was actually wondering about doing that, but did not realize how much work that would be :-( I was also afraid of your last assertion, i.e. the code being able to change the rule. It's one of those things where I would prefer to close my eyes and block my ears and say "la la la" to block it out. – mydoghasworms Feb 18 '15 at 09:21
  • @mydoghasworms Beware, I am a parse noob. My code works, but I can definitely not promise it is the best, shortest, or simplest. If I find a way to do this without it looking like so much "work", I'll post it too. – MarkI Feb 18 '15 at 14:26
2

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
rebolek
  • 1,281
  • 7
  • 16