From ebe227ec1b3c8a7d1ef915441df1e44a3a8aa5a6 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sun, 13 May 2012 16:04:45 +0000 Subject: [PATCH] Lunatic translator: better line numbering git-svn-id: https://svn.eduke32.com/eduke32@2649 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/lunacon.lua | 33 +++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/polymer/eduke32/source/lunatic/lunacon.lua b/polymer/eduke32/source/lunatic/lunacon.lua index 5d45cc416..aaaaace0d 100644 --- a/polymer/eduke32/source/lunatic/lunacon.lua +++ b/polymer/eduke32/source/lunatic/lunacon.lua @@ -342,7 +342,7 @@ local Ci = { setgamepalette = cmd(R), -- some commands taking defines - addammo = cmd(D,D), + addammo = cmd(D,D), -- exec SPECIAL HANDLING! addweapon = cmd(D,D), -- exec SPECIAL HANDLING! debris = cmd(D,D), addinventory = cmd(D,D), @@ -382,14 +382,14 @@ local Ci = { flash = cmd(), getlastpal = cmd(), insertspriteq = cmd(), - killit = cmd(), + killit = cmd(), -- exec SPECIAL HANDLING! mikesnd = cmd(), nullop = cmd(), pkick = cmd(), pstomp = cmd(), resetactioncount = cmd(), resetcount = cmd(), - resetplayer = cmd(), + resetplayer = cmd(), -- exec SPECIAL HANDLING! respawnhitag = cmd(), tip = cmd(), tossweapon = cmd(), @@ -611,14 +611,15 @@ end -- Preconditions: -- tab[i] < tab[i+1] for 1 <= i < #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) -- printf("bsearch(tab, %d)", searchelt) local l, r = 1, #tab local i - if (r <= 1) then - return + if (r < 2) then + return 0 end while (l ~= r) do @@ -644,8 +645,9 @@ end local function getlinecol(pos) local line = bsearch(newlineidxs, pos) - local col = pos-newlineidxs[line-1] - return line, col + assert(line and newlineidxs[line]<=pos and pos 0) then print('CR-only lineends detected.') end + end -- dummy newlines at beginning and end newlineidxs[#newlineidxs+1] = #contents+1 newlineidxs[0] = 0 @@ -873,6 +884,8 @@ if (not EDUKE32_LUNATIC) then setup_newlineidxs(contents) g_badids = {} + g_lastkw = nil + g_lastkwpos = nil local idx = lpeg.match(Grammar, contents) @@ -882,7 +895,7 @@ if (not EDUKE32_LUNATIC) then print("Matched whole contents.") else 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)", idx, i, col, #contents)