From 2e4d885d8124d67c5a63a001bca96049c1d8c17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Fri, 1 Oct 2010 22:52:01 +0200 Subject: [PATCH] - Updated comment parsing: comments and strings working - To do: assembler code --- pret-pas.lua | 96 ++++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/pret-pas.lua b/pret-pas.lua index cca53af..5ecd7cc 100644 --- a/pret-pas.lua +++ b/pret-pas.lua @@ -92,10 +92,10 @@ local function flush_whatever(str) end end -local inlongcomment, incompdirec, inasm = false, false, false +local inlongcomment, inlongcomment_alt, incompdirec, inasm = false, 0, false, false function visualizer.reset() - inlongcomment, incompdirec, inasm = false, false, false + inlongcomment, inlongcomment_alt, incompdirec, inasm = false, 0, false, false end --[[local function written(state, c, i) @@ -115,9 +115,9 @@ end end]] function visualizer.flush_line(str, nested) - local state, word = 0, nil + local state = 0 local incomment, instring = false, false - local code, comment = nil, nil + --local code, comment = nil, nil buffers.currentcolors = colors --[[ @@ -134,6 +134,7 @@ function visualizer.flush_line(str, nested) until not identifier ]] --TODO: handle inlongcomment etc. +--[[ if inlongcomment then comment, code = string.match(str,"^(.-})(.*)$") if not comment then @@ -151,20 +152,19 @@ function visualizer.flush_line(str, nested) 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 inlongcomment or (inlongcomment_alt == 2) or incompdirec then + incomment = true + if incompdirec then + state = buffers.changestate(1, state) + else + state = buffers.changestate(3, state) + end + tex.sprint(tex.ctxcatcodes, "\{\\bf") + end + + local c, word = nil, nil + for nextc in string.utfcharacters(str .. " ") do if c then if instring then if c == "'" then @@ -177,13 +177,20 @@ function visualizer.flush_line(str, nested) flush_whatever(c) end elseif incomment then - if inlongcomment and (c == "}") then - -- long comment ends + if ((inlongcomment or incompdirec) and (c == "}")) + or (inlongcomment_alt == 1) then + -- long comment/(alternative)/compiler directive ends tex.write(c) tex.sprint(tex.ctxcatcodes, "\}") state = buffers.finishstate(state) + incompdirec = false inlongcomment = false + inlongcomment_alt = 0 incomment = false + elseif (inlongcomment_alt == 2) and (c == "*") and (nextc == ")") then + -- long comment (alternative) ends after nextc + tex.write(c) + inlongcomment_alt = 1 else -- inside the comment flush_whatever(c) @@ -201,15 +208,11 @@ function visualizer.flush_line(str, nested) word = word .. c else -- number - if not incomment then - state = buffers.changestate(3, state) - end + state = buffers.changestate(3, state) tex.write(c) end else - if not incomment then - state = buffers.finishstate(state) - end + state = buffers.finishstate(state) -- identifier complete, check if it's a reserved word and flush flush_pas_word(word) word = nil @@ -221,35 +224,40 @@ function visualizer.flush_line(str, nested) 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 + -- string starts + instring = true + state = buffers.changestate(3, state) 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 + -- one-line comment starts + incomment = true + state = buffers.changestate(3, state) + tex.sprint(tex.ctxcatcodes, "\{\\bf") tex.write(c) elseif c == "{" then - if not incomment then + incomment = true + if nextc == "$" then + -- compiler directive starts + incompdirec = true + state = buffers.changestate(1, state) + else -- long comment starts - incomment = true inlongcomment = true state = buffers.changestate(3, state) - tex.sprint(tex.ctxcatcodes, "\{\\bf") end + tex.sprint(tex.ctxcatcodes, "\{\\bf") tex.write(c) - -- TODO: (*, asm, {$ + elseif (c == "(") and (nextc == "*") then + -- long comment (alternative) starts + incomment = true + inlongcomment_alt = 2 + state = buffers.changestate(3, state) + tex.sprint(tex.ctxcatcodes, "\{\\bf") + tex.write(c) + -- TODO: asm else -- symbol - if not incomment then - state = buffers.changestate(1, state) - end + state = buffers.changestate(1, state) tex.write(c) end end @@ -265,7 +273,7 @@ function visualizer.flush_line(str, nested) -- end the comment-line tex.sprint(tex.ctxcatcodes, "\}") end - + state = buffers.finishstate(state) --[[ local code, comment = string.match(str,"^(.-)%-%-%[%[(.*)$")