summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/l-aux.lua
diff options
context:
space:
mode:
authorTaco Hoekwater <taco@elvenkind.com>2009-08-23 11:11:32 +0000
committerTaco Hoekwater <taco@elvenkind.com>2009-08-23 11:11:32 +0000
commit8fc3039c82d48605b5ca8b2eda3f4fdd755681e1 (patch)
tree3cd9bbdd599bc4d1ac0409e167fee2136e4c0ec9 /Master/texmf-dist/tex/context/base/l-aux.lua
parent850fc99b7cd3ae7a20065531fe866ff7bae642ec (diff)
this is context 2009.08.19 17:10
git-svn-id: svn://tug.org/texlive/trunk@14827 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/context/base/l-aux.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/l-aux.lua205
1 files changed, 159 insertions, 46 deletions
diff --git a/Master/texmf-dist/tex/context/base/l-aux.lua b/Master/texmf-dist/tex/context/base/l-aux.lua
index f81d807cdc4..0e5babeeb26 100644
--- a/Master/texmf-dist/tex/context/base/l-aux.lua
+++ b/Master/texmf-dist/tex/context/base/l-aux.lua
@@ -1,64 +1,119 @@
--- filename : l-aux.lua
--- author : Hans Hagen, PRAGMA-ADE, Hasselt NL
--- copyright: PRAGMA ADE / ConTeXt Development Team
--- license : see context related readme files
+if not modules then modules = { } end modules ['l-aux'] = {
+ version = 1.001,
+ comment = "companion to luat-lib.tex",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
-if not versions then versions = { } end versions['l-aux'] = 1.001
-if not aux then aux = { } end
+aux = aux or { }
-do
+local concat, format, gmatch = table.concat, string.format, string.gmatch
+local tostring, type = tostring, type
- local hash = { }
+local space = lpeg.P(' ')
+local equal = lpeg.P("=")
+local comma = lpeg.P(",")
+local lbrace = lpeg.P("{")
+local rbrace = lpeg.P("}")
+local nobrace = 1 - (lbrace+rbrace)
+local nested = lpeg.P{ lbrace * (nobrace + lpeg.V(1))^0 * rbrace }
+local spaces = space^0
- local function set(key,value) -- using Carg is slower here
- hash[key] = value
+local value = lpeg.P(lbrace * lpeg.C((nobrace + nested)^0) * rbrace) + lpeg.C((nested + (1-comma))^0)
+
+local key = lpeg.C((1-equal-comma)^1)
+local pattern_a = (space+comma)^0 * (key * equal * value + key * lpeg.C(""))
+local pattern_c = (space+comma)^0 * (key * equal * value)
+
+local key = lpeg.C((1-space-equal-comma)^1)
+local pattern_b = spaces * comma^0 * spaces * (key * ((spaces * equal * spaces * value) + lpeg.C("")))
+
+-- "a=1, b=2, c=3, d={a{b,c}d}, e=12345, f=xx{a{b,c}d}xx, g={}" : outer {} removes, leading spaces ignored
+
+local hash = { }
+
+local function set(key,value) -- using Carg is slower here
+ hash[key] = value
+end
+
+local pattern_a_s = (pattern_a/set)^1
+local pattern_b_s = (pattern_b/set)^1
+local pattern_c_s = (pattern_c/set)^1
+
+aux.settings_to_hash_pattern_a = pattern_a_s
+aux.settings_to_hash_pattern_b = pattern_b_s
+aux.settings_to_hash_pattern_c = pattern_c_s
+
+function aux.make_settings_to_hash_pattern(set,how)
+ if how == "strict" then
+ return (pattern_c/set)^1
+ elseif how == "tolerant" then
+ return (pattern_b/set)^1
+ else
+ return (pattern_a/set)^1
end
+end
- local space = lpeg.P(' ')
- local equal = lpeg.P("=")
- local comma = lpeg.P(",")
- local lbrace = lpeg.P("{")
- local rbrace = lpeg.P("}")
- local nobrace = 1 - (lbrace+rbrace)
- local nested = lpeg.P{ lbrace * (nobrace + lpeg.V(1))^0 * rbrace }
-
- local key = lpeg.C((1-equal)^1)
- local value = lpeg.P(lbrace * lpeg.C((nobrace + nested)^0) * rbrace) + lpeg.C((nested + (1-comma))^0)
- local pattern = ((space^0 * key * equal * value * comma^0) / set)^1
-
- -- "a=1, b=2, c=3, d={a{b,c}d}, e=12345, f=xx{a{b,c}d}xx, g={}" : outer {} removes, leading spaces ignored
-
- function aux.settings_to_hash(str)
- if str and str ~= "" then
- hash = { }
- pattern:match(str)
- return hash
+function aux.settings_to_hash(str)
+ if str and str ~= "" then
+ hash = { }
+ if moretolerant then
+ pattern_b_s:match(str)
else
- return { }
+ pattern_a_s:match(str)
end
+ return hash
+ else
+ return { }
end
+end
- local seperator = comma * space^0
- local value = lpeg.P(lbrace * lpeg.C((nobrace + nested)^0) * rbrace) + lpeg.C((nested + (1-comma))^0)
- local pattern = lpeg.Ct(value*(seperator*value)^0)
-
- -- "aap, {noot}, mies" : outer {} removes, leading spaces ignored
-
- function aux.settings_to_array(str)
- return pattern:match(str)
+function aux.settings_to_hash_tolerant(str)
+ if str and str ~= "" then
+ hash = { }
+ pattern_b_s:match(str)
+ return hash
+ else
+ return { }
end
+end
- local function set(t,v)
- t[#t+1] = v
+function aux.settings_to_hash_strict(str)
+ if str and str ~= "" then
+ hash = { }
+ pattern_c_s:match(str)
+ return next(hash) and hash
+ else
+ return nil
end
+end
+
+local seperator = comma * space^0
+local value = lpeg.P(lbrace * lpeg.C((nobrace + nested)^0) * rbrace) + lpeg.C((nested + (1-comma))^0)
+local pattern = lpeg.Ct(value*(seperator*value)^0)
- local value = lpeg.P(lpeg.Carg(1)*value) / set
- local pattern = value*(seperator*value)^0 * lpeg.Carg(1)
+-- "aap, {noot}, mies" : outer {} removes, leading spaces ignored
- function aux.add_settings_to_array(t,str)
- return pattern:match(str, nil, t)
+aux.settings_to_array_pattern = pattern
+
+function aux.settings_to_array(str)
+ if not str or str == "" then
+ return { }
+ else
+ return pattern:match(str)
end
+end
+
+local function set(t,v)
+ t[#t+1] = v
+end
+local value = lpeg.P(lpeg.Carg(1)*value) / set
+local pattern = value*(seperator*value)^0 * lpeg.Carg(1)
+
+function aux.add_settings_to_array(t,str)
+ return pattern:match(str, nil, t)
end
function aux.hash_to_string(h,separator,yes,no,strict,omit)
@@ -84,7 +139,7 @@ function aux.hash_to_string(h,separator,yes,no,strict,omit)
end
end
end
- return table.concat(t,separator or ",")
+ return concat(t,separator or ",")
else
return ""
end
@@ -92,12 +147,20 @@ end
function aux.array_to_string(a,separator)
if a then
- return table.concat(a,separator or ",")
+ return concat(a,separator or ",")
else
return ""
end
end
+function aux.settings_to_set(str,t)
+ t = t or { }
+ for s in gmatch(str,"%s*([^,]+)") do
+ t[s] = true
+ end
+ return t
+end
+
-- temporary here
function aux.getparameters(self,class,parentclass,settings)
@@ -108,3 +171,53 @@ function aux.getparameters(self,class,parentclass,settings)
end
aux.add_settings_to_array(sc, settings)
end
+
+-- temporary here
+
+local digit = lpeg.R("09")
+local period = lpeg.P(".")
+local zero = lpeg.P("0")
+
+--~ local finish = lpeg.P(-1)
+--~ local nodigit = (1-digit) + finish
+--~ local case_1 = (period * zero^1 * #nodigit)/"" -- .000
+--~ local case_2 = (period * (1-(zero^0/"") * #nodigit)^1 * (zero^0/"") * nodigit) -- .010 .10 .100100
+
+local trailingzeros = zero^0 * -digit -- suggested by Roberto R
+local case_1 = period * trailingzeros / ""
+local case_2 = period * (digit - trailingzeros)^1 * (trailingzeros / "")
+
+local number = digit^1 * (case_1 + case_2)
+local stripper = lpeg.Cs((number + 1)^0)
+
+--~ local sample = "bla 11.00 bla 11 bla 0.1100 bla 1.00100 bla 0.00 bla 0.001 bla 1.1100 bla 0.100100100 bla 0.00100100100"
+--~ collectgarbage("collect")
+--~ str = string.rep(sample,10000)
+--~ local ts = os.clock()
+--~ stripper:match(str)
+--~ print(#str, os.clock()-ts, stripper:match(sample))
+
+function aux.strip_zeros(str)
+ return stripper:match(str)
+end
+
+function aux.definetable(target) -- defines undefined tables
+ local composed, t = nil, { }
+ for name in gmatch(target,"([^%.]+)") do
+ if composed then
+ composed = composed .. "." .. name
+ else
+ composed = name
+ end
+ t[#t+1] = format("%s = %s or { }",composed,composed)
+ end
+ return concat(t,"\n")
+end
+
+function aux.accesstable(target)
+ local t = _G
+ for name in gmatch(target,"([^%.]+)") do
+ t = t[name]
+ end
+ return t
+end