521 lines
13 KiB
Luau
521 lines
13 KiB
Luau
--!strict
|
|
-- codecs/model/Vrml/Parse: the VRML97 (ISO/IEC 14772-1) / X3D classic (ISO/IEC 19776-2) encoding parser.
|
|
-- Written from the specifications; no third-party code.
|
|
local Parse = {}
|
|
|
|
export type Node = { type: string, def: string?, fields: { [string]: any } }
|
|
export type Route = { from: Node, fromField: string, to: Node, toField: string }
|
|
export type Proto = { name: string, fields: { [string]: any }, body: { Node }, routes: { Route }, extern: boolean? }
|
|
export type Graph = {
|
|
roots: { Node },
|
|
routes: { Route },
|
|
header: { [string]: any },
|
|
meta: { [string]: string },
|
|
components: { string },
|
|
units: { [string]: { name: string, factor: number } },
|
|
protos: { [string]: Proto },
|
|
}
|
|
export type IsRef = { is: string }
|
|
|
|
local find, sub, byte, match, gmatch = string.find, string.sub, string.byte, string.match, string.gmatch
|
|
|
|
local ID1 = "^[^%c%s%d\"#',%+%-%.%[\\%]{}][^%c%s\"#',%.%[\\%]{}]*"
|
|
local MAX_DEPTH = 200
|
|
local DECL = { field = true, exposedField = true, eventIn = true, eventOut = true, initializeOnly = true, inputOutput = true, inputOnly = true, outputOnly = true }
|
|
local HAS_VALUE = { field = true, exposedField = true, initializeOnly = true, inputOutput = true }
|
|
|
|
local function copy(node: Node, values: { [string]: any }, defaults: { [string]: any }, memo: { [any]: any }): Node
|
|
local done = memo[node]
|
|
if done then
|
|
return done
|
|
end
|
|
local out: Node = { type = node.type, def = node.def, fields = {} }
|
|
memo[node] = out
|
|
for k, v in node.fields do
|
|
if type(v) == "table" and type((v :: any).is) == "string" then
|
|
local name = (v :: any).is
|
|
local val = values[name]
|
|
if val == nil then
|
|
val = defaults[name]
|
|
end
|
|
out.fields[k] = val
|
|
elseif type(v) == "table" then
|
|
local arr = table.create(#v)
|
|
for i, x in v do
|
|
arr[i] = if type(x) == "table" and x.type then copy(x, values, defaults, memo) else x
|
|
end
|
|
out.fields[k] = arr
|
|
else
|
|
out.fields[k] = v
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
function Parse.expand(proto: Proto, values: { [string]: any }, def: string?, routes: { Route }?): Node?
|
|
if proto.extern or #proto.body == 0 then
|
|
return nil
|
|
end
|
|
local memo = {}
|
|
local first: Node? = nil
|
|
for _, n in proto.body do
|
|
local c = copy(n, values, proto.fields, memo)
|
|
first = first or c
|
|
end
|
|
if routes then
|
|
for _, r in proto.routes do
|
|
local from, to = memo[r.from], memo[r.to]
|
|
if from and to then
|
|
table.insert(routes, { from = from, fromField = r.fromField, to = to, toField = r.toField })
|
|
end
|
|
end
|
|
end
|
|
local f = first :: Node
|
|
if def then
|
|
-- the instance gets its own copy of the node so its DEF name is its own
|
|
local out: Node = { type = f.type, def = def, fields = f.fields }
|
|
return out
|
|
end
|
|
return f
|
|
end
|
|
|
|
function Parse.parse(text: string): (Graph?, string?)
|
|
local n = #text
|
|
local p = 1
|
|
local graph: Graph = { roots = {}, routes = {}, header = {}, meta = {}, components = {}, units = {}, protos = {} }
|
|
local defs: { [string]: Node } = {}
|
|
local protos: { [string]: Proto } = graph.protos
|
|
local routes: { Route } = graph.routes
|
|
local failure: string? = nil
|
|
local depth = 0
|
|
|
|
local function line(at: number): number
|
|
local l = 1
|
|
for _ in gmatch(sub(text, 1, at), "\n") do
|
|
l += 1
|
|
end
|
|
return l
|
|
end
|
|
local function fail(msg: string)
|
|
if not failure then
|
|
failure = "line " .. line(p) .. ": " .. msg
|
|
end
|
|
end
|
|
|
|
-- header line: #VRML V2.0 utf8 / #X3D V3.3 utf8
|
|
local v, enc = match(text, "^#VRML V(%S+) (%S+)")
|
|
if v then
|
|
graph.header.version, graph.header.encoding = v, enc
|
|
if v ~= "2.0" then
|
|
return nil, "unsupported: VRML " .. v .. " (only VRML97 / X3D)"
|
|
end
|
|
else
|
|
v, enc = match(text, "^#X3D V(%S+) (%S+)")
|
|
if v then
|
|
graph.header.version, graph.header.encoding, graph.header.x3d = v, enc, true
|
|
end
|
|
end
|
|
|
|
local function skip()
|
|
while true do
|
|
local q = find(text, "[^%s,]", p)
|
|
if not q then
|
|
p = n + 1
|
|
return
|
|
end
|
|
p = q
|
|
if byte(text, p) == 35 then -- '#'
|
|
local e = find(text, "[\r\n]", p)
|
|
p = if e then e else n + 1
|
|
else
|
|
return
|
|
end
|
|
end
|
|
end
|
|
local function token(): (string, any)
|
|
skip()
|
|
if p > n then
|
|
return "eof", nil
|
|
end
|
|
local c = byte(text, p)
|
|
if c == 123 or c == 125 or c == 91 or c == 93 then -- { } [ ]
|
|
p += 1
|
|
return string.char(c), nil
|
|
elseif c == 34 then -- a string: \" and \\ escapes
|
|
local q = p + 1
|
|
local parts = {}
|
|
while true do
|
|
local e = find(text, '["\\]', q)
|
|
if not e then
|
|
fail("unterminated string")
|
|
p = n + 1
|
|
return "eof", nil
|
|
end
|
|
table.insert(parts, sub(text, q, e - 1))
|
|
if byte(text, e) == 92 then
|
|
table.insert(parts, sub(text, e + 1, e + 1))
|
|
q = e + 2
|
|
else
|
|
p = e + 1
|
|
return "str", table.concat(parts)
|
|
end
|
|
end
|
|
end
|
|
if (c >= 48 and c <= 57) or c == 43 or c == 45 or c == 46 then
|
|
local s, e = find(text, "^[+-]?0[xX]%x+", p)
|
|
if not s then
|
|
s, e = find(text, "^[+-]?%d*%.?%d*[eE][+-]?%d+", p)
|
|
end
|
|
if not s then
|
|
s, e = find(text, "^[+-]?%d*%.?%d*", p)
|
|
end
|
|
local num = tonumber(sub(text, p, e))
|
|
if num then
|
|
p = (e :: number) + 1
|
|
return "num", num
|
|
end
|
|
if c == 46 then
|
|
p += 1
|
|
return ".", nil
|
|
end
|
|
local _, we = find(text, "^[^%s,%[%]{}#\"]+", p)
|
|
local w = sub(text, p, we)
|
|
p = (we :: number) + 1
|
|
local wl = string.lower(w)
|
|
local val = if find(wl, "inf", 1, true) then (if c == 45 then -math.huge else math.huge) elseif find(wl, "nan", 1, true) then 0 else nil
|
|
if val == nil then
|
|
fail("bad number " .. w)
|
|
return "eof", nil
|
|
end
|
|
return "num", val
|
|
end
|
|
local s, e = find(text, ID1, p)
|
|
if s then
|
|
p = (e :: number) + 1
|
|
return "id", sub(text, s, e)
|
|
end
|
|
fail("unexpected character " .. sub(text, p, p))
|
|
p = n + 1
|
|
return "eof", nil
|
|
end
|
|
local function peek(): (string, any)
|
|
local save = p
|
|
local k, val = token()
|
|
p = save
|
|
return k, val
|
|
end
|
|
|
|
local parseNode: (first: string) -> (Node?, boolean)
|
|
local parseProto: (extern: boolean) -> ()
|
|
local parseRoute: () -> ()
|
|
|
|
local function value(): any
|
|
local k, val = token()
|
|
if k == "[" then
|
|
local out = {}
|
|
local close = find(text, "]", p, true)
|
|
if close then
|
|
local seg = sub(text, p, close - 1)
|
|
if not find(seg, "[{}\"#]") then
|
|
local ok = true
|
|
for w in gmatch(seg, "[^%s,]+") do
|
|
local x = tonumber(w)
|
|
if x then
|
|
table.insert(out, x)
|
|
elseif w == "TRUE" or w == "FALSE" then
|
|
table.insert(out, w == "TRUE")
|
|
else
|
|
ok = false
|
|
break
|
|
end
|
|
end
|
|
if ok then
|
|
p = close + 1
|
|
return out
|
|
end
|
|
table.clear(out)
|
|
end
|
|
end
|
|
while not failure do
|
|
k, val = token()
|
|
if k == "]" then
|
|
return out
|
|
elseif k == "num" or k == "str" then
|
|
table.insert(out, val)
|
|
elseif k == "id" and (val == "TRUE" or val == "FALSE") then
|
|
table.insert(out, val == "TRUE")
|
|
elseif k == "id" and val == "NULL" then
|
|
continue
|
|
elseif k == "id" then
|
|
local node = parseNode(val)
|
|
if node then
|
|
table.insert(out, node)
|
|
end
|
|
else
|
|
fail("unexpected " .. k .. " in a [ ] value")
|
|
return out
|
|
end
|
|
end
|
|
return out
|
|
elseif k == "num" then
|
|
local out = { val }
|
|
while true do
|
|
local save = p
|
|
local k2, v2 = token()
|
|
if k2 ~= "num" then
|
|
p = save
|
|
break
|
|
end
|
|
table.insert(out, v2)
|
|
end
|
|
return out
|
|
elseif k == "str" then
|
|
return { val }
|
|
elseif k == "id" then
|
|
if val == "TRUE" or val == "FALSE" then
|
|
return { val == "TRUE" }
|
|
elseif val == "NULL" then
|
|
return {}
|
|
end
|
|
local node = parseNode(val)
|
|
return if node then { node } else {}
|
|
end
|
|
fail("missing field value")
|
|
return {}
|
|
end
|
|
|
|
local function body(node: Node)
|
|
while not failure do
|
|
local k, name = token()
|
|
if k == "}" then
|
|
return
|
|
elseif k ~= "id" then
|
|
fail("expected a field name in " .. node.type .. ", got " .. k)
|
|
return
|
|
end
|
|
if name == "ROUTE" then
|
|
parseRoute()
|
|
elseif name == "PROTO" or name == "EXTERNPROTO" then
|
|
parseProto(name == "EXTERNPROTO")
|
|
elseif DECL[name] then
|
|
-- Script / shader interface: <access> <type> <name> [<value> | IS <name>]
|
|
local _, _t = token()
|
|
local fk, fname = token()
|
|
if fk ~= "id" then
|
|
fail("bad interface declaration in " .. node.type)
|
|
return
|
|
end
|
|
local pk, pv = peek()
|
|
if pk == "id" and pv == "IS" then
|
|
token()
|
|
local _, ref = token()
|
|
node.fields[fname] = { is = ref }
|
|
elseif HAS_VALUE[name] then
|
|
node.fields[fname] = value()
|
|
end
|
|
else
|
|
local pk, pv = peek()
|
|
if pk == "id" and pv == "IS" then
|
|
token()
|
|
local _, ref = token()
|
|
node.fields[name] = { is = ref }
|
|
else
|
|
node.fields[name] = value()
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- a node statement starting with the id `first` (DEF name Type { }, USE name, Type { })
|
|
function parseNode(first: string): (Node?, boolean)
|
|
if first == "USE" then
|
|
local k, name = token()
|
|
if k ~= "id" then
|
|
fail("USE without a name")
|
|
return nil, false
|
|
end
|
|
local node = defs[name]
|
|
if not node then
|
|
fail("USE of an undefined name " .. tostring(name))
|
|
end
|
|
return node, true
|
|
end
|
|
local def: string? = nil
|
|
local typ = first
|
|
if first == "DEF" then
|
|
local k, name = token()
|
|
local k2, t2 = token()
|
|
if k ~= "id" or k2 ~= "id" then
|
|
fail("bad DEF")
|
|
return nil, false
|
|
end
|
|
def, typ = name, t2
|
|
end
|
|
local k = token()
|
|
if k ~= "{" then
|
|
fail("expected { after " .. typ)
|
|
return nil, false
|
|
end
|
|
depth += 1
|
|
if depth > MAX_DEPTH then
|
|
fail("limit: nodes nested deeper than " .. MAX_DEPTH)
|
|
return nil, false
|
|
end
|
|
local node: Node = { type = typ, def = def, fields = {} }
|
|
body(node)
|
|
depth -= 1
|
|
local proto = protos[typ]
|
|
if proto and not proto.extern then
|
|
local inst = Parse.expand(proto, node.fields, def, routes)
|
|
if inst then
|
|
node = inst
|
|
end
|
|
elseif proto then
|
|
(node :: any).extern = true
|
|
end
|
|
if def then
|
|
defs[def] = node
|
|
end
|
|
return node, false
|
|
end
|
|
|
|
-- ROUTE from.field TO to.field
|
|
function parseRoute()
|
|
local function path(): (string?, string?)
|
|
local k1, a = token()
|
|
local k2 = token()
|
|
local k3, b = token()
|
|
if k1 ~= "id" or k2 ~= "." or k3 ~= "id" then
|
|
return nil, nil
|
|
end
|
|
return a, b
|
|
end
|
|
local a, af = path()
|
|
local k, to = token()
|
|
local b, bf = path()
|
|
if not (a and af and b and bf) or k ~= "id" or to ~= "TO" then
|
|
fail("bad ROUTE")
|
|
return
|
|
end
|
|
local from, dst = defs[a :: string], defs[b :: string]
|
|
if from and dst then
|
|
table.insert(routes, { from = from, fromField = af :: string, to = dst, toField = bf :: string })
|
|
end
|
|
end
|
|
|
|
-- PROTO name [ interface ] { body } / EXTERNPROTO name [ interface ] url
|
|
function parseProto(extern: boolean)
|
|
local k, name = token()
|
|
if k ~= "id" or token() ~= "[" then
|
|
fail("bad PROTO")
|
|
return
|
|
end
|
|
local proto: Proto = { name = name, fields = {}, body = {}, routes = {}, extern = extern or nil }
|
|
while not failure do
|
|
local k2, access = token()
|
|
if k2 == "]" then
|
|
break
|
|
elseif k2 ~= "id" or not DECL[access] then
|
|
fail("bad PROTO interface")
|
|
return
|
|
end
|
|
local _, _t = token()
|
|
local fk, fname = token()
|
|
if fk ~= "id" then
|
|
fail("bad PROTO interface")
|
|
return
|
|
end
|
|
if HAS_VALUE[access] and not extern then
|
|
proto.fields[fname] = value()
|
|
end
|
|
end
|
|
if extern then
|
|
value()
|
|
protos[name] = proto
|
|
return
|
|
end
|
|
if token() ~= "{" then
|
|
fail("PROTO " .. tostring(name) .. " without a body")
|
|
return
|
|
end
|
|
-- the body has its own DEF names and its own routes
|
|
local outerDefs, outerRoutes = defs, routes
|
|
defs, routes = {}, proto.routes
|
|
while not failure do
|
|
local k3, w = token()
|
|
if k3 == "}" then
|
|
break
|
|
elseif k3 ~= "id" then
|
|
fail("bad PROTO body")
|
|
break
|
|
elseif w == "ROUTE" then
|
|
parseRoute()
|
|
elseif w == "PROTO" or w == "EXTERNPROTO" then
|
|
parseProto(w == "EXTERNPROTO")
|
|
else
|
|
local node = parseNode(w)
|
|
if node then
|
|
table.insert(proto.body, node)
|
|
end
|
|
end
|
|
end
|
|
defs, routes = outerDefs, outerRoutes
|
|
protos[name] = proto
|
|
end
|
|
|
|
while not failure do
|
|
local k, w = token()
|
|
if k == "eof" then
|
|
break
|
|
elseif k ~= "id" then
|
|
fail("unexpected " .. k)
|
|
break
|
|
end
|
|
if w == "ROUTE" then
|
|
parseRoute()
|
|
elseif w == "PROTO" or w == "EXTERNPROTO" then
|
|
parseProto(w == "EXTERNPROTO")
|
|
elseif w == "PROFILE" then
|
|
local _, prof = token()
|
|
graph.header.profile = prof
|
|
elseif w == "COMPONENT" then
|
|
local _, comp = token() -- name:level is one id
|
|
table.insert(graph.components, tostring(comp))
|
|
elseif w == "META" then
|
|
local _, key = token()
|
|
local _, val = token()
|
|
graph.meta[tostring(key)] = tostring(val)
|
|
elseif w == "UNIT" then
|
|
local _, cat = token()
|
|
local _, uname = token()
|
|
local _, factor = token()
|
|
if type(cat) == "string" and type(factor) == "number" then
|
|
graph.units[cat] = { name = tostring(uname), factor = factor }
|
|
end
|
|
elseif w == "IMPORT" or w == "EXPORT" then
|
|
-- IMPORT inline.name AS name / EXPORT name AS name: no effect on the scene
|
|
local _, _a = token()
|
|
if peek() == "." then
|
|
token()
|
|
token()
|
|
end
|
|
local pk, pv = peek()
|
|
if pk == "id" and pv == "AS" then
|
|
token()
|
|
token()
|
|
end
|
|
else
|
|
local node = parseNode(w)
|
|
if node then
|
|
table.insert(graph.roots, node)
|
|
end
|
|
end
|
|
end
|
|
if failure then
|
|
return nil, failure
|
|
end
|
|
return graph, nil
|
|
end
|
|
|
|
return Parse
|