codecs/model/DoomMap: the Doom map-to-Scene builder moved out of archive/wad, plus a UDMF 1.1 TEXTMAP parser (fields kept, namespace flag bits); Wad.map loads UDMF maps; model/udmf for a standalone TEXTMAP; codecs/text/Lexer (C-style tokens for id Tech / UDMF text)
STYLE.md: layout block and the 2a table list DoomMap and Lexer. tests/archive/wad: the UDMF stub map now loads (empty) instead of UNSUPPORTED.
This commit is contained in:
1 parent
01c044805f
commit
7d4e0381fe
20 files changed
+3857
-1979
No files matched your search
@@ -0,0 +1,130 @@
|
||||
--!strict
|
||||
-- codecs/text/Lexer: C-style tokens for id Tech / UDMF text (idLib idLexer conventions, UDMF 1.1 §I; no code taken):
|
||||
-- // and /* */ comments, "strings" with backslash escapes, numbers (decimal, float, 0x hex), bare words, and single
|
||||
-- punctuation characters { } ( ) [ ] ; = ,.
|
||||
local Lexer = {}
|
||||
|
||||
export type Kind = "word" | "number" | "string" | "punct"
|
||||
export type Lexer = {
|
||||
s: string,
|
||||
p: number, -- next byte to read, 1-based
|
||||
n: number,
|
||||
raw: string?, -- the last token's text as written
|
||||
err: string?,
|
||||
next: (self: Lexer) -> (Kind?, any),
|
||||
peek: (self: Lexer) -> (Kind?, any),
|
||||
line: (self: Lexer, at: number?) -> number,
|
||||
}
|
||||
|
||||
local find, sub, byte, match = string.find, string.sub, string.byte, string.match
|
||||
local ESC: { [string]: string } = { n = "\n", t = "\t", r = "\r", ["0"] = "\0" }
|
||||
|
||||
local function skip(L: Lexer): number?
|
||||
local s, p = L.s, L.p
|
||||
while true do
|
||||
p = find(s, "[^ \t\r\n\f\v]", p)
|
||||
if not p then
|
||||
return nil
|
||||
end
|
||||
if byte(s, p) == 47 then
|
||||
local c2 = byte(s, p + 1)
|
||||
if c2 == 47 then
|
||||
local e = find(s, "\n", p + 2, true)
|
||||
if not e then
|
||||
return nil
|
||||
end
|
||||
p = e + 1
|
||||
elseif c2 == 42 then
|
||||
local _, e = find(s, "*/", p + 2, true)
|
||||
if not e then
|
||||
L.err = "unterminated comment"
|
||||
return nil
|
||||
end
|
||||
p = e + 1
|
||||
else
|
||||
return p
|
||||
end
|
||||
else
|
||||
return p
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function token(L: Lexer): (Kind?, any, number)
|
||||
local p = skip(L)
|
||||
if not p then
|
||||
return nil, nil, L.n + 1
|
||||
end
|
||||
local s = L.s
|
||||
local c = byte(s, p)
|
||||
if c == 34 then
|
||||
local parts: { string }? = nil
|
||||
local q = p + 1
|
||||
while true do
|
||||
local e = find(s, "[\"\\]", q)
|
||||
if not e then
|
||||
L.err = "unterminated string"
|
||||
return nil, nil, L.n + 1
|
||||
end
|
||||
if byte(s, e) == 34 then
|
||||
local tail = sub(s, q, e - 1)
|
||||
if parts then
|
||||
table.insert(parts, tail)
|
||||
L.raw = sub(s, p, e)
|
||||
return "string", table.concat(parts), e + 1
|
||||
end
|
||||
L.raw = sub(s, p, e)
|
||||
return "string", tail, e + 1
|
||||
end
|
||||
parts = parts or {}
|
||||
local ch = sub(s, e + 1, e + 1)
|
||||
table.insert(parts :: { string }, sub(s, q, e - 1))
|
||||
table.insert(parts :: { string }, ESC[ch] or ch)
|
||||
q = e + 2
|
||||
end
|
||||
end
|
||||
if find("{}()[];=,", string.char(c), 1, true) then
|
||||
L.raw = string.char(c)
|
||||
return "punct", L.raw, p + 1
|
||||
end
|
||||
local w = match(s, "^[^ \t\r\n\f\v{}()%[%];=,\"]+", p) :: string
|
||||
local e = p + #w
|
||||
-- a comment right after a word ends it
|
||||
local cm = find(w, "/[/*]")
|
||||
if cm then
|
||||
w = sub(w, 1, cm - 1)
|
||||
e = p + #w
|
||||
end
|
||||
L.raw = w
|
||||
if (c >= 48 and c <= 57) or c == 45 or c == 43 or c == 46 then
|
||||
local v = tonumber(w)
|
||||
if v and not find(w, "[iInN]") then
|
||||
return "number", v, e
|
||||
end
|
||||
end
|
||||
return "word", w, e
|
||||
end
|
||||
|
||||
local function nextToken(L: Lexer): (Kind?, any)
|
||||
local k, v, e = token(L)
|
||||
L.p = e
|
||||
return k, v
|
||||
end
|
||||
|
||||
local function peek(L: Lexer): (Kind?, any)
|
||||
local raw = L.raw
|
||||
local k, v = token(L)
|
||||
L.raw = raw
|
||||
return k, v
|
||||
end
|
||||
|
||||
local function line(L: Lexer, at: number?): number
|
||||
local _, n = string.gsub(sub(L.s, 1, (at or L.p) - 1), "\n", "")
|
||||
return n + 1
|
||||
end
|
||||
|
||||
function Lexer.new(s: string): Lexer
|
||||
return { s = s, p = 1, n = #s, raw = nil, err = nil, next = nextToken, peek = peek, line = line }
|
||||
end
|
||||
|
||||
return Lexer
|
||||
Reference in new issue
Block a user