cad formats: comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
8bd1a9327c
4 files changed
+22
-91
No files matched your search
@@ -1,15 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- cad/step/Parse: ISO 10303-21 (STEP physical file, editions 1-3) text -> header + entity instances. A single pass
|
||||
-- over the text: comments, the HEADER section, any number of DATA sections (edition 3 `DATA('name', ('schema'));`),
|
||||
-- ANCHOR / REFERENCE / SIGNATURE sections kept as raw text, simple and complex (external mapping) instances, every
|
||||
-- parameter kind (integer, real, string with its \S\ \X\ \X2\ \X4\ escapes decoded to UTF-8, enumeration, binary,
|
||||
-- entity reference, $, *, list, typed parameter). Never throws: failures return nil, message, offset.
|
||||
--
|
||||
-- Values: numbers; strings; { enum = "T" }; { ref = 12 }; { binary = "0FF" }; { typed = "LENGTH_MEASURE", value = v };
|
||||
-- lists as arrays; NULL / DERIVED sentinels (from the caller). A real literal with an integral value (`0.`, `1.E2`)
|
||||
-- is recorded in the metatable of the list (or typed table) holding it (`__reals = { [index | "value"] = true }`), so
|
||||
-- the writer prints it back as a real; integral numbers without that mark are written as integers.
|
||||
-- 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
|
||||
@@ -32,7 +24,6 @@ local function fail(ctx: Ctx, msg: string, at: number): nil
|
||||
return nil
|
||||
end
|
||||
|
||||
-- the next position that is not white space or inside a /* comment */ (n + 1 at the end; an open comment -> error)
|
||||
local function skip(ctx: Ctx, i: number): number
|
||||
local s, n = ctx.s, ctx.n
|
||||
while i <= n do
|
||||
@@ -60,7 +51,6 @@ 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
|
||||
|
||||
-- keyword at i ([!]A-Z_ A-Z0-9_*) -> word, next; nil when there is none
|
||||
local function keyword(ctx: Ctx, i: number): (string?, number)
|
||||
local s, n = ctx.s, ctx.n
|
||||
local j = i
|
||||
@@ -77,7 +67,6 @@ local function keyword(ctx: Ctx, i: number): (string?, number)
|
||||
return string.upper(ssub(s, i, j - 1)), j
|
||||
end
|
||||
|
||||
-- UTF-8 of a code point
|
||||
local function utf8char(cp: number): string
|
||||
if cp < 0x80 then
|
||||
return schar(cp)
|
||||
@@ -100,9 +89,7 @@ local function hexAt(s: string, i: number, k: number): number?
|
||||
return tonumber(h, 16)
|
||||
end
|
||||
|
||||
-- the text between the quotes of a string literal -> its value ('' -> ', \\ -> \, \S\c, \X\hh, \X2\..\X0\, \X4\..\X0\,
|
||||
-- \P?\ page switches ignored: every page reads as ISO 8859-1). Bytes >= 0x80 written raw (common, though not allowed)
|
||||
-- pass through.
|
||||
-- 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
|
||||
@@ -185,11 +172,9 @@ local function markReal(t: { [any]: any }, key: any)
|
||||
mt.__reals[key] = true
|
||||
end
|
||||
|
||||
-- one parameter at i -> value, next, integralReal (nil value + ctx.err on failure)
|
||||
local value: (ctx: Ctx, i: number, depth: number) -> (any, number, boolean)
|
||||
|
||||
local function list(ctx: Ctx, i: number, depth: number): ({ any }?, number)
|
||||
-- i is just past "("
|
||||
local s = ctx.s
|
||||
local out = {}
|
||||
i = skip(ctx, i)
|
||||
@@ -309,7 +294,6 @@ value = function(ctx: Ctx, i: number, depth: number): (any, number, boolean)
|
||||
end
|
||||
Parse.value = value
|
||||
|
||||
-- the simple record at i: NAME(params) -> type, args, next
|
||||
local function record(ctx: Ctx, i: number): (string?, { any }?, number)
|
||||
local word, j = keyword(ctx, i)
|
||||
if not word then
|
||||
@@ -326,7 +310,6 @@ local function record(ctx: Ctx, i: number): (string?, { any }?, number)
|
||||
return word, args, k
|
||||
end
|
||||
|
||||
-- expect(ctx, i, ch) -> next position after ch (skipping blanks), or nil
|
||||
local function expect(ctx: Ctx, i: number, ch: number, what: string): number?
|
||||
i = skip(ctx, i)
|
||||
if sbyte(ctx.s, i) ~= ch then
|
||||
@@ -353,7 +336,6 @@ export type Result = {
|
||||
terminated: boolean,
|
||||
}
|
||||
|
||||
-- parse(s, opts) -> Result | nil, message, offset (1-based position in s)
|
||||
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 = {
|
||||
|
||||
Reference in new issue
Block a user