- Updated comment parsing: comments and strings working

- To do: assembler code
This commit is contained in:
Stefan Müller 2010-10-01 22:52:01 +02:00
parent ecc6517eba
commit 2e4d885d81
1 changed files with 52 additions and 44 deletions

View File

@ -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
]]
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
for nextc in string.utfcharacters(code .. " ") do
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
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
@ -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
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(c)
elseif c == "{" then
if not incomment then
-- long comment starts
incomment = true
if nextc == "$" then
-- compiler directive starts
incompdirec = true
state = buffers.changestate(1, state)
else
-- long comment starts
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
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,"^(.-)%-%-%[%[(.*)$")