Lunatic translator: better line numbering

git-svn-id: https://svn.eduke32.com/eduke32@2649 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-05-13 16:04:45 +00:00
parent 9786e7e625
commit ebe227ec1b
1 changed files with 23 additions and 10 deletions

View File

@ -342,7 +342,7 @@ local Ci = {
setgamepalette = cmd(R), setgamepalette = cmd(R),
-- some commands taking defines -- some commands taking defines
addammo = cmd(D,D), addammo = cmd(D,D), -- exec SPECIAL HANDLING!
addweapon = cmd(D,D), -- exec SPECIAL HANDLING! addweapon = cmd(D,D), -- exec SPECIAL HANDLING!
debris = cmd(D,D), debris = cmd(D,D),
addinventory = cmd(D,D), addinventory = cmd(D,D),
@ -382,14 +382,14 @@ local Ci = {
flash = cmd(), flash = cmd(),
getlastpal = cmd(), getlastpal = cmd(),
insertspriteq = cmd(), insertspriteq = cmd(),
killit = cmd(), killit = cmd(), -- exec SPECIAL HANDLING!
mikesnd = cmd(), mikesnd = cmd(),
nullop = cmd(), nullop = cmd(),
pkick = cmd(), pkick = cmd(),
pstomp = cmd(), pstomp = cmd(),
resetactioncount = cmd(), resetactioncount = cmd(),
resetcount = cmd(), resetcount = cmd(),
resetplayer = cmd(), resetplayer = cmd(), -- exec SPECIAL HANDLING!
respawnhitag = cmd(), respawnhitag = cmd(),
tip = cmd(), tip = cmd(),
tossweapon = cmd(), tossweapon = cmd(),
@ -611,14 +611,15 @@ end
-- Preconditions: -- Preconditions:
-- tab[i] < tab[i+1] for 1 <= i < #tab -- tab[i] < tab[i+1] for 1 <= i < #tab
-- tab[1] <= searchelt < tab[#tab] -- tab[1] <= searchelt < tab[#tab]
-- If tab has less than 2 elements, returns nil. -- If #tab is less than 2, returns 0. This plays nicely with newline index
-- tables like { [0]=0, [1]=len+1 }, e.g. if the file doesn't contain any.
local function bsearch(tab, searchelt) local function bsearch(tab, searchelt)
-- printf("bsearch(tab, %d)", searchelt) -- printf("bsearch(tab, %d)", searchelt)
local l, r = 1, #tab local l, r = 1, #tab
local i local i
if (r <= 1) then if (r < 2) then
return return 0
end end
while (l ~= r) do while (l ~= r) do
@ -644,8 +645,9 @@ end
local function getlinecol(pos) local function getlinecol(pos)
local line = bsearch(newlineidxs, pos) local line = bsearch(newlineidxs, pos)
local col = pos-newlineidxs[line-1] assert(line and newlineidxs[line]<=pos and pos<newlineidxs[line+1])
return line, col local col = pos-newlineidxs[line]
return line+1, col-1
end end
-- Last keyword position, for error diagnosis. -- Last keyword position, for error diagnosis.
@ -698,6 +700,8 @@ local function Ident(idname) return TraceFunc(idname, "id", false) end
local function BadIdent(idname) return BadIdentFunc(idname) end local function BadIdent(idname) return BadIdentFunc(idname) end
local function Stmt(cmdpat) return TraceFunc(cmdpat, "st", false) end local function Stmt(cmdpat) return TraceFunc(cmdpat, "st", false) end
--local function Temp(kwname) return TraceFunc(kwname, "temp", true) end
--Ci["myosx"] = Temp(Ci["myosx"])
----==== Translator continued ====---- ----==== Translator continued ====----
-- attach the command names at the front! -- attach the command names at the front!
@ -800,7 +804,7 @@ local Grammar = Pat{
-- Deps. These appear here because we're hitting a limit with LPeg else: -- Deps. These appear here because we're hitting a limit with LPeg else:
-- http://lua-users.org/lists/lua-l/2008-11/msg00462.html -- http://lua-users.org/lists/lua-l/2008-11/msg00462.html
-- NOTE: NW demo (NWSNOW.CON) contains a Ctrl-Z char (dec 26) -- NOTE: NW demo (NWSNOW.CON) contains a Ctrl-Z char (decimal 26)
whitespace = Set(" \t\r\26") + newline + Set("(),;") + comment + linecomment, whitespace = Set(" \t\r\26") + newline + Set("(),;") + comment + linecomment,
t_identifier_all = t_broken_identifier + t_good_identifier, t_identifier_all = t_broken_identifier + t_good_identifier,
@ -856,6 +860,13 @@ local function setup_newlineidxs(contents)
for i in string.gmatch(contents, "()\n") do for i in string.gmatch(contents, "()\n") do
newlineidxs[#newlineidxs+1] = i newlineidxs[#newlineidxs+1] = i
end end
if (#newlineidxs == 0) then
-- try CR only (old Mac)
for i in string.gmatch(contents, "()\r") do
newlineidxs[#newlineidxs+1] = i
end
-- if (#newlineidxs > 0) then print('CR-only lineends detected.') end
end
-- dummy newlines at beginning and end -- dummy newlines at beginning and end
newlineidxs[#newlineidxs+1] = #contents+1 newlineidxs[#newlineidxs+1] = #contents+1
newlineidxs[0] = 0 newlineidxs[0] = 0
@ -873,6 +884,8 @@ if (not EDUKE32_LUNATIC) then
setup_newlineidxs(contents) setup_newlineidxs(contents)
g_badids = {} g_badids = {}
g_lastkw = nil
g_lastkwpos = nil
local idx = lpeg.match(Grammar, contents) local idx = lpeg.match(Grammar, contents)
@ -882,7 +895,7 @@ if (not EDUKE32_LUNATIC) then
print("Matched whole contents.") print("Matched whole contents.")
else else
local i, col = getlinecol(idx) local i, col = getlinecol(idx)
local bi, ei = newlineidxs[i]+1, newlineidxs[i+1]-1 local bi, ei = newlineidxs[i-1]+1, newlineidxs[i]-1
printf("Match succeeded up to %d (line %d, col %d; len=%d)", printf("Match succeeded up to %d (line %d, col %d; len=%d)",
idx, i, col, #contents) idx, i, col, #contents)