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 sin:: $intrinsic(sin),31 cos:: $intrinsic(cos),32 tan:: $intrinsic(tan),33 asin:: $intrinsic(asin),34 acos:: $intrinsic(acos),35 atan:: $intrinsic(atan),3637 isString(v):: std.type(v) == 'string',38 isNumber(v):: std.type(v) == 'number',39 isBoolean(v):: std.type(v) == 'boolean',40 isObject(v):: std.type(v) == 'object',41 isArray(v):: std.type(v) == 'array',42 isFunction(v):: std.type(v) == 'function',4344 toString(a)::45 if std.type(a) == 'string' then a else '' + a,4647 substr(str, from, len)::48 assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);49 assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);50 assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);51 assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;52 std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),5354 startsWith(a, b)::55 if std.length(a) < std.length(b) then56 false57 else58 std.substr(a, 0, std.length(b)) == b,5960 endsWith(a, b)::61 if std.length(a) < std.length(b) then62 false63 else64 std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6566 lstripChars(str, chars)::67 if std.length(str) > 0 && std.member(chars, str[0]) then68 std.lstripChars(str[1:], chars)69 else70 str,7172 rstripChars(str, chars)::73 local len = std.length(str);74 if len > 0 && std.member(chars, str[len - 1]) then75 std.rstripChars(str[:len - 1], chars)76 else77 str,7879 stripChars(str, chars)::80 std.lstripChars(std.rstripChars(str, chars), chars),8182 stringChars(str)::83 std.makeArray(std.length(str), function(i) str[i]),8485 local parse_nat(str, base) =86 assert base > 0 && base <= 16 : 'integer base %d invalid' % base;87 88 local zero_code = std.codepoint('0');89 local upper_a_code = std.codepoint('A');90 local lower_a_code = std.codepoint('a');91 local addDigit(aggregate, char) =92 local code = std.codepoint(char);93 local digit = if code >= lower_a_code then94 code - lower_a_code + 1095 else if code >= upper_a_code then96 code - upper_a_code + 1097 else98 code - zero_code;99 assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];100 base * aggregate + digit;101 std.foldl(addDigit, std.stringChars(str), 0),102103 parseInt(str)::104 assert std.isString(str) : 'Expected string, got ' + std.type(str);105 assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];106 if str[0] == '-' then107 -parse_nat(str[1:], 10)108 else109 parse_nat(str, 10),110111 parseOctal(str)::112 assert std.isString(str) : 'Expected string, got ' + std.type(str);113 assert std.length(str) > 0 : 'Not an octal number: ""';114 parse_nat(str, 8),115116 parseHex(str)::117 assert std.isString(str) : 'Expected string, got ' + std.type(str);118 assert std.length(str) > 0 : 'Not hexadecimal: ""';119 parse_nat(str, 16),120121 split(str, c)::122 assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);123 assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);124 assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);125 std.splitLimit(str, c, -1),126127 splitLimit(str, c, maxsplits)::128 assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);129 assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);130 assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);131 assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);132 local aux(str, delim, i, arr, v) =133 local c = str[i];134 local i2 = i + 1;135 if i >= std.length(str) then136 arr + [v]137 else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then138 aux(str, delim, i2, arr + [v], '') tailstrict139 else140 aux(str, delim, i2, arr, v + c) tailstrict;141 aux(str, c, 0, [], ''),142143 strReplace:: $intrinsic(strReplace),144145 asciiUpper(str)::146 local cp = std.codepoint;147 local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then148 std.char(cp(c) - 32)149 else150 c;151 std.join('', std.map(up_letter, std.stringChars(str))),152153 asciiLower(str)::154 local cp = std.codepoint;155 local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then156 std.char(cp(c) + 32)157 else158 c;159 std.join('', std.map(down_letter, std.stringChars(str))),160161 range:: $intrinsic(range),162163 repeat(what, count)::164 local joiner =165 if std.isString(what) then ''166 else if std.isArray(what) then []167 else error 'std.repeat first argument must be an array or a string';168 std.join(joiner, std.makeArray(count, function(i) what)),169170 slice:: $intrinsic(slice),171172 member(arr, x)::173 if std.isArray(arr) then174 std.count(arr, x) > 0175 else if std.isString(arr) then176 std.length(std.findSubstr(x, arr)) > 0177 else error 'std.member first argument must be an array or a string',178179 count(arr, x):: std.length(std.filter(function(v) v == x, arr)),180181 mod:: $intrinsic(mod),182183 map:: $intrinsic(map),184185 mapWithIndex(func, arr)::186 if !std.isFunction(func) then187 error ('std.mapWithIndex first param must be function, got ' + std.type(func))188 else if !std.isArray(arr) && !std.isString(arr) then189 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))190 else191 std.makeArray(std.length(arr), function(i) func(i, arr[i])),192193 mapWithKey(func, obj)::194 if !std.isFunction(func) then195 error ('std.mapWithKey first param must be function, got ' + std.type(func))196 else if !std.isObject(obj) then197 error ('std.mapWithKey second param must be object, got ' + std.type(obj))198 else199 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },200201 flatMap:: $intrinsic(flatMap),202203 join:: $intrinsic(join),204205 lines(arr)::206 std.join('\n', arr + ['']),207208 deepJoin(arr)::209 if std.isString(arr) then210 arr211 else if std.isArray(arr) then212 std.join('', [std.deepJoin(x) for x in arr])213 else214 error 'Expected string or array, got %s' % std.type(arr),215216217 format:: $intrinsic(format),218219 foldr:: $intrinsic(foldr),220221 foldl:: $intrinsic(foldl),222223 filterMap(filter_func, map_func, arr)::224 if !std.isFunction(filter_func) then225 error ('std.filterMap first param must be function, got ' + std.type(filter_func))226 else if !std.isFunction(map_func) then227 error ('std.filterMap second param must be function, got ' + std.type(map_func))228 else if !std.isArray(arr) then229 error ('std.filterMap third param must be array, got ' + std.type(arr))230 else231 std.map(map_func, std.filter(filter_func, arr)),232233 assertEqual(a, b)::234 if a == b then235 true236 else237 error 'Assertion failed. ' + a + ' != ' + b,238239 abs(n)::240 if !std.isNumber(n) then241 error 'std.abs expected number, got ' + std.type(n)242 else243 if n > 0 then n else -n,244245 sign(n)::246 if !std.isNumber(n) then247 error 'std.sign expected number, got ' + std.type(n)248 else249 if n > 0 then250 1251 else if n < 0 then252 -1253 else 0,254255 max(a, b)::256 if !std.isNumber(a) then257 error 'std.max first param expected number, got ' + std.type(a)258 else if !std.isNumber(b) then259 error 'std.max second param expected number, got ' + std.type(b)260 else261 if a > b then a else b,262263 min(a, b)::264 if !std.isNumber(a) then265 error 'std.min first param expected number, got ' + std.type(a)266 else if !std.isNumber(b) then267 error 'std.min second param expected number, got ' + std.type(b)268 else269 if a < b then a else b,270271 clamp(x, minVal, maxVal)::272 if x < minVal then minVal273 else if x > maxVal then maxVal274 else x,275276 flattenArrays(arrs)::277 std.foldl(function(a, b) a + b, arrs, []),278279 manifestIni(ini)::280 local body_lines(body) =281 std.join([], [282 local value_or_values = body[k];283 if std.isArray(value_or_values) then284 ['%s = %s' % [k, value] for value in value_or_values]285 else286 ['%s = %s' % [k, value_or_values]]287288 for k in std.objectFields(body)289 ]);290291 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),292 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],293 all_sections = [294 section_lines(k, ini.sections[k])295 for k in std.objectFields(ini.sections)296 ];297 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),298299 manifestToml(value):: std.manifestTomlEx(value, ' '),300301 manifestTomlEx(value, indent)::302 local303 escapeStringToml = std.escapeStringJson,304 escapeKeyToml(key) =305 local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));306 if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),307 isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),308 isSection(v) = std.isObject(v) || isTableArray(v),309 renderValue(v, indexedPath, inline, cindent) =310 if v == true then311 'true'312 else if v == false then313 'false'314 else if v == null then315 error 'Tried to manifest "null" at ' + indexedPath316 else if std.isNumber(v) then317 '' + v318 else if std.isString(v) then319 escapeStringToml(v)320 else if std.isFunction(v) then321 error 'Tried to manifest function at ' + indexedPath322 else if std.isArray(v) then323 if std.length(v) == 0 then324 '[]'325 else326 local range = std.range(0, std.length(v) - 1);327 local new_indent = if inline then '' else cindent + indent;328 local separator = if inline then ' ' else '\n';329 local lines = ['[' + separator]330 + std.join([',' + separator],331 [332 [new_indent + renderValue(v[i], indexedPath + [i], true, '')]333 for i in range334 ])335 + [separator + (if inline then '' else cindent) + ']'];336 std.join('', lines)337 else if std.isObject(v) then338 local lines = ['{ ']339 + std.join([', '],340 [341 [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]342 for k in std.objectFields(v)343 ])344 + [' }'];345 std.join('', lines),346 renderTableInternal(v, path, indexedPath, cindent) =347 local kvp = std.flattenArrays([348 [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]349 for k in std.objectFields(v)350 if !isSection(v[k])351 ]);352 local sections = [std.join('\n', kvp)] + [353 (354 if std.isObject(v[k]) then355 renderTable(v[k], path + [k], indexedPath + [k], cindent)356 else357 renderTableArray(v[k], path + [k], indexedPath + [k], cindent)358 )359 for k in std.objectFields(v)360 if isSection(v[k])361 ];362 std.join('\n\n', sections),363 renderTable(v, path, indexedPath, cindent) =364 cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'365 + (if v == {} then '' else '\n')366 + renderTableInternal(v, path, indexedPath, cindent + indent),367 renderTableArray(v, path, indexedPath, cindent) =368 local range = std.range(0, std.length(v) - 1);369 local sections = [370 (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'371 + (if v[i] == {} then '' else '\n')372 + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))373 for i in range374 ];375 std.join('\n\n', sections);376 if std.isObject(value) then377 renderTableInternal(value, [], [], '')378 else379 error 'TOML body must be an object. Got ' + std.type(value),380381 escapeStringJson:: $intrinsic(escapeStringJson),382383 escapeStringPython(str)::384 std.escapeStringJson(str),385386 escapeStringBash(str_)::387 local str = std.toString(str_);388 local trans(ch) =389 if ch == "'" then390 "'\"'\"'"391 else392 ch;393 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),394395 escapeStringDollars(str_)::396 local str = std.toString(str_);397 local trans(ch) =398 if ch == '$' then399 '$$'400 else401 ch;402 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),403404 manifestJson(value):: std.manifestJsonEx(value, ' '),405406 manifestJsonEx:: $intrinsic(manifestJsonEx),407408 manifestYamlDoc(value, indent_array_in_object=false)::409 local aux(v, path, cindent) =410 if v == true then411 'true'412 else if v == false then413 'false'414 else if v == null then415 'null'416 else if std.isNumber(v) then417 '' + v418 else if std.isString(v) then419 local len = std.length(v);420 if len == 0 then421 '""'422 else if v[len - 1] == '\n' then423 local split = std.split(v, '\n');424 std.join('\n' + cindent + ' ', ['|'] + split[0:std.length(split) - 1])425 else426 std.escapeStringJson(v)427 else if std.isFunction(v) then428 error 'Tried to manifest function at ' + path429 else if std.isArray(v) then430 if std.length(v) == 0 then431 '[]'432 else433 local params(value) =434 if std.isArray(value) && std.length(value) > 0 then {435 436 437 438 439 440 441 new_indent: cindent + ' ',442 space: '\n' + self.new_indent,443 } else if std.isObject(value) && std.length(value) > 0 then {444 new_indent: cindent + ' ',445 446 447 448 space: ' ',449 } else {450 451 new_indent: cindent,452 space: ' ',453 };454 local range = std.range(0, std.length(v) - 1);455 local parts = [456 '-' + param.space + aux(v[i], path + [i], param.new_indent)457 for i in range458 for param in [params(v[i])]459 ];460 std.join('\n' + cindent, parts)461 else if std.isObject(v) then462 if std.length(v) == 0 then463 '{}'464 else465 local params(value) =466 if std.isArray(value) && std.length(value) > 0 then {467 468 469 470 471 472 473 new_indent: if indent_array_in_object then cindent + ' ' else cindent,474 space: '\n' + self.new_indent,475 } else if std.isObject(value) && std.length(value) > 0 then {476 new_indent: cindent + ' ',477 space: '\n' + self.new_indent,478 } else {479 480 new_indent: cindent,481 space: ' ',482 };483 local lines = [484 std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)485 for k in std.objectFields(v)486 for param in [params(v[k])]487 ];488 std.join('\n' + cindent, lines);489 aux(value, [], ''),490491 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::492 if !std.isArray(value) then493 error 'manifestYamlStream only takes arrays, got ' + std.type(value)494 else495 '---\n' + std.join(496 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]497 ) + if c_document_end then '\n...\n' else '\n',498499500 manifestPython(v)::501 if std.isObject(v) then502 local fields = [503 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]504 for k in std.objectFields(v)505 ];506 '{%s}' % [std.join(', ', fields)]507 else if std.isArray(v) then508 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]509 else if std.isString(v) then510 '%s' % [std.escapeStringPython(v)]511 else if std.isFunction(v) then512 error 'cannot manifest function'513 else if std.isNumber(v) then514 std.toString(v)515 else if v == true then516 'True'517 else if v == false then518 'False'519 else if v == null then520 'None',521522 manifestPythonVars(conf)::523 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];524 std.join('\n', vars + ['']),525526 manifestXmlJsonml(value)::527 if !std.isArray(value) then528 error 'Expected a JSONML value (an array), got %s' % std.type(value)529 else530 local aux(v) =531 if std.isString(v) then532 v533 else534 local tag = v[0];535 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);536 local attrs = if has_attrs then v[1] else {};537 local children = if has_attrs then v[2:] else v[1:];538 local attrs_str =539 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);540 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);541542 aux(value),543544 local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',545 local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },546547 base64:: $intrinsic(base64),548549 base64DecodeBytes(str)::550 if std.length(str) % 4 != 0 then551 error 'Not a base64 encoded string "%s"' % str552 else553 local aux(str, i, r) =554 if i >= std.length(str) then555 r556 else557 558 local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];559 560 local n2 =561 if str[i + 2] == '=' then []562 else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];563 564 local n3 =565 if str[i + 3] == '=' then []566 else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];567 aux(str, i + 4, r + n1 + n2 + n3) tailstrict;568 aux(str, 0, []),569570 base64Decode(str)::571 local bytes = std.base64DecodeBytes(str);572 std.join('', std.map(function(b) std.char(b), bytes)),573574 reverse:: $intrinsic(reverse),575576 sortImpl:: $intrinsic(sortImpl),577578 sort(arr, keyF=id)::579 std.sortImpl(arr, keyF),580581 uniq(arr, keyF=id)::582 local f(a, b) =583 if std.length(a) == 0 then584 [b]585 else if keyF(a[std.length(a) - 1]) == keyF(b) then586 a587 else588 a + [b];589 std.foldl(f, arr, []),590591 set(arr, keyF=id)::592 std.uniq(std.sort(arr, keyF), keyF),593594 setMember(x, arr, keyF=id)::595 596 std.length(std.setInter([x], arr, keyF)) > 0,597598 setUnion(a, b, keyF=id)::599 600 local aux(a, b, i, j, acc) =601 if i >= std.length(a) then602 acc + b[j:]603 else if j >= std.length(b) then604 acc + a[i:]605 else606 local ak = keyF(a[i]);607 local bk = keyF(b[j]);608 if ak == bk then609 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict610 else if ak < bk then611 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict612 else613 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;614 aux(a, b, 0, 0, []),615616 setInter(a, b, keyF=id)::617 local aux(a, b, i, j, acc) =618 if i >= std.length(a) || j >= std.length(b) then619 acc620 else621 if keyF(a[i]) == keyF(b[j]) then622 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict623 else if keyF(a[i]) < keyF(b[j]) then624 aux(a, b, i + 1, j, acc) tailstrict625 else626 aux(a, b, i, j + 1, acc) tailstrict;627 aux(a, b, 0, 0, []) tailstrict,628629 setDiff(a, b, keyF=id)::630 local aux(a, b, i, j, acc) =631 if i >= std.length(a) then632 acc633 else if j >= std.length(b) then634 acc + a[i:]635 else636 if keyF(a[i]) == keyF(b[j]) then637 aux(a, b, i + 1, j + 1, acc) tailstrict638 else if keyF(a[i]) < keyF(b[j]) then639 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict640 else641 aux(a, b, i, j + 1, acc) tailstrict;642 aux(a, b, 0, 0, []) tailstrict,643644 mergePatch(target, patch)::645 if std.isObject(patch) then646 local target_object =647 if std.isObject(target) then target else {};648649 local target_fields =650 if std.isObject(target_object) then std.objectFields(target_object) else [];651652 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];653 local both_fields = std.setUnion(target_fields, std.objectFields(patch));654655 {656 [k]:657 if !std.objectHas(patch, k) then658 target_object[k]659 else if !std.objectHas(target_object, k) then660 std.mergePatch(null, patch[k]) tailstrict661 else662 std.mergePatch(target_object[k], patch[k]) tailstrict663 for k in std.setDiff(both_fields, null_fields)664 }665 else666 patch,667668 objectFields(o)::669 std.objectFieldsEx(o, false),670671 objectFieldsAll(o)::672 std.objectFieldsEx(o, true),673674 objectHas(o, f)::675 std.objectHasEx(o, f, false),676677 objectHasAll(o, f)::678 std.objectHasEx(o, f, true),679680 objectValues(o)::681 [o[k] for k in std.objectFields(o)],682683 objectValuesAll(o)::684 [o[k] for k in std.objectFieldsAll(o)],685686 equals:: $intrinsic(equals),687688 resolvePath(f, r)::689 local arr = std.split(f, '/');690 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),691692 prune(a)::693 local isContent(b) =694 if b == null then695 false696 else if std.isArray(b) then697 std.length(b) > 0698 else if std.isObject(b) then699 std.length(b) > 0700 else701 true;702 if std.isArray(a) then703 [std.prune(x) for x in a if isContent($.prune(x))]704 else if std.isObject(a) then {705 [x]: $.prune(a[x])706 for x in std.objectFields(a)707 if isContent(std.prune(a[x]))708 } else709 a,710711 findSubstr(pat, str)::712 if !std.isString(pat) then713 error 'findSubstr first parameter should be a string, got ' + std.type(pat)714 else if !std.isString(str) then715 error 'findSubstr second parameter should be a string, got ' + std.type(str)716 else717 local pat_len = std.length(pat);718 local str_len = std.length(str);719 if pat_len == 0 || str_len == 0 || pat_len > str_len then720 []721 else722 std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),723724 find(value, arr)::725 if !std.isArray(arr) then726 error 'find second parameter should be an array, got ' + std.type(arr)727 else728 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),729}