- 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
end end
local inlongcomment, incompdirec, inasm = false, false, false local inlongcomment, inlongcomment_alt, incompdirec, inasm = false, 0, false, false
function visualizer.reset() function visualizer.reset()
inlongcomment, incompdirec, inasm = false, false, false inlongcomment, inlongcomment_alt, incompdirec, inasm = false, 0, false, false
end end
--[[local function written(state, c, i) --[[local function written(state, c, i)
@ -115,9 +115,9 @@ end
end]] end]]
function visualizer.flush_line(str, nested) function visualizer.flush_line(str, nested)
local state, word = 0, nil local state = 0
local incomment, instring = false, false local incomment, instring = false, false
local code, comment = nil, nil --local code, comment = nil, nil
buffers.currentcolors = colors buffers.currentcolors = colors
--[[ --[[
@ -134,6 +134,7 @@ function visualizer.flush_line(str, nested)
until not identifier until not identifier
]] ]]
--TODO: handle inlongcomment etc. --TODO: handle inlongcomment etc.
--[[
if inlongcomment then if inlongcomment then
comment, code = string.match(str,"^(.-})(.*)$") comment, code = string.match(str,"^(.-})(.*)$")
if not comment then if not comment then
@ -151,20 +152,19 @@ function visualizer.flush_line(str, nested)
else else
code = str code = str
end 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 c then
if instring then if instring then
if c == "'" then if c == "'" then
@ -177,13 +177,20 @@ function visualizer.flush_line(str, nested)
flush_whatever(c) flush_whatever(c)
end end
elseif incomment then elseif incomment then
if inlongcomment and (c == "}") then if ((inlongcomment or incompdirec) and (c == "}"))
-- long comment ends or (inlongcomment_alt == 1) then
-- long comment/(alternative)/compiler directive ends
tex.write(c) tex.write(c)
tex.sprint(tex.ctxcatcodes, "\}") tex.sprint(tex.ctxcatcodes, "\}")
state = buffers.finishstate(state) state = buffers.finishstate(state)
incompdirec = false
inlongcomment = false inlongcomment = false
inlongcomment_alt = 0
incomment = false incomment = false
elseif (inlongcomment_alt == 2) and (c == "*") and (nextc == ")") then
-- long comment (alternative) ends after nextc
tex.write(c)
inlongcomment_alt = 1
else else
-- inside the comment -- inside the comment
flush_whatever(c) flush_whatever(c)
@ -201,15 +208,11 @@ function visualizer.flush_line(str, nested)
word = word .. c word = word .. c
else else
-- number -- number
if not incomment then
state = buffers.changestate(3, state) state = buffers.changestate(3, state)
end
tex.write(c) tex.write(c)
end end
else else
if not incomment then
state = buffers.finishstate(state) state = buffers.finishstate(state)
end
-- identifier complete, check if it's a reserved word and flush -- identifier complete, check if it's a reserved word and flush
flush_pas_word(word) flush_pas_word(word)
word = nil word = nil
@ -221,35 +224,40 @@ function visualizer.flush_line(str, nested)
tex.sprint(tex.ctxcatcodes,rep("\\obs ", buffers.visualizers.tablength)) tex.sprint(tex.ctxcatcodes,rep("\\obs ", buffers.visualizers.tablength))
end end
elseif c == "'" then elseif c == "'" then
if not incomment then -- "not instring" not necessary
-- string starts -- string starts
instring = true instring = true
state = buffers.changestate(3, state) state = buffers.changestate(3, state)
end
tex.write(c) tex.write(c)
elseif (c == "/") and (nextc == "/") then elseif (c == "/") and (nextc == "/") then
if not incomment then
-- one-line comment starts -- one-line comment starts
incomment = true incomment = true
state = buffers.changestate(3, state) state = buffers.changestate(3, state)
tex.sprint(tex.ctxcatcodes, "\{\\bf") tex.sprint(tex.ctxcatcodes, "\{\\bf")
end
tex.write(c) tex.write(c)
elseif c == "{" then elseif c == "{" then
if not incomment then
-- long comment starts
incomment = true incomment = true
if nextc == "$" then
-- compiler directive starts
incompdirec = true
state = buffers.changestate(1, state)
else
-- long comment starts
inlongcomment = true inlongcomment = true
state = buffers.changestate(3, state) state = buffers.changestate(3, state)
tex.sprint(tex.ctxcatcodes, "\{\\bf")
end end
tex.sprint(tex.ctxcatcodes, "\{\\bf")
tex.write(c) 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 else
-- symbol -- symbol
if not incomment then
state = buffers.changestate(1, state) state = buffers.changestate(1, state)
end
tex.write(c) tex.write(c)
end end
end end
@ -265,7 +273,7 @@ function visualizer.flush_line(str, nested)
-- end the comment-line -- end the comment-line
tex.sprint(tex.ctxcatcodes, "\}") tex.sprint(tex.ctxcatcodes, "\}")
end end
state = buffers.finishstate(state)
--[[ --[[
local code, comment = string.match(str,"^(.-)%-%-%[%[(.*)$") local code, comment = string.match(str,"^(.-)%-%-%[%[(.*)$")