chem-ini.lua /size: 1838 b    last modification: 2023-12-21 09:44
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-- The next code started out as adaptation of code from Wolfgang Schuster as posted
23-- on the mailing list. The current version supports nested braces and unbraced
24-- integers as scripts.
25
26local moleculeparser     = cpatterns.scripted
27chemistry.moleculeparser = moleculeparser
28
29function chemistry.molecule(str)
30    return lpegmatch(moleculeparser,str)
31end
32
33interfaces.implement {
34    name      = "molecule",
35    arguments = "string",
36    actions   = function(str)
37        if trace_molecules then
38            local rep = lpegmatch(moleculeparser,str)
39            report_chemistry("molecule %a becomes %a",str,rep)
40            context(rep)
41        else
42            context(lpegmatch(moleculeparser,str))
43        end
44    end,
45}
46
47-- interfaces.implement {
48--     name      = "molecule",
49--     scope     = "private",
50--     action    = function()
51--         local str = scanstring()
52--         if trace_molecules then
53--             local rep = lpegmatch(moleculeparser,str)
54--             report_chemistry("molecule %a becomes %a",str,rep)
55--             context(rep)
56--         else
57--             context(lpegmatch(moleculeparser,str))
58--         end
59--     end,
60-- }
61