diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/chem-ini.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/chem-ini.lua | 56 |
1 files changed, 30 insertions, 26 deletions
diff --git a/Master/texmf-dist/tex/context/base/chem-ini.lua b/Master/texmf-dist/tex/context/base/chem-ini.lua index 90874909207..4d47982a220 100644 --- a/Master/texmf-dist/tex/context/base/chem-ini.lua +++ b/Master/texmf-dist/tex/context/base/chem-ini.lua @@ -6,14 +6,19 @@ if not modules then modules = { } end modules ['chem-ini'] = { license = "see context related readme files" } -local format, texsprint = string.format, tex.sprint +local format = string.format local lpegmatch = lpeg.match +local P, R, V, Cc, Cs = lpeg.P, lpeg.R, lpeg.V, lpeg.Cc, lpeg.Cs + local trace_molecules = false trackers.register("chemistry.molecules", function(v) trace_molecules = v end) -local ctxcatcodes = tex.ctxcatcodes +local report_chemistry = logs.reporter("chemistry") + +local context = context -chemicals = chemicals or { } +chemicals = chemicals or { } +local chemicals = chemicals --[[ <p>The next code is an adaptation of code from Wolfgang Schuster @@ -25,38 +30,37 @@ of input then.</p> -- some lpeg, maybe i'll make an syst-lpg module -local lowercase = lpeg.R("az") -local uppercase = lpeg.R("AZ") -local backslash = lpeg.P("\\") -local csname = backslash * lpeg.P(1) * (1-backslash)^0 -local plus = lpeg.P("+") / "\\textplus " -local minus = lpeg.P("-") / "\\textminus " -local digit = lpeg.R("09") +local lowercase = R("az") +local uppercase = R("AZ") +local backslash = P("\\") +local csname = backslash * P(1) * (1-backslash)^0 +local plus = P("+") / "\\textplus " +local minus = P("-") / "\\textminus " +local digit = R("09") local sign = plus + minus local cardinal = digit^1 local integer = sign^0 * cardinal -local leftbrace = lpeg.P("{") -local rightbrace = lpeg.P("}") +local leftbrace = P("{") +local rightbrace = P("}") local nobrace = 1 - (leftbrace + rightbrace) -local nested = lpeg.P { leftbrace * (csname + sign + nobrace + lpeg.V(1))^0 * rightbrace } -local any = lpeg.P(1) +local nested = P { leftbrace * (csname + sign + nobrace + V(1))^0 * rightbrace } +local any = P(1) -local subscript = lpeg.P("_") -local superscript = lpeg.P("^") +local subscript = P("_") +local superscript = P("^") local somescript = subscript + superscript ---~ local content = lpeg.Cs(nested + integer + sign + any) -local content = lpeg.Cs(csname + nested + sign + any) +local content = Cs(csname + nested + sign + any) -- could be made more efficient -local lowhigh = lpeg.Cc("\\lohi{%s}{%s}") * subscript * content * superscript * content / format -local highlow = lpeg.Cc("\\hilo{%s}{%s}") * superscript * content * subscript * content / format -local low = lpeg.Cc("\\low{%s}") * subscript * content / format -local high = lpeg.Cc("\\high{%s}") * superscript * content / format +local lowhigh = Cc("\\lohi{%s}{%s}") * subscript * content * superscript * content / format +local highlow = Cc("\\hilo{%s}{%s}") * superscript * content * subscript * content / format +local low = Cc("\\low{%s}") * subscript * content / format +local high = Cc("\\high{%s}") * superscript * content / format local justtext = (1 - somescript)^1 -local parser = lpeg.Cs((csname + lowhigh + highlow + low + high + sign + any)^0) +local parser = Cs((csname + lowhigh + highlow + low + high + sign + any)^0) chemicals.moleculeparser = parser -- can be used to avoid functioncall @@ -67,9 +71,9 @@ end function commands.molecule(str) if trace_molecules then local rep = lpegmatch(parser,str) - logs.report("chemistry", "molecule %s => %s",str,rep) - texsprint(ctxcatcodes,rep) + report_chemistry("molecule %s => %s",str,rep) + context(rep) else - texsprint(ctxcatcodes,lpegmatch(parser,str)) + context(lpegmatch(parser,str)) end end |