556 lines
14 KiB
Luau
556 lines
14 KiB
Luau
--!strict
|
|
--!native
|
|
-- cad/step/Parse: ISO 10303-21 text -> header + entity instances, one pass; never throws (nil, message, offset).
|
|
-- An integral real (`0.`, `1.E2`) is marked in its holder's metatable (`__reals = { [index | "value"] = true }`).
|
|
local Parse = {}
|
|
|
|
local sbyte, ssub, sfind, schar, floor = string.byte, string.sub, string.find, string.char, math.floor
|
|
|
|
local MAX_DEPTH = 100
|
|
|
|
export type Ctx = {
|
|
s: string,
|
|
n: number,
|
|
err: string?,
|
|
errAt: number?,
|
|
null: any,
|
|
derived: any,
|
|
}
|
|
|
|
local function fail(ctx: Ctx, msg: string, at: number): nil
|
|
if not ctx.err then
|
|
ctx.err, ctx.errAt = msg, at
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function skip(ctx: Ctx, i: number): number
|
|
local s, n = ctx.s, ctx.n
|
|
while i <= n do
|
|
local c = sbyte(s, i)
|
|
if c == 32 or c == 10 or c == 13 or c == 9 or c == 12 then
|
|
i += 1
|
|
elseif c == 47 and sbyte(s, i + 1) == 42 then
|
|
local _, e = sfind(s, "*/", i + 2, true)
|
|
if not e then
|
|
fail(ctx, "unterminated comment", i)
|
|
return n + 1
|
|
end
|
|
i = e + 1
|
|
else
|
|
return i
|
|
end
|
|
end
|
|
return i
|
|
end
|
|
|
|
local function isAlpha(c: number): boolean
|
|
return (c >= 65 and c <= 90) or (c >= 97 and c <= 122) or c == 95
|
|
end
|
|
local function isWord(c: number): boolean
|
|
return (c >= 65 and c <= 90) or (c >= 97 and c <= 122) or c == 95 or (c >= 48 and c <= 57)
|
|
end
|
|
|
|
local function keyword(ctx: Ctx, i: number): (string?, number)
|
|
local s, n = ctx.s, ctx.n
|
|
local j = i
|
|
if j <= n and sbyte(s, j) == 33 then -- '!' user-defined
|
|
j += 1
|
|
end
|
|
if j > n or not isAlpha(sbyte(s, j) :: number) then
|
|
return nil, i
|
|
end
|
|
j += 1
|
|
while j <= n and isWord(sbyte(s, j) :: number) do
|
|
j += 1
|
|
end
|
|
return string.upper(ssub(s, i, j - 1)), j
|
|
end
|
|
|
|
local function utf8char(cp: number): string
|
|
if cp < 0x80 then
|
|
return schar(cp)
|
|
elseif cp < 0x800 then
|
|
return schar(0xC0 + floor(cp / 64), 0x80 + cp % 64)
|
|
elseif cp < 0x10000 then
|
|
return schar(0xE0 + floor(cp / 4096), 0x80 + floor(cp / 64) % 64, 0x80 + cp % 64)
|
|
elseif cp < 0x110000 then
|
|
return schar(0xF0 + floor(cp / 262144), 0x80 + floor(cp / 4096) % 64, 0x80 + floor(cp / 64) % 64, 0x80 + cp % 64)
|
|
end
|
|
return "\239\191\189"
|
|
end
|
|
Parse.utf8char = utf8char
|
|
|
|
local function hexAt(s: string, i: number, k: number): number?
|
|
local h = ssub(s, i, i + k - 1)
|
|
if #h ~= k or sfind(h, "[^%x]") then
|
|
return nil
|
|
end
|
|
return tonumber(h, 16)
|
|
end
|
|
|
|
-- Part 21 escapes: '' \\ \S\c \X\hh \X2\..\X0\ \X4\..\X0\; \P?\ ignored (ISO 8859-1); raw bytes >= 0x80 pass through
|
|
function Parse.unescape(raw: string): string
|
|
if not sfind(raw, "[\\']") then
|
|
return raw
|
|
end
|
|
local out = {}
|
|
local i, n = 1, #raw
|
|
local plain = 1
|
|
while i <= n do
|
|
local c = sbyte(raw, i)
|
|
if c == 39 then -- '' -> '
|
|
table.insert(out, ssub(raw, plain, i))
|
|
i += 2
|
|
plain = i
|
|
elseif c == 92 then
|
|
table.insert(out, ssub(raw, plain, i - 1))
|
|
local d = sbyte(raw, i + 1)
|
|
if d == 92 then
|
|
table.insert(out, "\\")
|
|
i += 2
|
|
elseif d == 83 and sbyte(raw, i + 2) == 92 and i + 3 <= n then -- \S\c
|
|
table.insert(out, utf8char(((sbyte(raw, i + 3) :: number) + 128) % 256))
|
|
i += 4
|
|
elseif d == 80 and sbyte(raw, i + 3) == 92 then -- \P?\ code page
|
|
i += 4
|
|
elseif d == 88 and sbyte(raw, i + 2) == 92 then -- \X\hh
|
|
local h = hexAt(raw, i + 3, 2)
|
|
if h then
|
|
table.insert(out, utf8char(h))
|
|
i += 5
|
|
else
|
|
table.insert(out, "\\")
|
|
i += 1
|
|
end
|
|
elseif d == 88 and (sbyte(raw, i + 2) == 50 or sbyte(raw, i + 2) == 52) and sbyte(raw, i + 3) == 92 then
|
|
local k = if sbyte(raw, i + 2) == 50 then 4 else 8
|
|
local j = i + 4
|
|
local hi: number? = nil
|
|
while j <= n do
|
|
if sbyte(raw, j) == 92 then
|
|
break
|
|
end
|
|
local cp = hexAt(raw, j, k)
|
|
if not cp then
|
|
break
|
|
end
|
|
if k == 4 and cp >= 0xD800 and cp < 0xDC00 then
|
|
hi = cp
|
|
elseif k == 4 and hi and cp >= 0xDC00 and cp < 0xE000 then
|
|
table.insert(out, utf8char(0x10000 + (hi - 0xD800) * 1024 + (cp - 0xDC00)))
|
|
hi = nil
|
|
else
|
|
table.insert(out, utf8char(cp))
|
|
hi = nil
|
|
end
|
|
j += k
|
|
end
|
|
if ssub(raw, j, j + 3) == "\\X0\\" then
|
|
j += 4
|
|
end
|
|
i = j
|
|
else
|
|
table.insert(out, "\\")
|
|
i += 1
|
|
end
|
|
plain = i
|
|
else
|
|
i += 1
|
|
end
|
|
end
|
|
table.insert(out, ssub(raw, plain, n))
|
|
return table.concat(out)
|
|
end
|
|
|
|
local function markReal(t: { [any]: any }, key: any)
|
|
local mt: any = getmetatable(t)
|
|
if not mt then
|
|
mt = { __reals = {} }
|
|
setmetatable(t, mt)
|
|
end
|
|
mt.__reals[key] = true
|
|
end
|
|
|
|
local value: (ctx: Ctx, i: number, depth: number) -> (any, number, boolean)
|
|
|
|
local function list(ctx: Ctx, i: number, depth: number): ({ any }?, number)
|
|
local s = ctx.s
|
|
local out = {}
|
|
i = skip(ctx, i)
|
|
if sbyte(s, i) == 41 then
|
|
return out, i + 1
|
|
end
|
|
local k = 0
|
|
while true do
|
|
local v, j, intReal = value(ctx, i, depth + 1)
|
|
if v == nil then
|
|
return nil, j
|
|
end
|
|
k += 1
|
|
out[k] = v
|
|
if intReal then
|
|
markReal(out, k)
|
|
end
|
|
j = skip(ctx, j)
|
|
local c = sbyte(s, j)
|
|
if c == 44 then
|
|
i = skip(ctx, j + 1)
|
|
elseif c == 41 then
|
|
return out, j + 1
|
|
elseif c == nil then
|
|
return fail(ctx, "truncated list", j), j
|
|
else
|
|
return fail(ctx, "expected , or ) in a list", j), j
|
|
end
|
|
end
|
|
end
|
|
Parse.list = list
|
|
|
|
value = function(ctx: Ctx, i: number, depth: number): (any, number, boolean)
|
|
if depth > MAX_DEPTH then
|
|
return fail(ctx, "lists nested too deeply", i), i, false
|
|
end
|
|
local s = ctx.s
|
|
i = skip(ctx, i)
|
|
local c = sbyte(s, i)
|
|
if c == nil then
|
|
return fail(ctx, "truncated parameter", i), i, false
|
|
elseif c == 35 then -- #ref
|
|
local _, e = sfind(s, "^%d+", i + 1)
|
|
if not e then
|
|
return fail(ctx, "bad entity reference", i), i, false
|
|
end
|
|
return { ref = tonumber(ssub(s, i + 1, e)) }, e + 1, false
|
|
elseif c == 39 then -- 'string'
|
|
local j = i + 1
|
|
while true do
|
|
local q = sfind(s, "'", j, true)
|
|
if not q then
|
|
return fail(ctx, "unterminated string", i), i, false
|
|
end
|
|
if sbyte(s, q + 1) == 39 then
|
|
j = q + 2
|
|
else
|
|
return Parse.unescape(ssub(s, i + 1, q - 1)), q + 1, false
|
|
end
|
|
end
|
|
elseif c == 36 then
|
|
return ctx.null, i + 1, false
|
|
elseif c == 42 then
|
|
return ctx.derived, i + 1, false
|
|
elseif c == 40 then
|
|
local l, j = list(ctx, i + 1, depth)
|
|
return l, j, false
|
|
elseif c == 46 then -- .ENUM.
|
|
local _, e = sfind(s, "^[%a_][%w_]*%.", i + 1)
|
|
if not e then
|
|
return fail(ctx, "bad enumeration", i), i, false
|
|
end
|
|
return { enum = string.upper(ssub(s, i + 1, e - 1)) }, e + 1, false
|
|
elseif c == 34 then -- "binary"
|
|
local _, e = sfind(s, "^[0-3]%x*\"", i + 1)
|
|
if not e then
|
|
return fail(ctx, "bad binary literal", i), i, false
|
|
end
|
|
return { binary = string.upper(ssub(s, i + 1, e - 1)) }, e + 1, false
|
|
elseif (c >= 48 and c <= 57) or c == 45 or c == 43 then
|
|
local _, e = sfind(s, "^[+-]?%d*%.?%d*[eE]?[+-]?%d*", i)
|
|
local tok = ssub(s, i, e :: number)
|
|
local num = tonumber(if c == 43 then ssub(tok, 2) else tok)
|
|
if num == nil then
|
|
-- "1.E" without digits and the like
|
|
local core = string.match(tok, "^[+-]?%d*%.?%d*")
|
|
num = core and tonumber(if c == 43 then ssub(core, 2) else core)
|
|
if num == nil then
|
|
return fail(ctx, "bad number", i), i, false
|
|
end
|
|
e = i + #(core :: string) - 1
|
|
end
|
|
local real = sfind(tok, "[.eE]") ~= nil
|
|
local nv = num :: number
|
|
return nv, (e :: number) + 1, real and nv == floor(nv) and nv == nv
|
|
elseif isAlpha(c) or c == 33 then -- typed parameter NAME(value)
|
|
local word, j = keyword(ctx, i)
|
|
j = skip(ctx, j)
|
|
if word == nil or sbyte(s, j) ~= 40 then
|
|
return fail(ctx, "expected a parameter", i), i, false
|
|
end
|
|
local v, k, intReal = value(ctx, j + 1, depth + 1)
|
|
if v == nil then
|
|
return nil, k, false
|
|
end
|
|
k = skip(ctx, k)
|
|
if sbyte(s, k) ~= 41 then
|
|
return fail(ctx, "expected ) after a typed parameter", k), k, false
|
|
end
|
|
local t = { typed = word, value = v }
|
|
if intReal then
|
|
markReal(t, "value")
|
|
end
|
|
return t, k + 1, false
|
|
end
|
|
return fail(ctx, "unexpected character " .. string.format("%q", schar(c)), i), i, false
|
|
end
|
|
Parse.value = value
|
|
|
|
local function record(ctx: Ctx, i: number): (string?, { any }?, number)
|
|
local word, j = keyword(ctx, i)
|
|
if not word then
|
|
return fail(ctx, "expected an entity name", i), nil, i
|
|
end
|
|
j = skip(ctx, j)
|
|
if sbyte(ctx.s, j) ~= 40 then
|
|
return fail(ctx, "expected ( after " .. word, j), nil, j
|
|
end
|
|
local args, k = list(ctx, j + 1, 0)
|
|
if not args then
|
|
return nil, nil, k
|
|
end
|
|
return word, args, k
|
|
end
|
|
|
|
local function expect(ctx: Ctx, i: number, ch: number, what: string): number?
|
|
i = skip(ctx, i)
|
|
if sbyte(ctx.s, i) ~= ch then
|
|
if i > ctx.n then
|
|
return fail(ctx, "truncated: expected " .. what, i)
|
|
end
|
|
return fail(ctx, "expected " .. what, i)
|
|
end
|
|
return i + 1
|
|
end
|
|
|
|
local function at(s: string, i: number, word: string): boolean
|
|
return ssub(s, i, i + #word - 1) == word
|
|
end
|
|
|
|
export type Result = {
|
|
header: { [string]: { any } },
|
|
headerOrder: { string },
|
|
entities: { [number]: any },
|
|
count: number,
|
|
sections: { { name: any?, schema: any? } },
|
|
extra: { { name: string, text: string } },
|
|
duplicates: number,
|
|
terminated: boolean,
|
|
}
|
|
|
|
function Parse.parse(s: string, null: any, derived: any, maxEntities: number): (Result?, string?, number?)
|
|
local ctx: Ctx = { s = s, n = #s, err = nil, errAt = nil, null = null, derived = derived }
|
|
local res: Result = {
|
|
header = {},
|
|
headerOrder = {},
|
|
entities = {},
|
|
count = 0,
|
|
sections = {},
|
|
extra = {},
|
|
duplicates = 0,
|
|
terminated = false,
|
|
}
|
|
local i = skip(ctx, 1)
|
|
if not at(s, i, "ISO-10303-21") then
|
|
return nil, "no ISO-10303-21 magic", i
|
|
end
|
|
local ni = expect(ctx, i + 12, 59, ";")
|
|
if not ni then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
i = ni
|
|
local sawData = false
|
|
while true do
|
|
i = skip(ctx, i)
|
|
if ctx.err then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
if i > ctx.n then
|
|
if not sawData then
|
|
return nil, "truncated: no DATA section", i
|
|
end
|
|
break
|
|
end
|
|
if at(s, i, "END-ISO-10303-21") then
|
|
res.terminated = true
|
|
local e = expect(ctx, i + 16, 59, ";")
|
|
if not e then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
break
|
|
end
|
|
local word, j = keyword(ctx, i)
|
|
if word == "HEADER" then
|
|
local e = expect(ctx, j, 59, ";")
|
|
if not e then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
i = e
|
|
while true do
|
|
i = skip(ctx, i)
|
|
if at(s, i, "ENDSEC") then
|
|
local e2 = expect(ctx, i + 6, 59, ";")
|
|
if not e2 then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
i = e2
|
|
break
|
|
end
|
|
if i > ctx.n then
|
|
return nil, "truncated header", i
|
|
end
|
|
local t, args, k = record(ctx, i)
|
|
if not t then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
local e2 = expect(ctx, k, 59, ";")
|
|
if not e2 then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
if res.header[t] == nil then
|
|
table.insert(res.headerOrder, t)
|
|
end
|
|
res.header[t] = args :: { any }
|
|
i = e2
|
|
end
|
|
elseif word == "DATA" then
|
|
sawData = true
|
|
local sec: { name: any?, schema: any? } = {}
|
|
j = skip(ctx, j)
|
|
if sbyte(s, j) == 40 then
|
|
local params, k = list(ctx, j + 1, 0)
|
|
if not params then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
sec.name, sec.schema = params[1], params[2]
|
|
j = k
|
|
end
|
|
table.insert(res.sections, sec)
|
|
local secIndex = #res.sections
|
|
local e = expect(ctx, j, 59, ";")
|
|
if not e then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
i = e
|
|
while true do
|
|
i = skip(ctx, i)
|
|
if ctx.err then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
local c = sbyte(s, i)
|
|
if c == nil then
|
|
return nil, "truncated: the DATA section has no ENDSEC", i
|
|
end
|
|
if c ~= 35 then
|
|
if at(s, i, "ENDSEC") then
|
|
local e2 = expect(ctx, i + 6, 59, ";")
|
|
if not e2 then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
i = e2
|
|
break
|
|
end
|
|
return nil, "expected an entity instance (#id = ...)", i
|
|
end
|
|
local _, idEnd = sfind(s, "^%d+", i + 1)
|
|
if not idEnd then
|
|
return nil, "bad entity instance name", i
|
|
end
|
|
local id = tonumber(ssub(s, i + 1, idEnd)) :: number
|
|
local k = expect(ctx, idEnd + 1, 61, "=")
|
|
if not k then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
k = skip(ctx, k)
|
|
local ent: any
|
|
if sbyte(s, k) == 40 then -- complex instance (A(..) B(..))
|
|
local parts = {}
|
|
k = skip(ctx, k + 1)
|
|
while sbyte(s, k) ~= 41 do
|
|
if k > ctx.n then
|
|
return nil, "truncated complex instance", k
|
|
end
|
|
local t, args, k2 = record(ctx, k)
|
|
if not t then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
table.insert(parts, { type = t, args = args })
|
|
k = skip(ctx, k2)
|
|
end
|
|
if #parts == 0 then
|
|
return nil, "empty complex instance", k
|
|
end
|
|
ent = { id = id, type = "COMPLEX", args = {}, parts = parts }
|
|
k += 1
|
|
else
|
|
local t, args, k2 = record(ctx, k)
|
|
if not t then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
ent = { id = id, type = t, args = args }
|
|
k = k2
|
|
end
|
|
local e2 = expect(ctx, k, 59, ";")
|
|
if not e2 then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
if secIndex > 1 then
|
|
ent.section = secIndex
|
|
end
|
|
if res.entities[id] then
|
|
res.duplicates += 1
|
|
else
|
|
res.count += 1
|
|
if res.count > maxEntities then
|
|
return nil, "LIMIT", i
|
|
end
|
|
end
|
|
res.entities[id] = ent
|
|
i = e2
|
|
end
|
|
elseif word == "ANCHOR" or word == "REFERENCE" or word == "SIGNATURE" then
|
|
-- kept as raw text (to the ENDSEC outside strings)
|
|
local k = j
|
|
local stop: number? = nil
|
|
while k <= ctx.n do
|
|
local c = sbyte(s, k)
|
|
if c == 39 then
|
|
local q = k + 1
|
|
while true do
|
|
local p = sfind(s, "'", q, true)
|
|
if not p then
|
|
return nil, "unterminated string", k
|
|
end
|
|
if sbyte(s, p + 1) == 39 then
|
|
q = p + 2
|
|
else
|
|
k = p + 1
|
|
break
|
|
end
|
|
end
|
|
elseif c == 69 and at(s, k, "ENDSEC") then
|
|
stop = k
|
|
break
|
|
else
|
|
k += 1
|
|
end
|
|
end
|
|
if not stop then
|
|
return nil, "truncated " .. word .. " section", i
|
|
end
|
|
table.insert(res.extra, { name = word, text = ssub(s, j, stop - 1) })
|
|
local e2 = expect(ctx, stop + 6, 59, ";")
|
|
if not e2 then
|
|
return nil, ctx.err, ctx.errAt
|
|
end
|
|
i = e2
|
|
else
|
|
return nil, "unknown section " .. tostring(word), i
|
|
end
|
|
end
|
|
if not sawData then
|
|
return nil, "no DATA section", i
|
|
end
|
|
return res, nil, nil
|
|
end
|
|
|
|
return Parse
|