-
Notifications
You must be signed in to change notification settings - Fork 25
/
parse.inc.js
1196 lines (1008 loc) · 39.9 KB
/
parse.inc.js
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Node.js API.
const util = require('util')
// Convert numbers and arrays to numbers to literals with hex literals.
const hex = require('./hex')
// Determine if an integer parse should be unrolled.
const { parse: homogeneous } = require('./homogeneous')
// Format source code maintaining indentation.
const $ = require('programmatic')
// Generate literal object construction.
const vivify = require('./vivify')
// Generate two's compliment conversion.
const unsign = require('./fiddle/unsign')
// Generate integer unpacking.
const unpack = require('./unpack')
// Determine necessary variables.
const { parse: declare } = require('./declare')
// Generate inline function source.
const Inliner = require('./inline')
// Generate required modules and functions.
const required = require('./required')
// Format source code maintaining indentation.
const join = require('./join')
const map = require('./map')
//
// Generate an incremental parser from our AST.
//
function generate (packet, { require = null }) {
// Whether or not to surround the switch statement with a forever loop.
let surround = false
// Current step being generated.
let $step = 0
// Current position in the array of array indices.
let $i = -1
// Current array length for length encoded arrays.
let $I = -1
let $sip = -1
// Map of parser scoped variable definitions to their initialization values.
const locals = {}
// Traverse the AST looking for all the variables that will be passed into
// in this parser from a best-foot-forward parse, all the accumulators that
// will be decalared, and the set of named parameters passed to the parser
// from outside the parser on construction.
const { variables, accumulators, parameters } = declare(packet)
// An object that tracks the declaration of accumulators, whether or not
// they are accumulating buffer contents, as well providing the `inliner`
// function with the state necessary to generate named function invocations.
const inliner = Inliner({
packet, variables, accumulators, parameters,
direction: 'parse'
})
//
// Generate an *absent* field by setting the property to `null` or an empty
// array `[]`.
//
function absent (path, field) {
return $(`
case ${$step++}:
${path} = ${util.inspect(field.value)}
`)
}
//
// Assign an integer extracted from the underlying buffer to an object
// property in the constructed object. This generated source is common to
// both rolled and unrolled `number` and `BigInt` serialization. Convert the
// object property value if it is a lookup value or a packed integer.
function assign (path, field) {
variables.register = true
// **TODO** This appears to proibit using a two's compliment value as a
// lookup value, which is probably okay, since how would you lookup
// negative values. Well, you could use a map, so...
return field.fields != null
? unpack(inliner, packet, path, field, '$_')
: field.compliment
? `${path} = ${unsign('$_', field.bits)}`
: field.lookup != null
? Array.isArray(field.lookup.values)
? `${path} = $lookup[${field.lookup.index}][$_]`
: `${path} = $lookup[${field.lookup.index}].forward[$_]`
: `${path} = $_`
}
//
// Parse a `number' or `BigInt` integer using a loop to advance to the next
// byte. We use this when the bit size and upper bits are the same for each
// byte which is the common case — use all 8 bits, set no bytes.
function rolled (path, field, write, $_, bite, stop) {
variables.bite = true
const direction = field.endianness == 'big' ? '--' : '++'
const { size } = field.bytes[0]
return $(`
case ${$step++}:
$_ = ${$_}
$bite = ${bite}
case ${$step++}:
while ($bite != ${stop}) {
if ($start == $end) {
$step = ${$step - 1}
`, inliner.exit(), `
return { start: $start, object: null, parse: $parse }
}
$_ += ${write}
$bite${direction}
}
`, assign(path, field), `
`)
}
function unrolled (path, field, writes, $_) {
const initialize = $(`
case ${$step++}:
$_ = ${$_}
`)
const steps = join(writes.map(write => {
return $(`
case ${$step++}:
if ($start == $end) {
$step = ${$step - 1}
`, inliner.exit(), `
return { start: $start, object: null, parse: $parse }
}
$_ += ${write}
`)
}))
return $(`
`, initialize, `
`, steps, `
`, assign(path, field), `
`)
}
//
// Parse a `number` as an integer.
function integer (path, field) {
const bytes = field.bits / 8
// Special case for single byte which is a single step.
// This special case an probably be just unrolled?
if (bytes == 1 && field.fields == null && field.lookup == null) {
// TODO Remove setup set when you set step on return.
return $(`
case ${$step++}:
case ${$step++}:
if ($start == $end) {
$step = ${$step - 1}
`, inliner.exit(), `
return { start: $start, object: null, parse: $parse }
}
${path} = $buffer[$start++]
`)
}
if (homogeneous(field)) {
const bytes = field.bits / 8
const bite = field.endianness == 'big' ? bytes - 1 : 0
const stop = field.endianness == 'big' ? -1 : bytes
const { size } = field.bytes[0]
const write = `$buffer[$start++] << $bite * ${size} >>> 0`
return rolled(path, field, write, '0', bite, stop)
}
return unrolled(path, field, field.bytes.map(({ shift, mask, upper }) => {
return upper != 0
? `$buffer[$start++] & ${mask} << ${shift}`
: `$buffer[$start++] << ${shift}`
}), 0)
}
//
// Parse a `BigInt` as an integer.
function bigint (path, field ) {
if (homogeneous(field)) {
const bytes = field.bits / 8
const bite = field.endianness == 'big' ? bytes - 1 : 0
const stop = field.endianness == 'big' ? -1 : bytes
const { size, upper, mask } = field.bytes[0]
const write = upper != 0
? `BigInt($buffer[$start++] & ${mask}) << $bite * ${size}n`
: `BigInt($buffer[$start++]) << $bite * ${size}n`
return rolled(path, field, write, '0n', `${bite}n`, `${stop}n`)
}
return unrolled(path, field, field.bytes.map(({ shift, mask, upper }) => {
return bits = upper != 0
? `BitInt($buffer[$start++] & ${mask}) << ${shift}`
: `BitInt($buffer[$start++]) << ${shift}`
}))
}
function literal (path, field) {
function write (literal) {
if (literal.repeat == 0) {
return null
}
variables.register = true
variables.bite = true
return $(`
case ${$step++}:
$_ = ${(literal.value.length >>> 1) * literal.repeat}
case ${$step++}:
$bite = Math.min($end - $start, $_)
$_ -= $bite
$start += $bite
if ($_ != 0) {
$step = ${$step - 1}
return { start: $start, object: null, parse: $parse }
}
`)
}
return $(`
`, write(field.before, 1), `
`, map(dispatch, path, field.fields), `
`, write(field.after, -1), `
`)
}
function inline (path, field) {
const inline = inliner.inline(path, field.after)
const before = inline.starts != null
? $(`
case ${$step++}:
`, inline.starts, `
`)
: null
return $(`
`, before, -1, `
`, map(dispatch, inline.path, field.fields), `
`, -1, inline.inlined, `
`, -1, inliner.pop(),`
`)
}
function lengthEncoded (path, field) {
const element = field.fields[0]
const I = `$I[${++$I}]`
const encoding = map(dispatch, I, field.encoding)
if (element.type == 'buffer') {
locals['index'] = 0
locals['buffers'] = '[]'
$I--
const assign = element.concat
? `${path} = $buffers.length == 1 ? $buffers[0] : Buffer.concat($buffers)`
: `${path} = $buffers`
return $(`
`, encoding, `
case ${$step++}:
{
const $length = Math.min(${I} - $index, $end - $start)
$buffers.push($buffer.slice($start, $start + $length))
$index += $length
$start += $length
}
if ($index != ${I}) {
$step = ${$step - 1}
`, inliner.exit(), `
return { start: $start, parse: $parse }
}
`, assign, `
$index = 0
$buffers = []
`)
}
const i = `$i[${++$i}]`
surround = true
const init = $step++
const redo = $step
const vivification = function () {
const vivification = vivify.assignment(`${path}[${i}]`, field)
if (vivification == null) {
return null
}
return $(`
case ${$step++}:
`, vivification, `
`)
} ()
const source = $(`
`, encoding, `
case ${init}:
${i} = 0
`, vivification, -1, `
`, map(dispatch,`${path}[${i}]`, field.fields), `
if (++${i} != ${I}) {
$step = ${redo}
continue
}
`)
$I--
$i--
return source
}
function accumulator (path, field) {
return $(`
case ${$step++}:
`, inliner.accumulator(field), `
`, map(dispatch, path, field.fields), `
`)
}
// We will have a special case for bite arrays where we can use index of to
// find the terminator, when the termiantor is zero or `\n\n` or the like,
// because we can use `indexOf` to find the boundary. Maybe byte arrays
// should always be returned as `Buffer`s?
// We will have a another special case for word arrays where the terminated
// word because we can jump right ito them.
// Seems like in the past I would read the terminator into an array and if
// it didn't match, I'd feed the array to the parser, this would handle long
// weird terminators.
//
function terminated (path, field) {
const length = field.calculated ? `$I[${++$I}]` : field.length
const calculated = field.calculated ? inliner.test(path, field.length) : null
// We will be looping.
surround = true
// Get the element type contained by the array.
const element = field.fields[field.fields.length - 1]
// Our terminator is the padding definition for padded fixed arrays.
const bytes = field.terminator || field.pad
// Generate any buffered function calls to process the buffer if we
// reach the end of the buffer.
// Skip the remainder for of a fixed padded buffer. Common to buffered
// and byte-by-byte fixed arrays, not used for terminated. Note that
// it's a function because of the `$step++`.
function skip (i) {
variables.register = true
return $(`
case ${$step++}: {
const length = Math.min($_, $end - $start)
$start += length
$_ -= length
if ($_ != 0) {
$step = ${$step - 1}
`, inliner.exit(), `
return { start: $start, object: null, parse: $parse }
}
}
`)
}
//
// Buffers are a special case. Data is raw, can be copied in bulk,
// terminators can be found with `indexOf`. Separate implemention for
// buffers.
//
if (field.fields[0].type == 'buffer') {
locals['buffers'] = '[]'
if (field.fixed) {
locals['length'] = 0
variables.register = true
}
const redo = $step + (field.type == 'fixed' ? 1 : 0)
// **TODO** This is off for a multi-byte terminator that occurs at
// the last element. Would begin trying to match the terminator and
// go past the end of the buffer.
const slice = field.type == 'fixed' ? $(`
case ${$step++}:
$_ = 0
${length} = `, calculated, `
case ${$step++}: {
const $index = $buffer.indexOf(${hex(bytes[0])}, $start)
if (~$index) {
if ($_ + $index > ${length}) {
const $length = ${length} - $_
$buffers.push($buffer.slice($start, $start + $length))
$_ += $length
$start += $length
$step = ${$step + field.pad.length - 1}
continue
} else {
$buffers.push($buffer.slice($start, $index))
$_ += ($index - $start) + 1
$start = $index + 1
$step = ${$step}
continue
}
} else if ($_ + ($end - $start) >= ${length}) {
const $length = ${length} - $_
$buffers.push($buffer.slice($start, $start + $length))
$_ += $length
$start += $length
$step = ${$step + field.pad.length - 1}
continue
} else {
$step = ${$step - 1}
$_ += $end - $start
$buffers.push($buffer.slice($start))
`, inliner.exit(), `
return { start: $end, object: null, parse: $parse }
}
}
`) : $(`
case ${$step++}: {
const $index = $buffer.indexOf(${hex(bytes[0])}, $start)
if (~$index) {
$buffers.push($buffer.slice($start, $index))
$start = $index + 1
$step = ${$step}
continue
} else {
$step = ${$step - 1}
$buffers.push($buffer.slice($start))
`, inliner.exit(), `
return { start: $end, object: null, parse: $parse }
}
}
`)
const subsequent = []
const done = $step + bytes.length
for (let i = 1; i < bytes.length; i++) {
const sofar = bytes.slice(0, i)
const rewind = sofar.slice(1)
const rewinds = []
let found = false
const seen = []
do {
while (rewind.length != 0) {
if (rewind.every((value, index) => bytes[index] == value)) {
break
}
rewind.shift()
}
if (bytes[rewind.length] != bytes[i] && !seen.includes(bytes[rewind.length])) {
seen.push(bytes[rewind.length])
rewinds.push($(`
if ($buffer[$start - 1] == ${hex(bytes[rewind.length])}) {
$buffers.push(Buffer.from(${hex(bytes.slice(0, sofar.length - rewind.length))}))
$step = ${redo + 1 + rewind.length}
continue
}
`))
}
rewind.shift()
} while (rewind.length != 0)
subsequent.push($(`
case ${$step++}:
if ($start == $end) {
$step = ${$step - 1}
`, inliner.exit(), `
return { start: $start, object: null, parse: $parse }
}
if ($buffer[$start++] != ${hex(bytes[i])}) {
`, rewinds.length != 0 ? rewinds.join('\n') : null, `
$buffers.push(Buffer.from(${hex(sofar)}.concat($buffer[$start - 1])))
$step = ${redo}
continue
}
`))
}
// Assignment buffer with a possible recording of length so far if
// we have to skip padding.
function assign () {
// **TODO** Could use the calculation of `$_` above, but would
// have to special case `$_` everywhere for fixed/terminated and
// make the code in here ugly.
const _length = field.type == 'fixed' ? $(`
$_ = ${length} - Math.min($buffers.reduce((sum, buffer) => {
return sum + buffer.length
}, ${bytes.length}), ${length})
`) : null
return element.concat ? $(`
case ${$step++}:
`, _length, `
${path} = $buffers.length == 1 ? $buffers[0] : Buffer.concat($buffers)
$buffers.length = 0
`) : $(`
case ${$step++}:
`, _length, `
${path} = $buffers
$buffers = []
`)
}
if (field.type == 'terminated') {
return $(`
`, slice, `
`, subsequent.length != 0 ? join(subsequent) : null, -1, `
`, assign(), `
`)
}
return $(`
`, slice, `
`, subsequent.length != 0 ? join(subsequent) : null, -1, `
`, assign(), `
`, skip(), `
`)
}
// Our regular parsing seeks terminators at the start of each iteration
// of the parse loop.
// Obtain a next index from the index array.
const i = `$i[${++$i}]`
// Initialization step.
const init = $step++
// Start of element fields, loop reset.
const redo = $step
// We need a step for each byte in the terminator.
const begin = $step += bytes.length
// We will sometimes have a vivification step to an object element.
const vivified = vivify.assignment(`${path}[${i}]`, field)
if (vivified != null) {
$step++
}
// Create the body of the loop.
const looped = map(dispatch, `${path}[${i}]`, field.fields)
// Release the length index from the array of lengths if calculated.
if (field.calculated) {
$I--
}
// Step of next field is after a final loop jump step.
const done = $step + 1
//
// Generate the terminator detection.
//
const fixed = field.type == 'fixed'
? $(`
if (${i} == ${length}) {
$step = ${done}
continue
}
`)
: null
const terminator = bytes.length == 1
// If we have a single byte terminator, we skip over the loop if the
// we see the byte. A multi-byte terminator is more complicated.
? $(`
case ${redo}:
$step = ${redo}
`, fixed, -1, `
if ($start == $end) {
`, inliner.exit(), `
return { start: $start, object: null, parse: $parse }
}
if ($buffer[$start] == ${hex(bytes[0])}) {
$start++
$step = ${done}
continue
}
$step = ${begin}
`)
// For a multi-byte terminator we have a step for each byte.
//
// For every terminator byte last check to see if it
// matches the byte in the buffer. If it does we fall through to
// test next byte. If not we set the `$step` to the start of the
// body.
//
// Subsequent to the first byte we will have matched and skipped
// bytes but we'll know what they where, so we can still parse them
// by calling the defined `$parse` function with a literal buffer.
//
// If the last byte does not match we jump to the end. The last byte
// might seem like a good place to fall through instead of jumping,
// but we will have already begun parsing by parsing the terminator
// literal and it will have proceded past the initialization of the
// next field. We won't know how many initialization steps there, it
// varies based on field and even if we did attempt to ensure that
// every field type had a single initialization step it would still
// vary due to nesting.
: join(bytes.map((bite, index) => {
const parse = index != 0
? `$parse(Buffer.from(${hex(bytes.slice(0, index))}), 0, ${index})`
: null
const next = index != literal.length - 1
? `$step = ${redo + index + 1}`
: $(`
$step = ${done}
continue
`)
return $(`
case ${redo + index}:
$step = ${redo + index}
`, index == 0 ? fixed : null, -1, `
if ($start == $end) {
`, inliner.exit(), `
return { start: $start, object: null, parse: $parse }
}
if ($buffer[$start] != ${hex(bite)}) {
$step = ${begin}
`, parse, `
continue
}
$start++
`, next, `
`)
}))
const initialize = vivified == null ? $(`
case ${init}:
${i} = 0
${length} = `, calculated, `
`, terminator, `
`) : $(`
case ${init}:
${i} = 0
${length} = `, calculated, `
`, terminator, `
case ${begin}:
`, vivify.assignment(`${path}[${i}]`, field), `
`)
// Put it all together.
const source = $(`
`, initialize, `
`, looped, `
case ${$step++}:
${i}++
$step = ${redo}
continue
`)
// Release the array index from the array of indices.
$i--
// If we are actually padded fixed array, we need to skip over the
// remaining bytes in the fixed width field.
locals['length'] = 0
// TODO And has padding?
if (field.type == 'fixed') {
return $(`
`, source, `
case ${$step++}:
$_ = ${length} != ${i}
? (${length} - ${i}) * ${element.bits >>> 3} - ${bytes.length}
: 0
`, skip(`(${i} + ${bytes.length})`), `
`)
}
return $(`
`, source, `
case ${$step++}:
`)
}
// TODO: Folling is notes on things to come.
// We will have a special case for bite arrays where we can use index of to
// find the terminator, when the termiantor is zero or `\n\n` or the like,
// because we can use `indexOf` to find the boundary. Maybe byte arrays
// should always be returned as `Buffer`s?
// We will have a another special case for word arrays where the terminated
// word because we can jump right ito them.
// Seems like in the past I would read the terminator into an array and if
// it didn't match, I'd feed the array to the parser, this would handle long
// weird terminators.
//
function fixed (path, field) {
if (field.pad.length != 0) {
return terminated(path, field)
}
const calculation = field.calculated ? inliner.test(path, field.length) : null
const element = field.fields[field.fields.length - 1]
const length = field.calculated ? `$I[${++$I}]` : field.length
//
// Use `Buffer` functions when fixed array is a `Buffer`.
//
// **TODO** I'm going to make this a todo, not an issue, but it would be
// nice to use `TypedArray` when we have an array of words and the
// desired byte order matches the machine byte order.
//
if (element.type == 'buffer') {
variables.register = true
locals['buffers'] = '[]'
const assign = element.concat
? `${path} = $buffers.length == 1 ? $buffers[0] : Buffer.concat($buffers)`
: `${path} = $buffers`
return $(`
case ${$step++}:
$_ = 0
${length} = `, calculation, `
case ${$step++}: {
const length = Math.min($end - $start, ${length} - $_)
$buffers.push($buffer.slice($start, $start + length))
$start += length
$_ += length
if ($_ != ${length}) {
$step = ${$step - 1}
`, inliner.exit(), `
return { start: $start, object: null, parse: $parse }
}
`, assign, `
$buffers = []
}
`)
}
//
// For everything but `Buffer`, generate byte-by-byte parsers.
//
surround = true
// Obtain a next index from the index array.
const i = `$i[${++$i}]`
// The loop return step is after loop index initialization.
const redo = $step + 1
// TODO Do we need remaining if there is no padding?
const remaining = field.calculated && field.fixed
? `$_ = (${length} - ${i}) * ${element.bits >>> 3} - ${field.pad.length}`
: null
// We sometimes have a vivification step to create an object element.
const vivified = vivify.assignment(`${path}[${i}]`, field)
const initialization = vivified == null ? $(`
case ${$step++}:
${i} = 0
${length} = `, calculation, `
`) : $(`
case ${$step++}:
${i} = 0
${length} = `, calculation, `
case ${$step++}:
`, vivified, `
`)
const source = $(`
`, initialization, `
`, map(dispatch,`${path}[${i}]`, field.fields), `
case ${$step++}:
${i}++
if (${i} != ${length}) {
$step = ${redo}
continue
}
`, remaining, `
`)
// Release the length index from the array of lengths if calculated.
if (field.calculated) {
$I--
}
if (field.pad.length != 0) {
variables.register = true
}
// Generate skip logic if we are fixed width.
const skip = field.pad.length != 0 ? $(`
case ${$step++}:
$bite = Math.min($end - $start, $_)
$_ -= $bite
$start += $bite
if ($_ != 0) {
$step = ${$step - 1}
return { start: $start, object: null, parse: $parse }
}
`) : null
// Release the array index from the array of indices.
$i--
return $(`
`, source, `
`, skip, `
`)
}
function conditional (path, field) {
return conditions(path, field.parse, [])
}
// It is going to annoy you that an initial special case that tests and
// returns and you have it within `conditional` everywhere, so you'll want
// to create an `'unconditional'` type so you can have an `'unconditional'`
// case or an `unconditional` function. However, that would be the type
// within either the `parse` or `conditional` property of the
// `'conditional'` AST node. You still have a sub-condition unless you
// flatten the AST so that the parse or serialize node becomes the
// 'conditional' node depending. So, now you add another transformation from
// the language. You must flatten in a transform unless the AST tree
// produced is entirely a parse AST tree and a serialize AST tree and you
// would do it only for split conditionals that are used solely to
// distinguish between parse and serialize.
// Furthermore you remove unconditional sips in the language parsing since
// they they are no-ops and the sip is unnecessary, so you may feel like
// you're halfway there to fixing everything in the language, but no, you
// would have to provide two AST trees. You can't have `'unconditional'` as
// a new type because only one side of the split conditional may be
// unconditional.
// This morning, with a clear mind, you reason quite reasonably that the
// unconditional is a hack for the syntax bashed language — using a an
// unconditional conditional to implement parse or serialize conditionals
// — and the hack in the language is properly modeled by a hack in code.
// And here we are.
//
function conditions (path, parse, rewound) {
// **TODO** Not certain what happens with an unconditional sip.
if (parse.unconditional) {
return map(dispatch, path, parse.conditions[0].fields)
}
surround = true
const signature = []
const sip = function () {
if (parse.sip == null) {
return null
}
rewound.push(parse.sip)
// TODO Decrement `$sip[]`.
const sip = `$sip[${++$sip}]`
signature.push(sip)
return map(dispatch, sip, parse.sip)
} ()
const rewind = function () {
if (rewound.length == 0) {
return null
}
const bytes = []
// If the sip is a spread integer, then the collected value is
// missing data from the buffer. For this we would have to have
// another array tracking the bitten bytes.
// ```
// $sip[0] |= ($store[0] = $buffer[$start++]) & 0x7f << 7
// ```
for (let i = 0, I = rewound.length; i < I; i++) {
if (rewound[i][0].endianness == 'big') {
for (let j = 0, J = rewound[i][0].bits / 8; j < J; j++) {
if (j == 0) {
bytes.push(`$sip[${i}] & 0xff`)
} else {
bytes.push(`$sip[${i}] >>> ${j * 8} & 0xff`)
}
}
} else {
for (let i = sip[0].bits / 8 - 1, I = -1; i > I; i--) {
bytes.push(`$sip >>> ${i * 8} & 0xff`)
}
}
}
return $(`
$parse(Buffer.from([
`, bytes.join(',\n'), `
]), 0, ${bytes.length})
`)
} ()
const tests = parse.conditions
.filter(condition => condition.test != null)
.map(condition => condition.test)
const invocations = inliner.accumulations(tests)
signature.push(packet.name)
const start = $step++
const steps = parse.conditions.map(({ test, fields }) => {
const step = $step
const sip = fields[0].type == 'sip'
const source = sip
? conditions(path, fields[0], rewound)
: map(dispatch, path, fields)
return {
step, source, sip, fields, test
}
})
let ladder = '', keywords = 'if'
for (const { test, fields, sip, source, step } of steps) {
const vivified = vivify.assignment(path, { vivify: 'descend', fields })
ladder = test != null ? $(`
`, ladder, `${keywords} (`, inliner.test(path, test, signature), `) {
`, vivified, -1, `
$step = ${step}
`, sip ? null : rewind, `
continue
}
`) : parse.unconditional ? $(`
`, vivified, -1, `
$step = ${step}
`, sip ? null : rewind, `
continue
`) : $(`
`, ladder, ` else {
`, vivified, -1, `
$step = ${step}
`, sip ? null : rewind, `
continue
}
`)
keywords = ' else if'
}
const done = $(`
$step = ${$step}
continue
`)