Created
November 23, 2019 14:37
-
-
Save toomasv/dbee79a944fd12eee53a8353a1ce5472 to your computer and use it in GitHub Desktop.
Study matching of `any-string!`, `any-word!` and `any-path!` values in `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 [ | |
Description: "Study of parsing `any-string!`, `any-word!` and `any-path!` values" | |
Date: 23-Nov-2019 | |
] | |
string-parse: function [s /case /quote /upper /block][ | |
l: append/dup copy "" #" " 15 | |
s2: either upper [uppercase copy s][s] | |
print append copy l "string url tag email file" | |
types: [to-string to-url to-tag to-email to-file] | |
foreach type types [ | |
t: get type append l pad mold t s2 7 | |
] | |
;print l | |
print rejoin [l pad/with newline 7 * 7 #"-"] | |
foreach type types [ | |
t1: get type | |
l: rejoin [pad mold s1: t1 s 6 "| "] | |
insert l pad form type? s1 7 | |
foreach type types [ | |
t2: get type | |
rule: append copy either quote [[quote]][[]] t2 s2 | |
match: either block [append clear [] s1][s1] | |
insert/only code: reduce [match rule] either case ['parse/case]['parse] | |
append l pad form do code 7 | |
] | |
print l | |
] | |
] | |
word-parse: function [s /case /quote /upper][ | |
l: append/dup copy "" #" " 8 | |
types: [to-word to-lit-word to-set-word to-get-word] | |
s2: either upper [load uppercase form s][s] | |
foreach type types [ | |
if long: any [quote type = 'to-lit-word][ | |
t: get type append l pad mold t s2 7 | |
] | |
] | |
;print l | |
print rejoin [l pad/with newline 7 * pick [5 4] long #"-"] | |
foreach type types [ | |
t1: get type | |
l: rejoin [pad mold s1: t1 s 6 "| "] | |
foreach type types [ | |
if any [quote type = 'to-lit-word][ | |
t2: get type | |
rule: append copy either quote [[quote]][[]] t2 s2 | |
match: append clear [] s1 | |
insert/only code: reduce [match rule] either case ['parse/case]['parse] | |
append l pad form do code 7 | |
] | |
] | |
print l | |
] | |
] | |
path-parse: function [s /case /quote /upper][ | |
l: append/dup copy "" #" " 8 | |
types: [to-path to-lit-path to-set-path to-get-path] | |
s2: either upper [load uppercase form s][s] | |
foreach type types [ | |
t: get type append l pad mold t s2 7 | |
] | |
;print l | |
print rejoin [l pad/with newline 7 * 5 #"-"] | |
foreach type types [ | |
t1: get type | |
l: rejoin [pad mold s1: t1 s 6 "| "] | |
foreach type types [ | |
t2: get type | |
rule: append/only copy either quote [[quote]][[]] t2 s2 | |
match: append/only clear [] s1 | |
insert/only code: reduce [match rule] either case ['parse/case]['parse] | |
append l pad form do code 7 | |
] | |
print l | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing those functions! 👍