summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/pret-lua.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/pret-lua.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/pret-lua.lua147
1 files changed, 103 insertions, 44 deletions
diff --git a/Master/texmf-dist/tex/context/base/pret-lua.lua b/Master/texmf-dist/tex/context/base/pret-lua.lua
index 3faf8108142..8803971f3eb 100644
--- a/Master/texmf-dist/tex/context/base/pret-lua.lua
+++ b/Master/texmf-dist/tex/context/base/pret-lua.lua
@@ -1,11 +1,14 @@
if not modules then modules = { } end modules ['pret-lua'] = {
version = 1.001,
- comment = "companion to buff-ver.tex",
+ comment = "companion to buff-ver.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
+-- this is not a real parser as we also want to typeset wrong output
+-- and a real parser would choke on that
+
local utf = unicode.utf8
local utfcharacters, utfvalues = string.utfcharacters, string.utfvalues
@@ -19,6 +22,12 @@ local visualizer = buffers.newvisualizer("lua")
visualizer.identifiers = { }
-- borrowed from scite
+--
+-- depricated:
+--
+-- gcinfo unpack getfenv setfenv loadlib
+-- table.maxn table.getn table.setn
+-- math.log10 math.mod math.modf math.fmod
visualizer.identifiers.core = {
"and", "break", "do", "else", "elseif", "end", "false", "for", "function",
@@ -27,15 +36,15 @@ visualizer.identifiers.core = {
}
visualizer.identifiers.base = {
- "assert", "collectgarbage", "dofile", "error", "gcinfo", "loadfile",
+ "assert", "collectgarbage", "dofile", "error", "loadfile",
"loadstring", "print", "rawget", "rawset", "require", "tonumber",
- "tostring", "type", "unpack",
+ "tostring", "type",
}
visualizer.identifiers.five = {
- "_G", "getfenv", "getmetatable", "ipairs", "loadlib", "next", "pairs",
- "pcall", "rawequal", "setfenv", "setmetatable", "xpcall", "string", "table",
- "math", "coroutine", "io", "os", "debug", "load", "module", "select"
+ "_G", "getmetatable", "ipairs", "next", "pairs",
+ "pcall", "rawequal", "setmetatable", "xpcall", "string", "table",
+ "math", "coroutine", "io", "os", "debug", "load", "module", "select",
}
visualizer.identifiers.libs = {
@@ -43,7 +52,7 @@ visualizer.identifiers.libs = {
"coroutine.create", "coroutine.resume", "coroutine.status", "coroutine.wrap",
"coroutine.yield", "coroutine.running",
-- package
- "package.cpath", "package.loaded", "package.loadlib", "package.path",
+ "package.cpath", "package.loaded", "package.loadlib", "package.path", "package.config",
-- io
"io.close", "io.flush", "io.input", "io.lines", "io.open", "io.output",
"io.read", "io.tmpfile", "io.type", "io.write", "io.stdin", "io.stdout",
@@ -51,28 +60,31 @@ visualizer.identifiers.libs = {
-- math
"math.abs", "math.acos", "math.asin", "math.atan", "math.atan2", "math.ceil",
"math.cos", "math.deg", "math.exp", "math.floor math.", "math.ldexp",
- "math.log", "math.log10", "math.max", "math.min math.mod math.pi", "math.pow",
+ "math.log", "math.max", "math.min", "math.pi", "math.pow",
"math.rad", "math.random", "math.randomseed", "math.sin", "math.sqrt",
- "math.tan", "math.cosh", "math.fmod", "math.modf", "math.sinh", "math.tanh",
+ "math.tan", "math.cosh", "math.sinh", "math.tanh",
"math.huge",
-- string
"string.byte", "string.char", "string.dump", "string.find", "string.len",
"string.lower", "string.rep", "string.sub", "string.upper", "string.format",
"string.gfind", "string.gsub", "string.gmatch", "string.match", "string.reverse",
-- table
- "table.maxn", "table.concat", "table.foreach", "table.foreachi", "table.getn",
- "table.sort", "table.insert", "table.remove", "table.setn",
+ "table.concat", "table.foreach", "table.foreachi",
+ "table.sort", "table.insert", "table.remove",
+ "table.pack", "table.unpack",
-- os
"os.clock", "os.date", "os.difftime", "os.execute", "os.exit", "os.getenv",
"os.remove", "os.rename", "os.setlocale", "os.time", "os.tmpname",
-- package
- "package.preload", "package.seeall"
+ "package.preload", "package.seeall",
+ -- bit
+ -- ... todo ...
}
local known_words = { }
-for k,v in pairs(visualizer.identifiers) do
- for _,w in pairs(v) do
+for k,v in next, visualizer.identifiers do
+ for _,w in next, v do
known_words[w] = k
end
end
@@ -131,6 +143,22 @@ end
-- we will also provide a proper parser based pretty printer although normaly
-- a pretty printer should handle faulty code too (educational purposes)
+local function written(state,c,i)
+ if c == " " then
+ state = finish_state(state)
+ texsprint(ctxcatcodes,"\\obs")
+ elseif c == "\t" then
+ state = finish_state(state)
+ texsprint(ctxcatcodes,"\\obs")
+ if buffers.visualizers.enabletab then
+ texsprint(ctxcatcodes,rep("\\obs ",i%buffers.visualizers.tablength))
+ end
+ else
+ texwrite(c)
+ end
+ return state, 0
+end
+
function visualizer.flush_line(str, nested)
local state, instr, inesc, word = 0, false, false, nil
buffers.currentcolors = colors
@@ -162,9 +190,38 @@ function visualizer.flush_line(str, nested)
if pre then
code = pre
end
- local p, s = nil, nil
+ local p, s, i = nil, nil, 0
for c in utfcharacters(code) do
- if c == "[" then
+ i = i + 1
+ if instr then
+ if p then
+ texwrite(p)
+ p = nil
+ end
+ if c == s then
+ if inesc then
+ texwrite(c)
+ inesc = false
+ else
+ state = change_state(states[c],state)
+ instr = false
+ texwrite(c)
+ state = finish_state(state)
+ end
+ s = nil
+ else
+ if c == "\\" then
+ inesc = not inesc
+ else
+ inesc = false
+ end
+ state, i = written(state,c,i)
+ end
+ elseif c == "[" then
+ if word then
+ texwrite(word)
+ word = nil
+ end
if p == "[" then
inlongstring = true
state = change_state(states["[["],state)
@@ -172,9 +229,16 @@ function visualizer.flush_line(str, nested)
state = finish_state(state)
p = nil
else
+ if p then
+ state, i = written(state,p,i)
+ end
p = c
end
elseif c == "]" then
+ if word then
+ texwrite(word)
+ word = nil
+ end
if p == "]" then
inlongstring = false
state = change_state(states["]]"],state)
@@ -182,47 +246,34 @@ function visualizer.flush_line(str, nested)
state = finish_state(state)
p = nil
else
+ if p then
+ state, i = written(state,p,i)
+ end
p = c
end
else
if p then
- state = change_state(states[c],state)
- texwrite(p,c)
+ state = change_state(states[p],state)
+ texwrite(p)
state = finish_state(state)
p = nil
end
- if c == " " then
+ if c == " " or c == "\t" then
if word then
state = flush_lua_word(state,word)
word = nil
end
- texsprint(ctxcatcodes,"\\obs")
+ state, i = written(state,c,i)
elseif inlongstring then
- texwrite(c)
- elseif instr then
- if c == s then
- if inesc then
- texwrite(c)
- inesc = false
- else
- state = change_state(states[c],state)
- instr = false
- texwrite(c)
- state = finish_state(state)
- end
- s = nil
- else
- if c == "\\" then
- inesc = not inesc
- else
- inesc = false
- end
- texwrite(c)
- end
+ state, i = written(state,c,i)
elseif c == '"' or c == "'" then
+if word then
+ state = flush_lua_word(state,word)
+ word = nil
+end
instr = true
state = change_state(states[c],state)
- texwrite(c)
+ state, i = written(state,c,i)
state = finish_state(state)
s = c
elseif find(c,"^[%a]$") then
@@ -239,13 +290,18 @@ function visualizer.flush_line(str, nested)
end
end
end
+ if p then
+ texwrite(p)
+ -- state, i = written(state,p,i)
+ p = nil
+ end
state = flush_lua_word(state,word)
if post then
state = change_state(states['--'], state)
texwrite("--")
state = finish_state(state)
for c in utfcharacters(post) do
- if c == " " then texsprint(ctxcatcodes,"\\obs") else texwrite(c) end
+ state, i = written(state,c,i)
end
end
end
@@ -254,7 +310,10 @@ function visualizer.flush_line(str, nested)
state = change_state(states['--'], state)
texwrite("[[")
state = finish_state(state)
- texwrite(comment)
+ -- texwrite(comment) -- maybe also split and
+ for c in utfcharacters(comment) do
+ state, i = written(state,c,i)
+ end
end
state = finish_state(state)
end