-
Notifications
You must be signed in to change notification settings - Fork 4
/
match.r
239 lines (213 loc) · 5.96 KB
/
match.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
Rebol [
Title: "Match"
Author: "Christopher Ross-Gill"
Date: 17-Sep-2013
Home: http://www.ross-gill.com/page/Match
File: %match.r
Version: 0.2.0
Purpose: {Extract structured data from an unstructured block.}
Rights: http://opensource.org/licenses/Apache-2.0
Type: 'module
Name: 'rgchris.match
Exports: [match]
History: [
17-Sep-2013 0.2.0 "Extracted from the QuarterMaster web framework"
]
Usage: [
result: match ["Product" $12.99][
name: string! ; requires a string value to be present, set to string value
price: some money! ; requires one or more money values, set to block
place: opt url! ; optional url value, set to url value or none
]
]
]
wrap: func [body [block!]][
use collect [
parse body [
any [body: set-word! (keep to word! body/1) | skip]
]
] head body
]
match: wrap [
interpolate: func [body [string!] escapes [any-block!] /local out][
body: out: copy body
parse/all body [
any [
to #"%" body: (
body: change/part body reduce any [
select/case escapes body/2 body/2
] 2
) :body
]
]
out
]
filter: [
result: errors: #[none]
messages: [
not-included "is not included in the list"
excluded "is reserved"
invalid "is missing or invalid"
not-confirmed "doesn't match confirmation"
not-accepted "must be accepted"
empty "can't be empty"
blank "can't be blank"
too-long "is too long (maximum is %d characters)"
too-short "is too short (minimum is %d characters)"
wrong-length "is the wrong length (should be %d characters)"
not-a-number "is not a number"
too-many "has too many arguments"
]
datatype: [
'any-string! | 'binary! | 'block! | 'char! | 'date! | 'decimal! | 'email! | 'file! |
'get-word! | 'integer! | 'issue! | 'lit-path! | 'lit-word! | 'logic! | 'money! |
'none! | 'number! | 'pair! | 'paren! | 'path! | 'range! | 'refinement! |
'set-path! | 'set-word! | 'string! | 'tag! | 'time! | 'tuple! | 'url! | 'word!
]
else: #[none]
otherwise: [
['else | 'or][
set else string! | copy else any [word! string!]
] | (else: #[none])
]
source: spec: rule: key: value: required: present: target: type: format: constraints: else: none
constraint: use [is is-not? is-or-length-is op val val-type range group][
op: val: val-type: none
is: ['is | 'are]
is-or-length-is: [
[
['length | 'size] (val: string-length? value val-type: integer!)
| (val: :value val-type: :type)
] is
]
is-not?: ['not (op: false) | (op: true)]
[
is [
'accepted otherwise (
unless true = value [report not-accepted]
) |
'confirmed opt 'by set val get-word! otherwise (
val: to-word val
unless value = as/where :type get-from source :val format [
report not-confirmed
]
) |
is-not? 'within set group any-block! otherwise (
either case [
block? value [value = intersect value group]
true [found? find group value]
][
unless op [report excluded]
][
if op [report not-included]
]
)
] |
is-or-length-is [
is-not? 'between [set range [range! | into [2 val-type]]] otherwise (
either op [
case [
val < target: range/1 [report too-short]
val > target: range/2 [report too-long]
]
][
unless any [
val < range/1
val > range/2
][report excluded]
]
) |
'more-than set target val-type otherwise (
unless val > target [report too-short]
) |
'less-than set target val-type otherwise (
unless val < target [report too-long]
) |
set target val-type otherwise (
unless val = target [report wrong-length]
)
]
]
]
do-constraints: does [constraints: [any constraint]]
skip-constraints: does [constraints: [to set-word! | to end]]
humanize: func [word][uppercase/part replace/all form word "-" " " 1]
report: func ['message [word!]][
message: any [
all [string? else else]
all [block? else select else message]
reform [humanize key any [select messages message ""]]
]
unless select errors :key [repend errors [:key copy []]]
append select errors :key interpolate message [
#"w" [form key]
#"W" [humanize key]
#"d" [form target]
#"t" [form type]
]
]
engage: does [parse spec rule]
]
make-filter: func [source spec rule][
spec: context compose/deep [
(filter)
errors: copy []
result: copy []
rule: [(copy/deep rule)]
spec: [(spec)]
]
spec/source: copy source
spec
]
get-one: func [data type /local res][
parse data [some [res: type to end break | skip]]
unless tail? res [take res]
]
get-some: func [data type /local pos res][
res: make block! length? data
parse data [some [pos: type (append/only res take pos) :pos | skip]]
unless empty? res [res]
]
match: func [
[catch] source [block!] spec [block!]
/report-to errs [block!]
][
spec: make-filter source spec [
(
remove-each item result: copy spec [not set-word? item]
result: context append result none
)
some [
set key set-word! (key: to-word key)
set required ['opt | 'any | 'some | none]
copy type [lit-word! any ['| lit-word!] | datatype any ['| datatype]]
otherwise
(
switch/default required [
any [
value: get-some source type
either value [do-constraints][skip-constraints]
]
opt [
value: get-one source type
either value [do-constraints][skip-constraints]
]
some [
value: get-some source type
either value [do-constraints][skip-constraints report invalid]
]
][
value: get-one source type
either value [do-constraints][skip-constraints report invalid]
]
result/(key): value
)
constraints
]
end (if all [not empty? source empty? errors][key: 'match report too-many])
]
unless spec/engage [raise "Could not parse Match specification"]
all [block? errs insert clear errs spec/errors]
if empty? spec/errors [spec/result]
]
]