summaryrefslogtreecommitdiff
path: root/mbld/parse.myr
blob: 7bd10d796dc3d929e7086dec2dc3db8695e9e1df (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
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
use std

use "types.use"
use "util.use"
use "opts.use"
use "fsel.use"

pkg bld =
	const load	: (b : build# -> bool)
;;

type parser = struct
	/* parse input */
	data	: byte[:]
	rest	: byte[:]
	fname	: byte[:]
	fdir	: byte[:]	/* directory relative to base */
	basedir	: byte[:]
	line	: int

	/* extracted data for further parsing */
	subdirs	: byte[:][:]
;;

const load = {b
	var ok
	ok = loadall(b, b.bldfile, "")
	if ok
		ok = sortdeps(b)
	;;
	-> ok
}

const loadall = {b, path, dir
	var p : parser#
	var subpath, subbld, subproj, ok
	var curbase

	p = mkparser(path, dir, b.basedir)
	ok = bld.parse(b, p, "")
	for s in p.subdirs
		subpath = std.pathcat(p.fdir, s)
		subbld = std.pathcat(subpath, "bld.sub")
		subproj = std.pathcat(subpath, "bld.proj")
		/*
		bld.sub is a subbuild. It doesn't change the
		build root.
		*/
		if std.fexists(subbld)
			loadall(b, subbld, subpath)
		/*
		bld.proj reroots the project -- @/
		is relative to the most recent bld.proj
		in the heirarchy.
		*/
		elif std.fexists(subproj)
			curbase = b.basedir
			b.basedir = subpath
			loadall(b, subproj, subpath)
			b.basedir = curbase
		else
			std.fatal(1, "could not open %s or %s \n", subbld, subproj)
		;;
		std.slfree(subbld)
		std.slfree(subproj)
		std.slfree(subpath)
	;;
	freeparser(p)
	-> ok
}

const sortdeps = {b
	var looped
	var marked
	var all

	all = [][:]
	looped = std.mkht(std.strhash, std.streq)
	marked = std.mkht(std.strhash, std.streq)
	for dep in b.all
		match gettarg(b.targs, dep)
		| `Bin _:	all = visit(all, b, dep, looped, marked)
		| `Lib _:	all = visit(all, b, dep, looped, marked)
		| targ:		all = std.slpush(all, dep)
		;;
	;;
	std.slfree(b.all)
	b.all = all
	-> true
}

const visit = {all, b, targ, looped, marked
	if std.hthas(looped, targ)
		std.fatal(1, "cycle in build depgraph involving %s\n", targ)
	elif std.hthas(marked, targ)
		-> all
	;;

	std.htput(looped, targ, true)
	for (dir, lib, dep) in getdeps(gettarg(b.targs, targ))
		all = visit(all, b, dep, looped, marked)
	;;
	std.htdel(looped, targ)
	std.htput(marked, targ, true)
	-> std.slpush(all, targ)
}

const getdeps = {targ
	match targ
	| `Bin t:	-> t.libdeps
	| `Lib t:	-> t.libdeps
	| _:	std.fatal(1, "depending on non-library target")
	;;
}

const mkparser = {path, dir, basedir
	var p

	p = std.zalloc()
	p.line = 1
	p.fname = std.sldup(path)
	p.fdir = std.sldup(dir)
	p.basedir = std.sldup(basedir)
	match std.slurp(path)
	| `std.Ok d:	p.data = d
	| `std.Fail _:	std.fatal(1, "could not open '%s'\n", path)
	;;
	p.rest = p.data
	-> p
}

const freeparser = {p
	std.slfree(p.fname)
	std.slfree(p.fdir)
	std.slfree(p.basedir)
	std.slfree(p.subdirs)
	std.slfree(p.data)
	std.free(p)
}

const failparse = {p, msg, args : ...
	var buf : byte[1024]
	var ap
	var sl

	ap = std.vastart(&args)
	sl = std.bfmtv(buf[:], msg, ap)
	std.fput(1, "%s:%i: %s", p.fname, p.line, sl)
	std.exit(1)
}

const parse = {b, p, path
	while true
		skipspace(p)
		if !target(b, p)
			break
		;;
	;;
	skipspace(p)
	if p.rest.len > 0
		failparse(p, "junk in file near %s\n", p.rest[:std.min(p.rest.len, 10)])
		-> false
	;;
	-> true
}

const target = {b, p
	match word(p)
	| `std.Some "bin":	bintarget(b, p)
	| `std.Some "test":	testtarget(b, p)
	| `std.Some "lib":	libtarget(b, p)
	| `std.Some "gen":	cmdtarget(b, p, "gen", false)
	| `std.Some "cmd":	cmdtarget(b, p, "cmd", true)
	| `std.Some "man":	mantarget(b, p)
	| `std.Some "sub":	subtarget(b, p)
	| `std.Some targtype:	failparse(p, "unknown targtype type %s\n", targtype)
	| `std.None:	-> false
	;;
	-> true
}

/* bintarget: myrtarget */
const bintarget = {b, p
	var t
	t = myrtarget(p, "bin")
	addtarg(p, b, t.name, `Bin t)
}

/* libtarget: myrtarget */
const libtarget = {b, p
	var t
	t = myrtarget(p, "lib")
	t.islib = true
	addtarg(p, b, t.name, `Lib t)
}

/* testtarget: myrtarget */
const testtarget = {b, p
	var t
	t = myrtarget(p, "test")
	addtarg(p, b, t.name, `Test t)
}

/* mantarget: anontarget */
const mantarget = {b, p
	var targ

	targ = std.mk([
		.dir=std.sldup(p.fdir),
		.pages=anontarget(p, "man")
	])
	addtarg(p, b, "__man__", `Man targ)
}

/* subtarget : anontarget */
const subtarget = {b, p
	var subs

	subs = anontarget(p, "sub")
	for s in subs
		p.subdirs = std.slpush(p.subdirs, std.pathcat(p.fdir, s))
	;;
}

/* gentarget: wordlist {attrs} = wordlist ;; */
const cmdtarget = {b, p, cmd, iscmd
	var outlist, deplist, cmdlist, systags
	var durable
	var attrs
	var gt

	match wordlist(p)
	| `std.None:	failparse(p, "%s target missing output files\n", cmd)
	| `std.Some out:
		if iscmd && out.len != 1
			failparse(p, "%s target takes one name\n", cmd)
		;;
		outlist = out
	;;

	skipspace(p)
	if !iscmd && matchc(p, '{')
		match attrlist(p)
		| `std.Some al:	attrs = al
		| `std.None:	failparse(p, "invalid attr list for %s\n", outlist[0])
		;;
	else
		attrs = [][:]
	;;

	skipspace(p)
	if !matchc(p, '=')
		failparse(p, "expected '=' after '%s %s'\n", cmdlist, outlist[outlist.len-1])
	;;

	match wordlist(p)
	| `std.None:	failparse(p, "%s target missing command\n", cmd)
	| `std.Some cl:
		cmdlist = cl
	;;

	if !matchc(p, ';') || !matchc(p, ';')
		failparse(p, "expected ';;' terminating genfile command, got %c\n", peekc(p))
	;;

	durable = false
	deplist = [][:]
	for elt in attrs
		match elt
		| ("durable", ""):	durable = true
		| ("dep", depname):	deplist = std.slpush(deplist, depname)
		| ("sys", tags):	systags = std.slpush(systags, tags)
		| (attr, ""):
			failparse(p, "attribute %s not valid on %s targets\n", attr, cmd)
		| (attr, val):
			failparse(p, "attribute %s=%s not valid on %s targets\n", attr, val, cmd)
		;;
	;;

	gt = std.mk([
		.dir=std.sldup(p.fdir),
		.out=outlist,
		.durable=durable,
		.deps=deplist,
		.cmd=cmdlist,
		.systags=systags,
	])
	for o in outlist
		if iscmd
			addtarg(p, b, o, `Cmd gt)
		else
			std.htput(b.gensrc, o, gt)
			addtarg(p, b, o, `Gen gt)
		;;
	;;
}

/*
myrtarget: name '=' inputlist ';;'
	| name attrlist = inputlist ';;'
*/
const myrtarget = {p, targ
	var ldscript, runtime, inst, incpath, systags
	var name, inputs, libdeps, attrs
	var fsel

	match word(p)
	| `std.Some n:	name = n
	| `std.None:	failparse(p, "expected target name after '%s'\n", targ)
	;;

	skipspace(p)
	if matchc(p, '{')
		match attrlist(p)
		| `std.Some al:	attrs = al
		| `std.None:	failparse(p, "invalid attr list for %s %s\n", targ, name)
		;;
	else
		attrs = [][:]
	;;

	skipspace(p)
	if !matchc(p, '=')
		failparse(p, "expected '=' after '%s %s'\n", targ, name)
	;;

	match inputlist(p)
	| `std.Some (wl, libs): 
		fsel = mkfsel()
		libdeps = libs
		for w in wl
			fseladd(fsel, w)
		;;
		inputs = fselfin(fsel)
		std.slfree(wl)
	| `std.None: failparse(p, "expected list of file names after '%s %s'\n", targ, name)
	;;

	skipspace(p)
	if !matchc(p, ';') || !matchc(p, ';')
		failparse(p, "expected ';;' terminating input list, got %c\n", peekc(p))
	;;

	inst = true
	ldscript = ""
	runtime = ""
	incpath = [][:]
	for elt in attrs
		match elt
		| ("ldscript", lds):	ldscript = std.sldup(lds)
		| ("runtime", rt):	runtime = std.sldup(rt)
		| ("inc", path):	incpath = std.slpush(incpath, std.sldup(path))
		| ("noinst", ""):	inst = false
		| ("sys", tags):	systags = std.slpush(systags, tags)
		| (invalid, _):
			std.fatal(1, "%s: got invalid attr '%s'\n", targ, invalid)
		;;
	;;
	for inc in bld.opt_incpaths
		incpath = std.slpush(incpath, std.sldup(inc))
	;;
	-> std.mk([
		/* target */
		.dir=std.sldup(p.fdir),
		.name=name,
		.inputs=inputs,
		.libdeps=libdeps,
		.islib=false,
		/* attrs */
		.systags=systags,
		.install=inst,
		.ldscript=ldscript,
		.runtime=runtime,
		.incpath=incpath,
	])
}

/* anontarget: '=' wordlist ';;' */
const anontarget = {p, targ
	var inputs

	inputs = [][:]
	skipspace(p)
	if !matchc(p, '=')
		failparse(p, "expected '=' after '%s' target\n", targ)
	;;

	match wordlist(p)
	| `std.None:	failparse(p, "expected list of file names after '%s' target\n", targ)
	| `std.Some wl:	inputs = wl
	;;
	skipspace(p)
	if !matchc(p, ';') || !matchc(p, ';')
		failparse(p, "expected ';;' terminating input list\n")
	;;
	-> inputs
}

/*
attrlist: attrs '}'

attrs	: EMPTY
	| attrs attr

attr	: name
	| name '=' name
*/
const attrlist = {p
	var al

	al = [][:]
	while true
		match word(p)
		| `std.Some k:
			skipspace(p)
			if matchc(p, '=')
				match word(p)
				| `std.Some v:
					al = std.slpush(al, (k, v))
				| `std.None:
					failparse(p, "invalid attr in attribute list\n")
				;;
			else
				al = std.slpush(al, (k, [][:]))
			;;
			if !matchc(p, ',')
				break
			;;
		| `std.None:	break
		;;
	;;
	if !matchc(p, '}')
		failparse(p, "expected '}' at end of attr list, got '%c'\n", peekc(p))
	;;
	if al.len == 0
		-> `std.None
	else
		-> `std.Some al
	;;
}

/*
inputlist: EMPTY
	| inputlist input

input	: word
	| "lib" word
*/
const inputlist = {p
	var dir, lib, targ
	var wl, libs

	wl = [][:]
	libs = [][:]
	while true
		match word(p)
		| `std.Some "lib":
			match word(p)
			| `std.Some l:
				(dir, lib, targ) = libpath(p, l)
				libs = std.slpush(libs, (dir, lib, targ))
			| `std.None:
				failparse(p, "expected lib name after 'lib'\n")
			;;
		| `std.Some w:	wl = std.slpush(wl, w)
		| `std.None:	break
		;;
	;;
	if wl.len == 0
		-> `std.None
	else
		-> `std.Some (wl, libs)
	;;
}

/* wordlist: EMPTY | wordlist word */
const wordlist = {p
	var wl

	wl = [][:]
	while true
		match word(p)
		| `std.Some w:	wl = std.slpush(wl, w)
		| `std.None:	break
		;;
	;;
	if wl.len == 0
		-> `std.None
	else
		-> `std.Some wl
	;;
}

/* word: /wordchar*/
const word = {p
	var c, n
	var start

	skipspace(p)

	c = peekc(p)
	if c == '"'
		n = 0
		getc(p)
		start = p.rest
		while p.rest.len > 0
			c = peekc(p)
			if c == '"'
				getc(p)
				goto done
			elif c == '\\'
				c = getc(p)
			;;
			getc(p)
			n += std.charlen(c)
		;;
		failparse(p, "input ended within quoted word\n")
	else
		n = 0
		start = p.rest
		while p.rest.len > 0
			c = peekc(p)
			if wordchar(c)
				getc(p)
				n += std.charlen(c)
			else
				break
			;;
		;;
	;;
:done
	if n > 0
		-> `std.Some std.sldup(start[:n])
	else
		-> `std.None
	;;
}

const wordchar = {c
	-> std.isalnum(c) || \
		c == '.' || c == '_' || c == '$' || c == '-' || \
		c == '/' || c == '@' || c == '!' || c == '~' || \
		c == '+' || c == '%' || c == '&' || c == '(' || \
		c == ')' || c == '[' || c == ']' || c == ':'
}

const skipspace = {p
	var c, r

	r = p.rest
	while r.len > 0
		c = peekc(p)
		match c
		| ' ':	getc(p)
		| '\t':	getc(p)
		| '\n':
			getc(p)
			p.line++
		| '#':
			while p.rest.len > 0 && peekc(p) != '\n'
				getc(p)
			;;
		| _:
			break
		;;
	;;
}

const matchc = {p, c
	var chr, s

	if p.rest.len == 0
		-> false
	;;
	(chr, s) = std.striter(p.rest)
	if c == chr
		p.rest = s
		-> true
	else
		-> false
	;;
}

const peekc = {p
	-> std.decode(p.rest)
}

const getc = {p
	var c, s

	(c, s) = std.striter(p.rest)
	p.rest = s
	-> c
}

const addtarg = {p, b, name, targ
	var tn

	tn = std.fmt("%s:%s", p.fdir, name)
	if std.hthas(b.targs, tn)
		failparse(p, "duplicate target %s\n", tn)
	;;
	b.all = std.slpush(b.all, tn)
	std.htput(b.targs, tn, targ)
}

const libpath = {p, libpath
	var dir, lib, targ

	match(std.strrfind(libpath, ":"))
	| `std.None:
		dir = std.sldup(".")
		lib = std.sldup(libpath)
		targ = std.fmt("%s:%s", p.fdir, lib)
	| `std.Some idx:
		if idx == libpath.len
			std.fatal(1, "libdep %s missing library after ':'\n")
		;;
		/* absolute path */
		if std.hasprefix(libpath, "@/") || std.hasprefix(libpath, "@:")
			dir = std.pathcat(p.basedir, libpath[2:idx])
			lib = std.sldup(libpath[idx+1:])
			targ = std.sldup(libpath[2:])
		/* relative path */
		else
			dir = std.sldup(libpath[:idx])
			lib = std.sldup(libpath[idx+1:])
			targ = std.pathcat(p.fdir, libpath)
			if std.hasprefix(targ, "../")
				std.fatal(1, "library %s outside of project\n", libpath)
			;;
		;;
	;;
	-> (dir, lib, targ)
}