buff-imp-mp.lua /size: 5136 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['buff-imp-mp'] = {
2    version   = 1.001,
3    comment   = "companion to buff-imp-mp.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-- Now that we also use lpeg lexers in scite, we can share the keywords
10-- so we have moved the keyword lists to mult-mps.lua. Don't confuse the
11-- scite lexers with the ones we use here. Of course all those lexers
12-- boil down to doing similar things, but here we need more control over
13-- the rendering and have a different way of nesting. It is no coincidence
14-- that the coloring looks similar: both are derived from earlier lexing (in
15-- texedit, mkii and the c++ scite lexer).
16--
17-- In the meantime we have lpeg based lexers in scite! And, as all this
18-- lexing boils down to the same principles (associating symbolic rendering
19-- with ranges of characters) and as the scite lexers do nesting, it makes
20-- sense at some point to share code. However, keep in mind that the pretty
21-- printers are also supposed to support invalid code (for educational
22-- purposes). The scite lexers are more recent and there a different color
23-- scheme is used. So, we might move away from the traditional coloring.
24
25local P, S, V, patterns = lpeg.P, lpeg.S, lpeg.V, lpeg.patterns
26
27local context                      = context
28local verbatim                     = context.verbatim
29local makepattern                  = visualizers.makepattern
30
31local MetapostSnippet              = context.MetapostSnippet
32local startMetapostSnippet         = context.startMetapostSnippet
33local stopMetapostSnippet          = context.stopMetapostSnippet
34
35local MetapostSnippetConstructor   = verbatim.MetapostSnippetConstructor
36local MetapostSnippetBoundary      = verbatim.MetapostSnippetBoundary
37local MetapostSnippetSpecial       = verbatim.MetapostSnippetSpecial
38local MetapostSnippetComment       = verbatim.MetapostSnippetComment
39local MetapostSnippetCommentText   = verbatim.MetapostSnippetCommentText
40local MetapostSnippetQuote         = verbatim.MetapostSnippetQuote
41local MetapostSnippetString        = verbatim.MetapostSnippetString
42local MetapostSnippetNamePrimitive = verbatim.MetapostSnippetNamePrimitive
43local MetapostSnippetNamePlain     = verbatim.MetapostSnippetNamePlain
44local MetapostSnippetNameMetafun   = verbatim.MetapostSnippetNameMetafun
45local MetapostSnippetName          = verbatim.MetapostSnippetName
46
47local primitives, plain, metafun
48
49local function initialize()
50    local mps = dofile(resolvers.findfile("mult-mps.lua","tex")) or {
51        primitives = { },
52        plain      = { },
53        metafun    = { },
54    }
55    primitives = table.tohash(mps.primitives)
56    plain      = table.tohash(mps.plain)
57    metafun    = table.tohash(mps.metafun)
58end
59
60local function visualizename(s)
61    if not primitives then
62        initialize()
63    end
64    if primitives[s] then
65        MetapostSnippetNamePrimitive(s)
66    elseif plain[s] then
67        MetapostSnippetNamePlain(s)
68    elseif metafun[s] then
69        MetapostSnippetNameMetafun(s)
70    else
71        MetapostSnippetName(s)
72    end
73end
74
75local handler = visualizers.newhandler {
76    startinline  = function() MetapostSnippet(false,"{") end,
77    stopinline   = function() context("}") end,
78    startdisplay = function() startMetapostSnippet() end,
79    stopdisplay  = function() stopMetapostSnippet() end ,
80    constructor  = function(s) MetapostSnippetConstructor(s) end,
81    boundary     = function(s) MetapostSnippetBoundary(s) end,
82    special      = function(s) MetapostSnippetSpecial(s) end,
83    comment      = function(s) MetapostSnippetComment(s) end,
84    commenttext  = function(s) MetapostSnippetCommentText(s) end,
85    string       = function(s) MetapostSnippetString(s) end,
86    quote        = function(s) MetapostSnippetQuote(s) end,
87    name         = visualizename,
88}
89
90local comment     = P("%")
91local name        = (patterns.letter + S("_"))^1
92local constructor = S("$@#")
93local boundary    = S('()[]:=<>;"')
94local special     = S("-+/*|`!?^&%.,")
95
96local grammar = visualizers.newgrammar("default", { "visualizer",
97
98    comment     = makepattern(handler,"comment",comment)
99                * makepattern(handler,"commenttext",(patterns.anything - patterns.newline)^0),
100    dstring     = makepattern(handler,"quote",patterns.dquote)
101                * makepattern(handler,"string",patterns.nodquote)
102                * makepattern(handler,"quote",patterns.dquote),
103    name        = makepattern(handler,"name",name),
104    constructor = makepattern(handler,"constructor",constructor),
105    boundary    = makepattern(handler,"boundary",boundary),
106    special     = makepattern(handler,"special",special),
107
108    pattern     =
109        V("comment") + V("dstring") + V("name") + V("constructor") + V("boundary") + V("special")
110      + V("newline") * V("emptyline")^0 * V("beginline")
111      + V("space")
112      + V("default"),
113
114    visualizer  =
115        V("pattern")^1
116
117} )
118
119local parser = P(grammar)
120
121visualizers.register("mp", { parser = parser, handler = handler, grammar = grammar } )
122