summaryrefslogtreecommitdiffstats
path: root/examples/railroad.lua
blob: 69da35d5ab75d74feed0a9e6f0ff7e51f38cb4f2 (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
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
package.cpath = package.cpath .. ";../target/debug/?.so"
package.path = package.path .. ";../lua-bindings/?.lua"

local dia = require("diaphragm")

dia.util.eprint("----------")

local nord_colors = {
	base00 = "#2E3440",
	base01 = "#3B4252",
	base02 = "#434C5E",
	base03 = "#4C566A",
	base04 = "#D8DEE9",
	base05 = "#E5E9F0",
	base06 = "#ECEFF4",
	base07 = "#8FBCBB",
	base08 = "#BF616A",
	base09 = "#D08770",
	base0A = "#EBCB8B",
	base0B = "#A3BE8C",
	base0C = "#88C0D0",
	base0D = "#81A1C1",
	base0E = "#B48EAD",
	base0F = "#5E81AC",
}

local function begin(params)
	local result = dia.shape(params)

	result.icon = dia.rectangle.surrounding(result, {
		height = 10,
		width = 10,
		fill_color = nord_colors.base00,
	})
	result.beginning = result.middle_left
	result.ending = result.middle_right

	function result:draw()
		self.icon:draw()
	end

	return result
end

local function stop(params)
	return begin(params)
end

function token(params)
	local result = dia.shape(params)

	result.name = params.name or "token"
	result.font = params.font or dia.text.font({ family = "Fira Code", size = 14 })
	result.text = params.text or dia.text.new({
		content = result.name,
		font = result.font,
	})

	result.fill_color = params.fill_color or nord_colors.base04
	result.padding = params.padding or 5
	result.box = dia.rectangle.surrounding(result.text, {
		fill_color = result.fill_color,
		-- TODO: make that a parameter
		margin_left = 5,
		margin_right = 5,
		margin_top = 2,
		margin_bottom = 2,
	})
	dia.constraint.inset(result.box, result)

	result.beginning = result.box.middle_left
	result.ending = result.box.middle_right

	function result:draw()
		self.box:draw()
		self.text:draw()
	end

	return result
end

function optional(params)
	local result = dia.shape(params)

	result.line_spacing = params.line_spacing or 10

	result.beginning = result.element.beginning - { x = result.line_spacing }
	result.ending = result.element.ending + { x = result.line_spacing }
	result.top_left = result.element.top_left - {
		x = result.line_spacing,
		y = result.line_spacing,
	}
	result.top_right = result.element.top_right + {
		x = result.line_spacing,
		y = -result.line_spacing,
	}

	result.overline = dia.straight_path.new({
		points = {
			result.beginning,
			result.top_left,
			result.top_right,
			result.ending,
		},
	})
	result.left_line = dia.straight_path.new({
		points = { result.beginning, result.element.beginning },
	})
	result.right_line = dia.straight_path.new({
		points = { result.element.ending, result.ending },
	})

	result.bottom = result.element.bottom

	function result:draw()
		result.overline:draw()
		result.left_line:draw()
		result.right_line:draw()
		result.element:draw()
	end

	return result
end

function choice(params)
	local result = dia.shape(params)

	result.line_spacing = params.line_spacing or 10
	result.padding = params.padding or 10

	result.stack = dia.layout.vstack({
		elements = result.choices,
		align = "left",
		top = result.top,
		bottom = result.bottom,
		spacing = result.padding,
	})

	result.beginning = result.choices[1].beginning - { x = result.line_spacing }
	result.ending = {
		x = result.stack.right + result.line_spacing,
		y = result.choices[1].ending.y,
	}

	result.left_hook = dia.straight_path.new({
		points = {
			result.choices[1].beginning,
			result.beginning,
			result.choices[#result.choices].beginning - { x = result.line_spacing },
			result.choices[#result.choices].beginning,
		},
	})
	result.right_hook = dia.straight_path.new({
		points = {
			result.choices[1].ending,
			result.ending,
			{
				x = result.ending.x,
				y = result.choices[#result.choices].ending.y,
			},
			result.choices[#result.choices].ending,
		},
	})

	result.left_connections = {}
	result.right_connections = {}
	for i = 2, #result.choices - 1 do
		table.insert(
			result.left_connections,
			dia.straight_path.new({
				points = {
					result.choices[i].beginning - { x = result.line_spacing },
					result.choices[i].beginning,
				},
			})
		)
		table.insert(
			result.right_connections,
			dia.straight_path.new({
				points = {
					result.choices[i].ending,
					{ x = result.ending.x, y = result.choices[i].ending.y },
				},
			})
		)
	end

	dia.constraint.inset(result.stack, result, {
		margin_left = result.line_spacing,
		margin_right = result.line_spacing,
	})

	function result:draw()
		result.left_hook:draw()
		result.right_hook:draw()
		for _, left_conn in ipairs(self.left_connections) do
			left_conn:draw()
		end
		for _, right_conn in ipairs(self.right_connections) do
			right_conn:draw()
		end

		self.stack:draw()
	end

	return result
end

function ident(params)
	return token(dia.util.tbl_extend({
		fill_color = nord_colors.base0A,
	}, params))
end

function typ(params)
	return token(dia.util.tbl_extend({
		fill_color = nord_colors.base08,
	}, params))
end

function tt(params)
	return token(dia.util.tbl_extend({
		fill_color = nord_colors.base0C,
	}, params))
end

function connect(params)
	local result = dia.shape(params)

	result.elements = params.elements or {}
	result.spacing = params.spacing or 10

	result.beginning = result.elements[1].beginning
	result.ending = result.elements[#result.elements].ending

	result.stack = dia.layout.hstack({
		elements = result.elements,
		align = "none",
		spacing = result.spacing,
	})
	dia.constraint.inset(result.stack, result)

	result.lines = {}

	for i = 1, #result.elements - 1 do
		local left_el = result.elements[i]
		local right_el = result.elements[i + 1]

		table.insert(
			result.lines,
			dia.straight_path.new({
				points = { left_el.ending, right_el.beginning },
			})
		)

		dia.constrain(left_el.ending.y:eq(right_el.beginning.y))
	end

	function result:draw()
		self.stack:draw()
		for _, line in ipairs(self.lines) do
			line:draw()
		end
	end

	return result
end

dia.draw({
	draw = function(self)
		local figure = connect({
			elements = {
				begin(),
				optional({ element = token({ name = "pub" }) }),
				ident({ name = "name" }),
				token({ name = "<" }),
				typ({ name = "a" }),
				choice({
					choices = {
						connect({
							elements = {
								token({ name = "," }),
								choice({
									choices = {
										connect({
											elements = {
												typ({ name = "i" }),
												token({ name = "," }),
												typ({ name = "o" }),
												optional({
													element = connect({
														elements = {
															token({ name = "," }),
															typ({ name = "e" }),
														},
													}),
												}),
											},
										}),
										typ({ name = "o" }),
									},
								}),
								token({ name = ">" }),
							},
						}),
						connect({
							elements = {
								token({ name = ">" }),
								optional({
									element = connect({
										elements = {
											token({ name = "(" }),
											typ({ name = "i" }),
											token({ name = ")" }),
											token({ name = "->" }),
											typ({ name = "o" }),
										},
									}),
								}),
							},
						}),
					},
				}),
				token({ name = "," }),
				optional({ element = token({ name = "mut" }) }),
				ident({ name = "self_" }),
				token({ name = "," }),
				ident({ name = "submac" }),
				token({ name = "!" }),
				token({ name = "(" }),
				optional({ element = tt({ name = "args" }) }),
				token({ name = ")" }),
				stop(),
			},
			center = self.center,
		})
		dia.rectangle
			.surrounding(self, {
				fill_color = nord_colors.base06,
				stroke_width = 0,
			})
			:draw()
		figure:draw()
		dia.constraint.inset(figure, self, { margin = 20 })
	end,
	output = {},
})