Overview
Comment: | add termcolors, update parvan and compose |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
54874eb3ebe45d0f80603bf99a0808e3 |
User & Date: | lexi on 2022-12-26 13:31:53 |
Other Links: | manifest | tags |
Context
2022-12-30
| ||
23:55 | make language more offensive check-in: b4fe00021c user: lexi tags: trunk | |
2022-12-26
| ||
13:31 | add termcolors, update parvan and compose check-in: 54874eb3eb user: lexi tags: trunk | |
2022-11-01
| ||
20:05 | fixes check-in: 987a1aac03 user: lexi tags: trunk | |
Changes
Modified clib/compose.c from [5d94fe752b] to [9e5668944d].
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
..
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
|
* compose.c contains some useful string utilities * that libc lacks. TODO add macro to use homebrew strlen instead of dragging in <string.h> */ #ifdef k_header // only emit the declarations # define fn(x) #else # define fn(x) x #endif #include <string.h> typedef struct pstr { size_t len; union { const char* ptr; char* mutptr; ................................................................................ # ifndef k_static bool heap; # endif size_t len; } safestr; #ifndef k_static void delstr(safestr s) fn ({ if (s.heap) { free(s.mutptr); } }); #endif void clrstr(safestr* s) fn ({ # ifndef k_static delstr(*s); s->heap = false; # endif s->ptr = NULL; s->len = 0; }) size_t pstrsum(pstr* lst,size_t ct) fn({ size_t len = 0; for (size_t i = 0; i < ct; ++i) { if (lst[i].len == 0) { if (lst[i].ptr == NULL) continue; lst[i].len = strlen(lst[i].ptr); } len += lst[i].len; } return len; }) char* pstrcoll(pstr* lst, size_t ct, char* ptr) fn({ for (size_t i = 0; i < ct; ++i) { if (lst[i].len == 0) continue; strncpy(ptr,lst[i].ptr,lst[i].len); ptr += lst[i].len; } return ptr; }) #ifndef k_static char* compose(pstr* lst,size_t ct, size_t* strsz) fn({ size_t len = pstrsum(lst,ct) if (strsz != NULL) *strsz = len; if (len == 0) return NULL; char* str = malloc(len + 1); char* ptr = pstrcoll(lst, ct, ptr); *ptr = 0; return str; }); #endif char* impose(pstr* lst,size_t ct, size_t* strsz, char* buf) fn({ size_t len = pstrsum(lst,ct); if (strsz != NULL) *strsz = len; if (len == 0) return NULL; char* ptr = pstrcoll(lst, ct, buf); *ptr = 0; return ptr; }); char* imprint(pstr lst, size_t* strsz, char* buf) fn({ size_t len = pstrsum(&lst,1); if (strsz != NULL) *strsz = len; if (len == 0) return NULL; char* ptr = pstrcoll(&lst,1,buf); *ptr = 0; return ptr; }); #undef fn |
|
|
|
|
|
|
|
|
|
|
|
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
..
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
|
* compose.c contains some useful string utilities * that libc lacks. TODO add macro to use homebrew strlen instead of dragging in <string.h> */ #ifdef k_header // only emit the declarations # define k_impl(x) #else # define k_impl(x) x #endif #include <string.h> typedef struct pstr { size_t len; union { const char* ptr; char* mutptr; ................................................................................ # ifndef k_static bool heap; # endif size_t len; } safestr; #ifndef k_static void delstr(safestr s) k_impl ({ if (s.heap) { free(s.mutptr); } }); #endif void clrstr(safestr* s) k_impl ({ # ifndef k_static delstr(*s); s->heap = false; # endif s->ptr = NULL; s->len = 0; }) size_t pstrsum(pstr* lst,size_t ct) k_impl ({ size_t len = 0; for (size_t i = 0; i < ct; ++i) { if (lst[i].len == 0) { if (lst[i].ptr == NULL) continue; lst[i].len = strlen(lst[i].ptr); } len += lst[i].len; } return len; }) char* pstrcoll(pstr* lst, size_t ct, char* ptr) k_impl ({ for (size_t i = 0; i < ct; ++i) { if (lst[i].len == 0) continue; strncpy(ptr,lst[i].ptr,lst[i].len); ptr += lst[i].len; } return ptr; }) #ifndef k_static char* compose(pstr* lst,size_t ct, size_t* strsz) k_impl ({ size_t len = pstrsum(lst,ct) if (strsz != NULL) *strsz = len; if (len == 0) return NULL; char* str = malloc(len + 1); char* ptr = pstrcoll(lst, ct, ptr); *ptr = 0; return str; }); #endif char* impose(pstr* lst,size_t ct, size_t* strsz, char* buf) k_impl({ size_t len = pstrsum(lst,ct); if (strsz != NULL) *strsz = len; if (len == 0) return NULL; char* ptr = pstrcoll(lst, ct, buf); *ptr = 0; return ptr; }); char* imprint(pstr lst, size_t* strsz, char* buf) k_impl({ size_t len = pstrsum(&lst,1); if (strsz != NULL) *strsz = len; if (len == 0) return NULL; char* ptr = pstrcoll(&lst,1,buf); *ptr = 0; return ptr; }); #undef k_impl |
Modified parvan.lua from [af37224306] to [3480abaab0].
6 7 8 9 10 11 12 13 14 15 16 17 18 19 .. 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 ... 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 ... 295 296 297 298 299 300 301 302 303 304 305 306 307 308 ... 315 316 317 318 319 320 321 322 323 324 325 326 327 328 ... 338 339 340 341 342 343 344 345 346 347 348 349 350 351 ... 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 ... 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 ... 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 ... 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 ... 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 ... 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 ... 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 ... 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 ... 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 ... 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 ... 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 ... 919 920 921 922 923 924 925 926 927 928 929 930 931 932 ... 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 975 976 977 978 979 980 981 982 983 984 ... 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 .... 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 .... 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 .... 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 .... 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 .... 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 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 .... 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 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 .... 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 1563 1564 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 1618 1619 1620 .... 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 .... 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 .... 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 .... 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 |
-- < Commission for Defense Communication > -- +WCO Worlds Culture Overdirectorate -- SSD Social Sciences Directorate -- ELS External Linguistics Subdirectorate -- +WSO Worlds Security Overdirectorate -- EID External Influence Directorate ] local function implies(a,b) return a==b or not(a) end local function map(lst,fn) local new = {} for k,v in pairs(lst) do local nv, nk = fn(v,k) new[nk or k] = nv ................................................................................ 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 = { levels = { plain = 0; ansi = 1; color = 2; color8b = 3; color24b = 4; ................................................................................ local id = function(...) return ... end local esc = '\27' local f = {} for k,v in pairs(ansi.seqs) do local lvl, on, off = table.unpack(v) if lvl <= cl then f[k] = function(s) return esc..on .. s .. esc..off end else f[k] = id end end local function ftoi(r,g,b) return math.ceil(r*0xff), math.ceil(g*0xff), math.ceil(b*0xff) end local reset = "\27[39m" function f.color(str, n, br) return string.format("\27[%s%cm", (bg and 4 or 3) + (br and 6 or 0), 0x30+n) .. str .. reset end function f.resetLine() return '\27[1K\13' end if cl == ansi.levels.color24b then function f.rgb(str, r,g,b, bg) return string.format("\27[%c8;2;%u;%u;%um", bg and 0x34 or 0x33, ................................................................................ for i=1,n do table.insert(vals, parse(t, s)) end return vals end; } end fmt.map = function(from,to,ity) local ent = fmt.list({ {'key', from}, {'val', to} }, ity) return { ................................................................................ end; decode = function(s) local lst = ent.decode(s) local m = {} for _,p in pairs(lst) do m[p.key] = p.val end return m end; } end fmt.enum = function(...) local vals,rmap = {...},{} for k,v in pairs(vals) do rmap[v] = k-1 end local ty = fmt.u8 ................................................................................ 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 ................................................................................ {'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)}; {'inflectionForms', fmt.list({ ................................................................................ {'name', fmt.tag}; {'abbrev', fmt.tag}; {'desc', fmt.string}; {'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)}; } function marshal(ty, val) if ty.encode then return ty.encode(val) end local ac = {} for idx,fld in ipairs(ty) do local name, fty = table.unpack(fld) table.insert(ac, marshal(fty, assert(val[name], string.format('marshalling error: missing field %s', name) ) )) end return table.concat(ac) end function parse(ty, stream) if ty.decode then return ty.decode(stream) end local obj = {} for idx,fld in ipairs(ty) do local name, fty = table.unpack(fld) obj[name] = parse(fty, stream) end return obj end local function atomizer() local map = {} ................................................................................ return 'PV0\2'..marshal(fmt.dict, d) end local function readDict(file) local s = stream(file) local magic = s:next 'c4' if magic ~= 'PV0\2' then id10t 'not a parvan file' end local d = parse(fmt.dict, s) -- handle atoms for lit,w in pairs(d.words) do for j,def in ipairs(w.defs) do def.part = d.header.partsOfSpeech[def.part] end ................................................................................ -- enable faster lookup that would otherwise require -- expensive scans rebuildRelationCache(d) return d end local function strwords(str) -- here be dragons local wds = {} local w = {} local state, d, quo, dquo = 0,0 local function flush(n,final) if next(w) or state ~= 0 and state < 10 then table.insert(wds, utf8.char(table.unpack(w))) w = {} ................................................................................ elseif final and state > 10 then table.insert(wds, '\\') end state = n quo = nil dquo = nil d = 0 end local function isws(c) return c == 0x20 or c == 0x09 or c == 0x0a end for p,cp in utf8.codes(str) do if state == 0 then -- begin if not(isws(cp)) then ................................................................................ -- 12 = quote escape, 11 = raw escape if cp == 0x63 then --n table.insert(w,0x0a) else table.insert(w,cp) end state = state - 10 end end flush(nil,true) return wds end local predicates local function parsefilter(str) local f = strwords(str) if #f == 1 then return function(e) return predicates.lit.fn(e,f[1]) end end if not predicates[f[1]] then id10t('no such predicate %s',f[1]) else local p = predicates[f[1]].fn return function(e) return p(e, table.unpack(f,2)) end end ................................................................................ end; local function p_none(e,pred,...) if pred == nil then return true end pred = parsefilter(pred) if pred(e) then return false end return p_none(e,...) end; local function p_some(e,count,pred,...) if count == 0 then return true end if pred == nil then return false end pred = parsefilter(pred) if pred(e) then count = count-1 end return p_some(e,count,...) end; local function prepScan(...) local map = {} local tgt = select('#',...) for _,v in pairs{...} do map[v] = true end return map,tgt end predicates = { all = { fn = p_all; syntax = '<pred>…'; help = 'every sub-<pred> matches' }; any = { fn = p_any; syntax = '<pred>…'; help = 'any sub-<pred> matches' }; none = { fn = p_none; syntax = '<pred>…'; help = 'no sub-<pred> matches' }; some = { fn = p_some; syntax = '<count> <pred>…'; help = '<count> or more sub-<pred>s match' }; def = { help = 'word has at least one definition that contains all <keyword>s'; syntax = '<keyword>…'; fn = function(e,...) local kw = {...} for i,d in ipairs(e.word.defs) do ................................................................................ ::notfound:: end end return false end; }; lit = { help = 'word is, begins with, or ends with <word>'; syntax = '<word> [(pfx|sfx)]'; fn = function(e,val,op) if not op then return e.lit == val elseif op == 'pfx' then return val == string.sub(e.lit,1,#val) elseif op == 'sfx' then return val == string.sub(e.lit,(#e.lit) - #val + 1) else id10t('[lit %s %s] is not a valid filter, “%s” should be either “pfx” or “sfx”',val,op,op) end end; }; form = { help = 'match against word\'s inflected forms'; syntax = '(<inflect> | <form> (set | is <inflect> | (pfx|sfx|match) <affix>))'; fn = function(e, k, op, v) end; }; part = { help = 'word has definitions for every <part> of speech'; syntax = '<part>…'; fn = function(e,...) local map, tgt = prepScan(...) ................................................................................ for i,d in ipairs(e.word.defs) do if map[d.part] then matches = matches + 1 end end return matches == tgt end }; root = { help = 'match a word that derives from every <word>'; syntax = '<word>…'; fn = function(e,...) local map, tgt = prepScan(...) for i,d in ipairs(e.word.defs) do local matches = 0 for j,r in ipairs(d.branch) do if map[r] then matches = matches + 1 end end if matches == tgt then return true end end end }; note = { help = 'word has a matching note'; syntax = '([kind <kind> [<term>]] | term <term> | (min|max|count) <n>)'; fn = function(e, op, k, t) if op == 'kind' or op == 'term' then if op == 'term' and t then id10t('too many arguments for [note term <term>]') end for _,d in ipairs(e.word.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) -- this is cursed, rewrite without regex pls TODO if p == '.' then return {} end local function comp(pfx) return pfx .. '([0-9]+)' end local function mtry(...) ................................................................................ end if w then break end end end if not w then w=p:match('^(.-)%.?$') end return {w = w, dn = tonumber(dn), mn = tonumber(mn), pn=tonumber(pn); nn = tonumber(nn), xn = tonumber(xn)} end local function pathString(p,styler) local function s(s, st, ...) if styler then return styler[st](tostring(s),...) else return s end end local function comp(c,n,...) ................................................................................ local t = {} if p.w then t[1] = s(p.w,'ul') else return '.' end if p.dn then t[2] = string.format(".%s", s(p.dn,'br')) end if p.pn then t[#t+1] = comp('p',p.pn,4,true) end if p.mn then t[#t+1] = comp('m',p.mn,5,true) end if p.xn then t[#t+1] = comp('x',p.xn,6,true) elseif p.nn then t[#t+1] = comp('n',p.nn,4) end if t[2] == nil then return p.w .. '.' --make sure paths are always valid end return s(table.concat(t),'em') end local function pathMatch(a,b) return a.w == b.w and a.dn == b.dn ................................................................................ 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 ................................................................................ elseif super.nn then if sub.xn then return false end if sub.nn ~= super.nn then return false end end return true end local cmds = { create = { help = "initialize a new dictionary file"; syntax = "<lang>"; raw = true; exec = function(ctx, lang) ................................................................................ local fd = safeopen(ctx.file,"wb") local new = { header = { lang = lang; meta = ""; partsOfSpeech = {}; inflectionForms = {}; }; words = {}; relsets = {}; } local o = writeDict(new); fd:write(o) fd:close() end; }; coin = { ................................................................................ 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}; ................................................................................ end; }; mean = { help = "add a meaning to a definition"; syntax = "<word> <def#> <meaning>"; write = true; exec = function(ctx,word,dn,m) local t = pathResolve(ctx,{w=word,dn=dn}) table.insert(t.d.means, {lit=m,notes={}}) end; }; rel = { help = "manage groups of related words"; syntax = { "(show|purge) <path> [<kind>]"; "(link|drop) <word> <group#> <path>…"; ................................................................................ end end end; }; mod = { help = "move, merge, split, or delete words or definitions"; syntax = { "<path> (drop | [move|merge|clobber] <path> | out [<part> [<root>…]])"; "path ::= <word>[(@<def#>[/<meaning#>[:<note#>]]|.)]"; }; write = true; }; note = { 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 = {...} ................................................................................ end end end function cmds.ls.exec(ctx,...) local filter = nil local out = {} for i,f in ipairs{...} do local fn = parsefilter(f) local of = filter or function() return false end filter = function(e) return fn(e) or of(e) end end for lit,w in pairs(ctx.dict.words) do local e = {lit=lit, word=w} if filter == nil or filter(e) then table.insert(out, e) end end table.sort(out, function(a,b) return a.lit < b.lit end) local fo = ctx.sty[io.stdout] ................................................................................ return { syn = flatten(synonymSets); ant = flatten(antonymSets); met = flatten(metonymSets); } end local function formatRels(rls, padlen) -- optimize for the common case if next(rls.syn) == nil and next(rls.ant) == nil and next(rls.met) == nil then return {} end local pad = string.rep(' ',padlen) local function format(label, set) local each = map(set, function(e) ................................................................................ local str = fo.ul(e.w) if ed then str = string.format('%s(%s)',str,ed.part) end if e.mn then str = string.format('%s§%u',str,e.mn) end return str end) return fo.em(string.format("%s%s %s",pad,label,table.concat(each,', '))) end local lines = {} local function add(l,c,lst) table.insert(lines, format(fo.color(l,c,true),lst)) end if next(rls.syn) then add('synonyms:',2,rls.syn) end if next(rls.ant) then add('antonyms:',1,rls.ant) end if next(rls.met) then add('metonyms:',4,rls.met) end return lines end local function meanings(w,d,md,n) local start = md and 2 or 1 local part = string.format('(%s)', d.part) local pad = md and string.rep(' ', #part) or '' local function note(n,insert) if not next(n.paras) then return end local pad = string.rep(' ',#(n.kind) + 9) insert(' ' .. fo.hl(' ' .. n.kind .. ' ') .. ' ' .. n.paras[1]) for i=2,#n.paras do insert(pad..n.paras[2]) end end local m = { (function() if d.means[1] then if md then local id = '' if ctx.flags.ident then id=' ['..pathString({w=w.lit,dn=n,mn=1}, fo)..']' end return string.format(" %s %s 1. %s", id, fo.em(part), d.means[1].lit) end else return fo.em(string.format(' %s [empty definition #%u]', part,n)) end end)() } tcatD(m, formatRels(gatherRelSets{w=w.lit,dn=n,mn=1}, 6)) for i=start,#d.means do local v = d.means[i] local id = '' if ctx.flags.ident then id='['..pathString({w=w.lit,dn=n,mn=n}, fo)..']' end table.insert(m, string.format(' %s%s %u. %s', pad, id, i, v.lit)) tcatD(m, formatRels(gatherRelSets{w=w.lit,dn=n,mn=i}, 6)) for j,n in ipairs(v.notes) do note(n, function(v) table.insert(m, v) end) end end return table.concat(m,'\n') end local function autobreak(str) if str ~= '' then return str..'\n' else return str 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 for j, def in ipairs(w.word.defs) do local syn if wsc and wsc[j] then syn = wsc[j] end d=d .. '\n' .. meanings(w,syn,def,true,j) .. '\n' .. autobreak(table.concat( formatRels(gatherRelSets{w=w.lit,dn=j}, 4), '\n' )) end d=d .. wordrels .. '\n' end io.stdout:write(d) end end function cmds.import.exec(ctx,file) local ifd = io.stdin if file then ifd = safeopen(file,'r') ................................................................................ local new = { header = { lang = lang; meta = ""; partsOfSpeech = {}; inflectionForms = {}; }; words = {}; relsets = {}; } local state = 0 local relsets = {} local path = {} local inflmap, lastinfl = {} for l in ifd:lines() do local words = strwords(l) local c = words[1] local function syn(mn,mx) local nw = #words - 1 if nw < mn or (mx ~= nil and nw > mx) then if mx ~= nil then id10t('command %s needs between %u~%u words',c,mn,mx) else id10t('command %s needs at least %u words',c,mn) end end end if c ~= '*' and c~='meta' then -- comments if state == 0 then 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 = {}; } ................................................................................ elseif c == 'fp' then syn(1) if not lastinfl then id10t 'fp can only be used after f' end 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 ................................................................................ ofd:write(o) ofd:close() end function cmds.export.exec(ctx,file) local ofd = io.stdout if file then ofd = safeopen(file, 'w+') end local function san(str) local d = 0 local r = {} for i,cp in utf8.codes(str) do -- insert backslashes for characters that would -- disrupt strwords() parsing if cp == 0x0a then table.insert(r, 0x5c) table.insert(r, 0x6e) else if cp == 0x5b then d = d + 1 elseif cp == 0x5d then if d >= 1 then d = d - 1 else table.insert(r, 0x5c) end end table.insert(r, cp) end end return '[' .. utf8.char(table.unpack(r)) .. ']' end local function o(lvl,...) local pfx = '' 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 -- DOES NOT preserve order!! local top = #lst for i=top,1,-1 do local m = lst[i] ................................................................................ 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 ................................................................................ showHelp(ctx, cmd, c) end end end local globalFlags <const> = { human = {'h','human','enable human-readable exports'}; ident = {'i','ident','show identifier paths for all items'} } local function usage(me,ctx) local ln = 0 local ct = {} local fe = ctx.sty[io.stderr] ................................................................................ showHelp(ctx,k,v) end return 64 end local function dispatch(argv, ctx) local ferr = ctx.sty[io.stderr] local args = {} local flags = {} local i = 1 while i <= #argv do local a = argv[i] if a == '--' then i=i+1 break elseif a:sub(1,2) == '--' then ................................................................................ if v[1] == c then flags[k] = true break end end end else table.insert(args, a) end i = i + 1 end for j=i,#argv do table.insert(args,argv[j]) end local file, cmd = table.unpack(args) if cmd and cmds[cmd] then local c,fd,dict = cmds[cmd] if (not c.raw) and not c.nofile then fd = safeopen(file, "rb") dict = readDict(fd:read 'a') fd:close() -- lua io has no truncate method, so we must -- rely on the clobbering behavior of the open() -- call instead :( end cmds[cmd].exec({ sty = ctx.sty; try = ctx.try; log = ctx.log; flags = flags; file = file; fd = fd; dict = dict; }, table.unpack(args,3)) |
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | > | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > | > > | | | | | > | > | > > | > > > > > | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | > > > > > > > > > > | > > | | < > | < > > > | | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > < > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > | < < < < < < | | > | > > > > > > > > > > > > > > > > > > > > > > > > > | | > | | | | > > > > > > > > > > > | | > | | < | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | > | > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | | | > > > > > > | > | | | | | | > > > > > > > > > > > > > > > | | < | < < < < < < < < < < < < < < < < < < < < < < | > > | > > > > > > > > > > > > > > > > > > | | < < | > > > > > > > > > > > > > > > > | > > > > > > > > > | > | < | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > | |
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 .. 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 ... 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 ... 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 ... 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 ... 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 ... 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 ... 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 ... 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 ... 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 ... 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 ... 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 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 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 975 976 977 978 979 980 981 982 983 984 985 ... 993 994 995 996 997 998 999 1000 1001 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 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 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 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 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 .... 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 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 .... 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 .... 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 .... 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 .... 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 .... 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 .... 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 .... 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 .... 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 .... 1561 1562 1563 1564 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 .... 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 .... 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 .... 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 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 .... 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 .... 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 .... 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 .... 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 .... 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 .... 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 .... 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 .... 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 |
-- < Commission for Defense Communication > -- +WCO Worlds Culture Overdirectorate -- SSD Social Sciences Directorate -- ELS External Linguistics Subdirectorate -- +WSO Worlds Security Overdirectorate -- EID External Influence Directorate ] --# parvan -- --## orthographies -- parvan supports encoding words using multiple -- orthographies. every database has a "primary" -- orthography, which must be Unicode- or ASCII- -- compatible, and which is used as the basis of the -- uniform object paths. other orthographies can be -- managed with the [$script] command, which can set -- how they are displayed to the user. every word -- can have zero or more representations mapped to a -- particular orthography. -- --## file format -- parvan defines two separate file formats, both -- representations of a dictionary. one, the "working -- format", is binary; the other, the "exchange format" -- is comprised of UTF8 codepoint sequences and can be -- (to some degree) written and read by human beings, -- tho its primary purpose is as a line-based format -- that allows parvan dictionaries to be managed with -- conventional source control solutions like fossil -- or git -- --## magic numbers -- all parvan files share the same 4-byte header. it -- is comprised of the sequence -- [$ 0x50 0x56 $VERS $SUBTYPE ] -- where [$$VERS] is a byte that is altered whenever a -- breaking change is made to the format. [$$SUBTYPE] -- indicates whether the file is binary- or text-based. -- the byte 0x20 indicates an exchange file, while -- 0x02 indicates a binary database. -- --## extensions -- parvan recommends the use of the extension [$*.pv] -- for its binary databases, and [$*.pvx] for the -- exchange format. -- --## styled text -- text in parvan documents should be written using -- cortav syntax. at some future time this will -- hopefully be used to generate styled output where -- possible local function implies(a,b) return a==b or not(a) end local function map(lst,fn) local new = {} for k,v in pairs(lst) do local nv, nk = fn(v,k) new[nk or k] = nv ................................................................................ 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 mergeD(dest, tbl, ...) if tbl == nil then return dest end for k,v in pairs(tbl) do dest[k] = v end return mergeD(dest, ...) end local function merge(...) local t = {} return mergeD(t, ...) end local function copy(t,...) return merge(t), copy(...) 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 function iota(n,m) if not m then return iota(1,n) end if n == m then return n end return n, iota(n+1,m) end local function keys(m) local i,ks = 1,{} for k in next, m do ks[i] = k i = i + 1 end return ks end local ansi = { levels = { plain = 0; ansi = 1; color = 2; color8b = 3; color24b = 4; ................................................................................ local id = function(...) return ... end local esc = '\27' local f = {} for k,v in pairs(ansi.seqs) do local lvl, on, off = table.unpack(v) if lvl <= cl then f[k] = function(s) return (esc..on) .. s .. (esc..off) end else f[k] = id end end local function ftoi(r,g,b) return math.ceil(r*0xff), math.ceil(g*0xff), math.ceil(b*0xff) end local reset = "\27[39m" function f.color(str, n, bright) if n<=15 then return string.format("\27[%s%cm", (bg and 4 or 3) + (br and 6 or 0), 0x30+n) .. str .. reset else return string.format("\27[%c8;5;%sm", (bg and 0x34 or 0x33), n) .. str .. reset end end function f.resetLine() return '\27[1K\13' end if cl == ansi.levels.color24b then function f.rgb(str, r,g,b, bg) return string.format("\27[%c8;2;%u;%u;%um", bg and 0x34 or 0x33, ................................................................................ for i=1,n do table.insert(vals, parse(t, s)) end return vals end; } end fmt.any = function(struct) local map, keylist = {},{} for i,v in ipairs(struct) do if type(v) ~= 'string' then map[v[1]] = v[2] v = v[1] else map[v] = true end table.insert(keylist, v) end local tdisc = fmt.enum(table.unpack(keylist)) return { encode = function(a) if type(a) == 'string' and map[a] == true then return marshal(tdisc, a) else local tname, obj = table.unpack(a) assert(map[tname] ~= true, '`any` enumeration '..tostring(tname)..' has no associated struct') return marshal(tdisc, tname) .. marshal(map[tname], obj) end end; decode = function(s) local tname = parse(tdisc, s) if map[tname] ~= true then local obj = parse(map[tname], s) return {tname,obj} else return tname end end; } end fmt.map = function(from,to,ity) local ent = fmt.list({ {'key', from}, {'val', to} }, ity) return { ................................................................................ end; decode = function(s) local lst = ent.decode(s) local m = {} for _,p in pairs(lst) do m[p.key] = p.val end return m end; null = function() return {} end; } end fmt.enum = function(...) local vals,rmap = {...},{} for k,v in pairs(vals) do rmap[v] = k-1 end local ty = fmt.u8 ................................................................................ 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.blob = fmt.string fmt.relatable = function(ty) return tcat(ty,{ {'rels',fmt.list(fmt.uid,fmt.u16)}; }) end ................................................................................ {'notes', fmt.list(fmt.note,fmt.u8)}; } fmt.phrase = fmt.relatable { {'str',fmt.label}; {'means',fmt.list(fmt.meaning,fmt.u8)}; } fmt.ortho = fmt.map(fmt.uid, fmt.blob, fmt.u8) -- UID <0> is always the UTF-8 representation of the primary ortho fmt.writing = { {'enc',fmt.ortho}; -- if empty, print the morphs in sequence {'info',fmt.label}; {'morphs',fmt.list(fmt.uid,fmt.u16)}; } fmt.def = fmt.relatable { {'writings', fmt.list(fmt.writing,fmt.u8)}; -- for japanese-like languages where words that are -- pronounced/written the same under the indexing -- orthography have alternate writings that are -- definition-specific. e.g. words よう and さま -- would both have a definition written as 様 -- ordinary languages will have only 1 writing {'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)}; -- store secondary encodings of this word {'enc', fmt.ortho}; } fmt.orthography = { {'uid', fmt.uid}; {'name', fmt.tag}; {'repr', fmt.any{ 'utf8'; -- display as utf-8 compatible text 'opaque'; -- do not display at all; used only by other tools 'bytes'; -- display as raw hexadecimal bytes {'int',fmt.u8}; -- display as a series of integers (n=byte len) {'glyphs',{ -- map to a palette of custom glyphs. -- treated as 'opaque' in text-only environments {'glyphs', fmt.list { {'image',fmt.blob}; {'name',fmt.tag}; }}; {'encoding', fmt.u8}; -- number of bytes per codepoint {'format',fmt.enum('svg','bmp','png')}; }}; }}; } fmt.dictHeader = { {'lang', fmt.tag}; {'meta', fmt.string}; {'partsOfSpeech', fmt.list(fmt.tag,fmt.u16)}; {'inflectionForms', fmt.list({ ................................................................................ {'name', fmt.tag}; {'abbrev', fmt.tag}; {'desc', fmt.string}; {'parts', fmt.list(fmt.tag,fmt.u8)}; -- which parts of speech does this form apply to? -- leave empty if not relevant },fmt.u16)}; {'orthographies', fmt.list(fmt.orthography,fmt.u8)} } 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.pair = function(k,v) return { {'key',k or fmt.tag}; {'val', v or fmt.blob}; } end fmt.morph = { {'name',fmt.tag}; {'enc', fmt.ortho}; {'meta', fmt.list(fmt.pair(nil,fmt.string),fmt.u16)}; {'rads', fmt.list(fmt.uid,fmt.u16)}; } fmt.dict = { {'header', fmt.dictHeader}; {'words', fmt.map(fmt.string,fmt.word)}; {'relsets', fmt.list(fmt.relSet)}; {'morphs', fmt.map(fmt.uid,fmt.morph)}; } function marshal(ty, val, pvers) pvers = pvers or 0 if ty.encode then return ty.encode(val) end local ac = {} for idx,fld in ipairs(ty) do local name, fty, vers = table.unpack(fld) vers = vers or 0 if pvers >= vers then table.insert(ac, marshal(fty, assert(val[name], string.format('marshalling error: missing field %s', name) ), pvers)) end end return table.concat(ac) end function parse(ty, stream, pvers) pvers = pvers or 0 if ty.decode then return ty.decode(stream) end local obj = {} for idx,fld in ipairs(ty) do local name, fty, vers, dflt = table.unpack(fld) vers = vers or 0 if pvers >= vers then obj[name] = parse(fty, stream, pvers) else obj[name] = dflt end end return obj end local function atomizer() local map = {} ................................................................................ return 'PV0\2'..marshal(fmt.dict, d) end local function readDict(file) local s = stream(file) local magic = s:next 'c4' if magic == 'PV0 ' then id10t 'text-based dictionaries must be translated to binary using the `import` command before they can be used' elseif magic ~= 'PV0\2' then id10t 'not a parvan0 file' end local d = parse(fmt.dict, s) -- handle atoms for lit,w in pairs(d.words) do for j,def in ipairs(w.defs) do def.part = d.header.partsOfSpeech[def.part] end ................................................................................ -- enable faster lookup that would otherwise require -- expensive scans rebuildRelationCache(d) return d end local function strwords(str,maxwords) -- here be dragons local wds = {} local w = {} local state, d, quo, dquo = 0,0 local function flush(n,final) if next(w) or state ~= 0 and state < 10 then table.insert(wds, utf8.char(table.unpack(w))) w = {} ................................................................................ elseif final and state > 10 then table.insert(wds, '\\') end state = n quo = nil dquo = nil d = 0 if #wds == maxwords then state = 100 end end local function isws(c) return c == 0x20 or c == 0x09 or c == 0x0a end for p,cp in utf8.codes(str) do if state == 0 then -- begin if not(isws(cp)) then ................................................................................ -- 12 = quote escape, 11 = raw escape if cp == 0x63 then --n table.insert(w,0x0a) else table.insert(w,cp) end state = state - 10 elseif state == 100 then -- word limit reached -- triggered from flush table.insert(wds, string.sub(str, p)) return wds end end flush(nil,true) return wds end local function strsan(str) local d,m = 0,0 local r = {} local unclosed = {} local i = 1 for bytepos,cp in utf8.codes(str) do -- insert backslashes for characters that would -- disrupt strwords() parsing if cp == 0x0a then table.insert(r, 0x5c) table.insert(r, 0x6e) i=i+2 else if cp == 0x5b then d = d + 1 table.insert(unclosed,i) elseif cp == 0x5d then if d >= 1 then d = d - 1 unclosed[rawlen(unclosed)] = nil else table.insert(r, 0x5c) i=i+1 end end table.insert(r, cp) i=i+1 end end for j=#unclosed,1,-1 do table.insert(r,unclosed[j],0x5c) end return '[' .. utf8.char(table.unpack(r)) .. ']' end local predicates local function parsefilter(str) local f = strwords(str) if #f == 1 then return function(e) return predicates.lit.fn(e,f[1]) end end if not next(f) then -- null predicate matches all return function() return true end elseif not predicates[f[1]] then id10t('no such predicate %s',f[1]) else local p = predicates[f[1]].fn return function(e) return p(e, table.unpack(f,2)) end end ................................................................................ end; local function p_none(e,pred,...) if pred == nil then return true end pred = parsefilter(pred) if pred(e) then return false end return p_none(e,...) end; local function p_some(e,cmp,count,...) local cfn = { eq = function(a,b) return a == b end; ne = function(a,b) return a ~= b end; lt = function(a,b) return a < b end; gt = function(a,b) return a > b end; } if not cfn[cmp] then id10t('[some %s]: invalid comparator', cmp) end count = tonumber(count) local function rec(n,pred,...) if pred == nil then return cfn[cmp](n,count) end pred = parsefilter(pred) if pred(e) then n=n+1 end return rec(n,...) end return rec(0,...) end; local function prepScan(...) local map = {} local tgt = select('#',...) for _,v in pairs{...} do map[v] = true end return map,tgt end predicates = { all = { fn = p_all; syntax = '<pred>…'; help = 'every sub-<pred> matches'; }; any = { fn = p_any; syntax = '<pred>…'; help = 'any sub-<pred> matches'; }; none = { fn = p_none; syntax = '<pred>…'; help = 'no sub-<pred> matches (also useful to force evaluation for side effects without creates matches)'; }; some = { fn = p_some; syntax = '(eq|ne|lt|gt) <count> <pred>…'; help = '<count> [or more/less] sub-<pred>s match'; }; seq = { syntax = "<wrap> '[' <arg>… ']' <pred>…"; help = 'reuse the same stack of arguments'; fn = function(e,wrap,args,...) local lst = {} local function eval(pred,...) if not pred then return end table.insert(lst, pred .. ' ' .. args) eval(...) end eval(...) local filter = wrap .. ' ' ..table.concat(map(lst, strsan), ' ') return parsefilter(filter)(e) end; }; mark = { syntax = '<mark> [<pred>]'; help = 'apply <mark> to the words that match <pred>, or all the words that are tested if no <pred> is supplied. use to visually indicate the reason that a given term matched the query'; fn = function(e, val, pred) if pred == nil or parsefilter(pred)(e) then e.mark = e.mark or {} for k,v in pairs(e.mark) do if v==val then return true end end table.insert(e.mark, val) return true end end; }; clear = { syntax = '<mark> [<pred>]'; help = 'like [mark] but clears marks instead of setting them'; fn = function(e, val, pred) if pred == nil or parsefilter(pred)(e) then e.mark = e.mark or {} for k,v in pairs(e.mark) do if v==val then table.remove(e.mark,k) return true end end return true end end; }; marked = { syntax = '(by <mark> [pred]|in <pred>)'; help = 'tests for an existing <mark> on the result'; fn = function(e, mode, val, pred) if mode == 'in' then pred = val val = nil if pred == nil then id10t '[marked in <pred>] requires a predicate' end elseif mode == 'by' then if val == nil then id10t '[marked by <mark>] requires a mark' end else id10t('invalid form [marked %s]', mode) end if pred == nil or parsefilter(pred)(e) then if e.mark == nil or not next(e.mark) then return false end if val then for k,v in pairs(e.mark) do if v==val then return true end end else return true end end end; }; def = { help = 'word has at least one definition that contains all <keyword>s'; syntax = '<keyword>…'; fn = function(e,...) local kw = {...} for i,d in ipairs(e.word.defs) do ................................................................................ ::notfound:: end end return false end; }; lit = { help = 'word is, begins with, matches, or ends with <search> in <script> or the primary orthography ("also" enables searching the primary as well as the listed scripts)'; syntax = '<search> [(pfx|sfx|match)] [any|(in|also) <script>…]'; fn = function(e,val,...) local opts,oc = {...},1 local scripts, op = {0} if opts[oc] == 'pfx' or opts[oc] == 'sfx' or opts[oc] == 'match' then op = opts[oc] oc = oc + 1 end if opts[oc] then if opts[oc] == 'any' then scripts = nil else if opts[oc] == 'in' then scripts = {} elseif opts[oc] ~= 'also' then id10t('[lit … %s]: invalid spec', opts[oc]) end if #opts < oc+1 then id10t('[lit … %s]: missing argument', opts[oc]) end for i=oc+1,#opts do table.insert(scripts, opts[oc]) end end end if not op then return e.lit == val elseif op == 'pfx' then return val == string.sub(e.lit,1,#val) elseif op == 'sfx' then return val == string.sub(e.lit,(#e.lit) - #val + 1) elseif op == 'match' then return string.find(e.lit, val) ~= nil else id10t('[lit %s %s] is not a valid filter, “%s” should be “pfx”, “sfx”, or “match”',val,op,op) end end; }; morph = { help = 'find words with specific morphs'; syntax = "(any|all|only) <script> [seq] ((lit|rec) <repr>|rad '[' <repr>… ']')…"; }; form = { help = 'match against word\'s inflected forms'; syntax = '(<inflect> | (of <form>|has) [any] ([un]set | is <inflect> | (pfx|sfx|sub) <affix>)…)'; fn = function(e, k, mode, ...) if k == nil then -- eq [form has set] for _,d in pairs(e.word.defs) do if next(d.forms) then return true end end elseif mode == 'of' or mode == 'has' then local match,mc = {...},1 if not next(match) then id10t('[form %s]: missing spec',mode) end local any = match[1]=='any' local eval = function()return true end; if any then nc = 2 eval = function()return false end; end local ok = false local fns = { set = function(a) return a~=nil end; unset = function(a) return a==nil end; is = function(a,b)return a and a==b end; pfx = function(a,b)return a and string.sub(a,1,#b) == b end; sfx = function(a,b)return a and string.sub(a,0-#b) == b end; sub = function(a,b)return a and string.find(a,b) ~= nil end; } repeat local n, op, arg = 1, table.unpack(match,mc) print(op,arg) if not op then id10t "missing argument for [form]" end if not fns[op] then id10t('[form … %s] is not a valid filter', op) end if op ~= "set" and op ~= 'unset' then n = 2 if not arg then id10t('[form … %s]: missing argument', op) end end local oe = eval eval = any and function(a,b) return fns[op](a,b) or oe(a,b) end or function(a,b) return fns[op](a,b) and oe(a,b) end ok = true mc = mc + n until mc > #match if not ok then id10t '[form]: incomplete spec' end for _,d in pairs(e.word.defs) do if mode=='has' then for cat,infd in pairs(d.forms) do if eval(infd) then return true end end else if eval(d.forms[k]) then return true end end end elseif mode ~= nil then id10t('[form %s]: invalid mode', mode) else for _,d in pairs(e.word.defs) do for _,v in pairs(d.forms) do if v == k then return true end end end end return false end; }; part = { help = 'word has definitions for every <part> of speech'; syntax = '<part>…'; fn = function(e,...) local map, tgt = prepScan(...) ................................................................................ for i,d in ipairs(e.word.defs) do if map[d.part] then matches = matches + 1 end end return matches == tgt end }; root = { help = 'match an entry that derives from every <word>'; syntax = '<word>…'; fn = function(e,...) local map, tgt = prepScan(...) for i,d in ipairs(e.word.defs) do local matches = 0 for j,r in ipairs(d.branch) do if map[r] then matches = matches + 1 end end if matches == tgt then return true end end end }; phrase = { syntax = '<pred>…'; help = 'match only words with phrases'; }; ex = { syntax= '[by <source>] [(any|all) <term>…]'; help = 'entry has an example by <source> with any/all of <term>s'; fn = function() end; }; note = { help = 'entry has a matching note'; syntax = '([kind <kind> [<term>]] | term <term> | (min|max|count) <n>)'; fn = function(e, op, k, t) if op == 'kind' or op == 'term' then if op == 'term' and t then id10t('too many arguments for [note term <term>]') end for _,d in ipairs(e.word.defs) do ................................................................................ if not fd then error(userError("cannot open file " .. file),2) end return fd else return file end end local function pathParse(p) -- this is cursed, rewrite without regex pls TODO if p == '.' then return {} end local function comp(pfx) return pfx .. '([0-9]+)' end local function mtry(...) ................................................................................ end if w then break end end end if not w then w=p:match('^(.-)%.?$') end return {w = w, dn = tonumber(dn), mn = tonumber(mn), pn=tonumber(pn); nn = tonumber(nn), xn = tonumber(xn)} end local function pathString(p,styler,display) local function s(s, st, ...) if styler then return styler[st](tostring(s),...) else return s end end local function comp(c,n,...) ................................................................................ local t = {} if p.w then t[1] = s(p.w,'ul') else return '.' end if p.dn then t[2] = string.format(".%s", s(p.dn,'br')) end if p.pn then t[#t+1] = comp('p',p.pn,4,true) end if p.mn then t[#t+1] = comp('m',p.mn,5,true) end if p.xn then t[#t+1] = comp('x',p.xn,6,true) elseif p.nn then t[#t+1] = comp('n',p.nn,4) end if t[2] == nil and not display then return p.w .. '.' --make sure paths are always valid end return s(table.concat(t),'em') end local function pathMatch(a,b) return a.w == b.w and a.dn == b.dn ................................................................................ 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) if a.mn then res.meaning = lookup('meaning', res.phrase.means, a.mn) else return res end 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 ................................................................................ elseif super.nn then if sub.xn then return false end if sub.nn ~= super.nn then return false end end return true end function ansi.formatMarkup(text, sty) return (string.gsub(text, '(.?)(%b[])', function(esc,seg) if esc == '\\' then return seg end local mode, text = seg:match('^%[(.)%s*(.-)%]$') local r if mode == '\\' then r = text elseif mode == '*' then r = sty.br(ansi.formatMarkup(text,sty)) elseif mode == '!' then r = sty.em(ansi.formatMarkup(text,sty)) elseif mode == '_' then r = sty.ul(ansi.formatMarkup(text,sty)) elseif mode == '$' then r = sty.color(ansi.formatMarkup(text,sty),6,true) elseif mode == '>' then r = pathString(pathParse(text),sty,true) else return seg end return esc..r end)) end local cmds = { create = { help = "initialize a new dictionary file"; syntax = "<lang>"; raw = true; exec = function(ctx, lang) ................................................................................ local fd = safeopen(ctx.file,"wb") local new = { header = { lang = lang; meta = ""; partsOfSpeech = {}; inflectionForms = {}; orthographies = {}; }; words = {}; relsets = {}; morphs = {}; } local o = writeDict(new); fd:write(o) fd:close() end; }; coin = { ................................................................................ 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={},enc={}} 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={},enc={}} end local n = #(ctx.dict.words[word].defs)+1 ctx.dict.words[word].defs[n] = { part = part; writings = {}; branch = etym; means = {means and { lit=means; examples={}; notes={}; rels={}; } or nil}; ................................................................................ end; }; mean = { help = "add a meaning to a definition"; syntax = "<word> <def#> <meaning>"; write = true; exec = function(ctx,word,dn,m) local t = pathResolve(ctx,{w=word,dn=tonumber(dn)}) table.insert(t.def.means, {lit=m,notes={},examples={},rels={}}) end; }; rel = { help = "manage groups of related words"; syntax = { "(show|purge) <path> [<kind>]"; "(link|drop) <word> <group#> <path>…"; ................................................................................ end end end; }; mod = { help = "move, merge, split, or delete words or definitions"; syntax = { "<path> (drop | [(to|move)|merge|clobber] <path>)"; "path ::= <word>[.[<def#>[/p<phrase#>][/m<meaning#>[(/n<note#>|/x<example#>)]]]]"; }; write = true; }; morph = { help = "manage and attach morphs (morphemes/composable glyphs)"; syntax = { "(<ls>|<define>|<mod>)"; "define ::= def (id <name>|as) [<form>]… [from <morph>…]"; "ls ::= ls (<morph>|meta <key> <value>|has <key>)…"; "mod ::= <morph> (drop|[un]link <path>|meta <key> [<value>]|inc [<morph>])"; "morph ::= (id <name>|enc <form>)"; "form ::= [<script>]=<repr>"; }; }; note = { 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#>[/p<phrase#]/m<meaning#>"}; write = true; exec = function(ctx,path,...) local paras, mng local dest = pathParse(path) local t = pathResolve(ctx,path) if dest.nn then paras = {...} ................................................................................ end end end function cmds.ls.exec(ctx,...) local filter = nil local out = {} local args = {...} for i=#args,1,-1 do local f <const> = args[i] local fn = parsefilter(f) local of = filter or function() return false end filter = function(e) return fn(e) or of(e) end end for lit,w in pairs(ctx.dict.words) do local e = {lit=lit, word=w, dict=ctx.dict} if filter == nil or filter(e) then table.insert(out, e) end end table.sort(out, function(a,b) return a.lit < b.lit end) local fo = ctx.sty[io.stdout] ................................................................................ return { syn = flatten(synonymSets); ant = flatten(antonymSets); met = flatten(metonymSets); } end local function formatRels(lines, rls, padlen) -- optimize for the common case if next(rls.syn) == nil and next(rls.ant) == nil and next(rls.met) == nil then return {} end local pad = string.rep(' ',padlen) local function format(label, set) local each = map(set, function(e) ................................................................................ local str = fo.ul(e.w) if ed then str = string.format('%s(%s)',str,ed.part) end if e.mn then str = string.format('%s§%u',str,e.mn) end return str end) return fo.em(string.format("%s%s %s",pad,label,table.concat(each,', '))) end local function add(l,c,lst) table.insert(lines, format(fo.color(l,c,true),lst)) end if next(rls.syn) then add('synonyms:',2,rls.syn) end if next(rls.ant) then add('antonyms:',1,rls.ant) end if next(rls.met) then add('metonyms:',4,rls.met) end return lines end local function formatMeaning(m, obj, path, indent) -- m = dest tbl local pad = string.rep(' ', indent) local function note(j,n,markup) if not next(n.paras) then return end local pad = string.rep(' ',#(n.kind) + 9) local nid = '' if ctx.flags.ident then nid='‹'..pathString(merge(path,{nn=j}), fo)..'›' end table.insert(m, nid..' ' .. fo.hl(' ' .. n.kind .. ' ') .. ' ' .. markup(n.paras[1])) for i=2,#n.paras do table.insert(m, pad..markup(n.paras[2])) end end local id = '' if ctx.flags.ident then id='‹'..pathString(path, fo)..'›' end table.insert(m, string.format('%s%s %u. %s', pad, id, path.mn, ansi.formatMarkup(obj.lit,fo))) formatRels(m,gatherRelSets(path), 6) for j,n in ipairs(obj.notes) do note(j,n, function(v) return ansi.formatMarkup(v,fo) end) end end local function defnMeanings(m, w,def,path,indent) local part = '' for i=1,#def.means do local v = def.means[i] formatMeaning(m, v, merge(path,{mn=i}),indent) end end local function parthead(def) local str = string.format('(%s)', def.part) return fo.color(fo.em(str), 199), #str end local markcolor, markcolors, markmap = 0, { 117, 75, 203, 48, 200, 190, 26, 48, 226, 198 }, {} for i, w in ipairs(out) do local lines = { fo.ul(fo.br(w.lit)) } local pad = 4 local ndefs = #w.word.defs if ndefs == 1 then local header = parthead(w.word.defs[1]) lines[1] = lines[1] .. ' ' .. header end local mark local markline if w.mark then local marks = {} for _,m in pairs(w.mark) do if not markmap[m] then markmap[m] = markcolors[markcolor+1] markcolor = (markcolor+1)%#markcolors end local c = markmap[m] table.insert(marks, fo.hl(fo.color(string.format(' %s ',m),c))) end mark = table.concat(marks, ' ') markline = #lines end for j, d in ipairs(w.word.defs) do local top = #lines -- :/ local header, hdln = parthead(d) defnMeanings(lines, w, d, {w=w.lit, dn=j}, ndefs==1 and 0 or hdln+1) if ctx.flags.rels then formatRels(lines, gatherRelSets{w=w.lit,dn=j}, 0) end if ndefs > 1 then lines[top+1] = ' ' .. header .. string.sub(lines[top+1],hdln+2) end end if ctx.flags.rels then formatRels(lines,gatherRelSets{w=w.lit}, 2) end if markline then lines[markline] = mark .. ' ' .. lines[markline] end io.stdout:write(table.concat(lines,'\n')..'\n') end end function cmds.import.exec(ctx,file) local ifd = io.stdin if file then ifd = safeopen(file,'r') ................................................................................ local new = { header = { lang = lang; meta = ""; partsOfSpeech = {}; inflectionForms = {}; orthographies = {}; }; words = {}; relsets = {}; morphs = {}; } local state = 0 local relsets = {} local path = {} local inflmap, lastinfl = {} local orthoIDs, lastOrtho = {} local morphIDs, lastMorph = {} local lastWriting for l in ifd:lines() do local words = strwords(l) local c = words[1] local function syn(mn,mx) local nw = #words - 1 if nw < mn or (mx ~= nil and nw > mx) then if mx ~= nil then id10t('command %s needs between %u~%u words',c,mn,mx) else id10t('command %s needs at least %u words',c,mn) end end end local function getuid(tbl, uid) if tonumber(uid,16) == nil then if not tbl[uid] then tbl[uid] = math.random(0,0xffffFFFF) end return tbl[uid] end return tonumber(uid,16) end if c ~= '*' and c~='meta' then -- comments if state == 0 then 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 = 10 path = {w=words[2]} new.words[words[2]] = {defs={},rels={},enc={}} lastMorph = nil elseif c == 'f' then syn(1) local nf = { name = words[2]; abbrev = words[3] or ""; desc = words[4] or ""; parts = {}; } ................................................................................ elseif c == 'fp' then syn(1) if not lastinfl then id10t 'fp can only be used after f' end 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],16) relsets[words[3]].members = relsets[words[3]].members or {} elseif c == 'mo' then syn(1) local uid,name = table.unpack(words,2) uid = getuid(morphIDs, uid) new.morphs[uid] = { name = name or ''; enc = {}; meta = {}; rads = {}; } lastMorph = new.morphs[uid] elseif lastMorph and state < 10 and c == 'M' then syn(2) local key, val = table.unpack(words,2) table.insert(lastMorph.meta, {key=key,val=val}) elseif lastMorph and state < 10 and c == 'r' then syn(1) local r = getuid(morphIDs, words[2]) table.insert(lastMorph.rads, r) elseif c == 'e' then syn(2) local scr, blob = table.unpack(words,2) scr = getuid(orthoIDs, scr) if state <= 10 and lastMorph then lastMorph.enc[scr] = blob elseif state == 10 then W.enc[scr] = blob elseif state >= 11 and lastWriting then lastWriting.enc[scr] = blob else id10t('encoding “%s” declared at bad position', blob) end elseif c == 'o' then syn(3) local uid, name, repr = table.unpack(words,2) repr = strwords(repr) uid = getuid(orthoIDs, uid) if #repr > 1 then local kind, p1,p2 = table.unpack(repr) repr = {kind,{}} if kind == 'glyphs' then repr[2].format = p1 repr[2].glyphs = {} repr[2].encoding = p2 elseif kind == 'int' then repr[2] = tonumber(p1,16) end else repr=repr[1] end table.insert(new.header.orthographies, { uid = uid; name = name; repr = repr; }) lastOrtho = new.header.orthographies[#(new.header.orthographies)] elseif c == 'og' then syn(2) if not lastOrtho then id10t '`og` must follow an orthography declaration' elseif lastOrtho.repr[1] ~= 'glyphs' then id10t('orthography declares %s representation', lastOrtho.repr[1]) end local name, data = table.unpack(words,2) table.insert(lastOrtho.repr[2].glyphs, { -- TODO decode base?? data for binary encodings name = name, image = data }) elseif state >= 10 and c == 'r' or c == 'rh' then syn(1) local rt if state == 10 then rt = W.rels elseif state == 11 then rt = D.rels elseif state == 12 then rt = D.rels elseif state == 14 then rt = P.rels end relsets[words[2]] = relsets[words[2]] or { uid = tonumber(words[2],16) or math.random(0,0xffffFFFF); members={}; } local mems = relsets[words[2]].members if c == 'rh' and next(mems) then mems[#mems+1] = mems[1] mems[1] = path else table.insert(mems,path) end elseif state >= 10 and c == 'd' then syn(1) state = 11 table.insert(W.defs, { part = words[2]; writings = {}; branch = {}; means = {}; forms = {}; phrases = {}; rels = {}; }) path = {w = path.w, dn = #(W.defs)} elseif state >= 11 and c == 'dr' then syn(1) table.insert(D.branch, words[2]) elseif state >= 11 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 >= 11 and c == 'p' then syn(1) state = 12 table.insert(D.phrases, { str = words[2]; means = {}; rels = {}; }) path = {w = path.w, dn = path.dn, pn = #(D.phrases)} elseif state >= 11 and c == 'm' then syn(1) state = 13 table.insert((P or D).means, { lit = words[2]; notes = {}; examples = {}; rels = {}; }); path = {w = path.w, dn = path.dn, pn=path.pn, mn = #((P or D).means)} elseif state >= 11 and c == 'W' then table.insert(D.writings, { info = words[2] or ''; enc = {}; morphs = {}; }) lastWriting = D.writings[#(D.writings)] elseif state >= 11 and lastWriting and c == 'Wmo' then syn(1) local morph = getuid(morphIDs, words[2]) table.insert(lastWriting.morphs, morph) elseif state >= 13 and c == 'x' then syn(1) table.insert(M.examples, { quote = words[2]; src = words[3] or ''; }) elseif state >= 13 and c == 'n' then syn(1) state = 14 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 >= 14 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 ................................................................................ ofd:write(o) ofd:close() end function cmds.export.exec(ctx,file) local ofd = io.stdout if file then ofd = safeopen(file, 'w+') end local san = strsan local function o(lvl,...) local pfx = '' 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 if d._relCache[v].mems[1].obj == obj then o(lvl,'rh %x',v) else o(lvl,'r %x',v) 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 i,s in pairs(d.header.orthographies) do local repr if type(s.repr) == 'string' then repr = s.repr else repr = s.repr[1] end if repr == 'int' then repr = repr .. ' ' .. tostring(s.repr[2]) elseif repr == 'glyphs' then repr = repr .. ' ' .. s.repr[2].format .. ' ' .. tostring(s.repr[2].encoding) end o(0, 'o %x %s %s', s.uid, san(s.name), san(repr)) if s.repr[1] == 'glyphs' then for _,g in ipairs(s.repr[2].glyphs) do o(1, 'og %s %s', san(g.name), san(g.image)) end end end local function scanMeans(tbl,lvl) for j,m in ipairs(tbl) do o(lvl,'m %s', san(m.lit)) checksyn(m,lvl+1) for k,x in ipairs(m.examples) do o(lvl+1,'x %s %s', san(x.quote,x.src)) end 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 local function scanMeta(n, meta) for i,m in ipairs(meta) do o(n, 'M %s %s', san(m.key), san(m.val)) end end local function scanEnc(n, tbl) for uid,enc in pairs(tbl) do o(n, 'e %x %s',uid,san(enc)) end end for uid, m in pairs(d.morphs) do o(0, 'mo %x %s', uid, san(m.name)) scanMeta(1, m.meta) scanEnc(1, m.enc) end for lit, w in pairs(d.words) do o(0,'w %s',san(lit)) checksyn(w,1) scanEnc(1, w.enc) for i,def in ipairs(w.defs) do o(1,'d %s',san(def.part)) for _, writ in ipairs(def.writings) do if writ.info == '' then o(2,'W') else o(2,'W %s',san(writ.info)) end for mid,uid in pairs(writ.morphs) do o(3, 'Wmo %x', uid) end for uid,enc in pairs(writ.enc) do o(3, 'e %x %s',uid,san(enc)) end end checksyn(def,2) for j,r in ipairs(def.branch) do o(2,'dr %s',san(r)) end scanMeans(def.means, 2) for j,p in ipairs(def.phrases) do o(2,'p %s',san(p.str)) scanMeans(p.means, 3) end end end for _,s in ipairs(d.relsets) do o(0,'s %s %x', s.kind, s.uid) end end local function filterD(lst, fn) -- cheap algorithm to destructively filter a list -- DOES NOT preserve order!! local top = #lst for i=top,1,-1 do local m = lst[i] ................................................................................ top = top - 1 end end return lst end function cmds.mod.exec(ctx, orig, oper, dest, ...) local ops = { word = { mask = { word = {move=true,merge=true,clobber=true}; }; move = function(from,to) end; merge = function(from,to) end; clobber = function(from,to) end; }; def = { mask = { word = {move=true}; def = {merge=true,clobber=true}; }; move = function(from,to) end; merge = function(from,to) end; clobber = function(from,to) end; }; phrase = { mask = { def = {move=true}; phrase = {clobber=true}; }; move = function(from,to) end; clobber = function(from,to) end; }; meaning = { mask = { def = {move=true}; phrase = {move=true}; meaning = {merge=true,clobber=true}; }; move = function(from,to) end; merge = function(from,to) end; clobber = function(from,to) end; }; example = { mask = { meaning={move=true}; example={merge=true,clobber=true}; }; move = function(from,to) end; merge = function(from,to) end; clobber = function(from,to) end; }; note = { mask = { meaning={move=true}; note={merge=true,clobber=true}; }; move = function(from,to) end; merge = function(from,to) end; clobber = function(from,to) 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 ................................................................................ showHelp(ctx, cmd, c) end end end local globalFlags <const> = { human = {'h','human','enable human-readable exports'}; ident = {'i','ident','show identifier paths for all items'}; rels = {'r','rels', 'show relationships between words'}; } local function usage(me,ctx) local ln = 0 local ct = {} local fe = ctx.sty[io.stderr] ................................................................................ showHelp(ctx,k,v) end return 64 end local function dispatch(argv, ctx) local loglevel = 2 local ferr = ctx.sty[io.stderr] local args = {} local flags = {} local i = 1 while i <= #argv do local a = argv[i] if a == '--' then i=i+1 break elseif a:sub(1,2) == '--' then ................................................................................ if v[1] == c then flags[k] = true break end end end else table.insert(args, a) end i = i + 1 end for j=i,#argv do table.insert(args,argv[j]) end do local ll = os.getenv('parvan_log') if ll then loglevel = tonumber(ll) end if flags[quiet] then loglevel=0 elseif flags[debug] then loglevel=4 end end local file, cmd = table.unpack(args) if cmd and cmds[cmd] then local c,fd,dict = cmds[cmd] if (not c.raw) and not c.nofile then fd = safeopen(file, "rb") dict = readDict(fd:read 'a') fd:close() -- lua io has no truncate method, so we must -- rely on the clobbering behavior of the open() -- call instead :( end local function log(lvl,...) local loglevels = { fatal = 1, warn = 2, info = 3, debug = 4 } if loglevels[lvl] <= loglevel then ctx.log(lvl,...) end end cmds[cmd].exec({ sty = ctx.sty; try = ctx.try; log = log; flags = flags; file = file; fd = fd; dict = dict; }, table.unpack(args,3)) |
Added termcolors.lua version [ccbbe9b5fc].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > |
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 |
-- [ʞ] termcolors.lua -- ~ lexi hale <lexi@hale.su> -- © AGPLv3 -- ? print grids comparing truecolor output to 256-color output function conv(a) return 16 + math.floor(a.r*5)*36 + math.floor(a.g*5)*6 + math.floor(a.b*5) end local S = 32 local M = 33 * 4 local grid = {} local X,Y = 0,0 for R=0,S do for G=0,S do for B=0,S do local r,g,b = R/S, G/S, B/S grid[Y*M + X] = {r=r,g=g,b=b} X = X + 1 if X == M then X = 0 Y = Y + 1 end end end end local function show(setcolor) for y=0,#grid/M - 1,2 do for x=0,M - 1 do local top,bot = grid[y*M + x], grid[(y+1)*M + x] setcolor(top, false) setcolor(bot, true) io.write(string.format('▀')) end print('\27[m') end end print "-- using 256-color escapes" show(function(clr,bottom) local tpl if clr then if bottom then tpl = '\27[48;5;%um' else tpl = '\27[38;5;%um' end io.write(string.format(tpl, conv(clr))) else if bottom then io.write('\27[49m') else io.write('\27[39m') end end end) print "-- using truecolor escapes" -- if false then show(function(clr,bottom) local tpl if clr then if bottom then tpl = '\27[48;2;%u;%u;%um' else tpl = '\27[38;2;%u;%u;%um' end io.write(string.format(tpl, math.floor(clr.r*0xff), math.floor(clr.g*0xff), math.floor(clr.b*0xff))) else if bottom then io.write('\27[49m') else io.write('\27[39m') end end end) -- end -- for R=0,S do for G=0,S do for B=0,S do -- local r,g,b = R/S, G/S, B/S -- io.write(string.format('\27[48;2;%u;%u;%um ', math.ceil(r*0xff), math.ceil(g*0xff), math.ceil(b*0xff))) -- end print('\27[m') end end -- -- print() |