- Updated and changed a lot: strings, //-comments and {..}-comments working

- Added local function flush_whatever to handle spaces/tabs properly
This commit is contained in:
Stefan Müller 2010-10-01 20:47:24 +02:00
parent 1a58b65f42
commit cb7d7e850c
1 changed files with 134 additions and 58 deletions

View File

@ -64,7 +64,7 @@ local states = {
local function flush_pas_word(word) local function flush_pas_word(word)
if word then if word then
local id = known_words[word] local id = known_words[string.lower(word)]
if id then if id then
tex.sprint(tex.ctxcatcodes, "\{\\bf ") tex.sprint(tex.ctxcatcodes, "\{\\bf ")
tex.write(word) tex.write(word)
@ -75,6 +75,23 @@ local function flush_pas_word(word)
end end
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 local inlongcomment, incompdirec, inasm = false, false, false
function visualizer.reset() function visualizer.reset()
@ -98,8 +115,9 @@ end
end]] end]]
function visualizer.flush_line(str, nested) function visualizer.flush_line(str, nested)
local state, word, lastc = 0, nil, nil local state, word = 0, nil
local incomment = false local incomment, instring = false, false
local code, comment = nil, nil
buffers.currentcolors = colors buffers.currentcolors = colors
--[[ --[[
@ -115,11 +133,62 @@ function visualizer.flush_line(str, nested)
end end
until not identifier until not identifier
]] ]]
--state = buffers.changestate(1, state)
--TODO: handle inlongcomment etc. --TODO: handle inlongcomment etc.
for c in string.utfcharacters(str) do if inlongcomment then
if string.find(c, "^[%a_]$") 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
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 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 -- char belongs to identifier
if word then if word then
word = word .. c word = word .. c
@ -141,27 +210,31 @@ function visualizer.flush_line(str, nested)
if not incomment then if not incomment then
state = buffers.finishstate(state) state = buffers.finishstate(state)
end end
-- identifier complete, check if it's a reserved word and flush
flush_pas_word(word) flush_pas_word(word)
word = nil word = nil
if c == " " then if c == " " then
--state = buffers.finishstate(state)
tex.sprint(tex.ctxcatcodes, "\\obs") tex.sprint(tex.ctxcatcodes, "\\obs")
elseif c == "\t" then elseif c == "\t" then
--state = buffers.finishstate(state)
tex.sprint(tex.ctxcatcodes, "\\obs") tex.sprint(tex.ctxcatcodes, "\\obs")
if buffers.visualizers.enabletab then if buffers.visualizers.enabletab then
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 lastc == "/" 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 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 end
tex.write("//") tex.write(c)
end
elseif c == "{" then elseif c == "{" then
if not incomment then if not incomment then
-- long comment starts -- long comment starts
@ -171,7 +244,7 @@ function visualizer.flush_line(str, nested)
tex.sprint(tex.ctxcatcodes, "\{\\bf") tex.sprint(tex.ctxcatcodes, "\{\\bf")
end end
tex.write(c) tex.write(c)
-- TODO: (*, asm, {$, ' -- TODO: (*, asm, {$
else else
-- symbol -- symbol
if not incomment then if not incomment then
@ -180,16 +253,19 @@ function visualizer.flush_line(str, nested)
tex.write(c) tex.write(c)
end end
end end
lastc = c end
c = nextc
end end
if not incomment then if not incomment then
state = buffers.finishstate(state) state = buffers.finishstate(state)
end end
-- maybe something left, check if it's a reserved word and flush
flush_pas_word(word) flush_pas_word(word)
if incomment then if incomment then
-- end the comment-line
tex.sprint(tex.ctxcatcodes, "\}") tex.sprint(tex.ctxcatcodes, "\}")
end end
--word = nil
--[[ --[[
local code, comment = string.match(str,"^(.-)%-%-%[%[(.*)$") local code, comment = string.match(str,"^(.-)%-%-%[%[(.*)$")