From cb7d7e850c4b1d4244b3a35e4527578809f6cf13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Fri, 1 Oct 2010 20:47:24 +0200 Subject: [PATCH] - Updated and changed a lot: strings, //-comments and {..}-comments working - Added local function flush_whatever to handle spaces/tabs properly --- pret-pas.lua | 192 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 134 insertions(+), 58 deletions(-) diff --git a/pret-pas.lua b/pret-pas.lua index 6e4145f..cca53af 100644 --- a/pret-pas.lua +++ b/pret-pas.lua @@ -64,7 +64,7 @@ local states = { local function flush_pas_word(word) if word then - local id = known_words[word] + local id = known_words[string.lower(word)] if id then tex.sprint(tex.ctxcatcodes, "\{\\bf ") tex.write(word) @@ -75,6 +75,23 @@ local function flush_pas_word(word) end end +local function flush_whatever(str) + if str then + for c in string.utfcharacters(str) do + if c == " " then + tex.sprint(tex.ctxcatcodes, "\\obs") + elseif c == "\t" then + tex.sprint(tex.ctxcatcodes, "\\obs") + if buffers.visualizers.enabletab then + tex.sprint(tex.ctxcatcodes,rep("\\obs ", buffers.visualizers.tablength)) + end + else + tex.write(c); + end + end + end +end + local inlongcomment, incompdirec, inasm = false, false, false function visualizer.reset() @@ -98,8 +115,9 @@ end end]] function visualizer.flush_line(str, nested) - local state, word, lastc = 0, nil, nil - local incomment = false + local state, word = 0, nil + local incomment, instring = false, false + local code, comment = nil, nil buffers.currentcolors = colors --[[ @@ -115,81 +133,139 @@ function visualizer.flush_line(str, nested) end until not identifier ]] - - --state = buffers.changestate(1, state) --TODO: handle inlongcomment etc. - for c in string.utfcharacters(str) do - if string.find(c, "^[%a_]$") then - -- char belongs to identifier - if word then - word = word .. c - else - word = c - end - elseif string.find(c, "^[%d]$") then - if word and (#word > 1) then - -- number, that belongs to identifier - word = word .. c - else - -- number - if not incomment then - state = buffers.changestate(3, state) - end - tex.write(c) - end + if inlongcomment then + comment, code = string.match(str,"^(.-})(.*)$") + if not comment then + comment = str + end + state = buffers.changestate(3, state) + tex.sprint(tex.ctxcatcodes, "\{\\bf") + flush_whatever(comment) + tex.sprint(tex.ctxcatcodes, "\}") + if code then + inlongcomment = false else - if not incomment then - state = buffers.finishstate(state) - end - flush_pas_word(word) - word = nil - if c == " " then - --state = buffers.finishstate(state) - tex.sprint(tex.ctxcatcodes, "\\obs") - elseif c == "\t" then - --state = buffers.finishstate(state) - tex.sprint(tex.ctxcatcodes, "\\obs") - if buffers.visualizers.enabletab then - tex.sprint(tex.ctxcatcodes,rep("\\obs ", buffers.visualizers.tablength)) + code = "" + end + else + code = str + end + + local c = nil +--[[ + for nextc in string.utfcharacters(code .. " ") do + if firstrun then + firstrun = false + else + print(c, nextc) + end + c = nextc + end +]] + + for nextc in string.utfcharacters(code .. " ") do + if c then + if instring then + if c == "'" then + -- string ends + tex.write(c) + state = buffers.finishstate(state) + instring = false + else + -- inside the string + flush_whatever(c) end - elseif c == "/" then - if lastc == "/" then + elseif incomment then + if inlongcomment and (c == "}") then + -- long comment ends + tex.write(c) + tex.sprint(tex.ctxcatcodes, "\}") + state = buffers.finishstate(state) + inlongcomment = false + incomment = false + else + -- inside the comment + flush_whatever(c) + end + elseif string.find(c, "^[%a_]$") then + -- char belongs to identifier + if word then + word = word .. c + else + word = c + end + elseif string.find(c, "^[%d]$") then + if word and (#word > 1) then + -- number, that belongs to identifier + word = word .. c + else + -- number + if not incomment then + state = buffers.changestate(3, state) + end + tex.write(c) + end + else + if not incomment then + state = buffers.finishstate(state) + end + -- identifier complete, check if it's a reserved word and flush + flush_pas_word(word) + word = nil + if c == " " then + tex.sprint(tex.ctxcatcodes, "\\obs") + elseif c == "\t" then + tex.sprint(tex.ctxcatcodes, "\\obs") + if buffers.visualizers.enabletab then + tex.sprint(tex.ctxcatcodes,rep("\\obs ", buffers.visualizers.tablength)) + end + elseif c == "'" then + if not incomment then -- "not instring" not necessary + -- string starts + instring = true + state = buffers.changestate(3, state) + end + tex.write(c) + elseif (c == "/") and (nextc == "/") then if not incomment then -- one-line comment starts incomment = true state = buffers.changestate(3, state) tex.sprint(tex.ctxcatcodes, "\{\\bf") end - tex.write("//") + tex.write(c) + elseif c == "{" then + if not incomment then + -- long comment starts + incomment = true + inlongcomment = true + state = buffers.changestate(3, state) + tex.sprint(tex.ctxcatcodes, "\{\\bf") + end + tex.write(c) + -- TODO: (*, asm, {$ + else + -- symbol + if not incomment then + state = buffers.changestate(1, state) + end + tex.write(c) end - elseif c == "{" then - if not incomment then - -- long comment starts - incomment = true - inlongcomment = true - state = buffers.changestate(3, state) - tex.sprint(tex.ctxcatcodes, "\{\\bf") - end - tex.write(c) - -- TODO: (*, asm, {$, ' - else - -- symbol - if not incomment then - state = buffers.changestate(1, state) - end - tex.write(c) end end - lastc = c + c = nextc end if not incomment then state = buffers.finishstate(state) end + -- maybe something left, check if it's a reserved word and flush flush_pas_word(word) if incomment then + -- end the comment-line tex.sprint(tex.ctxcatcodes, "\}") end - --word = nil + --[[ local code, comment = string.match(str,"^(.-)%-%-%[%[(.*)$")