Last active
March 20, 2025 23:37
-
-
Save GiuseppeChillemi/191e55404f76c24ffeb48beb4a8587f3 to your computer and use it in GitHub Desktop.
Delimits elements to be used with parse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
Title: "Delimit elements" | |
Description: "Delimit and quotes elements for parse" | |
Version: 1.1 | |
] | |
delimit-elements: func [ | |
"Delimits and quotes a block of elements to be used with parse" | |
block [block!] | |
/local | |
datatypes-rule | |
parse-dsl-rule | |
rule | |
] [ | |
datatypes-rule: [ | |
set rule word! | |
[ | |
if (datatype? get/any rule) | |
| if (typeset? get/any rule) | |
] | |
] | |
parse-dsl-rule: [ | |
set-word! | |
| get-word! | |
| word! | |
| integer! | |
| paren! | |
| block! | |
;--- We need to quote it, as it is forbiden | |
| path! | |
] | |
parse block [ | |
any [ | |
change set rule datatypes-rule (reduce [rule '|]) | |
| change set rule parse-dsl-rule (reduce ['quote rule '|]) | |
| change set rule any-type! (reduce [rule '|]) | |
] | |
] | |
if not empty? head block [remove at tail block -1] | |
block | |
] | |
comment [ | |
probe delimit-elements [a 'b c: x/y/z (aa) [bb] :d 1 2 3 4 integer! none fail path!] | |
;[quote a | 'b | quote c: | quote x/y/z | quote (aa) | quote [bb] | quote :d | quote 1 | quote 2 | quote 3 | quote 4 | integer! | quote none | quote fail | path!] | |
probe delimit-elements [integer! path!] | |
;[integer! | path!] | |
] | |
attempt
is (especially in Red) quite expensive function call.
>> rule: 'xxxxx
>> profile [loop 1000 [datatype? attempt [get/any rule]]]
== false
#1 attempt | 1000 | 0:00:00.0140765
#2 get/any | 1000 | 0:00:00.0070906
#3 datatype? | 1000 | 0:00:00.0039895
#4 loop | 1 | 0:00:00.0349344
>> profile [loop 1000 [datatype? get/any rule]]
== false
#1 get/any | 1000 | 0:00:00.0039802
#2 datatype? | 1000 | 0:00:00.0039978
#3 loop | 1 | 0:00:00.0219804
you should use just:
if (datatype? get/any rule) | if (typeset? get/any rule)
Thank you. I have added /any
later, but not remove attempt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't understand purpose of this script, but at least instead of:
you should use just:
Especially when this rule is used for every input value.