chem-ini.lua /size: 1846 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['chem-ini'] = {
2    version   = 1.001,
3    comment   = "companion to chem-ini.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
9local format = string.format
10local lpegmatch, patterns = lpeg.match, lpeg.patterns
11
12local trace_molecules = false  trackers.register("chemistry.molecules",  function(v) trace_molecules = v end)
13
14local report_chemistry = logs.reporter("chemistry")
15
16local context   = context
17local cpatterns = patterns.context
18
19chemistry       = chemistry or { }
20local chemistry = chemistry
21
22--[[
23<p>The next code started out as adaptation of code from Wolfgang Schuster as
24posted on the mailing list. The current version supports nested braces and
25unbraced integers as scripts.</p>
26]]--
27
28local moleculeparser     = cpatterns.scripted
29chemistry.moleculeparser = moleculeparser
30
31function chemistry.molecule(str)
32    return lpegmatch(moleculeparser,str)
33end
34
35interfaces.implement {
36    name      = "molecule",
37    arguments = "string",
38    actions   = function(str)
39        if trace_molecules then
40            local rep = lpegmatch(moleculeparser,str)
41            report_chemistry("molecule %a becomes %a",str,rep)
42            context(rep)
43        else
44            context(lpegmatch(moleculeparser,str))
45        end
46    end,
47}
48
49-- interfaces.implement {
50--     name      = "molecule",
51--     scope     = "private",
52--     action    = function()
53--         local str = scanstring()
54--         if trace_molecules then
55--             local rep = lpegmatch(moleculeparser,str)
56--             report_chemistry("molecule %a becomes %a",str,rep)
57--             context(rep)
58--         else
59--             context(lpegmatch(moleculeparser,str))
60--         end
61--     end,
62-- }
63