aboutsummaryrefslogtreecommitdiff
path: root/xC-gen-proto.awk
blob: de8e9b8d980300430699cf2c75d1e54fd667849e (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
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
# xC-gen-proto.awk: an XDR-derived code generator for network protocols.
#
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
# SPDX-License-Identifier: 0BSD
#
# You may read RFC 4506 for context, however it is only a source of inspiration.
# Grammar is easy to deduce from the parser.
#
# Native types: bool, u{8,16,32,64}, i{8,16,32,64}, string
#
# Don't define any new types, unless you hate yourself, then it's okay to do so.
# Both backends are a pain in the arse, for different reasons.
#
# All numbers are encoded in big-endian byte order.
# Booleans are one byte each.
# Strings must be valid UTF-8, use u8<> to lift that restriction.
# String and array lengths are encoded as u32.
# Enumeration values automatically start at 1, and are encoded as i8.
# Any struct or union field may be a variable-length array.
#
# Message framing is done externally, but also happens to prefix u32 lengths.
#
# Usage: env LC_ALL=C awk -v prefix=Relay \
#  -f xC-gen-proto.awk        < xC-proto \
#  -f xC-gen-proto-{c,go}.awk > xC-proto.{c,go} | {clang-format,gofmt}

# --- Utilities ----------------------------------------------------------------

function cameltosnake(s) {
	while (match(s, /[[:lower:]][[:upper:]]/)) {
		s = substr(s, 1, RSTART) "_" \
			tolower(substr(s, RSTART + 1, RLENGTH - 1)) \
			substr(s, RSTART + RLENGTH)
	}
	return tolower(s)
}

function snaketocamel(s) {
	s = toupper(substr(s, 1, 1)) tolower(substr(s, 2))
	while (match(s, /_[[:alnum:]]/)) {
		s = substr(s, 1, RSTART - 1) \
			toupper(substr(s, RSTART + 1, RLENGTH - 1)) \
			substr(s, RSTART + RLENGTH)
	}
	return s
}

function decapitalize(s) {
	if (match(s, /[[:upper:]][[:lower:]]/)) {
		return tolower(substr(s, 1, 1)) substr(s, 2)
	}
	return s
}

function indent(s) {
	if (!s)
		return s

	gsub(/\n/, "\n\t", s)
	sub(/\t*$/, "", s)
	return "\t" s
}

function append(a, key, value) {
	a[key] = a[key] value
}

# --- Parsing ------------------------------------------------------------------

function fatal(message) {
	print "// " FILENAME ":" FNR ": fatal error: " message
	print FILENAME ":" FNR ": fatal error: " message > "/dev/stderr"
	exit 1
}

function skipcomment() {
	do {
		if (match($0, /[*][/]/)) {
			$0 = substr($0, RSTART + RLENGTH)
			return
		}
	} while (getline > 0)
	fatal("unterminated block comment")
}

function nexttoken() {
	do {
		if (match($0, /^[[:space:]]+/)) {
			$0 = substr($0, RLENGTH + 1)
		} else if (match($0, /^[/][/].*/)) {
			$0 = ""
		} else if (match($0, /^[/][*]/)) {
			$0 = substr($0, RLENGTH + 1)
			skipcomment()
		} else if (match($0, /^[[:alpha:]][[:alnum:]_]*/)) {
			Token = substr($0, 1, RLENGTH)
			$0 = substr($0, RLENGTH + 1)
			return Token
		} else if (match($0, /^(0[xX][0-9a-fA-F]+|[1-9][0-9]*)/)) {
			Token = substr($0, 1, RLENGTH)
			$0 = substr($0, RLENGTH + 1)
			return Token
		} else if (/./) {
			Token = substr($0, 1, 1)
			$0 = substr($0, 2)
			return Token
		}
	} while (/./ || getline > 0)
	Token = ""
	return Token
}

function expect(v) {
	if (!v)
		fatal("broken expectations at `" Token "' before `" $0 "'")
	return v
}

function accept(what) {
	if (Token != what)
		return 0
	nexttoken()
	return 1
}

function identifier(    v) {
	if (Token !~ /^[[:alpha:]]/)
		return 0
	v = Token
	nexttoken()
	return v
}

function number(    v) {
	if (Token !~ /^[0-9]/)
		return 0
	v = Token
	nexttoken()
	return v
}

function readnumber(    ident) {
	ident = identifier()
	if (!ident)
		return expect(number())
	if (!(ident in Consts))
		fatal("unknown constant: " ident)
	return Consts[ident]
}

function defconst(    ident, num) {
	if (!accept("const"))
		return 0

	ident = expect(identifier())
	expect(accept("="))
	num = readnumber()
	if (ident in Consts)
		fatal("constant redefined: " ident)

	Consts[ident] = num
	codegen_constant(ident, num)
	return 1
}

function readtype(    ident) {
	ident = deftype()
	if (ident)
		return ident

	ident = identifier()
	if (!ident)
		return 0

	if (!(ident in Types))
		fatal("unknown type: " ident)
	return ident
}

function defenum(    name, ident, value, cg) {
	delete cg[0]

	name = expect(identifier())
	expect(accept("{"))
	while (!accept("}")) {
		ident = expect(identifier())
		value = value + 1
		if (accept("="))
			value = readnumber()
		if (!value)
			fatal("enumeration values cannot be zero")
		if (value < -128 || value > 127)
			fatal("enumeration value out of range")
		expect(accept(","))
		append(EnumValues, name, SUBSEP ident)
		if (EnumValues[name, ident]++)
			fatal("duplicate enum value: " ident)
		codegen_enum_value(name, ident, value, cg)
	}

	Types[name] = "enum"
	codegen_enum(name, cg)
	return name
}

function readfield(out,    nonvoid) {
	nonvoid = !accept("void")
	if (nonvoid) {
		out["type"] = expect(readtype())
		out["name"] = expect(identifier())
		# TODO: Consider supporting XDR's VLA length limits here.
		# TODO: Consider supporting XDR's fixed-length syntax for string limits.
		out["isarray"] = accept("<") && expect(accept(">"))
	}
	expect(accept(";"))
	return nonvoid
}

function defstruct(    name, d, cg) {
	delete d[0]
	delete cg[0]

	name = expect(identifier())
	expect(accept("{"))
	while (!accept("}")) {
		if (readfield(d))
			codegen_struct_field(d, cg)
	}

	Types[name] = "struct"
	codegen_struct(name, cg)
	return name
}

function defunion(    name, tag, tagtype, tagvalue, cg, scg, d, a, i, unseen) {
	delete cg[0]
	delete scg[0]
	delete d[0]

	name = expect(identifier())
	expect(accept("switch"))
	expect(accept("("))
	tag["type"] = tagtype = expect(readtype())
	tag["name"] = expect(identifier())
	expect(accept(")"))

	if (Types[tagtype] != "enum")
		fatal("not an enum type: " tagtype)
	codegen_union_tag(tag, cg)

	split(EnumValues[tagtype], a, SUBSEP)
	for (i in a)
		unseen[a[i]]++

	expect(accept("{"))
	while (!accept("}")) {
		if (accept("case")) {
			if (tagvalue)
				codegen_union_struct(name, tagvalue, cg, scg)

			tagvalue = expect(identifier())
			expect(accept(":"))
			if (!unseen[tagvalue]--)
				fatal("no such value or duplicate case: " tagtype "." tagvalue)
			codegen_struct_tag(tag, scg)
		} else if (tagvalue) {
			if (readfield(d))
				codegen_struct_field(d, scg)
		} else {
			fatal("union fields must fall under a case")
		}
	}
	if (tagvalue)
		codegen_union_struct(name, tagvalue, cg, scg)

	# What remains non-zero in unseen[2..] is simply not recognized/allowed.
	Types[name] = "union"
	codegen_union(name, cg)
	return name
}

function deftype() {
	if (accept("enum"))
		return defenum()
	if (accept("struct"))
		return defstruct()
	if (accept("union"))
		return defunion()
	return 0
}

BEGIN {
	PrefixLower = "relay_"
	PrefixUpper = "RELAY_"
	PrefixCamel = "Relay"

	print "// Generated by xC-gen-proto.awk. DO NOT MODIFY."
	codegen_begin()

	nexttoken()
	while (Token != "") {
		expect(defconst() || deftype())
		expect(accept(";"))
	}
}