diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/chem-str.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/chem-str.lua | 132 |
1 files changed, 69 insertions, 63 deletions
diff --git a/Master/texmf-dist/tex/context/base/chem-str.lua b/Master/texmf-dist/tex/context/base/chem-str.lua index ad4cc6c1be4..1a6ed4dc213 100644 --- a/Master/texmf-dist/tex/context/base/chem-str.lua +++ b/Master/texmf-dist/tex/context/base/chem-str.lua @@ -13,15 +13,21 @@ if not modules then modules = { } end modules ['chem-str'] = { local trace_structure = false trackers.register("chemistry.structure", function(v) trace_structure = v end) local trace_textstack = false trackers.register("chemistry.textstack", function(v) trace_textstack = v end) +local report_chemistry = logs.reporter("chemistry") + local format, gmatch, match, lower, gsub = string.format, string.gmatch, string.match, string.lower, string.gsub local concat, insert, remove = table.concat, table.insert, table.remove -local apply = structure.processors.apply -local texsprint, ctxcatcodes = tex.sprint, tex.ctxcatcodes +local apply = structures.processors.apply local lpegmatch = lpeg.match +local settings_to_array = utilities.parsers.settings_to_array + +local P, R, S, C, Cs, Ct, Cc = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Cs, lpeg.Ct, lpeg.Cc local variables = interfaces.variables +local context = context chemicals = chemicals or { } +local chemicals = chemicals chemicals.instance = "metafun" -- "ppchtex" chemicals.format = "metafun" @@ -150,53 +156,52 @@ function chemicals.define(name,spec,text) local dn = definitions[name] if not dn then dn = { } definitions[name] = dn end dn[#dn+1] = { - spec = aux.settings_to_array(lower(spec)), - text = aux.settings_to_array(text), + spec = settings_to_array(lower(spec)), + text = settings_to_array(text), } end -local metacode, kind, keys, bonds, max, txt, textsize, rot, pstack +local metacode, variant, keys, bonds, max, txt, textsize, rot, pstack local molecule = chemicals.molecule -- or use lpegmatch(chemicals.moleculeparser,...) local function fetch(txt) local st = stack[txt] local t = st.text[st.n] ---~ st.n = st.n + 1 while not t and txt > 1 do txt = txt - 1 st = stack[txt] t = st.text[st.n] ---~ st.n = st.n + 1 end if t then if trace_textstack then - logs.report("chemical", "fetching from stack %s slot %s: %s",txt,st.n,t) + report_chemistry("fetching from stack %s slot %s: %s",txt,st.n,t) end -st.n = st.n + 1 + st.n = st.n + 1 end return txt, t end -local digit = lpeg.R("09")/tonumber -local colon = lpeg.P(":") -local equal = lpeg.P("=") +local digit = R("09")/tonumber +local colon = P(":") +local equal = P("=") local other = 1 - digit - colon - equal -local remapped = lpeg.S("+-") / remapper -local operation = lpeg.Cs((remapped^0 * other)^1) +local remapped = S("+-") / remapper +local operation = Cs((remapped^0 * other)^1) local amount = digit local single = digit -local special = (colon * lpeg.C(other^1)) + lpeg.Cc("") -local range = digit * lpeg.P("..") * digit -local set = lpeg.Ct(digit^2) -local text = (equal * lpeg.C(lpeg.P(1)^0)) + lpeg.Cc(false) +local special = (colon * C(other^1)) + Cc("") +local range = digit * P("..") * digit +local set = Ct(digit^2) +local text = (equal * C(P(1)^0)) + Cc(false) + local pattern = - (amount + lpeg.Cc(1)) * + (amount + Cc(1)) * operation * special * ( - range * lpeg.Cc(false) * text + - lpeg.Cc(false) * lpeg.Cc(false) * set * text + - single * lpeg.Cc(false) * lpeg.Cc(false) * text + - lpeg.Cc(false) * lpeg.Cc(false) * lpeg.Cc(false) * text + range * Cc(false) * text + + Cc(false) * Cc(false) * set * text + + single * Cc(false) * Cc(false) * text + + Cc(false) * Cc(false) * Cc(false) * text ) --~ local n, operation, index, upto, set, text = lpegmatch(pattern,"RZ1357") @@ -209,6 +214,7 @@ local pattern = local function process(spec,text,n,rulethickness,rulecolor,offset) insert(stack,{ spec=spec, text=text, n=n }) local txt = #stack + local m = #metacode for i=1,#spec do local s = spec[i] local d = definitions[s] @@ -220,8 +226,8 @@ local function process(spec,text,n,rulethickness,rulecolor,offset) else local rep, operation, special, index, upto, set, text = lpegmatch(pattern,s) if operation == "pb" then - insert(pstack,kind) - metacode[#metacode+1] = syntax.pb.direct + insert(pstack,variant) + m = m + 1 ; metacode[m] = syntax.pb.direct if keys[special] == "text" and index then if keys["c"..special] == "text" then -- can be option: auto ... metacode[#metacode+1] = format('chem_c%s(%s,%s,"");',special,bonds,index) @@ -230,22 +236,22 @@ local function process(spec,text,n,rulethickness,rulecolor,offset) end end elseif operation == "save" then - insert(pstack,kind) - metacode[#metacode+1] = syntax.save.direct + insert(pstack,variant) + m = m + 1 ; metacode[m] = syntax.save.direct elseif operation == "pe" or operation == "restore" then - kind = remove(pstack) - local ss = syntax[kind] + variant = remove(pstack) + local ss = syntax[variant] local prev = bonds or 6 keys, bonds, max, rot = ss.keys, ss.n, ss.max, 1 - metacode[#metacode+1] = syntax[operation].direct - metacode[#metacode+1] = format("chem_set(%s,%s) ;",prev,bonds) + m = m + 1 ; metacode[m] = syntax[operation].direct + m = m + 1 ; metacode[m] = format("chem_set(%s,%s) ;",prev,bonds) elseif operation == "front" then - if syntax[kind .. "_front"] then - kind = kind .. "_front" - local ss = syntax[kind] + if syntax[variant .. "_front"] then + variant = variant .. "_front" + local ss = syntax[variant] local prev = bonds or 6 keys, bonds, max, rot = ss.keys, ss.n, ss.max, 1 - metacode[#metacode+1] = format("chem_set(%s,%s) ;",prev,bonds) + m = m + 1 ; metacode[m] = format("chem_set(%s,%s) ;",prev,bonds) end elseif operation then local ss = syntax[operation] @@ -255,18 +261,18 @@ local function process(spec,text,n,rulethickness,rulecolor,offset) local sa = ss.arguments if sa == 1 then local one ; txt, one = fetch(txt) - metacode[#metacode+1] = format(ds,one or "") + m = m + 1 ; metacode[m] = format(ds,one or "") elseif sa ==2 then local one ; txt, one = fetch(txt) local two ; txt, two = fetch(txt) - metacode[#metacode+1] = format(ds,one or "",two or "") + m = m + 1 ; metacode[m] = format(ds,one or "",two or "") else - metacode[#metacode+1] = ds + m = m + 1 ; metacode[m] = ds end elseif ss.keys then local prev = bonds or 6 - kind, keys, bonds, max, rot = s, ss.keys, ss.n, ss.max, 1 - metacode[#metacode+1] = format("chem_set(%s,%s) ;",prev,bonds) + variant, keys, bonds, max, rot = s, ss.keys, ss.n, ss.max, 1 + m = m + 1 ; metacode[m] = format("chem_set(%s,%s) ;",prev,bonds) end else local what = keys[operation] @@ -274,35 +280,35 @@ local function process(spec,text,n,rulethickness,rulecolor,offset) if set then for i=1,#set do local si = set[i] - metacode[#metacode+1] = format("chem_%s(%s,%s,%s,%s,%s);",operation,bonds,si,si,rulethickness,rulecolor) + m = m + 1 ; metacode[m] = format("chem_%s(%s,%s,%s,%s,%s);",operation,bonds,si,si,rulethickness,rulecolor) end elseif upto then - metacode[#metacode+1] = format("chem_%s(%s,%s,%s,%s,%s);",operation,bonds,index,upto,rulethickness,rulecolor) + m = m + 1 ; metacode[m] = format("chem_%s(%s,%s,%s,%s,%s);",operation,bonds,index,upto,rulethickness,rulecolor) elseif index then - metacode[#metacode+1] = format("chem_%s(%s,%s,%s,%s,%s);",operation,bonds,index,index,rulethickness,rulecolor) + m = m + 1 ; metacode[m] = format("chem_%s(%s,%s,%s,%s,%s);",operation,bonds,index,index,rulethickness,rulecolor) else - metacode[#metacode+1] = format("chem_%s(%s,%s,%s,%s,%s);",operation,bonds,1,max,rulethickness,rulecolor) + m = m + 1 ; metacode[m] = format("chem_%s(%s,%s,%s,%s,%s);",operation,bonds,1,max,rulethickness,rulecolor) end elseif what == "number" then if set then for i=1,#set do local si = set[i] - metacode[#metacode+1] = format('chem_%s(%s,%s,"\\dochemicaltext{%s}");',operation,bonds,si,si) + m = m + 1 ; metacode[m] = format('chem_%s(%s,%s,"\\dochemicaltext{%s}");',operation,bonds,si,si) end elseif upto then for i=index,upto do local si = set[i] - metacode[#metacode+1] = format('chem_%s(%s,%s,"\\dochemicaltext{%s}");',operation,bonds,si,si) + m = m + 1 ; metacode[m] = format('chem_%s(%s,%s,"\\dochemicaltext{%s}");',operation,bonds,si,si) end elseif index then - metacode[#metacode+1] = format('chem_%s(%s,%s,"\\dochemicaltext{%s}");',operation,bonds,index,index) + m = m + 1 ; metacode[m] = format('chem_%s(%s,%s,"\\dochemicaltext{%s}");',operation,bonds,index,index) else for i=1,max do - metacode[#metacode+1] = format('chem_%s(%s,%s,"\\dochemicaltext{%s}");',operation,bonds,i,i) + m = m + 1 ; metacode[m] = format('chem_%s(%s,%s,"\\dochemicaltext{%s}");',operation,bonds,i,i) end end elseif what == "text" then - local align = syntax[kind].align + local align = syntax[variant].align align = align and align[operation] align = align and align[rot] if set then @@ -313,7 +319,7 @@ local function process(spec,text,n,rulethickness,rulecolor,offset) if t then local a = align and align[si] if a then a = "." .. a else a = "" end - metacode[#metacode+1] = format('chem_%s%s(%s,%s,"\\dochemicaltext{%s}");',operation,a,bonds,si,molecule(apply(t))) + m = m + 1 ; metacode[m] = format('chem_%s%s(%s,%s,"\\dochemicaltext{%s}");',operation,a,bonds,si,molecule(apply(t))) end end elseif upto then @@ -323,14 +329,14 @@ local function process(spec,text,n,rulethickness,rulecolor,offset) if t then local s = align and align[i] if s then s = "." .. s else s = "" end - metacode[#metacode+1] = format('chem_%s%s(%s,%s,"\\dochemicaltext{%s}");',operation,s,bonds,i,molecule(apply(t))) + m = m + 1 ; metacode[m] = format('chem_%s%s(%s,%s,"\\dochemicaltext{%s}");',operation,s,bonds,i,molecule(apply(t))) end end elseif index == 0 then local t = text if not t then txt, t = fetch(txt) end if t then - metacode[#metacode+1] = format('chem_%s_zero("\\dochemicaltext{%s}");',operation,molecule(apply(t))) + m = m + 1 ; metacode[m] = format('chem_%s_zero("\\dochemicaltext{%s}");',operation,molecule(apply(t))) end elseif index then local t = text @@ -338,7 +344,7 @@ local function process(spec,text,n,rulethickness,rulecolor,offset) if t then local s = align and align[index] if s then s = "." .. s else s = "" end - metacode[#metacode+1] = format('chem_%s%s(%s,%s,"\\dochemicaltext{%s}");',operation,s,bonds,index,molecule(apply(t))) + m = m + 1 ; metacode[m] = format('chem_%s%s(%s,%s,"\\dochemicaltext{%s}");',operation,s,bonds,index,molecule(apply(t))) end else for i=1,max do @@ -347,21 +353,21 @@ local function process(spec,text,n,rulethickness,rulecolor,offset) if t then local s = align and align[i] if s then s = "." .. s else s = "" end - metacode[#metacode+1] = format('chem_%s%s(%s,%s,"\\dochemicaltext{%s}");',operation,s,bonds,i,molecule(apply(t))) + m = m + 1 ; metacode[m] = format('chem_%s%s(%s,%s,"\\dochemicaltext{%s}");',operation,s,bonds,i,molecule(apply(t))) end end end elseif what == "transform" then if index then for r=1,rep do - metacode[#metacode+1] = format('chem_%s(%s,%s);',operation,bonds,index) + m = m + 1 ; metacode[m] = format('chem_%s(%s,%s);',operation,bonds,index) end if operation == "rot" then rot = index end end elseif what == "fixed" then - metacode[#metacode+1] = format("chem_%s(%s,%s,%s);",operation,bonds,rulethickness,rulecolor) + m = m + 1 ; metacode[m] = format("chem_%s(%s,%s,%s);",operation,bonds,rulethickness,rulecolor) end end end @@ -434,14 +440,14 @@ function chemicals.start(settings) l/25, r/25, t/25, b/25, scale, tostring(settings.axis == variables.on), tostring(width), tostring(height), tostring(offset) ) } - kind, keys, bonds, stack, rot, pstack = "six", { }, 6, { }, 1, { } + variant, keys, bonds, stack, rot, pstack = "six", { }, 6, { }, 1, { } end function chemicals.stop() metacode[#metacode+1] = "chem_stop_structure ;" local mpcode = concat(metacode,"\n") if trace_structure then - logs.report("chemical", "metapost code:\n%s", mpcode) + report_chemistry("metapost code:\n%s", mpcode) end metapost.graphic(chemicals.instance,chemicals.format,mpcode) metacode = nil @@ -449,8 +455,8 @@ end function chemicals.component(spec,text,settings) rulethickness, rulecolor, offset = settings.rulethickness, settings.rulecolor - local spec = aux.settings_to_array(lower(spec)) - local text = aux.settings_to_array(text) + local spec = settings_to_array(lower(spec)) + local text = settings_to_array(text) metacode[#metacode+1] = "chem_start_component ;" process(spec,text,1,rulethickness,rulecolor) metacode[#metacode+1] = "chem_stop_component ;" @@ -471,14 +477,14 @@ local inline = { -- todo: top / bottom function chemicals.inline(spec) - local spec = aux.settings_to_array(spec) + local spec = settings_to_array(spec) for i=1,#spec do local s = spec[i] local inl = inline[lower(s)] if inl then - texsprint(ctxcatcodes,inl) + context(inl) else - texsprint(ctxcatcodes,format("\\chemicalinline{%s}",molecule(s))) + context.chemicalinline(molecule(s)) end end end |