-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.bqn
664 lines (576 loc) · 23.8 KB
/
parse.bqn
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
⟨
HeaderToFFI # Read string 𝕩 and optional function prefix 𝕨, outputs metaprogrammed file as string
ToFFI # turns header data from ParseHeader into ffi file characters
ParseHeader # Read string 𝕩 and optional function prefix 𝕨, outputs header data
ToPrettyJson # Read header data from ParseHeader and makes a pretty json file
SetPath # Set the path of the resulting FFI script from ToFFI
⟩⇐
json ← •Import "json.bqn"
# util functions
nl ← @+10 # newline
wnl ← @+13 # windows newline prefix
Box ← ⟨⟩⊸≡◶⟨>⋄↕0‿0⟩⊢»¨⟜<' '⥊˜0⌈´≠¨
LowerNames ← +⟜(32×1="A["⊸⍋)⌾(1↑⎉1⊢)⍟(⟨⟩⊸≢)
_MetaParse ← {∾˘´[𝕗,𝕨]{F‿w𝕊𝕩:<w⊸∾˘Box F𝕩}˘○⍉↕∘0‿3⍟(⟨⟩⊸≡)𝕩}
remap ← {𝕏⌾⌽}{𝕎𝕏}´{(𝕨⊸≡∧"*"≡1↑⊣)◶⟨⋈⋄𝕩⋈˜1↓⊣⟩´}´¨⟨
"unsigned char" ‿"ustr"
"char" ‿"str"
"void" ‿"ptr"
"rAudioBuffer" ‿"ptr"
"rAudioProcessor"‿"ptr"
⟩
constText←"const "
SwitchOrder ← {⌽⌾((𝕩∊'0'+↕10)⊸/)1⊸⌽⌾((𝕩∊"[]")⊸/)𝕩}⌽
shortendStructNames←[
"v2"‿"Vector2"
"v3"‿"Vector3"
"v4"‿"Vector4"
]
typeMapping ← ⟨
"UNKNOWN"
"MACRO"
"GUARD"
"INT"
"INT_MATH"
"LONG"
"LONG_MATH"
"FLOAT"
"FLOAT_MATH"
"DOUBLE"
"DOUBLE_MATH"
"CHAR"
"STRING"
"COLOR"
⟩
typeReplacements ← ⍉shortendStructNames∾[
# remove since they're not defined
"ptr"‿"AudioCallback"
"ptr"‿"LoadFileDataCallback"
"ptr"‿"LoadFileTextCallback"
"ptr"‿"SaveFileDataCallback"
"ptr"‿"SaveFileTextCallback"
"ptr"‿"TraceLogCallback"
"""f64"""‿"double"
"""i64"""‿"long"
"f" ‿"float"
"i" ‿"int"
"ptr" ‿"..."
"u" ‿"unsigned int"
"u8" ‿"char"
"u8" ‿"unsigned char"
"""u16"""‿"unsigned short"
" " ‿"void"
]
SetPath ← {path𝕊ffi:
default ← "⊑•args" # defaultPathDefinition
(⊐⟜1 default⍷ffi)(↑∾'"'∾path∾'"'∾default∾⁼↓)ffi
}
# turns header data from ParseHeader into ffi file characters
# 𝕨 is an optional name mapping, used so you can rename functions and fix name collisions
## Example1: ToFFI ParseHeader file.h
## Example2: [⟨"bad_name"⟩⋄⟨"CoolName"⟩] ToFFI ParseHeader file.h
ToFFI ← {𝕨𝕊data: nameMapping ← 𝕨⊣↕2‿0
# TODO check validity of names in the mapping
MapNames ← {
[old⋄new]←nameMapping
(new⊏˜old⊸⊐)⌾((𝕩∊old)⊸/)𝕩
}
mapArgTypes ← {
[bqn,c] ← typeReplacements
{
consts←constText⊸(⊣≡≠⊸↑)¨𝕩
s ← ¬consts∨¬⊑¨'*'⊸∊¨𝕩
cleaned←constText⊸∾⁼¨⌾(consts⊸/)𝕩
[kept,removed] ← ⍉Remap˘(↕0‿2)∾´∨`∘∊⟜"[*"⊸(¬⊸/∨`⌾⌽∘≠⟜' '⊸/⊸⋈SwitchOrder∘/)¨cleaned
replaced ← kept LowerNames∘(MapNames⌾⋈)∘⊣⍟(0⊸≡)¨(c⊐kept)⊏bqn∾0
removed {""𝕊𝕩:𝕩;'"'≡⊑𝕩?𝕨⊸∾⌾('"'⊸∾⁼)𝕩;'"'∾𝕨∾"""∾"∾𝕩}¨ replaced
}
}
MapStructTypes ← {
[bqn,c] ← typeReplacements
consts←constText⊸(⊣≡≠⊸↑)¨𝕩
s ← ¬consts∨¬⊑¨'*'⊸∊¨𝕩
cleaned←constText⊸∾⁼¨⌾(consts⊸/)𝕩
[kept,removed] ← ⍉Remap˘(↕0‿2)∾´∨`∘∊⟜"[*"⊸(¬⊸/∨`⌾⌽∘≠⟜' '⊸/⊸⋈SwitchOrder∘/)¨cleaned
AddRemoved ← {""𝕊𝕩:𝕩;'"'≡⊑𝕩?𝕨⊸∾⌾('"'⊸∾⁼)𝕩;'"'∾𝕨∾"""∾"∾𝕩}
replaced ← kept LowerNames∘(MapNames⌾⋈)∘⊣⍟(0⊸≡)¨(c⊐kept)⊏bqn∾0
removed AddRemoved¨ replaced
}
mapStructNames ← {
[bqn,c]←⍉shortendStructNames
{𝕩LowerNames∘(MapNames⌾⋈)∘⊣⍟(0⊸≡)¨(c⊐𝕩)⊏bqn∾0}
}
mapFuncReturnType ← {
[bqn,c] ← typeReplacements
{
cleaned←𝕩↓˜¨constText≠⊸×constText⊸(⊣≡≠⊸↑)¨𝕩
[kept,removed] ← ⍉Remap˘(↕0‿2)∾´ ∨`∘∊⟜"[*"⊸(¬⊸/∾⟜" "⁼⍟(" "≡¯1⊸↑)⊸⋈SwitchOrder∘/)¨cleaned
AddRemoved ← {""𝕊𝕩:𝕩;'"'≡⊑𝕩?𝕨⊸∾⌾('"'⊸∾⁼)𝕩;'"'∾𝕨∾"""∾"∾𝕩}
replaced ← kept LowerNames∘(MapNames⌾⋈)∘⊣⍟(0⊸≡)¨(c⊐kept)⊏bqn∾0
{'('∾𝕩∾')'}⍟(∨´"∾⊣"⊸∊)¨"ptr⊣"⊸∾⍟(("ustr"≡¯4⊸↑)∨"str"≡¯3⊸↑)¨ removed AddRemoved¨ replaced
}
}
defines‿structs‿aliases‿enums‿callbacks‿functions←{
!𝕩⊣˝⊸≡"defines"‿"structs"‿"aliases"‿"enums"‿"callbacks"‿"functions"
⊢˝¨¨⌾(0‿1‿3‿4‿5⊸⊏)⊢˝𝕩
} json.Parse ToPrettyJson data
top ← 1↓∾nl∾¨⟨
"#### THIS TEXT HAS BEEN AUTOGENERATED ####"
""
•FChars "setupMacro.bqn"
⟩
# so you mean if there are no collisions it's all fine
# but if there are collisions it errors,
# unless you provide the header to ffi script a input
# that's a mapping of c names to bqn names
# TODO HANDLE THESE LATER
# typedef struct rAudioBuffer rAudioBuffer;
# typedef struct rAudioProcessor rAudioProcessor;
definesDone ← {
exclTypes ← "GUARD"‿"MACRO"‿"UNKNOWN"‿"FLOAT_MATH"‿"COLOR"
filtered ← {𝕊name‿type‿val‿desc:
¬∨´⟨"PI"≡name, ∨´type⊸≡¨exclTypes, ⟨⟩≡val ⟩
}˘⊸/(↕0‿4)∾´defines
<˘""‿""‿" ⇐ "‿" # "⟨LowerNames¨MapNames,⟨⟩¨,'-'⍟('¯'⊸≡)¨∘•Repr¨,⊢⟩_MetaParse filtered
}
enumsDone ← {
[names,descriptions,values]←⍉(↕0‿3)∾´enums
tabled ←((↕0‿3)∾´⊢˝¨)¨ values
bodys ← (<˘(" ")‿" ⇐ "‿" # "⟨LowerNames¨MapNames,•Repr¨,⊢⟩_MetaParse⊢)¨tabled
headers ← (MapNames names) LowerNames⊸∾¨<˘" ⇐ { # "⊸∾˘Box descriptions
∾headers<⊸∾¨∾⟜⟨"}"⟩¨bodys
}
structsDone ← {
[names,descriptions,fields]←⍉(↕0‿3)∾´structs
headers ← (MapStructNames names) ∾⟜" ⇐ ⟨ # "⊸∾¨ descriptions
bodys ← {⟨"⟩"⟩∾˜<˘" "‿" # "‿" # "⟨MapStructTypes,⊢,⊢⟩_MetaParse (↕0‿3)∾´𝕩}¨⊢˝¨¨fields
assembled ← headers <⊸∾¨ bodys
aliasLines←»⊸∨∘≠⟜' '⊸/¨<˘""‿" ⇐ "‿" # "⟨LowerNames¨MapNames,MapArgTypes,⊢⟩_MetaParse 1‿0‿2⊸⊏˘(↕0‿3)∾´⊢˝¨aliases
p←⊑¨⊢˝¨aliases
∾ ∾∾⟜(<⋈)¨⟜aliasLines⌾(¯1⊸↓)assembled⊔˜+`»+˝p≡⌜names
}
functionsDone ← {
s ← {
((⊑'*'⊸∊)¨∧constText⊸(⊣≢≠⊸↑)¨)1‿0⊸⊑¨¯1⊑𝕩
}¨∾⟜⟨⟨⟩⟩⍟(3=≠)¨functions
sLines←∨´¨s
s2 ← {
"const void *"⊸≡¨1‿0⊸⊑¨¯1⊑𝕩
}¨∾⟜⟨⟨⟩⟩⍟(3=≠)¨functions
s2Lines←∨´¨s2
!∧´¬s2Lines∧sLines # I don't want the lines to collide
sAllLinesExtended ← (↕∘≠⊸+⌾/↑˜+´+≠)s2Lines∨sLines
s1LinesExtended‿s2LinesExtended ← s2Lines{((+´+≠)𝕩+𝕨)↑𝕨⊸{((/𝕨)¬∘∊˜𝕩)/𝕩+↕≠𝕩}⌾/𝕨+𝕩}{𝔽⋈𝔽˜}sLines
MakeConstDistinct ← (sAllLinesExtended¬⊸×(1+s2Lines∨sLines)/s+2×s2){'⟩'∾˜1↓∾'⋄'∾¨(⊑"ptr"‿"ustr"‿"str"⊐<)◶⟨"""&u8"""⋄"""&u8"""⋄"""&u8:c8"""⋄"""&"⊸∾"""*"⊸∾⁼⟩¨⌾((1=𝕨)⊸/)"""*i8"""¨⌾((2=𝕨)⊸/)𝕩}¨⊢
<˘""‿" ⇐ "‿""""‿" _R⟨"‿"#"‿" # "⟨
∾⟜"Raw"¨⌾((»s2LinesExtended)⊸/)·∾⟜"Ref"¨⌾((»s1LinesExtended)⊸/)LowerNames¨∘MapNames ⋄ MapFuncReturnType ⋄ ∾⟜'"'¨ ⋄ MakeConstDistinct MapArgTypes¨ ⋄ (∾' '⊸∾¨)¨ ⋄ ⊢
⟩_MetaParse⍟(⟨⟩⊸≢){
0‿2‿0‿3‿4‿1⊏(3↑𝕩)∾⍉{<˘⍉(↕0‿2)∾´⊢˝¨<⁼𝕩}˘⊢˝𝕩
}⌾⍉⍟(⟨⟩⊸≢)(1+s2Lines∨sLines)/(↕0‿4)∾´∾⟜⟨⟨⟩⟩⍟(3=≠)¨functions
}
top∾∾nl∾¨∨`⌾⌽∘≠⟜' '⊸/¨∾1↓∾⟨""⟩⊸⋈¨definesDone‿enumsDone‿structsDone‿functionsDone
}
# Type of parsed define
⟨
unknown
macro
guard
int
int_math
long
long_math
float
float_math
double
double_math
char
string
color
⟩ ← ↕14
# Get data type and name from a string containing both
# NOTE: Useful to parse function parameters and struct fields
GetDataTypeAndName ← (∨`⌾⌽∊⟜"* ">∨`∘=⟜',')⊸(/⋈¬⊸/) #‿type‿name: # const char *typeName, int typeNameLen, char *type, char *name)
# Get comment from a line, do nothing if no comment in line
GetDescription ← ∨`∘≠⟜' '⊸/2↓∨`∘∧⟜«∘=⟜'/'⊸/ # str → description (str)
defMacro ← "#define"
# Reads list of characters 𝕩 (headerChars) that represent a header file.
# Optional 𝕨 (definePrefix), needed if you want to parse functions. It's the function prefix of choice.
# Outputs header data.
# Example1: "RLAPI" ParseHeader •FChars "file-name.h"
# Example2: ParseHeader •FChars "other-file.h"
ParseHeader ← {𝕨𝕊headerChars:
# Example:
# 𝕨 ← "RLAPI" # Functions define (i.e. RLAPI for raylib.h)
definePrefix ← 𝕨⊣"NO_FUNCTIONS"
headerLines ← nl((+`׬)⊸-∘=⊔⊢) wnl⊸≠⊸/ headerChars
lines ← ' '⍟((9+@)⊸≡)¨¨ headerLines
defines ← (∊⊑¨)⊸/ ¯1⊑¨{lastDefines𝕊𝕩: defineLine←⊑𝕩
name‿args ← defineLine {𝕎𝕩}´⌽⟨
(∨`∧∨`⌾⌽)∘(∧`∘∨⟜«⊸∧˝"/ "≠⌜⊢)⊸/
defMacro⊸∾⁼ # Skip defMacro
∨`∘≠⟜' '⊸/ # skip spaces
≤´∘⊐⟜" ("◶⟨1+⊐⟜')' ⋄ ⊐⟜' '⟩⊸(↑⋈∨`∘≠⟜' '⊸/∘↓) # name‿args
⟩
# Determine type
ishex ← 0
type ← {
""""≡1↑args?
string
;
"'"≡1↑args?
char
;
"CLITERAL(Color)"(⊣≡≠⊸↑)args?
color
;
⊑"0123456789"∊˜1↑args?
chars ← ∧`∘≠⟜' '⊸/args # up to first space
isHex ↩ ∨´'x'=chars
{𝕤
∨´'.'=chars?
double‿float⊑˜'f'=𝕩
;
int‿long⊑˜'L'=𝕩
}⊑¬∘«∘∊⟜"0123456789xL.+-ABCDEFabcdef"⊸/chars
;
type ← unknown‿macro⊑˜")"≡¯1↑name
type‿guard⊑˜0=≠args
}
# Extracting value
value ← args↓˜-⊑type∊long‿float # Remove number postfix
# Extracting description
description ← GetDescription defineLine
# Parse defines of type UNKNOWN to find calculated numbers
{𝕤
numberLetters ← "0123456789xL.+-abcdefABCDEF"
numbers ← (¬-˜⊢×·+`»⊸>)∘∊⟜"0123456789."⊸⊔ value
operands ← (¬-˜⊢×·+`»⊸>)∘∊⟜"()+-*/ "⊸⊔ value
numTypes ← ⟨
int
int_math
long
long_math
float
float_math
double
double_math
⟩
# Search previous defines for operand
# Found operand and it's a number -> update type
[names,types] ← ⍉(↕0‿2)∾´2↑¨lastDefines
areNums ← numTypes∊˜typeMapping⊐types
type ⌈´↩ {⌈´types/˜areNums∧𝕩}˘operands≡⌜names
# Read number operand
# Found a valid number -> update largestType
type ⌈´↩ {
[
long_math‿int_math
float_math‿double_math
]⊑˜(∨´'.'=𝕩)(⊣⋈⊑)"Lf"=⊑𝕩
}¨∧´∘∊⟜numberLetters¨⊸/numbers
}⍟⊢type=unknown
lastNames←⊑¨lastDefines
value {'-'⍟('¯'⊸≡)¨•Repr 16⊸×⊸+˜´⌽(∾"0A"+⟜↕¨10‿6)⊐-⟜(32×1="a{"⊸⍋)"0x"∾⁼𝕩}⍟ishex↩
value {
∨´type=int‿long‿float‿double?
•ParseFloat𝕩
;
type = string?
𝕩
;
'"'∾𝕩∾'"'
}↩
𝕨 ∾⟜< name‿(type⊑typeMapping)‿value‿description
}`´(<⟨⟩)⋈↓⟜lines¨/defMacro⊸(⊣≡≠⊸↑)¨∨`∘≠⟜' '⊸/¨lines
# Structs info data
structs ← {𝕤
structLines ← ∨`∘≠⟜' '⊸/¨𝕩↓lines
desc ← GetDescription (𝕩-1)⊑lines # Parse struct description
name←' '⊸≠⊸/∧`∘≠⟜'{'⊸/"typedef struct "∾⁼⊑structLines # Get struct name: typedef struct name {
fields ← ∾´{ # Get struct fields and count them -> fields finish with ;
fieldType‿fieldNamesUnparsed ← ∨`∘≠⟜' '⊸/⌾⌽¨ GetDataTypeAndName ⊐⟜';'⊸↑𝕩
fieldNames ← ∨`∘≠⟜' '⊸/¨ ','((⊢-˜+`׬)∘=⊔⊢)fieldNamesUnparsed
fieldDesc ← GetDescription 𝕩
# Move type len info from name to type
fieldTypes ← fieldType⊸∾¨∨`∘=⟜'['⊸/¨fieldnames
fieldnames ∧`∘≠⟜'['⊸/¨↩
(<fieldDesc)⊸∾¨fieldTypes⋈¨fieldNames
}¨(∧´"struct"‿"/"‿" "≢¨6‿1‿1↑¨<)¨⊸/∨`∘≠⟜' '⊸/¨(∧`'}'≠·∾´1⊸↑¨)⊸/1↓structLines
name‿desc‿fields
# Read struct lines
# Find structs
# starting with "typedef struct ... {" or "typedef struct ... ; \n struct ... {"
# ending with "} ... ;"
# i.e. excluding "typedef struct rAudioBuffer rAudioBuffer;" -> Typedef and forward declaration only
}¨/{
[m1,m2]←"typedef struct"‿"struct"(⊣≡≠⊸↑)⌜ ∨`∘≠⟜' '⊸/¨𝕩
m1∧m2«⊸∨<´∘⊐⟜"{;"¨𝕩
} lines
# Alias info data
aliases ← {𝕤
aliasLine ← 𝕩⊑lines
desc ← (lines⊑˜𝕩-1)⊣⍟(""⊸≡)○GetDescription aliasLine
type‿name ← ' '((¬-˜⊢×·+`»⊸>)∘≠⊔⊢)"typedef "∾⁼⊐⟜'/'⊸↑aliasLine
name ∾⟜";"⁼↩
type‿name‿desc
}¨/(∨´∘=⟜';'∧"typedef"⊸(⊣≡≠⊸↑)∧2=(+´' '⊸=>∨`∘=⟜';'))¨lines # Find aliases (lines with "typedef ... ...;")
enums ← {
# Parse enum description
# NOTE: This is not necessarily from the line immediately before,
# some of the enums have extra lines between the "description"
# and the typedef enum
desc ← GetDescription ⟨⟩⊸≡◶⊑‿""(∧`("// "≡3⊸↑)¨)⊸/⌾⌽𝕩↑lines
# Parse enum value line, possible options:
#ENUM_VALUE_NAME,
#ENUM_VALUE_NAME
#ENUM_VALUE_NAME = 99
#ENUM_VALUE_NAME = 99,
#ENUM_VALUE_NAME = 0x00000040, # Value description
values ← {𝕤
valueInteger ← ⟨⟩
# We start reading the value name
valueName←(∧`·∧˝", ="≠⌜⊢)⊸/𝕩
# After the name we can have:
# '=' -> value is provided
# ',' -> value is equal to previous + 1, there could be a description if not '\0'
# ' ' -> value is equal to previous + 1, there could be a description if not '\0'
# '\0' -> value is equal to previous + 1
# Let's start checking if the line is not finished
# Two options:
# '=' -> value is provided
# ' ' -> value is equal to previous + 1, there could be a description if not '\0'
valuesUnparsed←¬∘∊⟜", "⊸/valueName∾⁼⊐⟜'/'⊸↑𝕩
value ← {"="≡1↑𝕩?
{"0x"≡2↑𝕩?
16⊸×⊸+˜´⌽(∾"0A"+⟜↕¨10‿6)⊐-⟜(32×1="a{"⊸⍋)2↓𝕩
;
•ParseFloat𝕩
}'='⊸∾⁼𝕩
;@
} valuesUnparsed
# Parse value description
valueDesc ← GetDescription 𝕩
valueName‿value‿valueDesc
}¨((≥⟜'A'∧'Z'⊸≥)∘⊑¨1↑¨⊢)⊸/ ∨`∘≠⟜' '⊸/¨(∧`'}'≠·⊑¨1↑¨⊢)⊸/lines↓˜1+𝕩
values {𝕩=@?𝕨+1;𝕩}`⌾(1⊸⊑¨)↩
values ('-'⍟('¯'⊸≡)¨•Repr)¨⌾(1⊸⊑¨)↩
# Get enum name from typedef
name←"} "∾⁼⊐⟜';'⊸↑ (⊑∘⊐⟜'}'·∾´1↑¨⊢)⊸⊑lines↓˜1+𝕩
name‿desc‿values
# Read enum lines
# ignore inline enums
}¨/("typedef enum {"⊸(⊣≡≠⊸↑)∧∨´∘≠⟜';')¨lines
callbacks ← {
RLS ← ∨`∘≠⟜' '⊸/
# Return type
afterDef ← RLS "typedef "∾⁼𝕩
retType ← RLS⌾⌽ ⊐⟜'('⊸↑afterDef
afterStar ← RLS "(*"∾⁼ RLS retType∾⁼RLS afterDef
name ← RLS⌾⌽ ⊐⟜')'⊸↑ afterStar
afterName ← RLS")("∾⁼RLS name∾⁼afterStar
# ⟨paramType‿paramName, ...⟩
[paramTypes,paramNames] ← ⍉>GetDataTypeAndName¨ ','((⊢-˜+`׬)∘=⊔⊢) ⊐⟜')'⊸↑afterName
# Move array sizes from name to type
paramTypes∾¨↩⊐⟜'['⊸↓¨paramNames
paramNames ⊐⟜'['⊸↑¨↩
paramTypes (∨`∧∨`⌾⌽)∘≠⟜' '⊸/¨↩
# Description
desc ← GetDescription 𝕩
name‿desc‿retType‿paramTypes‿paramNames
}¨{
# Read callback lines
# Find callbacks (lines with "typedef ... (* ... )( ... );")
m1←"typedef"⊸(⊣≡≠⊸↑)¨𝕩
m1∧{((∨´");"⊸≡˘)∧(∨´")("⊸≡˘)∧(∨´"(*"⊸≡˘))2↕" "⍟(""⊸≡)𝕩}¨𝕩
}⊸/lines
functions ← {
funcSignature ← 𝕩∾⁼˜definePrefix∾' '
retType‿name ← GetDataTypeAndName ⊐⟜'('⊸↑funcSignature
retType (∨`∧∨`⌾⌽)∘≠⟜' '⊸/↩
[paramTypes⋄paramNames]←⟨⟩⊸≢◶⟨↕2‿0⋄⍉>⟩("void"≢¯4⊸↑)◶⟨
⟨⟩ ⋄ {GetDataTypeAndName¨(∨`∧∨⟜«)∘≠⟜' '⊸/¨','((⊢-˜+`׬)∘=⊔⊢)𝕩}
⟩ ⊐⟜')'⊸↑(1+⊐⟜'(')⊸↓funcSignature
# Move array sizes from name to type
paramTypes∾¨↩⊐⟜'['⊸↓¨paramNames
paramTypes (∨`∧∨`⌾⌽)∘≠⟜' '⊸/¨↩
paramNames ⊐⟜'['⊸↑¨↩
paramTypes‿paramNames {<˘"..."⊸≡◶⟨⋈⋄"..."‿"args"⟩´˘⌾⍉>𝕩}↩
desc ← GetDescription 𝕩
name‿desc‿retType‿paramNames‿paramTypes
# Read function lines
# Read function line (starting with `define`)
}¨definePrefix⊸(⊣≡≠⊸↑)¨⊸/lines
defines‿structs‿aliases‿enums‿callbacks‿functions
}
⟨UnEscape, Escape⟩ ← {
in ← """\/bfrnt"
out ← (3↑in)∾@+8‿12‿13‿10‿9
diff ← (out-in) ∾ 0
Basic ← {
i ← in ⊐ 𝕩
"Unknown escape" ! ∧´𝕨≤i<≠in
𝕩 + 𝕨 × i ⊏ diff
}
hc ← "0Aa"
hb ← ⥊hc+0≍˘10‿6‿6 # Hex boundaries, start and after-end
ho ← 2/hc-0‿10‿10 # Corresponding offsets
Hex ← { u 𝕊 𝕩:
d ← 𝕩 /˜ m ← ≠` (4⥊0)⊸»⊸≠ »u
t ← hb ⍋ d
"String \u must be followed by 4 hex characters" ! ∧´1=2|t
# Now m can't run past the end or self-intersect,
# or it would have hit a closing quote or backslash
v ← 16⊸×⊸+˜˝⌽ ⍉∘‿4⥊ d-t⊏ho
w‿e ← Surrogate v
⟨(w+@-'u')⊸+⌾(u⊸/)𝕩, e⌾(u⊸/)m⟩
}
sr ← 2⋆10 # Surrogate base/radix
sb ← sr×52+2+↕3 # Surrogate character boundaries
Surrogate ← {
c ← (≠sb)|sb⍋𝕩 # 0 for non-surrogate, 1 then 2 for surrogate
h ← 1=c # First half
"Unmatched surrogate pair" ! (0∾h) ≡ (2=c)∾0
r ← 𝕩 - c⊏0∾sb # Numeric value of surrogates
v ← r + »h×sr×(2⋆6)+r
⟨v, h⟩
}
UnEscape ⇐ { e 𝕊 𝕩:
u ← e ∧ 𝕩='u'
(u<e)⊸Basic⌾⊑ u Hex⟜⊑⍟(∨´u) 𝕩‿0
}
Hex32 ← { # Convert 𝕩<32 to two hex digits
u‿l ← 16(⌊∘÷˜⋈|)𝕩 # Upper and lower digits; ∧´u<2
'0'+u≍˘l-(10+-´"0A")×10≤l
}
Escape ⇐ {
e ← (𝕩∊2↑in) ∨ 𝕩<@+32 # Quote, backslash, and control characters
j ← /e ⋄ k ← ¬e # Their indices; characters to keep
c ← j ⊏ 𝕩 # Characters to be escaped
m ← c - (out⊐c)⊏diff # Escape the basic ones
g ← 2 ∾˜ m<@+32 # Group 𝕨 to separate...
n‿u ← g ⊔ m # Characters requiring basic, hex escapes
i ← ∾⟨/k⟩∾2‿6/¨g⊔j # Target indices
i ⍋⊸⊏ ∾⟨ # Use them to reorder:
k/𝕩 # Kept characters
⥊'\'≍˘n # Basic escapes
⥊"\u00"⊸∾˘ Hex32 u-@ # Hex escapes
⟩
}
}
# Reads header data from ParseHeader and makes a pretty json file
ToPrettyJson ← {𝕊defines‿structs‿aliases‿enums‿callbacks‿functions:
EscapeBackslashes←∾'\'⊸=◶⋈‿"\\"¨
# Export parsed data in json
1↓∾nl∾¨∾⟨
⋈"{"
# Print defines info
⋈" ""defines"": ["
¯1⊸↓⌾(¯1⊸⊑)⍟(⟨⟩⊸≢)∾{𝕊name‿type‿value‿description:∾⟨
⋈" {"
⋈" ""name"": """∾name∾""","
⋈" ""type"": """∾type∾""","
⋈" ""value"": "∾('-'⍟('¯'⊸≡)¨•Repr⍟(0≡=) value)∾','
⋈" ""description"": """∾(Escape description)∾""""
⋈" },"
⟩}¨defines
⋈" ],"
# Print structs info
⋈" ""structs"": ["
¯1⊸↓⌾(¯1⊸⊑)⍟(⟨⟩⊸≢)∾{𝕊name‿desc‿fields:∾⟨
⋈" {"
⋈" ""name"": """∾name∾""","
⋈" ""description"": """∾(Escape desc)∾""","
⋈" ""fields"": ["
¯1⊸↓⌾(¯1⊸⊑)⍟(⟨⟩⊸≢)⟨⟩∾´{𝕊fieldDesc‿fieldType‿fieldName:⟨
" {"
" ""type"": """∾fieldType∾""","
" ""name"": """∾fieldName∾""","
" ""description"": """∾(Escape fieldDesc)∾""""
" },"
⟩}¨fields
⋈" ]"
⋈" },"
⟩}¨structs
⋈" ],"
# Print aliases info
⋈" ""aliases"": ["
¯1⊸↓⌾(¯1⊸⊑)⍟(⟨⟩⊸≢)∾{𝕊type‿name‿desc:⟨
" {"
" ""type"": """∾type∾""","
" ""name"": """∾name∾""","
" ""description"": """∾desc∾'"'
" },"
⟩}¨aliases
⋈" ],"
# Print enums info
⋈" ""enums"": ["
¯1⊸↓⌾(¯1⊸⊑)⍟(⟨⟩⊸≢)∾{𝕊name‿desc‿values:
∾⟨
⋈" {"
⋈" ""name"": """∾name∾""","
⋈" ""description"": """∾(Escape desc)∾""","
⋈" ""values"": ["
¯1⊸↓⌾(¯1⊸⊑)⍟(⟨⟩⊸≢)∾{𝕊valueName‿value‿valueDesc:
⟨
" {"
" ""name"": """∾valueName∾""","
" ""value"": "∾value∾","
" ""description"": """∾(Escape valueDesc)∾'"'
" },"
⟩
}¨values
⋈" ]"
⋈" },"
⟩
}¨enums
⋈" ],"
# Print callbacks info
⋈" ""callbacks"": ["
¯1⊸↓⌾(¯1⊸⊑)⍟(⟨⟩⊸≢)∾{𝕊name‿desc‿retType‿paramTypes‿paramNames:∾⟨
⋈" {"
⋈" ""name"": """∾name∾""","
⋈" ""description"": """∾(Escape desc)∾""",",
⋈" ""returnType"": """∾retType∾""""∾","/˜⟨⟩≢paramTypes
⟨⟩⍟(⟨⟩≡paramTypes)∾⟨
⋈" ""params"": ["
¯1⊸↓⌾(¯1⊸⊑)⍟(⟨⟩⊸≢)∾paramTypes{
⟨
" {"
" ""type"": """∾𝕨∾""","
" ""name"": """∾𝕩∾""""
" },"
⟩
}¨paramNames
⋈" ]"
⟩
⋈" },"
⟩}¨callbacks
⋈" ],"
# Print functions info
⋈" ""functions"": ["
¯1⊸↓⌾(¯1⊸⊑)⍟(⟨⟩⊸≢)∾{𝕊name‿desc‿retType‿paramNames‿paramTypes:
∾⟨
⋈" {"
⋈" ""name"": """∾name∾""","
⋈" ""description"": """∾(Escape desc)∾""","
⋈" ""returnType"": """∾retType∾""""∾","/˜⟨⟩≢paramTypes
⟨⟩⍟(⟨⟩≡paramTypes) ∾⟨
⋈" ""params"": ["
¯1⊸↓⌾(¯1⊸⊑)⍟(⟨⟩⊸≢)∾paramTypes{
⟨
" {"
" ""type"": """∾𝕨∾""","
" ""name"": """∾𝕩∾'"'
" },"
⟩
}¨paramNames
⋈" ]"
⟩
⋈" },"
⟩
}¨functions
⋈" ]"
⋈"}"
⟩
}
HeaderToFFI ← ToFFI ParseHeader