forked from MJKWoolnough/ics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genSections.sh
executable file
·450 lines (422 loc) · 11.4 KB
/
genSections.sh
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/bin/bash
source "names.sh";
source "comments.sh";
declare -a currSection;
declare -a requirements;
declare sectionName;
declare longest=0;
function addToSection {
declare name="$(getName "${1%#*}")";
currSection[${#currSection[@]}]="$name ${1##*#} $2 $3 $4";
local l=${#name};
if [ $l -gt $longest ]; then
longest=$l;
fi;
}
function printSection {
declare sName="$(getName $sectionName)";
# type declaration
getComment "$sName";
declare checkRequired=false;
if [ ${#currSection[@]} -eq 0 ]; then
echo "type $sName struct{}";
else
echo "type $sName struct {";
IFS="$OFS";
for tline in "${currSection[@]}"; do
aline=( $tline ); # 0:name 1:KEYWORD 2:required 3:multiple 4:section 5:requiredAlso 6:requiredInstead
name="${aline[0]}";
required=${aline[2]};
multiple=${aline[3]};
section=${aline[4]};
if $required; then
checkRequired=true;
fi;
echo -n " $name ";
for i in $(seq $(( $longest - ${#name} ))); do
echo -n " ";
done;
if $multiple; then
echo -n "[]";
elif ! $required; then
echo -n "*";
fi;
if ! $section; then
echo -n "Prop";
fi;
echo "$name";
done;
echo "}";
fi;
echo;
# decoder
echo "func (s *$sName) decode(t tokeniser) error {";
# required bools
if [ ${#currSection[@]} -gt 0 ]; then
echo -n " var";
declare first=false;
for tline in "${currSection[@]}"; do
aline=( $tline ); # 0:name 1:KEYWORD 2:required 3:multiple 4:section 5:requiredAlso 6:requiredInstead
name="${aline[0]}";
required=${aline[2]};
multiple=${aline[3]};
if $required && ! $multiple; then
if $first; then
echo -n ",";
fi;
first=true;
echo -n " required$name";
fi;
done;
echo " bool";
fi;
# type switch
echo "Loop:";
echo " for {"
echo " p, err := t.GetPhrase()";
echo " if err != nil {";
echo " return errors.WithContext(\"error decoding $sName: \", err)";
echo " } else if p.Type == parser.PhraseDone {";
echo " return errors.WithContext(\"error decoding $sName: \", io.ErrUnexpectedEOF)";
echo " }";
if [ ${#currSection[@]} -gt 0 ]; then
echo " params := p.Data[1 : len(p.Data)-1]";
fi;
echo " value := p.Data[len(p.Data)-1].Data";
echo " switch strings.ToUpper(p.Data[0].Data) {";
echo " case \"BEGIN\":";
echo " switch n := strings.ToUpper(value); n {";
# BEGIN keywords
for tline in "${currSection[@]}"; do
aline=( $tline ); # 0:name 1:KEYWORD 2:required 3:multiple 4:section 5:requiredAlso 6:requiredInstead
name="${aline[0]}";
keyword="${aline[1]}";
required=${aline[2]};
multiple=${aline[3]};
section=${aline[4]};
if ! $section; then
continue;
fi;
echo " case \"$keyword\":";
if $required && ! $multiple; then
echo " if required$name {";
echo " return errors.Error(\"error decoding $sName: multiple $name\")";
echo " }";
echo " required$name = true";
echo " if err := s.${name}.decode(t); err != nil {";
echo " return errors.WithContext(\"error decoding $sName->$name: \", err)";
echo " }";
elif $multiple; then
echo " var e $name";
echo " if err := e.decode(t); err != nil {";
echo " return errors.WithContext(\"error decoding $sName->$name: \", err)";
echo " }";
echo " s.$name = append(s.$name, e)";
else
echo " if s.$name != nil {";
echo " return errors.Error(\"error decoding $sName: multiple $name\")";
echo " }";
echo " s.$name = new($name)";
echo " if err := s.${name}.decode(t); err != nil {";
echo " return errors.WithContext(\"error decoding $sName->$name: \", err)";
echo " }";
fi;
done;
echo " default:";
echo " if err := decodeDummy(t, n); err != nil {";
echo " return errors.WithContext(\"error decoding $sName: \", err)";
echo " }";
echo " }";
# non-BEGIN keywords
for tline in "${currSection[@]}"; do
aline=( $tline ); # 0:name 1:KEYWORD 2:required 3:multiple 4:section 5:requiredAlso 6:requiredInstead
name="${aline[0]}";
keyword="${aline[1]}";
required=${aline[2]};
multiple=${aline[3]};
section=${aline[4]};
if $section; then
continue;
fi;
echo " case \"$keyword\":";
if $required && ! $multiple; then
echo " if required$name {";
echo " return errors.Error(\"error decoding $sName: multiple $name\")";
echo " }";
echo " required$name = true";
echo " if err := s.${name}.decode(params, value); err != nil {";
echo " return errors.WithContext(\"error decoding $sName->$name: \", err)";
echo " }";
elif $multiple; then
echo " var e Prop$name";
echo " if err := e.decode(params, value); err != nil {";
echo " return errors.WithContext(\"error decoding $sName->$name: \", err)";
echo " }";
echo " s.$name = append(s.$name, e)";
else
echo " if s.$name != nil {";
echo " return errors.Error(\"error decoding $sName: multiple $name\")";
echo " }";
echo " s.$name = new(Prop$name)";
echo " if err := s.${name}.decode(params, value); err != nil {";
echo " return errors.WithContext(\"error decoding $sName->$name: \", err)";
echo " }";
fi;
done;
echo " case \"END\":"
if [ "${sectionName:0:6}" = "VALARM" ]; then
echo " if value != \"VALARM\" {";
else
echo " if value != \"$sectionName\" {";
fi;
echo " return errors.WithContext(\"error decoding $sName: \", ErrInvalidEnd)"
echo " }";
echo " break Loop";
echo " }";
echo " }";
# check required bools
if $checkRequired; then
first=false;
echo -n " if";
for tline in "${currSection[@]}"; do
aline=( $tline ); # 0:name 1:KEYWORD 2:required 3:multiple 4:section 5:requiredAlso 6:requiredInstead
name="${aline[0]}";
required=${aline[2]};
multiple=${aline[3]};
if $required; then
if $first; then
echo -n " ||";
fi;
first=true;
if $multiple; then
echo -n " len(s.$name) == 0";
else
echo -n " !required$name";
fi;
fi;
done;
echo " {";
echo " return errors.WithContext(\"error decoding $sName: \", ErrMissingRequired)";
echo " }";
fi;
# check other requirements
for req in "${requirements[@]}"; do
first=false
declare second=false
echo -n " if";
reqs=( $req );
typ=${reqs[0]};
reqs[0]="";
reqs=( ${reqs[@]} );
if [ "$typ" = "AND" ]; then
for r in ${reqs[@]}; do
if $second; then
echo -n " &&";
fi;
if $first; then
echo -n " t == (s.$(getName "$r") == nil)";
second=true;
else
echo -n " t := s.$(getName "$r") == nil;";
first=true;
fi;
done;
elif [ "$typ" = "OR" ]; then
for r in ${reqs[@]}; do
if $first; then
echo -n " &&";
fi;
first=true;
echo -n " s.$(getName "$r") == nil";
done;
elif [ "$typ" = "ONE" ]; then
for r in ${reqs[@]}; do
if $first; then
echo -n " &&";
fi;
first=true;
echo -n " s.$(getName "$r") != nil";
done;
elif [ "$typ" = "ERGO" ]; then
for r in ${reqs[@]}; do
if $second; then
echo -n " || ";
fi;
if $first; then
echo -n "s.$(getName "$r") == nil";
second=true;
else
echo -n " s.$(getName "$r") != nil && (";
first=true;
fi;
done;
echo -n ")";
fi;
echo " {";
echo " return errors.WithContext(\"error decoding $sName: \", ErrRequirementNotMet)";
echo " }";
done;
# end of decoder
echo " return nil";
echo "}";
echo;
# encoder
echo "func (s *$sName) encode(w writer) {"
if [ "${sectionName:0:6}" != "VALARM" ]; then
echo " w.WriteString(\"BEGIN:$sectionName\r\n\")";
fi;
for tline in "${currSection[@]}"; do
aline=( $tline ); # 0:name 1:KEYWORD 2:required 3:multiple 4:section 5:requiredAlso 6:requiredInstead
name="${aline[0]}";
required=${aline[2]};
multiple=${aline[3]};
if $multiple; then
echo " for n := range s.$name {";
echo " s.$name[n].encode(w)";
echo " }";
elif $required; then
echo " s.${name}.encode(w)";
else
echo " if s.$name != nil {";
echo " s.${name}.encode(w)";
echo " }";
fi;
done;
if [ "${sectionName:0:6}" != "VALARM" ]; then
echo " w.WriteString(\"END:$sectionName\r\n\")";
fi;
echo "}";
echo;
# validator
echo "func (s *$sName) valid() error {";
for tline in "${currSection[@]}"; do
aline=( $tline ); # 0:name 1:KEYWORD 2:required 3:multiple 4:section 5:requiredAlso 6:requiredInstead
name="${aline[0]}";
required=${aline[2]};
multiple=${aline[3]};
if $multiple; then
if $required; then
echo " if len(s.$name) == 0 {";
echo " return errors.Error(\"error validating $sName: missing $name\")";
echo " }";
fi;
echo " for n := range s.$name {";
echo " if err := s.$name[n].valid(); err != nil {";
echo " return errors.WithContext(\"error validating $sName->$name: \", err)";
echo " }";
echo " }";
else
if $required; then
echo " if err := s.${name}.valid(); err != nil {";
echo " return errors.WithContext(\"error validating $sName->$name: \", err)";
echo " }";
else
echo " if s.$name != nil {";
echo " if err := s.${name}.valid(); err != nil {";
echo " return errors.WithContext(\"error validating $sName->$name: \", err)";
echo " }";
echo " }";
fi;
fi;
done;
echo " return nil";
echo "}";
echo;
currSection=();
requirements=();
IFS=$(echo);
longest=0;
}
OFS="$IFS";
(
echo "package ics";
echo;
echo "// File automatically generated with ./genSections.sh";
echo;
echo "import (";
echo " \"io\"";
echo " \"strings\"";
echo;
echo " \"vimagination.zapto.org/errors\"";
echo " \"vimagination.zapto.org/parser\"";
echo ")";
echo;
{
read sectionName;
OFS="$IFS";
IFS=$(echo);
while read line; do
if [ "${line:0:1}" != " " ]; then
printSection
sectionName="$line"
continue;
fi;
field="${line:1}";
required=false;
multiple=false;
section=false;
fc="${field:0:1}"
if [ "$fc" = "?" ]; then
field="${field:1}";
if [ ! -z "$(echo "$field" | grep "!")" ]; then
requirements[${#requirements}]="ONE $(echo "$field" | tr "!" " ")";
elif [ ! -z "$(echo "$field" | grep "+")" ]; then
requirements[${#requirements}]="AND $(echo "$field" | tr "+" " ")";
elif [ ! -z "$(echo "$field" | grep ">")" ]; then
requirements[${#requirements}]="ERGO $(echo "$field" | tr ">" " ")";
elif [ ! -z "$(echo "$field" | grep "|")" ]; then
requirements[${#requirements}]="OR $(echo "$field" | tr "|" " ")";
fi;
continue;
elif [ "$fc" = "!" ]; then
required=true;
field="${field:1}";
elif [ "$fc" = "+" ]; then
multiple=true;
required=true;
field="${field:1}";
elif [ "$fc" = "*" ]; then
multiple=true;
field="${field:1}";
fi;
if [ "${field:0:6}" = "BEGIN:" ]; then
section=true;
field="${field:6}";
else
section=false;
fi;
addToSection $field $required $multiple $section;
done;
}< sections.gen;
printSection
cat <<HEREDOC
// decodeDummy reads unknown sections, discarding the data
func decodeDummy(t tokeniser, n string) error {
for {
p, err := t.GetPhrase()
if err != nil {
return err
} else if p.Type == parser.PhraseDone {
return io.ErrUnexpectedEOF
}
switch strings.ToUpper(p.Data[0].Data) {
case "BEGIN":
if err := decodeDummy(t, p.Data[len(p.Data)-1].Data); err != nil {
return err
}
case "END":
if strings.ToUpper(p.Data[len(p.Data)-1].Data) == n {
return nil
}
return ErrInvalidEnd
}
}
}
// Errors
const (
ErrInvalidEnd errors.Error = "invalid end of section"
ErrMissingRequired errors.Error = "required property missing"
ErrRequirementNotMet errors.Error = "requirement not met"
)
HEREDOC
) > sections.go