1{2 local std = self,3 local id = std.id,45 6 length:: $intrinsic(length),7 type:: $intrinsic(type),8 makeArray:: $intrinsic(makeArray),9 codepoint:: $intrinsic(codepoint),10 objectFieldsEx:: $intrinsic(objectFieldsEx),11 objectHasEx:: $intrinsic(objectHasEx),12 primitiveEquals:: $intrinsic(primitiveEquals),13 modulo:: $intrinsic(modulo),14 floor:: $intrinsic(floor),15 ceil:: $intrinsic(ceil),16 extVar:: $intrinsic(extVar),17 native:: $intrinsic(native),18 filter:: $intrinsic(filter),19 char:: $intrinsic(char),20 encodeUTF8:: $intrinsic(encodeUTF8),21 md5:: $intrinsic(md5),22 trace:: $intrinsic(trace),23 id:: $intrinsic(id),24 parseJson:: $intrinsic(parseJson),2526 log:: $intrinsic(log),27 pow:: $intrinsic(pow),28 sqrt:: $intrinsic(sqrt),2930 isString(v):: std.type(v) == 'string',31 isNumber(v):: std.type(v) == 'number',32 isBoolean(v):: std.type(v) == 'boolean',33 isObject(v):: std.type(v) == 'object',34 isArray(v):: std.type(v) == 'array',35 isFunction(v):: std.type(v) == 'function',3637 toString(a)::38 if std.type(a) == 'string' then a else '' + a,3940 substr(str, from, len)::41 assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);42 assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);43 assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);44 assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;45 std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),4647 startsWith(a, b)::48 if std.length(a) < std.length(b) then49 false50 else51 std.substr(a, 0, std.length(b)) == b,5253 endsWith(a, b)::54 if std.length(a) < std.length(b) then55 false56 else57 std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,5859 lstripChars(str, chars)::60 if std.length(str) > 0 && std.member(chars, str[0]) then61 std.lstripChars(str[1:], chars)62 else63 str,6465 rstripChars(str, chars)::66 local len = std.length(str);67 if len > 0 && std.member(chars, str[len - 1]) then68 std.rstripChars(str[:len - 1], chars)69 else70 str,7172 stripChars(str, chars)::73 std.lstripChars(std.rstripChars(str, chars), chars),7475 stringChars(str)::76 std.makeArray(std.length(str), function(i) str[i]),7778 local parse_nat(str, base) =79 assert base > 0 && base <= 16 : 'integer base %d invalid' % base;80 81 local zero_code = std.codepoint('0');82 local upper_a_code = std.codepoint('A');83 local lower_a_code = std.codepoint('a');84 local addDigit(aggregate, char) =85 local code = std.codepoint(char);86 local digit = if code >= lower_a_code then87 code - lower_a_code + 1088 else if code >= upper_a_code then89 code - upper_a_code + 1090 else91 code - zero_code;92 assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];93 base * aggregate + digit;94 std.foldl(addDigit, std.stringChars(str), 0),9596 parseInt(str)::97 assert std.isString(str) : 'Expected string, got ' + std.type(str);98 assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];99 if str[0] == '-' then100 -parse_nat(str[1:], 10)101 else102 parse_nat(str, 10),103104 parseOctal(str)::105 assert std.isString(str) : 'Expected string, got ' + std.type(str);106 assert std.length(str) > 0 : 'Not an octal number: ""';107 parse_nat(str, 8),108109 parseHex(str)::110 assert std.isString(str) : 'Expected string, got ' + std.type(str);111 assert std.length(str) > 0 : 'Not hexadecimal: ""';112 parse_nat(str, 16),113114 split(str, c)::115 assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);116 assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);117 assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);118 std.splitLimit(str, c, -1),119120 splitLimit(str, c, maxsplits)::121 assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);122 assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);123 assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);124 assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);125 local aux(str, delim, i, arr, v) =126 local c = str[i];127 local i2 = i + 1;128 if i >= std.length(str) then129 arr + [v]130 else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then131 aux(str, delim, i2, arr + [v], '') tailstrict132 else133 aux(str, delim, i2, arr, v + c) tailstrict;134 aux(str, c, 0, [], ''),135136 strReplace:: $intrinsic(strReplace),137138 asciiUpper(str)::139 local cp = std.codepoint;140 local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then141 std.char(cp(c) - 32)142 else143 c;144 std.join('', std.map(up_letter, std.stringChars(str))),145146 asciiLower(str)::147 local cp = std.codepoint;148 local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then149 std.char(cp(c) + 32)150 else151 c;152 std.join('', std.map(down_letter, std.stringChars(str))),153154 range:: $intrinsic(range),155156 repeat(what, count)::157 local joiner =158 if std.isString(what) then ''159 else if std.isArray(what) then []160 else error 'std.repeat first argument must be an array or a string';161 std.join(joiner, std.makeArray(count, function(i) what)),162163 slice:: $intrinsic(slice),164165 member(arr, x)::166 if std.isArray(arr) then167 std.count(arr, x) > 0168 else if std.isString(arr) then169 std.length(std.findSubstr(x, arr)) > 0170 else error 'std.member first argument must be an array or a string',171172 count(arr, x):: std.length(std.filter(function(v) v == x, arr)),173174 mod:: $intrinsic(mod),175176 map:: $intrinsic(map),177178 mapWithIndex(func, arr)::179 if !std.isFunction(func) then180 error ('std.mapWithIndex first param must be function, got ' + std.type(func))181 else if !std.isArray(arr) && !std.isString(arr) then182 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))183 else184 std.makeArray(std.length(arr), function(i) func(i, arr[i])),185186 mapWithKey(func, obj)::187 if !std.isFunction(func) then188 error ('std.mapWithKey first param must be function, got ' + std.type(func))189 else if !std.isObject(obj) then190 error ('std.mapWithKey second param must be object, got ' + std.type(obj))191 else192 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },193194 flatMap:: $intrinsic(flatMap),195196 join:: $intrinsic(join),197198 lines(arr)::199 std.join('\n', arr + ['']),200201 deepJoin(arr)::202 if std.isString(arr) then203 arr204 else if std.isArray(arr) then205 std.join('', [std.deepJoin(x) for x in arr])206 else207 error 'Expected string or array, got %s' % std.type(arr),208209210 format:: $intrinsic(format),211212 foldr:: $intrinsic(foldr),213214 foldl:: $intrinsic(foldl),215216 filterMap(filter_func, map_func, arr)::217 if !std.isFunction(filter_func) then218 error ('std.filterMap first param must be function, got ' + std.type(filter_func))219 else if !std.isFunction(map_func) then220 error ('std.filterMap second param must be function, got ' + std.type(map_func))221 else if !std.isArray(arr) then222 error ('std.filterMap third param must be array, got ' + std.type(arr))223 else224 std.map(map_func, std.filter(filter_func, arr)),225226 assertEqual(a, b)::227 if a == b then228 true229 else230 error 'Assertion failed. ' + a + ' != ' + b,231232 abs(n)::233 if !std.isNumber(n) then234 error 'std.abs expected number, got ' + std.type(n)235 else236 if n > 0 then n else -n,237238 sign(n)::239 if !std.isNumber(n) then240 error 'std.sign expected number, got ' + std.type(n)241 else242 if n > 0 then243 1244 else if n < 0 then245 -1246 else 0,247248 max(a, b)::249 if !std.isNumber(a) then250 error 'std.max first param expected number, got ' + std.type(a)251 else if !std.isNumber(b) then252 error 'std.max second param expected number, got ' + std.type(b)253 else254 if a > b then a else b,255256 min(a, b)::257 if !std.isNumber(a) then258 error 'std.min first param expected number, got ' + std.type(a)259 else if !std.isNumber(b) then260 error 'std.min second param expected number, got ' + std.type(b)261 else262 if a < b then a else b,263264 clamp(x, minVal, maxVal)::265 if x < minVal then minVal266 else if x > maxVal then maxVal267 else x,268269 flattenArrays(arrs)::270 std.foldl(function(a, b) a + b, arrs, []),271272 manifestIni(ini)::273 local body_lines(body) =274 std.join([], [275 local value_or_values = body[k];276 if std.isArray(value_or_values) then277 ['%s = %s' % [k, value] for value in value_or_values]278 else279 ['%s = %s' % [k, value_or_values]]280281 for k in std.objectFields(body)282 ]);283284 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),285 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],286 all_sections = [287 section_lines(k, ini.sections[k])288 for k in std.objectFields(ini.sections)289 ];290 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),291292 manifestToml(value):: std.manifestTomlEx(value, ' '),293294 manifestTomlEx(value, indent)::295 local296 escapeStringToml = std.escapeStringJson,297 escapeKeyToml(key) =298 local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));299 if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),300 isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),301 isSection(v) = std.isObject(v) || isTableArray(v),302 renderValue(v, indexedPath, inline, cindent) =303 if v == true then304 'true'305 else if v == false then306 'false'307 else if v == null then308 error 'Tried to manifest "null" at ' + indexedPath309 else if std.isNumber(v) then310 '' + v311 else if std.isString(v) then312 escapeStringToml(v)313 else if std.isFunction(v) then314 error 'Tried to manifest function at ' + indexedPath315 else if std.isArray(v) then316 if std.length(v) == 0 then317 '[]'318 else319 local range = std.range(0, std.length(v) - 1);320 local new_indent = if inline then '' else cindent + indent;321 local separator = if inline then ' ' else '\n';322 local lines = ['[' + separator]323 + std.join([',' + separator],324 [325 [new_indent + renderValue(v[i], indexedPath + [i], true, '')]326 for i in range327 ])328 + [separator + (if inline then '' else cindent) + ']'];329 std.join('', lines)330 else if std.isObject(v) then331 local lines = ['{ ']332 + std.join([', '],333 [334 [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]335 for k in std.objectFields(v)336 ])337 + [' }'];338 std.join('', lines),339 renderTableInternal(v, path, indexedPath, cindent) =340 local kvp = std.flattenArrays([341 [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]342 for k in std.objectFields(v)343 if !isSection(v[k])344 ]);345 local sections = [std.join('\n', kvp)] + [346 (347 if std.isObject(v[k]) then348 renderTable(v[k], path + [k], indexedPath + [k], cindent)349 else350 renderTableArray(v[k], path + [k], indexedPath + [k], cindent)351 )352 for k in std.objectFields(v)353 if isSection(v[k])354 ];355 std.join('\n\n', sections),356 renderTable(v, path, indexedPath, cindent) =357 cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'358 + (if v == {} then '' else '\n')359 + renderTableInternal(v, path, indexedPath, cindent + indent),360 renderTableArray(v, path, indexedPath, cindent) =361 local range = std.range(0, std.length(v) - 1);362 local sections = [363 (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'364 + (if v[i] == {} then '' else '\n')365 + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))366 for i in range367 ];368 std.join('\n\n', sections);369 if std.isObject(value) then370 renderTableInternal(value, [], [], '')371 else372 error 'TOML body must be an object. Got ' + std.type(value),373374 escapeStringJson:: $intrinsic(escapeStringJson),375376 escapeStringPython(str)::377 std.escapeStringJson(str),378379 escapeStringBash(str_)::380 local str = std.toString(str_);381 local trans(ch) =382 if ch == "'" then383 "'\"'\"'"384 else385 ch;386 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),387388 escapeStringDollars(str_)::389 local str = std.toString(str_);390 local trans(ch) =391 if ch == '$' then392 '$$'393 else394 ch;395 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),396397 manifestJson(value):: std.manifestJsonEx(value, ' '),398399 manifestJsonEx:: $intrinsic(manifestJsonEx),400401 manifestYamlDoc(value, indent_array_in_object=false)::402 local aux(v, path, cindent) =403 if v == true then404 'true'405 else if v == false then406 'false'407 else if v == null then408 'null'409 else if std.isNumber(v) then410 '' + v411 else if std.isString(v) then412 local len = std.length(v);413 if len == 0 then414 '""'415 else if v[len - 1] == '\n' then416 local split = std.split(v, '\n');417 std.join('\n' + cindent + ' ', ['|'] + split[0:std.length(split) - 1])418 else419 std.escapeStringJson(v)420 else if std.isFunction(v) then421 error 'Tried to manifest function at ' + path422 else if std.isArray(v) then423 if std.length(v) == 0 then424 '[]'425 else426 local params(value) =427 if std.isArray(value) && std.length(value) > 0 then {428 429 430 431 432 433 434 new_indent: cindent + ' ',435 space: '\n' + self.new_indent,436 } else if std.isObject(value) && std.length(value) > 0 then {437 new_indent: cindent + ' ',438 439 440 441 space: ' ',442 } else {443 444 new_indent: cindent,445 space: ' ',446 };447 local range = std.range(0, std.length(v) - 1);448 local parts = [449 '-' + param.space + aux(v[i], path + [i], param.new_indent)450 for i in range451 for param in [params(v[i])]452 ];453 std.join('\n' + cindent, parts)454 else if std.isObject(v) then455 if std.length(v) == 0 then456 '{}'457 else458 local params(value) =459 if std.isArray(value) && std.length(value) > 0 then {460 461 462 463 464 465 466 new_indent: if indent_array_in_object then cindent + ' ' else cindent,467 space: '\n' + self.new_indent,468 } else if std.isObject(value) && std.length(value) > 0 then {469 new_indent: cindent + ' ',470 space: '\n' + self.new_indent,471 } else {472 473 new_indent: cindent,474 space: ' ',475 };476 local lines = [477 std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)478 for k in std.objectFields(v)479 for param in [params(v[k])]480 ];481 std.join('\n' + cindent, lines);482 aux(value, [], ''),483484 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::485 if !std.isArray(value) then486 error 'manifestYamlStream only takes arrays, got ' + std.type(value)487 else488 '---\n' + std.join(489 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]490 ) + if c_document_end then '\n...\n' else '\n',491492493 manifestPython(v)::494 if std.isObject(v) then495 local fields = [496 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]497 for k in std.objectFields(v)498 ];499 '{%s}' % [std.join(', ', fields)]500 else if std.isArray(v) then501 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]502 else if std.isString(v) then503 '%s' % [std.escapeStringPython(v)]504 else if std.isFunction(v) then505 error 'cannot manifest function'506 else if std.isNumber(v) then507 std.toString(v)508 else if v == true then509 'True'510 else if v == false then511 'False'512 else if v == null then513 'None',514515 manifestPythonVars(conf)::516 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];517 std.join('\n', vars + ['']),518519 manifestXmlJsonml(value)::520 if !std.isArray(value) then521 error 'Expected a JSONML value (an array), got %s' % std.type(value)522 else523 local aux(v) =524 if std.isString(v) then525 v526 else527 local tag = v[0];528 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);529 local attrs = if has_attrs then v[1] else {};530 local children = if has_attrs then v[2:] else v[1:];531 local attrs_str =532 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);533 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);534535 aux(value),536537 local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',538 local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },539540 base64:: $intrinsic(base64),541542 base64DecodeBytes(str)::543 if std.length(str) % 4 != 0 then544 error 'Not a base64 encoded string "%s"' % str545 else546 local aux(str, i, r) =547 if i >= std.length(str) then548 r549 else550 551 local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];552 553 local n2 =554 if str[i + 2] == '=' then []555 else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];556 557 local n3 =558 if str[i + 3] == '=' then []559 else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];560 aux(str, i + 4, r + n1 + n2 + n3) tailstrict;561 aux(str, 0, []),562563 base64Decode(str)::564 local bytes = std.base64DecodeBytes(str);565 std.join('', std.map(function(b) std.char(b), bytes)),566567 reverse:: $intrinsic(reverse),568569 sortImpl:: $intrinsic(sortImpl),570571 sort(arr, keyF=id)::572 std.sortImpl(arr, keyF),573574 uniq(arr, keyF=id)::575 local f(a, b) =576 if std.length(a) == 0 then577 [b]578 else if keyF(a[std.length(a) - 1]) == keyF(b) then579 a580 else581 a + [b];582 std.foldl(f, arr, []),583584 set(arr, keyF=id)::585 std.uniq(std.sort(arr, keyF), keyF),586587 setMember(x, arr, keyF=id)::588 589 std.length(std.setInter([x], arr, keyF)) > 0,590591 setUnion(a, b, keyF=id)::592 593 local aux(a, b, i, j, acc) =594 if i >= std.length(a) then595 acc + b[j:]596 else if j >= std.length(b) then597 acc + a[i:]598 else599 local ak = keyF(a[i]);600 local bk = keyF(b[j]);601 if ak == bk then602 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict603 else if ak < bk then604 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict605 else606 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;607 aux(a, b, 0, 0, []),608609 setInter(a, b, keyF=id)::610 local aux(a, b, i, j, acc) =611 if i >= std.length(a) || j >= std.length(b) then612 acc613 else614 if keyF(a[i]) == keyF(b[j]) then615 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict616 else if keyF(a[i]) < keyF(b[j]) then617 aux(a, b, i + 1, j, acc) tailstrict618 else619 aux(a, b, i, j + 1, acc) tailstrict;620 aux(a, b, 0, 0, []) tailstrict,621622 setDiff(a, b, keyF=id)::623 local aux(a, b, i, j, acc) =624 if i >= std.length(a) then625 acc626 else if j >= std.length(b) then627 acc + a[i:]628 else629 if keyF(a[i]) == keyF(b[j]) then630 aux(a, b, i + 1, j + 1, acc) tailstrict631 else if keyF(a[i]) < keyF(b[j]) then632 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict633 else634 aux(a, b, i, j + 1, acc) tailstrict;635 aux(a, b, 0, 0, []) tailstrict,636637 mergePatch(target, patch)::638 if std.isObject(patch) then639 local target_object =640 if std.isObject(target) then target else {};641642 local target_fields =643 if std.isObject(target_object) then std.objectFields(target_object) else [];644645 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];646 local both_fields = std.setUnion(target_fields, std.objectFields(patch));647648 {649 [k]:650 if !std.objectHas(patch, k) then651 target_object[k]652 else if !std.objectHas(target_object, k) then653 std.mergePatch(null, patch[k]) tailstrict654 else655 std.mergePatch(target_object[k], patch[k]) tailstrict656 for k in std.setDiff(both_fields, null_fields)657 }658 else659 patch,660661 objectFields(o)::662 std.objectFieldsEx(o, false),663664 objectFieldsAll(o)::665 std.objectFieldsEx(o, true),666667 objectHas(o, f)::668 std.objectHasEx(o, f, false),669670 objectHasAll(o, f)::671 std.objectHasEx(o, f, true),672673 objectValues(o)::674 [o[k] for k in std.objectFields(o)],675676 objectValuesAll(o)::677 [o[k] for k in std.objectFieldsAll(o)],678679 equals:: $intrinsic(equals),680681 resolvePath(f, r)::682 local arr = std.split(f, '/');683 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),684685 prune(a)::686 local isContent(b) =687 if b == null then688 false689 else if std.isArray(b) then690 std.length(b) > 0691 else if std.isObject(b) then692 std.length(b) > 0693 else694 true;695 if std.isArray(a) then696 [std.prune(x) for x in a if isContent($.prune(x))]697 else if std.isObject(a) then {698 [x]: $.prune(a[x])699 for x in std.objectFields(a)700 if isContent(std.prune(a[x]))701 } else702 a,703704 findSubstr(pat, str)::705 if !std.isString(pat) then706 error 'findSubstr first parameter should be a string, got ' + std.type(pat)707 else if !std.isString(str) then708 error 'findSubstr second parameter should be a string, got ' + std.type(str)709 else710 local pat_len = std.length(pat);711 local str_len = std.length(str);712 if pat_len == 0 || str_len == 0 || pat_len > str_len then713 []714 else715 std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),716717 find(value, arr)::718 if !std.isArray(arr) then719 error 'find second parameter should be an array, got ' + std.type(arr)720 else721 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),722}