aboutsummaryrefslogtreecommitdiff
path: root/tools/lxdrgen-swift.awk
blob: 22de56f49f602dc20a875f82e01ffab41d8020f8 (plain)
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
# lxdrgen-swift.awk: Swift backend for lxdrgen.awk.
#
# Copyright (c) 2023, Přemysl Eric Janouch <p@janouch.name>
# SPDX-License-Identifier: 0BSD

function define_internal(name, swifttype) {
	Types[name] = "internal"
	CodegenSwiftType[name] = swifttype
	CodegenDeserialize[name] = "%s.read()"
}

function define_sint(size,    shortname, swifttype) {
	shortname = "i" size
	swifttype = "Int" size
	define_internal(shortname, swifttype)
}

function define_uint(size,    shortname, swifttype) {
	shortname = "u" size
	swifttype = "UInt" size
	define_internal(shortname, swifttype)
}

function codegen_begin() {
	define_sint("8")
	define_sint("16")
	define_sint("32")
	define_sint("64")
	define_uint("8")
	define_uint("16")
	define_uint("32")
	define_uint("64")
	define_internal("bool", "Bool")
	define_internal("string", "String")

	print "// Code generated from " FILENAME ". DO NOT EDIT."
	print "import Foundation"
	print ""
	print "public struct " PrefixCamel "Reader {"
	print "\tpublic var data: Data"
	print ""
	print "\tpublic enum ReadError: Error {"
	print "\t\tcase unexpectedEOF"
	print "\t\tcase invalidEncoding"
	print "\t\tcase overflow"
	print "\t\tcase unexpectedValue"
	print "\t}"
	print ""
	print "\tpublic mutating func read<T: FixedWidthInteger>() throws -> T {"
	print "\t\tlet size = MemoryLayout<T>.size"
	print "\t\tguard data.count >= size else {"
	print "\t\t\tthrow ReadError.unexpectedEOF"
	print "\t\t}"
	print "\t\tvar acc: T = 0"
	print "\t\tdata.prefix(size).forEach { acc = acc << 8 | T($0) }"
	print "\t\tdata = data.dropFirst(size)"
	print "\t\treturn acc"
	print "\t}"
	print ""
	print "\tpublic mutating func read() throws -> Bool {"
	print "\t\ttry read() != UInt8(0)"
	print "\t}"
	print ""
	print "\tpublic mutating func read() throws -> String {"
	print "\t\tlet size: UInt32 = try self.read()"
	print "\t\tguard let count = Int(exactly: size) else {"
	print "\t\t\tthrow ReadError.overflow"
	print "\t\t}"
	print "\t\tguard data.count >= count else {"
	print "\t\t\tthrow ReadError.unexpectedEOF"
	print "\t\t}"
	print "\t\tdefer {"
	print "\t\t\tdata = data.dropFirst(count)"
	print "\t\t}"
	print "\t\tif let s = String(data: data.prefix(count), encoding: .utf8) {"
	print "\t\t\treturn s"
	print "\t\t} else {"
	print "\t\t\tthrow ReadError.invalidEncoding"
	print "\t\t}"
	print "\t}"
	print ""
	print "\tpublic mutating func read<" \
		"T: RawRepresentable<Int8>>() throws -> T {"
	print "\t\tguard let value = T(rawValue: try read()) else {"
	print "\t\t\tthrow ReadError.unexpectedValue"
	print "\t\t}"
	print "\t\treturn value"
	print "\t}"
	print ""
	print "\tpublic mutating func read<T>("
	print "\t\t\t_ read: (inout Self) throws -> T) throws -> [T] {"
	print "\t\tlet size: UInt32 = try self.read()"
	print "\t\tguard let count = Int(exactly: size) else {"
	print "\t\t\tthrow ReadError.overflow"
	print "\t\t}"
	print "\t\tvar array = [T]()"
	print "\t\tarray.reserveCapacity(count)"
	print "\t\tfor _ in 0..<count {"
	print "\t\t\tarray.append(try read(&self))"
	print "\t\t}"
	print "\t\treturn array"
	print "\t}"
	print "}"
	print ""
	print "public struct " PrefixCamel "Writer {"
	print "\tpublic var data = Data()"
	print ""
	print "\tpublic mutating func append<T: FixedWidthInteger>(_ number: T) {"
	print "\t\tvar n = number.byteSwapped"
	print "\t\tfor _ in 0..<MemoryLayout<T>.size {"
	print "\t\t\tdata.append(UInt8(truncatingIfNeeded: n))"
	print "\t\t\tn >>= 8"
	print "\t\t}"
	print "\t}"
	print ""
	print "\tpublic mutating func append(_ bool: Bool) {"
	print "\t\tappend(UInt8(bool ? 1 : 0))"
	print "\t}"
	print ""
	print "\tpublic mutating func append(_ string: String) {"
	print "\t\tlet bytes = string.data(using: .utf8)!"
	print "\t\tappend(UInt32(bytes.count))"
	print "\t\tdata.append(bytes)"
	print "\t}"
	print ""
	print "\tpublic mutating func append<T: " \
		"RawRepresentable<Int8>>(_ value: T) {"
	print "\t\tappend(value.rawValue)"
	print "\t}"
	print ""
	print "\tpublic mutating func append<T>("
	print "\t\t\t_ array: Array<T>, _ write: (inout Self, T) -> ()) {"
	print "\t\tappend(UInt32(array.count))"
	print "\t\tfor i in 0..<array.count {"
	print "\t\t\twrite(&self, array[i])"
	print "\t\t}"
	print "\t}"
	print ""
	print "\tpublic mutating func append<T: " \
		PrefixCamel "Encodable>(_ value: T) {"
	print "\t\tvalue.encode(to: &self)"
	print "\t}"
	print "}"
	print ""
	print "public protocol " PrefixCamel "Encodable { " \
		"func encode(to: inout " PrefixCamel "Writer) }"
}

function codegen_constant(name, value) {
	print ""
	print "public let " decapitalize(PrefixCamel snaketocamel(name)) " = " value
}

function codegen_enum_value(name, subname, value, cg) {
	append(cg, "fields",
		"\tcase " decapitalize(snaketocamel(subname)) " = " value "\n")
}

function codegen_enum(name, cg,    swifttype) {
	swifttype = PrefixCamel name
	print ""
	print "public enum " swifttype ": Int8 {"
	print cg["fields"] "}"

	CodegenSwiftType[name] = swifttype
	CodegenDeserialize[name] = "%s.read()"
	for (i in cg)
		delete cg[i]
}

function codegen_struct_field(d, cg,    camel) {
	camel = decapitalize(snaketocamel(d["name"]))
	if (!d["isarray"]) {
		append(cg, "fields",
			"\tpublic var " camel ": " CodegenSwiftType[d["type"]] "\n")
		append(cg, "deserialize",
			"\t\tself." camel " = try " \
				sprintf(CodegenDeserialize[d["type"]], "from") "\n")
		append(cg, "serialize",
			"\t\tto.append(self." camel ")\n")
		return
	}

	append(cg, "fields",
		"\tpublic var " camel ": [" CodegenSwiftType[d["type"]] "]\n")
	append(cg, "deserialize",
		"\t\tself." camel " = try from.read() { r in try " \
			sprintf(CodegenDeserialize[d["type"]], "r") " }\n")
	append(cg, "serialize",
		"\t\tto.append(self." camel ") { (w, value) in w.append(value) }\n")
}

function codegen_struct_tag(d, cg,    camel) {
	camel = decapitalize(snaketocamel(d["name"]))
	append(cg, "serialize",
		"\t\tto.append(self." camel ")\n")
}

function codegen_struct(name, cg,    swifttype) {
	swifttype = PrefixCamel name
	print ""
	print "public struct " swifttype " {\n" cg["fields"] "}"
	print ""
	print "extension " swifttype ": " PrefixCamel "Encodable {"
	print "\tpublic init(from: inout " PrefixCamel "Reader) throws {"
	print cg["deserialize"] "\t}"
	print ""
	print "\tpublic func encode(to: inout " PrefixCamel "Writer) {"
	print cg["serialize"] "\t}"
	print "}"

	CodegenSwiftType[name] = swifttype
	CodegenDeserialize[name] = "%s.read()"
	for (i in cg)
		delete cg[i]
}

function codegen_union_tag(name, d, cg) {
	cg["tagtype"] = d["type"]
	cg["tagname"] = decapitalize(snaketocamel(d["name"]))
}

function codegen_union_struct(name, casename, cg, scg,     swifttype) {
	# And thus not all generated structs are present in Types.
	swifttype = PrefixCamel name snaketocamel(casename)
	casename = decapitalize(snaketocamel(casename))
	print ""
	print "public struct " swifttype ": " PrefixCamel name " {"
	print "\tpublic var " cg["tagname"] \
		": " CodegenSwiftType[cg["tagtype"]] " { ." casename " }"
	print scg["fields"] "}"
	print ""
	print "extension " swifttype ": " PrefixCamel "Encodable {"
	print "\tfileprivate init(from: inout " PrefixCamel "Reader) throws {"
	print scg["deserialize"] "\t}"
	print ""
	print "\tpublic func encode(to: inout " PrefixCamel "Writer) {"
	print scg["serialize"] "\t}"
	print "}"

	append(cg, "cases", "\tcase ." casename ":\n" \
		"\t\treturn try " swifttype "(from: &from)\n")

	CodegenSwiftType[name] = swifttype
	CodegenDeserialize[name] = "%s.read()"
	for (i in scg)
		delete scg[i]
}

function codegen_union(name, cg, exhaustive,    swifttype, init) {
	# Classes don't have automatic member-wise initializers,
	# thus using structs and protocols.
	swifttype = PrefixCamel name
	print ""
	print "public protocol " swifttype ": " PrefixCamel "Encodable {"
	print "\tvar " cg["tagname"] ": " CodegenSwiftType[cg["tagtype"]] " { get }"
	print "}"

	if (!exhaustive)
		append(cg, "cases", "\tdefault:\n" \
			"\t\tthrow " PrefixCamel "Reader.ReadError.unexpectedValue\n")

	init = decapitalize(swifttype)
	print ""
	print "public func " init \
		"(from: inout " PrefixCamel "Reader) throws -> " swifttype " {"
	print "\tlet " cg["tagname"] ": " CodegenSwiftType[cg["tagtype"]] \
		" = try from.read()"
	print "\tswitch " cg["tagname"] " {"
	print cg["cases"] "\t}"
	print "}"

	CodegenSwiftType[name] = swifttype
	CodegenDeserialize[name] = init "(from: &%s)"
	for (i in cg)
		delete cg[i]
}