--!strict --!native -- cad/step/Write: header + entity instances -> ISO 10303-21 text. Values as cad/step/Parse produces them; strings -- re-escaped (' -> '', \ -> \\, UTF-8 outside printable ASCII -> \X2\..\X0\ / \X4\..\X0\, control bytes and invalid -- UTF-8 -> \X\hh), reals printed with the fewest digits (15..17) that read back to the same double. Never throws: -- failures return nil, message. local Write = {} local sformat, sbyte, ssub, sfind, floor, huge = string.format, string.byte, string.sub, string.find, math.floor, math.huge local MAX_DEPTH = 100 -- the shortest %g form that reads back exactly, in Part 21 syntax (a '.' always, upper-case E) function Write.real(v: number): string? if v ~= v or v == huge or v == -huge then return nil end local s = sformat("%.15g", v) if tonumber(s) ~= v then s = sformat("%.16g", v) if tonumber(s) ~= v then s = sformat("%.17g", v) end end s = string.upper(s) local e = sfind(s, "E", 1, true) if not sfind(s, ".", 1, true) then if e then s = ssub(s, 1, e - 1) .. "." .. ssub(s, e) else s ..= "." end end return s end local HEX = "0123456789ABCDEF" local function hex(v: number, k: number): string local out = table.create(k) for i = k, 1, -1 do local d = v % 16 out[i] = ssub(HEX, d + 1, d + 1) v = floor(v / 16) end return table.concat(out) end -- a string's Part 21 literal (with the quotes) function Write.str(s: string): string if not sfind(s, "[^ -&(-[%]^-~]") then -- printable ASCII without ' and \ return "'" .. s .. "'" end local out = { "'" } local i, n = 1, #s local wide: number? = nil -- 4 (\X2\) or 8 (\X4\) while in an extended run local function close() if wide then table.insert(out, "\\X0\\") wide = nil end end while i <= n do local c = sbyte(s, i) :: number if c >= 32 and c < 127 then close() if c == 39 then table.insert(out, "''") elseif c == 92 then table.insert(out, "\\\\") else table.insert(out, string.char(c)) end i += 1 else -- a UTF-8 sequence -> code point local cp: number? = nil local len = 1 if c >= 0xC2 and c < 0xE0 then local c2 = sbyte(s, i + 1) if c2 and c2 >= 0x80 and c2 < 0xC0 then cp, len = (c - 0xC0) * 64 + (c2 - 0x80), 2 end elseif c >= 0xE0 and c < 0xF0 then local c2, c3 = sbyte(s, i + 1, i + 2) if c2 and c3 and c2 >= 0x80 and c2 < 0xC0 and c3 >= 0x80 and c3 < 0xC0 then local v = (c - 0xE0) * 4096 + (c2 - 0x80) * 64 + (c3 - 0x80) if v >= 0x800 and (v < 0xD800 or v >= 0xE000) then cp, len = v, 3 end end elseif c >= 0xF0 and c < 0xF5 then local c2, c3, c4 = sbyte(s, i + 1, i + 3) if c2 and c3 and c4 and c2 >= 0x80 and c2 < 0xC0 and c3 >= 0x80 and c3 < 0xC0 and c4 >= 0x80 and c4 < 0xC0 then local v = (c - 0xF0) * 262144 + (c2 - 0x80) * 4096 + (c3 - 0x80) * 64 + (c4 - 0x80) if v >= 0x10000 and v < 0x110000 then cp, len = v, 4 end end end if cp == nil then close() table.insert(out, "\\X\\" .. hex(c, 2)) i += 1 else local k = if cp >= 0x10000 then 8 else 4 if wide ~= k then close() table.insert(out, if k == 4 then "\\X2\\" else "\\X4\\") wide = k end table.insert(out, hex(cp, k)) i += len end end end close() table.insert(out, "'") return table.concat(out) end local function isName(s: any): boolean return type(s) == "string" and sfind(s, "^!?[%a_][%w_]*$") ~= nil end Write.isName = isName export type Ctx = { out: { string }, null: any, derived: any, err: string? } local value: (ctx: Ctx, v: any, real: boolean, depth: number) -> boolean local function reals(t: any): { [any]: boolean }? local mt = getmetatable(t) return if type(mt) == "table" and type(mt.__reals) == "table" then mt.__reals else nil end local function list(ctx: Ctx, t: { any }, depth: number): boolean local out = ctx.out table.insert(out, "(") local marks = reals(t) local n = #t for i = 1, n do if i > 1 then table.insert(out, ",") end if not value(ctx, t[i], marks ~= nil and marks[i] == true, depth + 1) then return false end end table.insert(out, ")") return true end Write.list = list value = function(ctx: Ctx, v: any, real: boolean, depth: number): boolean local out = ctx.out if depth > MAX_DEPTH then ctx.err = "values nested too deeply (or a cycle)" return false end local tv = type(v) if tv == "number" then if not real and v == floor(v) and v > -2 ^ 53 and v < 2 ^ 53 then table.insert(out, sformat("%.0f", v)) return true end local r = Write.real(v) if not r then ctx.err = "a number that STEP cannot hold (nan / inf)" return false end table.insert(out, r) return true elseif tv == "string" then table.insert(out, Write.str(v)) return true elseif tv == "boolean" then table.insert(out, if v then ".T." else ".F.") return true elseif tv == "table" then if v == ctx.null then table.insert(out, "$") return true elseif v == ctx.derived then table.insert(out, "*") return true end local ref = v.ref if ref ~= nil then if type(ref) ~= "number" or ref ~= floor(ref) or ref < 0 then ctx.err = "bad entity reference " .. tostring(ref) return false end table.insert(out, sformat("#%.0f", ref)) return true end local enum = v.enum if enum ~= nil then if type(enum) ~= "string" or not sfind(enum, "^[%a_][%w_]*$") then ctx.err = "bad enumeration " .. tostring(enum) return false end table.insert(out, "." .. string.upper(enum) .. ".") return true end local bin = v.binary if bin ~= nil then if type(bin) ~= "string" or not sfind(bin, "^[0-3]%x*$") then ctx.err = "bad binary literal" return false end table.insert(out, "\"" .. string.upper(bin) .. "\"") return true end local typed = v.typed if typed ~= nil then if not isName(typed) then ctx.err = "bad typed parameter name " .. tostring(typed) return false end if v.value == nil then ctx.err = "typed parameter " .. typed .. " without a value" return false end local marks = reals(v) table.insert(out, string.upper(typed) .. "(") if not value(ctx, v.value, marks ~= nil and marks.value == true, depth + 1) then return false end table.insert(out, ")") return true end return list(ctx, v, depth) end ctx.err = "cannot write a " .. tv .. " value" return false end Write.value = value -- record(ctx, type, args) -> appends TYPE(args) function Write.record(ctx: Ctx, t: any, args: any): boolean if not isName(t) then ctx.err = "bad entity type " .. tostring(t) return false end if type(args) ~= "table" then ctx.err = "entity " .. t .. " without an args list" return false end table.insert(ctx.out, string.upper(t)) return list(ctx, args, 0) end return Write