util  Check-in [bc37f02701]

Overview
Comment:clean up rels mechanism, get parvan mostly working again
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA3-256: bc37f027013083243953b8bff99ce8e282d199349990bb2bcaa7de56e5dc53ca
User & Date: lexi on 2022-04-28 22:52:49
Other Links: manifest | tags
Context
2022-04-28
22:52
clean up rels mechanism, get parvan mostly working again Leaf check-in: bc37f02701 user: lexi tags: trunk
21:01
commit to preserve old code im about to axe, parvan is broken currently check-in: f996abb5e5 user: lexi tags: trunk
Changes

Modified parvan.lua from [30400bd2f4] to [af37224306].

50
51
52
53
54
55
56







57
58
59
60
61
62
63
...
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
...
378
379
380
381
382
383
384
385
386
387
388
389
390


391
392
393
394
395
396
397
...
441
442
443
444
445
446
447
448
449
450
451
452
453
454



















455
456
457
458
459
460
461
462
...
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
...
840
841
842
843
844
845
846


847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
...
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965

966
967


968
969
970
971
972
973
974
....
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023

1024

1025
1026
1027
1028








1029
1030
1031
1032
1033
1034
1035
....
1049
1050
1051
1052
1053
1054
1055
1056

1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
....
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
....
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
....
1216
1217
1218
1219
1220
1221
1222
1223
1224

1225

1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
....
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
....
1393
1394
1395
1396
1397
1398
1399
1400
1401







1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
....
1418
1419
1420
1421
1422
1423
1424










1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436

1437
1438
1439
1440
1441
1442
1443
1444
1445







1446
1447
1448
1449
1450

1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
....
1463
1464
1465
1466
1467
1468
1469




1470
1471
1472
1473
1474
1475
1476
....
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535














1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555

1556
1557
1558
1559
1560
1561
1562
....
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
		if src == nil then return end
		local sc = #src
		for j=1,sc do dest[i+j] = src[j] end
		i = i + sc
		iter(...)
	end
	iter(...)







end
local function tcat(...)
	local new = {}
	tcatD(new, ...)
	return new
end
local ansi = {
................................................................................
		decode = function(s)
			local n = parse(ty,s)
			if (n+1) > #vals then error(string.format('enum "%s" does not have %u members', table.concat(vals,'","'),n),3) end
			return vals[n+1]
		end;
	}
end









fmt.note = {
	{'kind', fmt.tag};
	{'paras', fmt.list(fmt.string)};
}

fmt.example = {
	{'quote',fmt.string};
	{'src',fmt.label};
}

fmt.meaning = {
	{'lit', fmt.string};
	{'examples', fmt.list(fmt.example,fmt.u8)};
	{'notes', fmt.list(fmt.note,fmt.u8)};
}

fmt.phrase = {
	{'str',fmt.label};
	{'means',fmt.list(fmt.meaning,fmt.u8)};
	{'xref',fmt.list(fmt.path,fmt.u16)};
}

fmt.def = {
	{'part', fmt.u8};
	{'branch', fmt.list(fmt.label,fmt.u8)};
	{'means', fmt.list(fmt.meaning,fmt.u8)};
	{'forms', fmt.map(fmt.u16,fmt.label,fmt.u16)};
	{'phrases', fmt.list(fmt.phrase,fmt.u16)};
}

fmt.word = {
	{'defs', fmt.list(fmt.def,fmt.u8)};
}

fmt.dictHeader = {
	{'lang', fmt.tag};
	{'meta', fmt.string};
	{'partsOfSpeech', fmt.list(fmt.tag,fmt.u16)};
................................................................................
		{'parts', fmt.list(fmt.tag,fmt.u8)};
		-- which parts of speech does this form apply to?
		-- leave empty if not relevant
	},fmt.u16)};
}

fmt.relSet = {
	{'uid', fmt.u32};
		-- IDs are persistent random values so they can be used
		-- as reliable identifiers even when merging exports in
		-- a parvan-unaware VCS
	{'members', fmt.list(fmt.path,fmt.u16)};
	{'kind', fmt.enum('syn','ant','met')};


}

fmt.dict = {
	{'header', fmt.dictHeader};
	{'words', fmt.map(fmt.string,fmt.word)};
	{'relsets', fmt.list(fmt.relSet)};
}
................................................................................
end

local function rebuildRelationCache(d)
-- (re)build a dictionary's relation cache; needed
-- at load time and whenever any changes to relsets
-- are made (unless they're simple enough to update
-- the cache directly by hand, but that's very eeeh)
	local sc = {}
	for i,s in ipairs(d.relsets) do
		for j,m in ipairs(s.members) do
			sc[m.w] = sc[m.w] or {}
			table.insert(sc[m.w], s)
		end
	end



















	d._relCache = sc
end

local function
writeDict(d)
	local atomizePoS, posMap = atomizer()
	for lit,w in pairs(d.words) do
		for j,def in ipairs(w.defs) do
................................................................................
		if not fd then error(userError("cannot open file " .. file),2) end
		return fd
	else
		return file
	end
end

local function
safeNavWord(ctx, word, dn, mn, nn)
	local w = ctx.dict.words[word]
	if not w then id10t 'bad word' end
	if dn == nil then return w end

	local d = w.defs[tonumber(dn)]
	if not d then id10t('no definition #%u',dn) end
	if mn == nil then return w,d end

	local m = d.means[tonumber(mn)]
	if not m then id10t('no meaning #%u',mn) end
	if nn == nil then return w,d,m end

	local n = m.notes[tonumber(nn)]
	if not n then id10t('no note #%u',nn) end
	return w,d,m,n
end

local function copy(tab)
	local new = {}
	for k,v in pairs(tab) do new[k] = v end
	return new
end

local function pathParse(p)
................................................................................
		and a.dn == b.dn
		and a.mn == b.mn
		and a.pn == b.pn
		and a.nn == b.nn
		and a.xn == b.xn
end
local function pathResolve(ctx, a)


	if not a.w then return end -- empty paths are valid!
	local function lookup(seg, tbl,val)
		if not tbl then error('bad table',2) end
		local v = tbl[val]
		if v then return v end
		id10t('bad %s in path: %s', seg, val)
	end

	local res = {}
	res.word = lookup('word', ctx.dict.words, a.w)
	if not a.dn then return res end

	res.def = lookup('definition', w.defs, a.dn)
	if (not a.pn) and (not a.mn) then return res end

	local m if a.pn then
		res.phrase = lookup('phrase', d.phrases, a.pn)
		res.meaning = lookup('meaning', p.means, a.mn)
	else
		res.meaning = lookup('meaning', d.means, a.mn)
	end

	if a.xn then
		res.ex = lookup('example',m.examples,a.xn)
	elseif a.nn then
		res.note = lookup('note',m.notes,a.nn)
	end

	return res
end

local function pathNav(...)
	local t = pathResolve(...)
................................................................................
		help = "add a new word";
		syntax = "<word>";
		write = true;
		exec = function(ctx,word)
			if ctx.dict.words[word] then
				id10t "word already coined"
			end
			ctx.dict.words[word] = {defs={}}
		end;
	};
	def = {
		help = "define a word";
		syntax = "<word> <part-of-speech> [<meaning> [<root>…]]";
		write = true;
		exec = function(ctx,word,part,means,...)
			local etym = {...}
			if (not word) or not part then
				id10t 'bad definition'
			end
			if not ctx.dict.words[word] then
				ctx.dict.words[word] = {defs={}}
			end
			local n = #(ctx.dict.words[word].defs)+1
			ctx.dict.words[word].defs[n] = {
				part = part;
				branch = etym;
				means = {means and {
					lit=means;
					examples={};
					notes={};

				} or nil};
				forms = {};


			}
			ctx.log('info', string.format('added definition #%u to “%s”', n, word))
		end;
	};
	mean = {
		help = "add a meaning to a definition";
		syntax = "<word> <def#> <meaning>";
................................................................................
				end
				local links={}
				for i,l in ipairs{select(2,...)} do
					links[i] = pathParse(l)
				end
				local newstruct = {
					uid=math.random(1,0xffffFFFF);
					members=links;
					kind = rel;
				}
				table.insert(ctx.dict.relsets, newstruct)

				local rc = ctx.dict._relCache
				for i,l in pairs(links) do
					rc[l.w] = rc[l.w] or {}
					table.insert(rc[l.w], newstruct)
				end
				rebuildRelationCache(ctx.dict)
			else -- assemble a list of groups
				local tgtw = ...
				local wp = pathParse(tgtw)
				local w,d,m = pathNav(ctx, wp)

				for i,ss in ipairs(ctx.dict.relsets) do

					for j,s in ipairs(ss.members) do
						if pathSub(s, wp) then
-- 						if s.word == wp.w and (wp.dn == nil or s.def == wp.dn) then
							table.insert(groups, {set = ss, mem = s, id = i})








							break
						end
					end
				end

				if op == 'show' then
					for i, g in ipairs(groups) do
................................................................................
								end
							end

							return repr
						end

						local others = {}
						for j, o in ipairs(g.set.members) do

							local ow = pathResolve(ctx, {w=o.w}).w
							if (g.set.kind == 'ant' or not pathMatch(o, g.mem)) and
								--exclude antonym headwords
								not (g.set.kind == 'ant' and j==1) then
								table.insert(others, '      '..label(o,ow))
							end
						end
						local llab do
							local cdw = ctx.dict.words
							if g.set.kind == 'ant' then
								local ap = g.set.members[1]
								llab = fo.br(label(ap,cdw[ap.w]) or '')
							else
								llab = fo.br(label(g.mem,cdw[g.mem.w]) or '')
							end
						end
						local kls = {
							syn = fo.color('synonyms',2,true)..' of';
							ant = fo.color('antonyms',1,true)..' of';
							met = fo.color('metonyms',4,true)..' of';
						}
................................................................................
					local tgtn, paths = (select(2,...)), { select(3, ...) }
					rebuildRelationCache(ctx.dict)
				elseif op == 'destroy' then
					local tgtw, tgtn = ...
					if not tgtn then id10t 'missing group number' end
					local delendum = groups[tonumber(tgtn)]
					if not delendum then id10t 'bad group number' end
					local rs = ctx.dict.relsets
					local last = #rs
					if delendum.id == last then
						rs[delendum.id] = nil
					else -- since order doesn't matter, we can use a
						-- silly swapping trick to reduce the deletion
						-- worst case from O(n) to O(2)
						rs[delendum.id] = rs[last]
						rs[last] = nil
					end
					rebuildRelationCache(ctx.dict)
				else
					id10t 'invalid operation'
				end
			end
		end;
	};
................................................................................
		help = "add a note to a definition or a paragraph to a note";
		syntax = {"(<m-path> (add|for) <kind> | <m-path>:<note#>) <para>…";
			"m-path ::= <word>@<def#>/<meaning#>"};
		write = true;
		exec = function(ctx,path,...)
			local paras, mng
			local dest = pathParse(path)
			local _,_,m = safeNavWord(ctx,dest.w,dest.dn,dest.mn)
			if dest.nn then
				paras = {...}
			else
				local op, kind = ...
				paras = { select(3, ...) }
				if op == 'add' then
					dest.nn = #(m.notes) + 1
					m.notes[dest.nn] = {kind=kind, paras=paras}
					return
				elseif op == 'for' then
					for i,nn in ipairs(m.notes) do
						if nn.kind == kind then
							dest.nn = i break
						end
					end
					if not dest.nn then
						id10t('no note of kind %s in %s',kind,path)
					end
				end
			end
			local dpa = m.notes[dest.nn].paras
			local top = #dpa
			for i,p in ipairs(paras) do
				dpa[top+i] = p
			end
		end
	};
	shell = {
................................................................................
		end
	end
	table.sort(out, function(a,b) return a.lit < b.lit end)
	local fo = ctx.sty[io.stdout]

	local function gatherRelSets(path)
		local antonymSets, synonymSets, metonymSets = {},{},{}
		if ctx.dict._relCache[path.w] then
			for i, rel in ipairs(ctx.dict._relCache[path.w]) do

				local specuset,tgt,anto = {}

				for j, mbr in ipairs(rel.members) do
					if pathMatch(mbr, path) then
						if     rel.kind == 'syn' then tgt = synonymSets
						elseif rel.kind == 'met' then tgt = metonymSets
						elseif rel.kind == 'ant' then
							if j == 1 -- is this the headword?
								then tgt = antonymSets
								else tgt = synonymSets
							end
						end
					elseif j == 1 and rel.kind == 'ant' then
						anto = mbr
					else
						table.insert(specuset, mbr)
					end
				end
				if tgt then
					table.insert(tgt, specuset)
					if anto then
						table.insert(antonymSets, {anto})
					end
................................................................................
	end
	for i, w in ipairs(out) do
		local d = fo.ul(fo.br(w.lit))
		local wordrels = autobreak(table.concat(
				formatRels(gatherRelSets{w=w.lit}, 2),
				'\n'
			))
		local wc = ctx.dict._relCache[w.lit]
		if #w.word.defs == 1 then
			d=d .. ' '
			    .. fo.rgb(fo.em('('..(w.word.defs[1].part)..')'),.8,.5,1) .. '\n'
			    .. meanings(w,w.word.defs[1],false,1) .. '\n'
			    .. autobreak(table.concat(formatRels(gatherRelSets{w=w.lit,dn=1}, 4), '\n'))
				 .. wordrels .. '\n'
		else
................................................................................
				if c ~= 'pv0' then
					id10t "not a parvan export"
				end
				new.header.lang = words[2]
				new.header.meta = words[3]
				state = 1
			else
				print(pathString(path, ctx.sty[io.stderr]))
				local W,D,M,N = pathNav({dict=new}, path)







				if c == 'w' then syn(1) state = 2
					path = {w=words[2]}
					new.words[words[2]] = {defs={}}
				elseif c == 'f' then syn(1)
					local nf = {
						name = words[2];
						abbrev = words[3] or "";
						desc = words[4] or "";
						parts = {};
					}
................................................................................
					table.insert(lastinfl.parts,words[2])
				elseif c == 's' then syn(2)
					relsets[words[3]] = relsets[words[3]] or {}
					relsets[words[3]].kind = words[2]
					relsets[words[3]].uid = tonumber(words[3])
					relsets[words[3]].members = relsets[words[3]].members or {}
				elseif state >= 2 and c == 'r' then syn(1)










					relsets[words[2]] = relsets[words[2]] or {
						uid = tonumber(words[2]);
						members={};
					}
					table.insert(relsets[words[2]].members, path)
				elseif state >= 2 and c == 'd' then syn(1) state = 3
					table.insert(W.defs, {
						part = words[2];
						branch = {};
						means = {};
						forms = {};
						phrases = {};

					})
					path = {w = path.w, dn = #(W.defs)}
				elseif state >= 3 and c == 'dr' then syn(1)
					table.insert(D.branch, words[2])
				elseif state >= 3 and c == 'df' then syn(2)
					if not inflmap[words[2]] then
						id10t('no inflection form %s defined', words[2])
					end
					D.forms[inflmap[words[2]]] = words[3]







				elseif state >= 3 and c == 'm' then syn(1) state = 4
					table.insert(D.means, {
						lit = words[2];
						notes = {};
						examples = {};

					});
					path = {w = path.w, dn = path.dn, mn = #(D.means)}
				elseif state >= 4 and c == 'n' then syn(1) state = 5
					table.insert(M.notes, {kind=words[2], paras={}})
					path = {w = path.w, dn = path.dn, mn = path.mn, nn = #(M.notes)};
				elseif state >= 5 and c == 'np' then syn(1)
					table.insert(N.paras, words[2])
				end
				-- we ignore invalid ctls, for sake of forward-compat
			end
		end
	end
................................................................................

	for k,v in pairs(relsets) do
		if not v.uid then
			--handle non-numeric export ids
			v.uid = math.random(0,0xffffFFFF)
		end
		table.insert(new.relsets, v)




	end

	local ofd = safeopen(ctx.file,"w+b")
	local o = writeDict(new);
	ofd:write(o)
	ofd:close()
end
................................................................................
		if ctx.flags.human and lvl > 0 then
			pfx = string.rep('\t', lvl)
		end
		ofd:write(pfx..string.format(...)..'\n')
	end
	local d = ctx.dict
	o(0,'pv0 %s %s', san(d.header.lang), san(d.header.meta))
	local function checksyn(obj)
-- 		for _,s in ipairs(d.synonyms) do
		local lvl = 0
		if     obj.nn then lvl = 4
		elseif obj.mn then lvl = 3
		elseif obj.dn then lvl = 2
		elseif obj.w  then lvl = 1 end
		if not d._relCache[obj.w] then return end
		for _,s in ipairs(d._relCache[obj.w]) do
			for _,sm in ipairs(s.members) do
				if pathMatch(obj, sm) then
					o(lvl,'r %u',s.uid)
					break
				end
			end
		end
	end
	for i,f in pairs(d.header.inflectionForms) do
		o(0,'f %s %s %s', san(f.name), san(f.abbrev), san(f.desc))
		for j,p in pairs(f.parts) do
			o(1,'fp %s', san(p))
		end














	end
	for lit, w in pairs(d.words) do
		o(0,'w %s',san(lit))
		checksyn{w=lit}
		for i,def in ipairs(w.defs) do
			o(1,'d %s',san(def.part))
			checksyn{w=lit,dn=i}
			for j,r in ipairs(def.branch) do
				o(2,'dr %s',san(r))
			end
			for j,m in ipairs(def.means) do
				o(2,'m %s', san(m.lit))
				checksyn{w=lit,dn=i,mn=j}
				for k,n in ipairs(m.notes) do
					o(3,'n %s', san(n.kind))
					for a,p in ipairs(n.paras) do
						o(4,'np %s', san(p))
					end
				end
			end

		end
	end
	for _,s in ipairs(d.relsets) do o(0,'s %s %u', s.kind, s.uid) end
end

local function filterD(lst, fn)
-- cheap algorithm to destructively filter a list
................................................................................
			top = top - 1
		end
	end
	return lst
end

function cmds.mod.exec(ctx, orig, oper, dest, ...)
	if (not orig) or not oper then
		id10t '`mod` requires at least an origin and an operation'
	end
	local op, dp = pathParse(orig)
	local w,d,m,n = safeNavWord(ctx, op.w,op.dn,op.mn,op.nn)
	-- unfortunately, "pointers" exist elsewhere in the
	-- structure, currently just from relsets, that must
	-- be updated whenever an object moves or changes.
	-- this is miserable and takes a lot of work, using
	-- algorithms provided by the following functions.
	-- note that we don't bother trying to update the
	-- relcache as we go, it's simply not worth the work;
	-- instead we simply rebuild the whole cache when
	-- this command returns
	local function cleanupRels(path, fn)
		local rc = ctx.dict._relCache[path.w]
		if rc then
			for k,s in pairs(rc) do fn(s,k) end
		end
	end
	local function cleanupRelsEach(path, fn)
		cleanupRels(path, function(s,k)
			local top = #s.members
			for i=1,top do local m=s.members[i]
				if pathSub(path, m) then
					local val = fn(m,s,i)
					if val ~= nil then
						s.members[i] = val
					end
				end
			end
		end)
	end
	local function deleteRefsTo(path)
		cleanupRels(path, function(s)
		-- antonyms: delete the headword and transform the group
		-- to a list of synonyms
			if s.kind == 'ant' and pathSub(path,s.members[1]) then
				s.kind = 'syn'
			end
			filterD(s.members, function(m)
				return not pathSub(path, m)
			end)
		end)
		if not path.dn then
			ctx.dict._relCache[path.w] = nil
		end
	end
	local function moveRelTree(op,dp)
		cleanupRelsEach(op, function(old,set,idx)
			local new = {}
			for _,elt in pairs{'w','dn','mn','nn'} do
				if dp[elt] ~= nil then
					new[elt] = dp[elt]
				else
					new[elt] = op[elt] or old[elt]
				end
			end
			return new
		end)
	end
	local function shiftRelTree(dp, fld, nid, amt)
		local cleanupTargetMask = ({
			dn = {w=dp.w};
			mn = {w=dp.w,dn=dp.dn};
			nn = {w=dp.w,dn=dp.dn,mn=dp.mn};
		})[fld] -- >____<
		cleanupRelsEach(cleanupTargetMask, function(old,set,i)
			if old[fld] >= nid then
				old[fld] = old[fld] + amt
			end
		end)
	end
	local function insertAndMoveRelTree(tbl,n,op,dp,fld)
		local nid = #tbl
		local path = copy(dp)
		path[fld] = nid
		tbl[nid] = n
		shiftRelTree(dp,fld,1)
		moveRelTree(op, path)
	end
	if oper == 'drop' then
		-- clean out the cache and delete relationships
		deleteRefsTo(op)
		if not d then
			ctx.dict.words[op.w] = nil
		elseif not m then
			table.remove(w.defs, op.dn)
		elseif not n then
			table.remove(d.means, op.mn)
		else
			table.remove(m.notes, op.nn)
		end
	elseif oper == 'out' then
		if n or not m then id10t '`mod out` must target a meaning' end
		if not dest then id10t '`mod out` requires at least a part of speech' end
		local newdef = {
			part = dest;
			branch = {...};
			forms = {};
			means = {m};
		}
		shiftRelTree(op, 'dn', op.dn, 1)
		table.insert(w.defs,op.dn+1, newdef)
		moveRelTree(op,{w=op.w, dn=op.dn+1, mn=1})
		table.remove(d.means,op.mn)
	elseif oper == 'move' or oper == 'merge' or oper == 'clobber' then
		if dest
			then dp = pathParse(dest)
			else id10t('`mod %s` requires a target',oper)
		end
		if n then
			if not dp.mn then
				id10t '`mod` on a note requires a note or meaning destination'
			end
			local _,_,dm = safeNavWord(ctx, dp.w,dp.dn,dp.mn)
			if dp.nn then
				if oper == 'move' then
					shiftRelTree(dp, 'nn', dp.nn, 1)
					table.insert(dm.notes, dp.nn, n)
				elseif oper == 'merge' then
					local top = #(dm.notes[dp.nn].paras)
					for i, v in ipairs(n.paras) do
						dm.notes[dp.nn].paras[i+top] = v
					end
				elseif oper == 'clobber' then
					deleteRefsTo(dp)
					dm.notes[dp.nn] = n
				end
				moveRelTree(op,dp)
			else
				if oper ~= 'move' then
					id10t('`mod note %s` requires a note target', oper)
				end
				insertAndMoveRelTree(dm.notes,n,op,dp,'nn')
			end
			if oper == 'move' and dp.nn and dm == m and op.nn > dp.nn then
				table.remove(m.notes,op.nn+1)
			else
				table.remove(m.notes,op.nn)
			end
		elseif m then
			if not dp.dn then
				local newdef = {
					part = d.part;
					branch = copy(d.branch);
					forms = copy(d.forms);
					means = {m};
				}
				local didx
				if ctx.dict.words[dp.w] then
					local defst = ctx.dict.words[dp.w].defs
					didx = #defst
					defst[didx] = newdef
				else
					ctx.dict.words[dp.w] = {
						defs = {newdef};
					}
					didx = 1
				end
				cleanupRelsEach(op, function(oldpath,set,mi)
					return {w=dp.w,dn=didx,mn=1,nn=oldpath.nn}
				end)
				table.remove(d.means,dp.mn)
			else
				local dw, dd = safeNavWord(ctx, dp.w, dp.dn)
				if dp.mn then
					if dd.means[dp.mn] and (oper == 'merge' or oper=='clobber') then
						if oper == 'merge' then
							dd.means[dp.mn] = dd.means[dp.mn] .. '; ' .. m
						elseif oper == 'clobber' then
							deleteRefsTo(dp)
							dd.means[dp.mn] = m
						end
					else
						cleanupRelsEach({w=dp.w,dn=dp.dn}, function(old,set,i)
							if old.mn >= dp.mn then
								old.mn = old.mn + 1
							end
						end)
						table.insert(dd.means, dp.mn, m)
					end
					moveRelTree(op,dp)
				else
					insertAndMoveRelTree(dd.means,m, op,dp,'mn')
-- 					table.insert(dd.means, m)
				end
				if oper == 'move' and dp.mn and dd.means == d.means and op.mn > dp.mn then
					table.remove(d.means,op.mn+1)
				else
					table.remove(d.means,op.mn)
				end
			end
		elseif d then
			local ddefs = safeNavWord(ctx, dp.w).defs
			if dp.dn then
				if oper == 'merge' then
					local top = #(ddefs[dp.dn].means)
					for i,om in ipairs(d.means) do
						ddefs[dp.dn].means[top+i] = om
					end
					for k,p in pairs(d.forms) do
						deleteRefsTo(dp)
						ddefs[dp.dn].forms[k] = p -- clobbers!
					end
				else
					shiftRelTree(dp,'dn',dp.dn,1)
					table.insert(ddefs, dp.dn, d)
				end
				moveRelTree(op,dp)
			else
				insertAndMoveRelTree(ddefs,d, op,dp,'dn')
-- 				table.insert(ddefs, d)
			end
			if oper == 'move' and dp.mn and w.defs == ddefs and op.mn > dp.mn then
				table.remove(w.defs,op.dn+1)
			else
				table.remove(w.defs,op.dn)
			end
		else
			if ctx.dict.words[dp.w] then
				if oper ~= 'merge' then
					id10t('the word “%s” already exists; use `merge` if you want to merge the words together', dp.w)
				end
				for i,def in ipairs(w.defs) do
					local odp = copy(op) odp.dn = i
					local ddp = {w=dp.w, dn=dp.dn+i-1}
					if dp.dn then
						shiftRelTree(dp, 'dn', dp.dn+i-1, 1)
						table.insert(ctx.dict.words[dp.w].defs, dp.dn+i-1, def)
						moveRelTree(odp,ddp)
					else
-- 						table.insert(ctx.dict.words[dp.w].defs, def)
						insertAndMoveRelTree(ctx.dict.words[dp.w].defs, def,
													odp,dp,'dn')
					end
				end
			else
				ctx.dict.words[dp.w] = w
				moveRelTree(op,dp)
-- 				ctx.dict._relCache[dp.w] = ctx.dict._relCache[op.w]
-- 				ctx.dict._relCache[op.w] = nil
			end
			ctx.dict.words[op.w] = nil
		end
	end
	rebuildRelationCache(ctx.dict)
end

local function fileLegible(file)
	-- check if we can access the file
	local fd = io.open(file,"rb")
	local ret = false







>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>










>
|





|


<


|







|







 







|



<

>
>







 







|
|
|
|
|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
|







<



|



|
|

|



|

|







 







|












|









>


>
>







 







<



|
|
|
|
|
<




|
>
|
>
|
|
<
|
>
>
>
>
>
>
>
>







 







|
>

|








|


|







 







|
|
|
|
|
|
|
|
|
|







 







|






|
|


|









|







 







|
|
>

>
|
|









|

|







 







<







 







<
|
>
>
>
>
>
>
>


|







 







>
>
>
>
>
>
>
>
>
>

|










>









>
>
>
>
>
>
>





>

|


|







 







>
>
>
>







 







|
|
<
<
<
<
<
<
<
<
<
|
<
<
<







>
>
>
>
>
>
>
>
>
>
>
>
>
>



|


|



|
|
|
<
<
<
<
|
<
<
>







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
...
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
...
393
394
395
396
397
398
399
400
401
402
403

404
405
406
407
408
409
410
411
412
413
...
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
...
784
785
786
787
788
789
790



















791
792
793
794
795
796
797
...
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872

873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
...
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
....
1022
1023
1024
1025
1026
1027
1028

1029
1030
1031
1032
1033
1034
1035
1036

1037
1038
1039
1040
1041
1042
1043
1044
1045
1046

1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
....
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
....
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
....
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
....
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
....
1357
1358
1359
1360
1361
1362
1363

1364
1365
1366
1367
1368
1369
1370
....
1422
1423
1424
1425
1426
1427
1428

1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
....
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
....
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
....
1565
1566
1567
1568
1569
1570
1571
1572
1573









1574



1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608




1609


1610
1611
1612
1613
1614
1615
1616
1617
....
1624
1625
1626
1627
1628
1629
1630






















































































































































































































































1631
1632
1633
1634
1635
1636
1637
		if src == nil then return end
		local sc = #src
		for j=1,sc do dest[i+j] = src[j] end
		i = i + sc
		iter(...)
	end
	iter(...)
end
local function fastDelete(table,idx)
-- delete without preserving table order
	local l = #table
	table[idx] = table[l]
	table[l] = nil
	return table
end
local function tcat(...)
	local new = {}
	tcatD(new, ...)
	return new
end
local ansi = {
................................................................................
		decode = function(s)
			local n = parse(ty,s)
			if (n+1) > #vals then error(string.format('enum "%s" does not have %u members', table.concat(vals,'","'),n),3) end
			return vals[n+1]
		end;
	}
end

fmt.uid = fmt.u32

fmt.relatable = function(ty)
	return tcat(ty,{
		{'rels',fmt.list(fmt.uid,fmt.u16)};
	})
end

fmt.note = {
	{'kind', fmt.tag};
	{'paras', fmt.list(fmt.string)};
}

fmt.example = {
	{'quote',fmt.string};
	{'src',fmt.label};
}

fmt.meaning = fmt.relatable {
	{'lit', fmt.string};
	{'examples', fmt.list(fmt.example,fmt.u8)};
	{'notes', fmt.list(fmt.note,fmt.u8)};
}

fmt.phrase = fmt.relatable {
	{'str',fmt.label};
	{'means',fmt.list(fmt.meaning,fmt.u8)};

}

fmt.def = fmt.relatable {
	{'part', fmt.u8};
	{'branch', fmt.list(fmt.label,fmt.u8)};
	{'means', fmt.list(fmt.meaning,fmt.u8)};
	{'forms', fmt.map(fmt.u16,fmt.label,fmt.u16)};
	{'phrases', fmt.list(fmt.phrase,fmt.u16)};
}

fmt.word = fmt.relatable {
	{'defs', fmt.list(fmt.def,fmt.u8)};
}

fmt.dictHeader = {
	{'lang', fmt.tag};
	{'meta', fmt.string};
	{'partsOfSpeech', fmt.list(fmt.tag,fmt.u16)};
................................................................................
		{'parts', fmt.list(fmt.tag,fmt.u8)};
		-- which parts of speech does this form apply to?
		-- leave empty if not relevant
	},fmt.u16)};
}

fmt.relSet = {
	{'uid', fmt.uid};
		-- IDs are persistent random values so they can be used
		-- as reliable identifiers even when merging exports in
		-- a parvan-unaware VCS

	{'kind', fmt.enum('syn','ant','met')};
	-- membership is stored in individual objects, using a field
	-- attached by the 'relatable' template
}

fmt.dict = {
	{'header', fmt.dictHeader};
	{'words', fmt.map(fmt.string,fmt.word)};
	{'relsets', fmt.list(fmt.relSet)};
}
................................................................................
end

local function rebuildRelationCache(d)
-- (re)build a dictionary's relation cache; needed
-- at load time and whenever any changes to relsets
-- are made (unless they're simple enough to update
-- the cache directly by hand, but that's very eeeh)
	local setMems = {} -- indexed by set id
	local function scan(obj,path)
		for _,v in pairs(obj.rels) do
			setMems[v] = setMems[v] or {mems={}}
			table.insert(setMems[v].mems, {path=path, obj=obj})
		end
	end
	for wk,wv in pairs(d.words) do
		scan(wv, {w=wk})
		for dk,dv in pairs(wv.defs) do
			scan(dv, {w=wk, dn=dk})
			for mk,mv in pairs(dv.means) do
				scan(mv, {w=wk, dn=dk, mn=mk})
			end
			for pk,pv in pairs(dv.phrases) do
				scan(pv, {w=wk, dn=dk, pn=pk})
				for mk,mv in pairs(pv.means) do
					scan(mv, {w=wk, dn=dk, pn=pk, mn=mk})
				end
			end
		end
	end
	for sk,sv in pairs(d.relsets) do
		setMems[sv.uid] = setMems[sv.uid] or {}
		setMems[sv.uid].set = sv
	end
	d._relCache = setMems
end

local function
writeDict(d)
	local atomizePoS, posMap = atomizer()
	for lit,w in pairs(d.words) do
		for j,def in ipairs(w.defs) do
................................................................................
		if not fd then error(userError("cannot open file " .. file),2) end
		return fd
	else
		return file
	end
end




















local function copy(tab)
	local new = {}
	for k,v in pairs(tab) do new[k] = v end
	return new
end

local function pathParse(p)
................................................................................
		and a.dn == b.dn
		and a.mn == b.mn
		and a.pn == b.pn
		and a.nn == b.nn
		and a.xn == b.xn
end
local function pathResolve(ctx, a)
	local res = {}

	if not a.w then return res end -- empty paths are valid!
	local function lookup(seg, tbl,val)
		if not tbl then error('bad table',2) end
		local v = tbl[val]
		if v then return v end
		id10t('bad %s in path: %s', seg, val)
	end


	res.word = lookup('word', ctx.dict.words, a.w)
	if not a.dn then return res end

	res.def = lookup('definition', res.word.defs, a.dn)
	if (not a.pn) and (not a.mn) then return res end

	local m if a.pn then
		res.phrase = lookup('phrase', res.def.phrases, a.pn)
		res.meaning = lookup('meaning', res.phrase.means, a.mn)
	else
		res.meaning = lookup('meaning', res.def.means, a.mn)
	end

	if a.xn then
		res.ex = lookup('example',res.meaning.examples,a.xn)
	elseif a.nn then
		res.note = lookup('note',res.meaning.notes,a.nn)
	end

	return res
end

local function pathNav(...)
	local t = pathResolve(...)
................................................................................
		help = "add a new word";
		syntax = "<word>";
		write = true;
		exec = function(ctx,word)
			if ctx.dict.words[word] then
				id10t "word already coined"
			end
			ctx.dict.words[word] = {defs={},rels={}}
		end;
	};
	def = {
		help = "define a word";
		syntax = "<word> <part-of-speech> [<meaning> [<root>…]]";
		write = true;
		exec = function(ctx,word,part,means,...)
			local etym = {...}
			if (not word) or not part then
				id10t 'bad definition'
			end
			if not ctx.dict.words[word] then
				ctx.dict.words[word] = {defs={},rels={}}
			end
			local n = #(ctx.dict.words[word].defs)+1
			ctx.dict.words[word].defs[n] = {
				part = part;
				branch = etym;
				means = {means and {
					lit=means;
					examples={};
					notes={};
					rels={};
				} or nil};
				forms = {};
				phrases = {};
				rels={};
			}
			ctx.log('info', string.format('added definition #%u to “%s”', n, word))
		end;
	};
	mean = {
		help = "add a meaning to a definition";
		syntax = "<word> <def#> <meaning>";
................................................................................
				end
				local links={}
				for i,l in ipairs{select(2,...)} do
					links[i] = pathParse(l)
				end
				local newstruct = {
					uid=math.random(1,0xffffFFFF);

					kind = rel;
				}
				table.insert(ctx.dict.relsets, newstruct)
				for _, m in pairs(links) do
					local obj = pathRef(ctx,m)
					table.insert(obj.rels,newstruct.uid)
				end


				rebuildRelationCache(ctx.dict)
			else -- assemble a list of groups
				local tgtw = ...
				local wp = pathParse(tgtw)
				local o = pathResolve(ctx, wp)

				for i,rs in pairs(ctx.dict.relsets) do
					local allMembers = ctx.dict._relCache[rs.uid].mems
					for j,s in ipairs(allMembers) do
						if pathSub(s.path, wp) then

							table.insert(groups, {
								set = {
									uid = rs.uid;
									kind = rs.kind;
									members = allMembers;
								};
								mem = s;
								id = i;
							})
							break
						end
					end
				end

				if op == 'show' then
					for i, g in ipairs(groups) do
................................................................................
								end
							end

							return repr
						end

						local others = {}
						for j, oo in ipairs(g.set.members) do
							local o = oo.path
							local ow = pathResolve(ctx, {w=o.w}).w
							if (g.set.kind == 'ant' or not pathMatch(o, g.mem.path)) and
								--exclude antonym headwords
								not (g.set.kind == 'ant' and j==1) then
								table.insert(others, '      '..label(o,ow))
							end
						end
						local llab do
							local cdw = ctx.dict.words
							if g.set.kind == 'ant' then
								local ap = g.set.members[1].path
								llab = fo.br(label(ap,cdw[ap.w]) or '')
							else
								llab = fo.br(label(g.mem.path,cdw[g.mem.w]) or '')
							end
						end
						local kls = {
							syn = fo.color('synonyms',2,true)..' of';
							ant = fo.color('antonyms',1,true)..' of';
							met = fo.color('metonyms',4,true)..' of';
						}
................................................................................
					local tgtn, paths = (select(2,...)), { select(3, ...) }
					rebuildRelationCache(ctx.dict)
				elseif op == 'destroy' then
					local tgtw, tgtn = ...
					if not tgtn then id10t 'missing group number' end
					local delendum = groups[tonumber(tgtn)]
					if not delendum then id10t 'bad group number' end

					for k,v in pairs(delendum.set.members) do
						for idx, e in pairs(v.obj.rels) do
							if e == delendum.set.uid then
								fastDelete(v.obj.rels,idx)
							end
						end
					end
					fastDelete(ctx.dict.relsets, delendum.id)

					rebuildRelationCache(ctx.dict)
				else
					id10t 'invalid operation'
				end
			end
		end;
	};
................................................................................
		help = "add a note to a definition or a paragraph to a note";
		syntax = {"(<m-path> (add|for) <kind> | <m-path>:<note#>) <para>…";
			"m-path ::= <word>@<def#>/<meaning#>"};
		write = true;
		exec = function(ctx,path,...)
			local paras, mng
			local dest = pathParse(path)
			local t = pathResolve(ctx,path)
			if dest.nn then
				paras = {...}
			else
				local op, kind = ...
				paras = { select(3, ...) }
				if op == 'add' then
					dest.nn = #(t.m.notes) + 1
					t.m.notes[dest.nn] = {kind=kind, paras=paras}
					return
				elseif op == 'for' then
					for i,nn in ipairs(t.m.notes) do
						if nn.kind == kind then
							dest.nn = i break
						end
					end
					if not dest.nn then
						id10t('no note of kind %s in %s',kind,path)
					end
				end
			end
			local dpa = t.m.notes[dest.nn].paras
			local top = #dpa
			for i,p in ipairs(paras) do
				dpa[top+i] = p
			end
		end
	};
	shell = {
................................................................................
		end
	end
	table.sort(out, function(a,b) return a.lit < b.lit end)
	local fo = ctx.sty[io.stdout]

	local function gatherRelSets(path)
		local antonymSets, synonymSets, metonymSets = {},{},{}
		local obj = pathRef(ctx,path)
		if next(obj.rels) then
			for i, relid in ipairs(obj.rels) do
				local specuset,tgt,anto = {}
				local rel = ctx.dict._relCache[relid].set
				for j, mbr in ipairs(ctx.dict._relCache[relid].mems) do
					if pathMatch(mbr.path, path) then
						if     rel.kind == 'syn' then tgt = synonymSets
						elseif rel.kind == 'met' then tgt = metonymSets
						elseif rel.kind == 'ant' then
							if j == 1 -- is this the headword?
								then tgt = antonymSets
								else tgt = synonymSets
							end
						end
					elseif j == 1 and rel.kind == 'ant' then
						anto = mbr.path
					else
						table.insert(specuset, mbr.path)
					end
				end
				if tgt then
					table.insert(tgt, specuset)
					if anto then
						table.insert(antonymSets, {anto})
					end
................................................................................
	end
	for i, w in ipairs(out) do
		local d = fo.ul(fo.br(w.lit))
		local wordrels = autobreak(table.concat(
				formatRels(gatherRelSets{w=w.lit}, 2),
				'\n'
			))

		if #w.word.defs == 1 then
			d=d .. ' '
			    .. fo.rgb(fo.em('('..(w.word.defs[1].part)..')'),.8,.5,1) .. '\n'
			    .. meanings(w,w.word.defs[1],false,1) .. '\n'
			    .. autobreak(table.concat(formatRels(gatherRelSets{w=w.lit,dn=1}, 4), '\n'))
				 .. wordrels .. '\n'
		else
................................................................................
				if c ~= 'pv0' then
					id10t "not a parvan export"
				end
				new.header.lang = words[2]
				new.header.meta = words[3]
				state = 1
			else

				local T = pathResolve({dict=new}, path)
				local W,D,P,M,N,X =
					T.word,
					T.def,
					T.phrase,
					T.meaning,
					T.note,
					T.ex
				if c == 'w' then syn(1) state = 2
					path = {w=words[2]}
					new.words[words[2]] = {defs={},rels={}}
				elseif c == 'f' then syn(1)
					local nf = {
						name = words[2];
						abbrev = words[3] or "";
						desc = words[4] or "";
						parts = {};
					}
................................................................................
					table.insert(lastinfl.parts,words[2])
				elseif c == 's' then syn(2)
					relsets[words[3]] = relsets[words[3]] or {}
					relsets[words[3]].kind = words[2]
					relsets[words[3]].uid = tonumber(words[3])
					relsets[words[3]].members = relsets[words[3]].members or {}
				elseif state >= 2 and c == 'r' then syn(1)
					local rt
					if state == 2 then
						rt = W.rels
					elseif state == 3 then
						rt = D.rels
					elseif state == 4 then
						rt = D.rels
					elseif state == 14 then
						rt = P.rels
					end
					relsets[words[2]] = relsets[words[2]] or {
						uid = tonumber(words[2]) or math.random(0,0xffffFFFF);
						members={};
					}
					table.insert(relsets[words[2]].members, path)
				elseif state >= 2 and c == 'd' then syn(1) state = 3
					table.insert(W.defs, {
						part = words[2];
						branch = {};
						means = {};
						forms = {};
						phrases = {};
						rels = {};
					})
					path = {w = path.w, dn = #(W.defs)}
				elseif state >= 3 and c == 'dr' then syn(1)
					table.insert(D.branch, words[2])
				elseif state >= 3 and c == 'df' then syn(2)
					if not inflmap[words[2]] then
						id10t('no inflection form %s defined', words[2])
					end
					D.forms[inflmap[words[2]]] = words[3]
				elseif state >= 3 and c == 'p' then syn(1) state = 14
					table.insert(D.phrases, {
						str = words[2];
						means = {};
						rels = {};
					})
					path = {w = path.w, dn = path.dn, pn = #(D.phrases)}
				elseif state >= 3 and c == 'm' then syn(1) state = 4
					table.insert(D.means, {
						lit = words[2];
						notes = {};
						examples = {};
						rels = {};
					});
					path = {w = path.w, dn = path.dn, pn=path.pn, mn = #(D.means)}
				elseif state >= 4 and c == 'n' then syn(1) state = 5
					table.insert(M.notes, {kind=words[2], paras={}})
					path = {w = path.w, dn = path.dn, pn = path.pn, mn = path.mn, nn = #(M.notes)};
				elseif state >= 5 and c == 'np' then syn(1)
					table.insert(N.paras, words[2])
				end
				-- we ignore invalid ctls, for sake of forward-compat
			end
		end
	end
................................................................................

	for k,v in pairs(relsets) do
		if not v.uid then
			--handle non-numeric export ids
			v.uid = math.random(0,0xffffFFFF)
		end
		table.insert(new.relsets, v)

		for q,m in pairs(v.members) do
			table.insert(pathRef({dict=new},m).rels, v.uid)
		end
	end

	local ofd = safeopen(ctx.file,"w+b")
	local o = writeDict(new);
	ofd:write(o)
	ofd:close()
end
................................................................................
		if ctx.flags.human and lvl > 0 then
			pfx = string.rep('\t', lvl)
		end
		ofd:write(pfx..string.format(...)..'\n')
	end
	local d = ctx.dict
	o(0,'pv0 %s %s', san(d.header.lang), san(d.header.meta))
	local function checksyn(obj,lvl)
		for k,v in pairs(obj.rels) do









			o(lvl,'r %u',s.uid)



		end
	end
	for i,f in pairs(d.header.inflectionForms) do
		o(0,'f %s %s %s', san(f.name), san(f.abbrev), san(f.desc))
		for j,p in pairs(f.parts) do
			o(1,'fp %s', san(p))
		end
	end
	local function scanMeans(tbl,path,lvl)
		for j,m in ipairs(def.means) do
			o(lvl,'m %s', san(m.lit))
			local lp = copy(path)
			lp.mn = j
			checksyn(m,lp,lvl+1)
			for k,n in ipairs(m.notes) do
				o(lvl+1,'n %s', san(n.kind))
				for a,p in ipairs(n.paras) do
					o(lvl+2,'np %s', san(p))
				end
			end
		end
	end
	for lit, w in pairs(d.words) do
		o(0,'w %s',san(lit))
		checksyn(w,{w=lit},1)
		for i,def in ipairs(w.defs) do
			o(1,'d %s',san(def.part))
			checksyn(def,{w=lit,dn=i},2)
			for j,r in ipairs(def.branch) do
				o(2,'dr %s',san(r))
			end
			for j,p in ipairs(def.phrases) do
				o(2,'p %s',san(p.str))
				scanMeans(p.means, {w=lit,dn=i,pn=j}, 3)




			end


			scanMeans(def.means, {w=lit,dn=i}, 2)
		end
	end
	for _,s in ipairs(d.relsets) do o(0,'s %s %u', s.kind, s.uid) end
end

local function filterD(lst, fn)
-- cheap algorithm to destructively filter a list
................................................................................
			top = top - 1
		end
	end
	return lst
end

function cmds.mod.exec(ctx, orig, oper, dest, ...)






















































































































































































































































	rebuildRelationCache(ctx.dict)
end

local function fileLegible(file)
	-- check if we can access the file
	local fd = io.open(file,"rb")
	local ret = false