buff-imp-lua.lua /size: 7729 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['buff-imp-lua'] = {
2    version   = 1.001,
3    comment   = "companion to buff-imp-lua.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- borrowed from ctx scite lexers
10-- add goto/label scanning
11--
12-- deprecated:
13--
14-- gcinfo unpack getfenv setfenv loadlib
15-- table.maxn table.getn table.setn
16-- math.log10 math.mod math.modf math.fmod
17
18local format, tohash = string.format, table.tohash
19local P, S, V, patterns = lpeg.P, lpeg.S, lpeg.V, lpeg.patterns
20local C, Cs, Cg, Cb, Cmt, Carg = lpeg.C, lpeg.Cs, lpeg.Cg, lpeg.Cb, lpeg.Cmt, lpeg.Carg
21
22local core = tohash {
23    "and", "break", "do", "else", "elseif", "end", "false", "for", "function",
24    "if", "in", "local", "nil", "not", "or", "repeat", "return", "then",
25    "true", "until", "while"
26}
27
28local base = tohash {
29    "assert", "collectgarbage", "dofile", "error", "loadfile",
30    "loadstring", "load", "print", "rawget", "rawset", "require", "tonumber",
31    "tostring", "type", "_G", "getmetatable", "ipairs", "next", "pairs",
32    "pcall", "rawequal", "setmetatable", "xpcall", "module", "select", "goto",
33}
34
35local libraries = {
36    coroutine = tohash {
37        "create", "resume", "status", "wrap", "yield", "running",
38    },
39    package = tohash{
40        "cpath", "loaded", "loadlib", "path", "config", "preload", "seeall",
41    },
42    io = tohash{
43        "close", "flush", "input", "lines", "open", "output", "read", "tmpfile",
44        "type", "write", "stdin", "stdout", "stderr", "popen",
45    },
46    math = tohash{
47        "abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "deg", "exp",
48        "floor ", "ldexp", "log", "max", "min", "pi", "pow", "rad", "random",
49        "randomseed", "sin", "sqrt", "tan", "cosh", "sinh", "tanh", "huge",
50    },
51    string = tohash{
52        "byte", "char", "dump", "find", "len", "lower", "rep", "sub", "upper",
53        "format", "gfind", "gsub", "gmatch", "match", "reverse",
54    },
55    table = tohash{
56        "concat", "foreach", "foreachi", "sort", "insert", "remove", "pack",
57        "unpack",
58    },
59    os = tohash{
60        "clock", "date", "difftime", "execute", "exit", "getenv", "remove",
61        "rename", "setlocale", "time", "tmpname",
62    },
63    lpeg = tohash{
64        "print", "match", "locale", "type", "version", "setmaxstack",
65        "P", "R", "S", "C", "V", "Cs", "Ct", "Cs", "Cc", "Cp", "Carg",
66        "Cg", "Cb", "Cmt", "Cf", "B",
67    },
68    -- bit
69    -- debug
70}
71
72local context                 = context
73local verbatim                = context.verbatim
74local makepattern             = visualizers.makepattern
75
76local LuaSnippet              = context.LuaSnippet
77local startLuaSnippet         = context.startLuaSnippet
78local stopLuaSnippet          = context.stopLuaSnippet
79
80local LuaSnippetBoundary      = verbatim.LuaSnippetBoundary
81local LuaSnippetQuote         = verbatim.LuaSnippetQuote
82local LuaSnippetString        = verbatim.LuaSnippetString
83local LuaSnippetSpecial       = verbatim.LuaSnippetSpecial
84local LuaSnippetComment       = verbatim.LuaSnippetComment
85local LuaSnippetCommentText   = verbatim.LuaSnippetCommentText
86local LuaSnippetNameCore      = verbatim.LuaSnippetNameCore
87local LuaSnippetNameBase      = verbatim.LuaSnippetNameBase
88local LuaSnippetNameLibraries = verbatim.LuaSnippetNameLibraries
89local LuaSnippetName          = verbatim.LuaSnippetName
90
91local namespace
92
93local function visualizename_a(s)
94    if core[s] then
95        namespace = nil
96        LuaSnippetNameCore(s)
97    elseif base[s] then
98        namespace = nil
99        LuaSnippetNameBase(s)
100    else
101        namespace = libraries[s]
102        if namespace then
103            LuaSnippetNameLibraries(s)
104        else
105            LuaSnippetName(s)
106        end
107    end
108end
109
110local function visualizename_b(s)
111    if namespace and namespace[s] then
112        namespace = nil
113        LuaSnippetNameLibraries(s)
114    else
115        LuaSnippetName(s)
116    end
117end
118
119local function visualizename_c(s)
120    LuaSnippetName(s)
121end
122
123local handler = visualizers.newhandler {
124    startinline  = function() LuaSnippet(false,"{") end,
125    stopinline   = function() context("}") end,
126    startdisplay = function() startLuaSnippet() end,
127    stopdisplay  = function() stopLuaSnippet() end ,
128    boundary     = function(s) LuaSnippetBoundary(s) end,
129    special      = function(s) LuaSnippetSpecial(s) end,
130    comment      = function(s) LuaSnippetComment(s) end,
131    commenttext  = function(s) LuaSnippetCommentText(s) end,
132    quote        = function(s) LuaSnippetQuote(s) end,
133    string       = function(s) LuaSnippetString(s) end,
134    period       = function(s) verbatim(s) end,
135    name_a       = visualizename_a,
136    name_b       = visualizename_b,
137    name_c       = visualizename_c,
138}
139
140----- comment     = P("--")
141local comment     = P("--") * (patterns.anything - patterns.newline)^0
142local comment_lb  = P("--[[")
143local comment_le  = P("--]]")
144local comment_lt  = patterns.utf8char - comment_le - patterns.newline
145
146local name        = (patterns.letter + patterns.underscore)
147                  * (patterns.letter + patterns.underscore + patterns.digit)^0
148local boundary    = S('()[]{}')
149local special     = S("-+/*^%=#~|<>") + P("..")
150
151-- The following longstring parser is taken from Roberto's documentation
152-- that can be found at http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html.
153
154local equals      = P("=")^0
155local open        = P("[") * Cg(equals, "init") * P("[") * P("\n")^-1 -- maybe better: patterns.newline^-1
156local close       = P("]") * C(equals) * P("]")
157local closeeq     = Cmt(close * Cb("init"), function(s,i,a,b) return a == b end) -- wrong return value
158local longstring  = open * Cs((P(1) - closeeq)^0) * close * Carg(1)
159
160local function long(content,equals,settings)
161    handler.boundary(format("[%s[",equals or ""))
162    visualizers.write(content,settings) -- unhandled
163    handler.boundary(format("]%s]",equals or ""))
164end
165
166local grammar = visualizers.newgrammar("default", { "visualizer",
167    sstring =
168        makepattern(handler,"quote",patterns.dquote)
169      * (V("whitespace") + makepattern(handler,"string",(1-patterns.dquote-V("whitespace"))^1))^0 -- patterns.nodquote
170      * makepattern(handler,"quote",patterns.dquote),
171    dstring =
172        makepattern(handler,"quote",patterns.squote)
173      * (V("whitespace") + makepattern(handler,"string",(1-patterns.squote-V("whitespace"))^1))^0 -- patterns.nosquote
174      * makepattern(handler,"quote",patterns.squote),
175    longstring =
176        longstring / long,
177    comment =
178        makepattern(handler, "comment", comment_lb)
179      * (   makepattern(handler, "commenttext", comment_lt)
180          + V("whitespace")
181        )^0
182      * makepattern(handler, "comment", comment_le)
183      + makepattern(handler,"comment",comment),
184    name =
185        makepattern(handler,"name_a",name)
186      * (   V("optionalwhitespace")
187          * makepattern(handler,"default",patterns.period)
188          * V("optionalwhitespace")
189          * makepattern(handler,"name_b",name)
190        )^-1
191      * (   V("optionalwhitespace")
192          * makepattern(handler,"default",patterns.period)
193          * V("optionalwhitespace")
194          * makepattern(handler,"name_c",name)
195        )^0,
196
197    pattern =
198        V("comment")
199      + V("longstring")
200      + V("dstring")
201      + V("sstring")
202      + V("name")
203      + makepattern(handler,"boundary",boundary)
204      + makepattern(handler,"special",special)
205
206      + V("space")
207      + V("line")
208      + V("default"),
209
210    visualizer =
211        V("pattern")^1
212} )
213
214local parser = P(grammar)
215
216visualizers.register("lua", { parser = parser, handler = handler, grammar = grammar } )
217