From 21a95c79864a97aa2fe4829227d8318d3a18ee54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Sat, 2 Oct 2010 15:23:10 +0200 Subject: [PATCH] - Added: pretty printing of asm code blocks --- pret-pas.lua | 80 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/pret-pas.lua b/pret-pas.lua index 57c76eb..d8f92a8 100644 --- a/pret-pas.lua +++ b/pret-pas.lua @@ -68,17 +68,37 @@ local states = { local reserved_style = "\{\\bf " local comment_style = "\{\\sl " -local function flush_pas_word(word) +local inlongcomment, inlongcomment_alt, incompdirec, inasm = false, 0, false, false + +local function flush_pas_word(word, state) if word then - local id = known_words[string.lower(word)] + local lword = string.lower(word) + local id = known_words[lword] if id then - tex.sprint(tex.ctxcatcodes, reserved_style) - tex.write(word) - tex.sprint(tex.ctxcatcodes, "\}") + if inasm and (lword == "end") then + -- asm mode ends + state = buffers.finishstate(state) + inasm = false + print("leave asm") + end + if not inasm then + tex.sprint(tex.ctxcatcodes, reserved_style) + tex.write(word) + tex.sprint(tex.ctxcatcodes, "\}") + if lword == "asm" then + -- asm mode begins + print("enter asm") + inasm = true + state = buffers.changestate(2, state) + end + else + tex.write(word) + end else tex.write(word) end end + return state end local function flush_whatever(str) @@ -98,8 +118,6 @@ local function flush_whatever(str) end end -local inlongcomment, inlongcomment_alt, incompdirec, inasm = false, 0, false, false - function visualizer.reset() inlongcomment, inlongcomment_alt, incompdirec, inasm = false, 0, false, false end @@ -167,6 +185,8 @@ function visualizer.flush_line(str, nested) state = buffers.changestate(3, state) end tex.sprint(tex.ctxcatcodes, comment_style) + elseif inasm then + state = buffers.changestate(2, state) end local c, word = nil, nil @@ -188,7 +208,12 @@ function visualizer.flush_line(str, nested) -- long comment/(alternative)/compiler directive ends tex.write(c) tex.sprint(tex.ctxcatcodes, "\}") - state = buffers.finishstate(state) + if inasm then + -- resume to asm mode + state = buffers.changestate(2, state) + else + state = buffers.finishstate(state) + end incompdirec = false inlongcomment = false inlongcomment_alt = 0 @@ -213,14 +238,18 @@ function visualizer.flush_line(str, nested) -- number, that belongs to identifier word = word .. c else - -- number - state = buffers.changestate(3, state) + if not inasm then + -- number + state = buffers.changestate(3, state) + end tex.write(c) end else - state = buffers.finishstate(state) + if not inasm then + state = buffers.finishstate(state) + end -- identifier complete, check if it's a reserved word and flush - flush_pas_word(word) + state = flush_pas_word(word, state) word = nil if c == " " then tex.sprint(tex.ctxcatcodes, "\\obs") @@ -230,12 +259,14 @@ function visualizer.flush_line(str, nested) tex.sprint(tex.ctxcatcodes,rep("\\obs ", buffers.visualizers.tablength)) end elseif c == "'" then - -- string starts - instring = true - state = buffers.changestate(3, state) + if not inasm then + -- string begins + instring = true + state = buffers.changestate(3, state) + end tex.write(c) elseif (c == "/") and (nextc == "/") then - -- one-line comment starts + -- one-line comment begins incomment = true state = buffers.changestate(3, state) tex.sprint(tex.ctxcatcodes, comment_style) @@ -243,38 +274,39 @@ function visualizer.flush_line(str, nested) elseif c == "{" then incomment = true if nextc == "$" then - -- compiler directive starts + -- compiler directive begins incompdirec = true state = buffers.changestate(1, state) else - -- long comment starts + -- long comment begins inlongcomment = true state = buffers.changestate(3, state) end tex.sprint(tex.ctxcatcodes, comment_style) tex.write(c) elseif (c == "(") and (nextc == "*") then - -- long comment (alternative) starts + -- long comment (alternative) begins incomment = true inlongcomment_alt = 2 state = buffers.changestate(3, state) tex.sprint(tex.ctxcatcodes, comment_style) tex.write(c) - -- TODO: asm else - -- symbol - state = buffers.changestate(1, state) + if not inasm then + -- symbol + state = buffers.changestate(1, state) + end tex.write(c) end end end c = nextc end - if not incomment then + if not incomment and not inasm then state = buffers.finishstate(state) end -- maybe something left, check if it's a reserved word and flush - flush_pas_word(word) + state = flush_pas_word(word, state) if incomment then -- end the comment-line tex.sprint(tex.ctxcatcodes, "\}")