- 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:
parent
1a58b65f42
commit
cb7d7e850c
192
pret-pas.lua
192
pret-pas.lua
|
@ -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,81 +133,139 @@ 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,"^(.-})(.*)$")
|
||||||
-- char belongs to identifier
|
if not comment then
|
||||||
if word then
|
comment = str
|
||||||
word = word .. c
|
end
|
||||||
else
|
state = buffers.changestate(3, state)
|
||||||
word = c
|
tex.sprint(tex.ctxcatcodes, "\{\\bf")
|
||||||
end
|
flush_whatever(comment)
|
||||||
elseif string.find(c, "^[%d]$") then
|
tex.sprint(tex.ctxcatcodes, "\}")
|
||||||
if word and (#word > 1) then
|
if code then
|
||||||
-- number, that belongs to identifier
|
inlongcomment = false
|
||||||
word = word .. c
|
|
||||||
else
|
|
||||||
-- number
|
|
||||||
if not incomment then
|
|
||||||
state = buffers.changestate(3, state)
|
|
||||||
end
|
|
||||||
tex.write(c)
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
if not incomment then
|
code = ""
|
||||||
state = buffers.finishstate(state)
|
end
|
||||||
end
|
else
|
||||||
flush_pas_word(word)
|
code = str
|
||||||
word = nil
|
end
|
||||||
if c == " " then
|
|
||||||
--state = buffers.finishstate(state)
|
local c = nil
|
||||||
tex.sprint(tex.ctxcatcodes, "\\obs")
|
--[[
|
||||||
elseif c == "\t" then
|
for nextc in string.utfcharacters(code .. " ") do
|
||||||
--state = buffers.finishstate(state)
|
if firstrun then
|
||||||
tex.sprint(tex.ctxcatcodes, "\\obs")
|
firstrun = false
|
||||||
if buffers.visualizers.enabletab then
|
else
|
||||||
tex.sprint(tex.ctxcatcodes,rep("\\obs ", buffers.visualizers.tablength))
|
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
|
end
|
||||||
elseif c == "/" then
|
elseif incomment then
|
||||||
if lastc == "/" 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
|
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)
|
||||||
|
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
|
||||||
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
|
||||||
end
|
end
|
||||||
lastc = c
|
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,"^(.-)%-%-%[%[(.*)$")
|
||||||
|
|
Loading…
Reference in New Issue