chem-ini.lua /size: 1846 b    last modification: 2020-07-01 14:35
1
if
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 9
local
format
=
string
.
format
10
local
lpegmatch
,
patterns
=
lpeg
.
match
,
lpeg
.
patterns
11 12
local
trace_molecules
=
false
trackers
.
register
(
"
chemistry.molecules
"
,
function
(
v
)
trace_molecules
=
v
end
)
13 14
local
report_chemistry
=
logs
.
reporter
(
"
chemistry
"
)
15 16
local
context
=
context
17
local
cpatterns
=
patterns
.
context
18 19
chemistry
=
chemistry
or
{
}
20
local
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 28
local
moleculeparser
=
cpatterns
.
scripted
29
chemistry
.
moleculeparser
=
moleculeparser
30 31
function
chemistry
.
molecule
(
str
)
32
return
lpegmatch
(
moleculeparser
,
str
)
33
end
34 35
interfaces
.
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