summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/buff-imp-mp.lua
blob: daf8187ae4872642b512c751a18beefe724f0c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
if not modules then modules = { } end modules ['buff-imp-mp'] = {
    version   = 1.001,
    comment   = "companion to buff-imp-mp.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- Now that we also use lpeg lexers in scite, we can share the keywords
-- so we have moved the keyword lists to mult-mps.lua. Don't confuse the
-- scite lexers with the ones we use here. Of course all those lexers
-- boil down to doing similar things, but here we need more control over
-- the rendering and have a different way of nesting. It is no coincidence
-- that the coloring looks similar: both are derived from earlier lexing (in
-- texedit, mkii and the c++ scite lexer).
--
-- In the meantime we have lpeg based lexers in scite! And, as all this
-- lexing boils down to the same principles (associating symbolic rendering
-- with ranges of characters) and as the scite lexers do nesting, it makes
-- sense at some point to share code. However, keep in mind that the pretty
-- printers are also supposed to support invalid code (for educational
-- purposes). The scite lexers are more recent and there a different color
-- scheme is used. So, we might move away from the traditional coloring.

local P, S, V, patterns = lpeg.P, lpeg.S, lpeg.V, lpeg.patterns

local context                      = context
local verbatim                     = context.verbatim
local makepattern                  = visualizers.makepattern

local MetapostSnippet              = context.MetapostSnippet
local startMetapostSnippet         = context.startMetapostSnippet
local stopMetapostSnippet          = context.stopMetapostSnippet

local MetapostSnippetConstructor   = verbatim.MetapostSnippetConstructor
local MetapostSnippetBoundary      = verbatim.MetapostSnippetBoundary
local MetapostSnippetSpecial       = verbatim.MetapostSnippetSpecial
local MetapostSnippetComment       = verbatim.MetapostSnippetComment
local MetapostSnippetCommentText   = verbatim.MetapostSnippetCommentText
local MetapostSnippetQuote         = verbatim.MetapostSnippetQuote
local MetapostSnippetString        = verbatim.MetapostSnippetString
local MetapostSnippetNamePrimitive = verbatim.MetapostSnippetNamePrimitive
local MetapostSnippetNamePlain     = verbatim.MetapostSnippetNamePlain
local MetapostSnippetNameMetafun   = verbatim.MetapostSnippetNameMetafun
local MetapostSnippetName          = verbatim.MetapostSnippetName

local primitives, plain, metafun

local function initialize()
    local mps = dofile(resolvers.findfile("mult-mps.lua","tex")) or {
        primitives = { },
        plain      = { },
        metafun    = { },
    }
    primitives = table.tohash(mps.primitives)
    plain      = table.tohash(mps.plain)
    metafun    = table.tohash(mps.metafun)
end

local function visualizename(s)
    if not primitives then
        initialize()
    end
    if primitives[s] then
        MetapostSnippetNamePrimitive(s)
    elseif plain[s] then
        MetapostSnippetNamePlain(s)
    elseif metafun[s] then
        MetapostSnippetNameMetafun(s)
    else
        MetapostSnippetName(s)
    end
end

local handler = visualizers.newhandler {
    startinline  = function() MetapostSnippet(false,"{") end,
    stopinline   = function() context("}") end,
    startdisplay = function() startMetapostSnippet() end,
    stopdisplay  = function() stopMetapostSnippet() end ,
    constructor  = function(s) MetapostSnippetConstructor(s) end,
    boundary     = function(s) MetapostSnippetBoundary(s) end,
    special      = function(s) MetapostSnippetSpecial(s) end,
    comment      = function(s) MetapostSnippetComment(s) end,
    commenttext  = function(s) MetapostSnippetCommentText(s) end,
    string       = function(s) MetapostSnippetString(s) end,
    quote        = function(s) MetapostSnippetQuote(s) end,
    name         = visualizename,
}

local comment     = P("%")
local name        = (patterns.letter + S("_"))^1
local constructor = S("$@#")
local boundary    = S('()[]:=<>;"')
local special     = S("-+/*|`!?^&%.,")

local grammar = visualizers.newgrammar("default", { "visualizer",

    comment     = makepattern(handler,"comment",comment)
                * makepattern(handler,"commenttext",(patterns.anything - patterns.newline)^0),
    dstring     = makepattern(handler,"quote",patterns.dquote)
                * makepattern(handler,"string",patterns.nodquote)
                * makepattern(handler,"quote",patterns.dquote),
    name        = makepattern(handler,"name",name),
    constructor = makepattern(handler,"constructor",constructor),
    boundary    = makepattern(handler,"boundary",boundary),
    special     = makepattern(handler,"special",special),

    pattern     =
        V("comment") + V("dstring") + V("name") + V("constructor") + V("boundary") + V("special")
      + V("newline") * V("emptyline")^0 * V("beginline")
      + V("space")
      + V("default"),

    visualizer  =
        V("pattern")^1

} )

local parser = P(grammar)

visualizers.register("mp", { parser = parser, handler = handler, grammar = grammar } )