summaryrefslogtreecommitdiff
path: root/tex/context/base/phys-dim.lua
diff options
context:
space:
mode:
authorHans Hagen <pragma@wxs.nl>2012-01-05 19:27:36 +0100
committerHans Hagen <pragma@wxs.nl>2012-01-05 19:27:36 +0100
commit1b023b411cc3c2b9701ddca2f3caaba010e51730 (patch)
tree6632c508ca4f3095f0260ae074d53ba90227460d /tex/context/base/phys-dim.lua
parent6ef8659db06139f3e00aa60ac699434d1f4665d3 (diff)
downloadcontext-1b023b411cc3c2b9701ddca2f3caaba010e51730.tar.gz
beta 2011.11.29 18:47
Diffstat (limited to 'tex/context/base/phys-dim.lua')
-rw-r--r--tex/context/base/phys-dim.lua956
1 files changed, 596 insertions, 360 deletions
diff --git a/tex/context/base/phys-dim.lua b/tex/context/base/phys-dim.lua
index 0cf878470..9b9b59372 100644
--- a/tex/context/base/phys-dim.lua
+++ b/tex/context/base/phys-dim.lua
@@ -16,14 +16,35 @@ if not modules then modules = { } end modules ['phys-dim'] = {
-- todo: collect used units for logging (and list of units, but then we need
-- associations too).
--- todo: degrees celsius 0x2103
--- todo: degrees fahrenheit 0x2109
-
-local V, P, S, R, C, Cc, Cs, matchlpeg, Carg = lpeg.V, lpeg.P, lpeg.S, lpeg.R, lpeg.C, lpeg.Cc, lpeg.Cs, lpeg.match, lpeg.Carg
+-- The lists have been checked and completed by Robin Kirkham.
+
+-- dubious/wrong
+
+-- Atom = [[u]], -- should be amu (atomic mass unit)
+-- Bell = [[B]], -- should be bel
+-- Sterant = [[sr]], -- should be steradian
+-- Equivalent = [[eql]], -- qualifier?
+-- At = [[at]], -- qualifier?
+-- Force = [[f]], -- qualifier?
+-- eVolt = [[eV]],
+-- -- AC or DC voltages should be qualified in the text
+-- VoltAC = [[V\unitsbackspace\unitslower{ac}]],
+-- VoltDC = [[V\unitsbackspace\unitslower{dc}]],
+-- AC = [[V\unitsbackspace\unitslower{ac}]],
+-- DC = [[V\unitsbackspace\unitslower{dc}]],
+-- -- probably not harmful but there are better alternatives
+-- -- e.g., revolution per second (rev/s)
+-- RPS = [[RPS]],
+-- RPM = [[RPM]],
+-- RevPerSec = [[RPS]],
+-- RevPerMin = [[RPM]],
+
+local V, P, S, R, C, Cc, Cs, matchlpeg = lpeg.V, lpeg.P, lpeg.S, lpeg.R, lpeg.C, lpeg.Cc, lpeg.Cs, lpeg.match
local format, lower = string.format, string.lower
local appendlpeg = lpeg.append
local mergetable, mergedtable, keys, loweredkeys = table.merge, table.merged, table.keys, table.loweredkeys
local setmetatablenewindex = table.setmetatablenewindex
+local utfchar = utf.char
physics = physics or { }
physics.units = physics.units or { }
@@ -39,6 +60,9 @@ trackers.register("physics.units", function(v) trace_units = v end)
-- digits parser (todo : use patterns)
+local math_one = Cs((P("$") /"") * (1-P("$"))^1 * (P("$")/"")) / context.m
+local math_two = Cs((P("\\m {")/"") * (1-P("}"))^1 * (P("}")/"")) / context.m -- watch the space after \m
+
local digit = R("09")
local sign = S("+-")
local power = S("^e")
@@ -59,17 +83,15 @@ local space = P(" ")
local digits = digit^1
local ddigitspace = digitspace / "" / context.digitsspace
-local dcommayes = semicolon / "" / context.digitsfinalcomma
-local dcommanop = semicolon / "" / context.digitsseparatorspace
-local dperiodyes = colon / "" / context.digitsfinalperiod
-local dperiodnop = colon / "" / context.digitsseparatorspace
local ddigit = digits / context.digitsdigit
+local dsemicomma = semicolon / "" / context.digitsseparatorspace
+local dsemiperiod = colon / "" / context.digitsseparatorspace
local dfinalcomma = comma / "" / context.digitsfinalcomma
local dfinalperiod = period / "" / context.digitsfinalperiod
-local dintercomma = comma * #(digitspace) / "" / context.digitsseparatorspace
- + comma / "" / context.digitsintermediatecomma
-local dinterperiod = period * #(digitspace) / "" / context.digitsseparatorspace
- + period / "" / context.digitsintermediateperiod
+local dintercomma = comma / "" / context.digitsintermediatecomma
+local dinterperiod = period / "" / context.digitsintermediateperiod
+local dskipcomma = comma / "" / context.digitsseparatorspace
+local dskipperiod = period / "" / context.digitsseparatorspace
local dsignspace = signspace / "" / context.digitssignspace
local dpositive = positive / "" / context.digitspositive
local dnegative = negative / "" / context.digitsnegative
@@ -87,28 +109,35 @@ local dleader = (dpositive + dnegative + dhighspace + dsomesign + dsignsp
local dtrailer = dpower^0
local dfinal = P(-1) + #P(1 - comma - period - semicolon - colon)
local dnumber = (ddigitspace + ddigit)^1
-local dtemplate = ddigitspace^1
-
--- probably too complex, due to lookahead (lookback with state is probably easier)
-
-local dpcfinalnumber = dtemplate * (dfinalcomma + dcommanop ) + dnumber * (dfinalcomma + dcommayes )
-local dcpfinalnumber = dtemplate * (dfinalperiod + dperiodnop) + dnumber * (dfinalperiod + dperiodyes)
-
-local dpcinternumber = dtemplate * (dintercomma + dcommanop ) + dnumber * (dintercomma + dcommayes )
-local dcpinternumber = dtemplate * (dinterperiod + dperiodnop) + dnumber * (dinterperiod + dperiodyes)
-
-local dfallback = (dtemplate * (dcommanop + dperiodnop)^0)^0 * (dcommayes + dperiodyes + ddigit)^0
-
-local p_c_number = (dcpinternumber)^0 * (dpcfinalnumber)^0 * ddigit + dfallback -- 000.000.000,00
-local c_p_number = (dpcinternumber)^0 * (dcpfinalnumber)^0 * ddigit + dfallback -- 000,000,000.00
-
--- ony signs before numbers (otherwise we get s / seconds issues)
-
-local p_c_dparser = dleader * p_c_number * dtrailer * dfinal
-local c_p_dparser = dleader * c_p_number * dtrailer * dfinal
--- local p_c_dparser = p_c_number * dtrailer * dfinal
--- local c_p_dparser = c_p_number * dtrailer * dfinal
+-- ___,000,000 ___,___,000 ___,___,__0 000,000,000 000.00 000,000,000.00 000,000,000.==
+
+-- : ; for the moment not used, maybe for invisible fraction . , when no leading number
+
+local c_p = (ddigitspace^1 * dskipcomma)^0 -- ___,
+ * (ddigitspace^0 * ddigit * dintercomma)^0 -- _00, 000,
+ * ddigitspace^0 * ddigit^0 -- _00 000
+ * (
+ dfinalperiod * ddigit -- .00
+ + dskipperiod * dpadding^1 -- .==
+ + dsemiperiod * ddigit -- :00
+ + dsemiperiod * dpadding^1 -- :==
+ )^0
+ + ddigit -- 00
+
+local p_c = (ddigitspace^1 * dskipperiod)^0 -- ___.
+ * (ddigitspace^0 * ddigit * dinterperiod)^0 -- _00. 000.
+ * ddigitspace^0 * ddigit^0 -- _00 000
+ * (
+ dfinalcomma * ddigit -- ,00
+ + dskipcomma * dpadding^1 -- ,==
+ + dsemicomma * ddigit -- :00
+ + dsemicomma * dpadding^1 -- :==
+ )^0
+ + ddigit -- 00
+
+local p_c_dparser = math_one + math_two + dleader * p_c * dtrailer * dfinal
+local c_p_dparser = math_one + math_two + dleader * c_p * dtrailer * dfinal
function commands.digits(str,p_c)
if p_c == v_reverse then
@@ -121,225 +150,313 @@ end
-- tables:
local long_prefixes = {
- Yocto = [[y]], -- 10^{-24}
- Zepto = [[z]], -- 10^{-21}
- Atto = [[a]], -- 10^{-18}
- Femto = [[f]], -- 10^{-15}
- Pico = [[p]], -- 10^{-12}
- Nano = [[n]], -- 10^{-9}
- Micro = [[\mu]],-- 10^{-6}
- Milli = [[m]], -- 10^{-3}
- Centi = [[c]], -- 10^{-2}
- Deci = [[d]], -- 10^{-1}
-
- Deca = [[da]], -- 10^{1}
- Hecto = [[h]], -- 10^{2}
- Kilo = [[k]], -- 10^{3}
- Mega = [[M]], -- 10^{6}
- Giga = [[G]], -- 10^{9}
- Tera = [[T]], -- 10^{12}
- Peta = [[P]], -- 10^{15}
- Exa = [[E]], -- 10^{18}
- Zetta = [[Z]], -- 10^{21}
- Yotta = [[Y]], -- 10^{24}
-
- Kibi = [[ki]], -- 2^{10}
- Mebi = [[Mi]], -- 2^{20}
- Gibi = [[Gi]], -- 2^{30}
- Tebi = [[Ti]], -- 2^{40}
- Pebi = [[Pi]], -- 2^{50}
-
- Kibi = [[Ki]], -- binary
- Mebi = [[Mi]], -- binary
- Gibi = [[Gi]], -- binary
- Tebi = [[Ti]], -- binary
- Pebi = [[Pi]], -- binary
- Exbi = [[Ei]], -- binary
- Zebi = [[Zi]], -- binary
- Yobi = [[Yi]], -- binary
-
- Micro = [[µ]], -- 0x00B5 \textmu
- Root = [[√]], -- 0x221A
+
+ -- Le Système international d'unités (SI) 8e édition (Table 5)
+
+ Yocto = "yocto", -- 10^{-24}
+ Zepto = "zepto", -- 10^{-21}
+ Atto = "atto", -- 10^{-18}
+ Femto = "femto", -- 10^{-15}
+ Pico = "pico", -- 10^{-12}
+ Nano = "nano", -- 10^{-9}
+ Micro = "micro", -- 10^{-6}
+ Milli = "milli", -- 10^{-3}
+ Centi = "centi", -- 10^{-2}
+ Deci = "deci", -- 10^{-1}
+
+ Deca = "deca", -- 10^{1}
+ Hecto = "hecto", -- 10^{2}
+ Kilo = "kilo", -- 10^{3}
+ Mega = "mega", -- 10^{6}
+ Giga = "giga", -- 10^{9}
+ Tera = "tera", -- 10^{12}
+ Peta = "peta", -- 10^{15}
+ Exa = "exa", -- 10^{18}
+ Zetta = "zetta", -- 10^{21}
+ Yotta = "yotta", -- 10^{24}
+
+ -- IEC 60027-2: 2005, third edition, Part 2
+
+ Kibi = "kibi", -- 2^{10} (not ki)
+ Mebi = "mebi", -- 2^{20}
+ Gibi = "gibi", -- 2^{30}
+ Tebi = "tebi", -- 2^{40}
+ Pebi = "pebi", -- 2^{50}
+ Exbi = "exbi", -- 2^{60}
+
+ -- not standard
+
+ Zebi = "zebi", -- binary
+ Yobi = "yobi", -- binary
+
+ Micro = "micro",
+ Root = "root",
}
local long_units = {
- Meter = [[m]],
- Hertz = [[Hz]],
- Second = [[s]],
- Hour = [[h]],
- Liter = [[l]],
--- Litre = [[l]],
- Gram = [[g]],
- Newton = [[N]],
- Pascal = [[Pa]],
- Atom = [[u]],
- Bell = [[B]],
- Katal = [[kat]],
- Dalton = [[Da]],
- Joule = [[J]],
- Watt = [[W]],
- Celsius = [[C]], -- no SI
- Kelvin = [[K]],
- Fahrenheit = [[F]], -- no SI
- Mol = [[mol]],
- Mole = [[mol]],
- Equivalent = [[eql]],
- Farad = [[F]],
- Ohm = [[\Omega]],
- Siemens = [[S]],
- Ampere = [[A]],
- Coulomb = [[C]],
- Volt = [[V]],
- eVolt = [[eV]],
- eV = [[eV]],
- Tesla = [[T]],
- VoltAC = [[V\unitsbackspace\unitslower{ac}]],
- VoltDC = [[V\unitsbackspace\unitslower{dc}]],
- AC = [[V\unitsbackspace\unitslower{ac}]],
- DC = [[V\unitsbackspace\unitslower{dc}]],
- Bit = [[bit]],
- Baud = [[Bd]],
- Byte = [[B]],
- Erlang = [[E]],
- Bequerel = [[Bq]],
- Sievert = [[Sv]],
- Candela = [[cd]],
- Bel = [[B]],
- At = [[at]],
- Atm = [[atm]],
- Bar = [[bar]],
- Foot = [[ft]],
- Inch = [[inch]],
- Cal = [[cal]],
- Force = [[f]],
- Lux = [[lx]],
- Gray = [[Gr]],
- Weber = [[Wb]],
- Henry = [[H]],
- Sterant = [[sr]],
- Tonne = [[t]],
- Angstrom = [[Å]],
- Gauss = [[G]],
- Rad = [[rad]],
- RPS = [[RPS]],
- RPM = [[RPM]],
- RevPerSec = [[RPS]],
- RevPerMin = [[RPM]],
- Ohm = [[Ω]], -- 0x2126 \textohm
- ["Metric Ton"] = [[t]],
- ["Electron Volt"] = [[eV]],
+
+ -- Le Système international d'unités (SI) 8e édition (except synonyms)
+ -- SI base units (Table 1)
+
+ Meter = "meter",
+ Gram = "gram",
+ Second = "second",
+ Ampere = "ampere",
+ Kelvin = "kelvin",
+ Mole = "mole",
+ Candela = "candela",
+
+ -- synonyms
+
+ Mol = "mole",
+ Metre = "meter",
+
+ -- SI derived units with special names (Table 3)
+
+ Radian = "radian",
+ Steradian = "steradian",
+ Hertz = "hertz",
+ Newton = "newton",
+ Pascal = "pascal",
+ Joule = "joule",
+ Watt = "watt",
+ Coulomb = "coulomb",
+ Volt = "volt",
+ Farad = "farad",
+ Ohm = "ohm",
+ Siemens = "siemens",
+ Weber = "weber",
+ Tesla = "tesla",
+ Henry = "henry",
+ Celsius = "celsius",
+ Lumen = "lumen",
+ Lux = "lux",
+ Bequerel = "bequerel",
+ Gray = "gray",
+ Sievert = "sievert",
+ Katal = "katal",
+
+ -- non SI units accepted for use with SI (Table 6)
+
+ Minute = "minute",
+ Hour = "hour",
+ Day = "day",
+
+ -- (degree, minute, second of arc are treated specially later)
+
+ Gon = "gon",
+ Grad = "grad",
+ Hectare = "hectare",
+ Liter = "liter",
+
+ Tonne = "tonne",
+
+ -- synonyms
+
+ ["Metric Ton"] = "tonne",
+ Litre = "liter",
+
+ -- non-SI units whose values must be obtained experimentally (Table 7)
+
+ ["Electron Volt"] = "electronvolt",
+ Dalton = "dalton",
+ ["Atomic Mass Unit"] = "atomicmassunit",
+ ["Astronomical Unit"] = "astronomicalunit",
+
+ -- special cases (catch doubles, okay, a bit over the top)
+
+ ["Degrees Celsius"] = "celsius",
+ ["Degrees Fahrenheit"] = "fahrenheit",
+ ["Degree Celsius"] = "celsius",
+ ["Degree Fahrenheit"] = "fahrenheit",
+
+ -- too late as we already have connected symbols catched:
+ --
+ -- ["° Celsius"] = "celsius",
+ -- ["° Fahrenheit"] = "fahrenheit",
+ -- ["°Celsius"] = "celsius",
+ -- ["°Fahrenheit"] = "fahrenheit",
+
+ -- the "natural units" and "atomic units" are omitted for now
+ -- synonyms
+
+ eV = "electronvolt",
+ AMU = "atomicmassunit",
+
+ -- other non-SI units (Table 8)
+
+ Bar = "bar",
+ -- ["Millimetre Of Mercury"] = [[mmHg]],
+ Angstrom = "angstrom", -- strictly Ångström
+ ["Nautical Mile"] = "nauticalmile",
+ Barn = "barn",
+ Knot = "knot",
+ Neper = "neper",
+ Bel = "bel", -- in practice only decibel used
+
+ -- other non-SI units from CGS system (Table 9)
+
+ Erg = "erg",
+ Dyne = "dyne",
+ Poise = "poise",
+ Stokes = "stokes",
+ Stilb = "stilb",
+ Phot = "phot",
+ Gal = "gal",
+ Maxwell = "maxwell",
+ Gauss = "gauss",
+ Oersted = "oersted",
+
+ -- end of SI
+
+ -- data: for use with the binary prefixes (except Erlang)
+
+ Bit = "bit",
+ Byte = "byte" ,
+ Baud = "baud",
+ Erlang = "erlang",
+
+ -- common units, not part of SI
+
+ Atmosphere = "atmosphere",
+ Revolution = "revolution",
+
+ -- synonyms
+
+ Atm = "atmosphere",
+ Rev = "revolution",
+
+ -- imperial units (very incomplete)
+
+ Fahrenheit = "fahrenheit",
+ Foot = "foot",
+ Inch = "inch",
+ Calorie = "calorie",
+
+ -- synonyms
+
+ Cal = "calorie",
+
}
local long_operators = {
- Times = [[\unitsTIMES]], -- cdot
- Solidus = [[\unitsSOLIDUS]],
- Per = [[\unitsSOLIDUS]],
- OutOf = [[\unitsOUTOF]],
+
+ Times = "times",
+ Solidus = "solidus",
+ Per = "per",
+ OutOf = "outof",
+
}
local long_suffixes = {
- Linear = [[1]],
- Square = [[2]],
- Cubic = [[3]],
- Inverse = [[-1]],
- ILinear = [[-1]],
- ISquare = [[-2]],
- ICubic = [[-3]],
+
+ Linear = "linear",
+ Square = "square",
+ Cubic = "cubic",
+ Inverse = "inverse",
+ ILinear = "ilinear",
+ ISquare = "isquare",
+ ICubic = "icubic",
+
}
-local short_prefixes_to_long = {
- y = "Yocto",
- z = "Zetto",
- a = "Atto",
- f = "Femto",
- p = "Pico",
- n = "Nano",
- u = "Micro",
- m = "Milli",
- c = "Centi",
- d = "Deci",
- da = "Deca",
- h = "Hecto",
- k = "Kilo",
- M = "Mega",
- G = "Giga",
- T = "Tera",
- P = "Peta",
- E = "Exa",
- Z = "Zetta",
- Y = "Yotta",
+local short_prefixes = {
+
+ y = "yocto",
+ z = "zetto",
+ a = "atto",
+ f = "femto",
+ p = "pico",
+ n = "nano",
+ u = "micro",
+ m = "milli",
+ c = "centi",
+ d = "deci",
+ da = "deca",
+ h = "hecto",
+ k = "kilo",
+ M = "mega",
+ G = "giga",
+ T = "tera",
+ P = "peta",
+ E = "exa",
+ Z = "zetta",
+ Y = "yotta",
+
}
-local short_units_to_long = { -- I'm not sure about casing
- m = "Meter",
- Hz = "Hertz",
- hz = "Hertz",
- B = "Bel",
- b = "Bel",
- lx = "Lux",
- -- da = "Dalton",
- u = "Hour",
- h = "Hour",
- s = "Second",
- g = "Gram",
- n = "Newton",
- v = "Volt",
- t = "Tonne",
-
- l = "Liter",
- -- w = "Watt",
- W = "Watt",
- -- a = "Ampere",
- A = "Ampere",
-
- Litre = "Liter",
- Metre = "Meter",
+local short_units = { -- I'm not sure about casing
+
+ m = "meter",
+ Hz = "hertz",
+ hz = "hertz",
+ B = "bel",
+ b = "bel",
+ lx = "lux",
+ -- da = "dalton",
+ h = "hour",
+ s = "second",
+ g = "gram",
+ n = "newton",
+ v = "volt",
+ t = "tonne",
+ l = "liter",
+ -- w = "watt",
+ W = "watt",
+ -- a = "ampere",
+ A = "ampere",
+
+ [utfchar(0x2103)] = "celsius",
+ [utfchar(0x2109)] = "fahrenheit",
}
-local short_operators_to_long = {
- ["."] = "Times",
- ["*"] = "Times",
- ["/"] = "Solidus",
- [":"] = "OutOf",
+local short_operators = {
+ ["."] = "times",
+ ["*"] = "times",
+ ["/"] = "solidus",
+ [":"] = "outof",
}
local short_suffixes = { -- maybe just raw digit match
- ["1"] = long_suffixes.Linear,
- ["2"] = long_suffixes.Square,
- ["3"] = long_suffixes.Cubic,
- ["+1"] = long_suffixes.Linear,
- ["+2"] = long_suffixes.Square,
- ["+3"] = long_suffixes.Cubic,
- ["-1"] = long_suffixes.Inverse,
- ["-1"] = long_suffixes.ILinear,
- ["-2"] = long_suffixes.ISquare,
- ["-3"] = long_suffixes.ICubic,
- ["^1"] = long_suffixes.Linear,
- ["^2"] = long_suffixes.Square,
- ["^3"] = long_suffixes.Cubic,
- ["^+1"] = long_suffixes.Linear,
- ["^+2"] = long_suffixes.Square,
- ["^+3"] = long_suffixes.Cubic,
- ["^-1"] = long_suffixes.Inverse,
- ["^-1"] = long_suffixes.ILinear,
- ["^-2"] = long_suffixes.ISquare,
- ["^-3"] = long_suffixes.ICubic,
+ ["1"] = "linear",
+ ["2"] = "square",
+ ["3"] = "cubic",
+ ["+1"] = "linear",
+ ["+2"] = "square",
+ ["+3"] = "cubic",
+ ["-1"] = "inverse",
+ ["-1"] = "ilinear",
+ ["-2"] = "isquare",
+ ["-3"] = "icubic",
+ ["^1"] = "linear",
+ ["^2"] = "square",
+ ["^3"] = "cubic",
+ ["^+1"] = "linear",
+ ["^+2"] = "square",
+ ["^+3"] = "cubic",
+ ["^-1"] = "inverse",
+ ["^-1"] = "ilinear",
+ ["^-2"] = "isquare",
+ ["^-3"] = "icubic",
}
local symbol_units = {
- Degrees = [[°]],
- Degree = [[°]],
- Deg = [[°]],
- ["°"] = [[°]],
- ArcMinute = [[\checkedtextprime]], -- ′ 0x2032
- ArcSecond = [[\checkedtextdoubleprime]], -- ″ 0x2033
- Percent = [[\percent]],
- Promille = [[\promille]],
- Permille = [[\promille]],
+ Degrees = "degree",
+ Degree = "degree",
+ Deg = "degree",
+ ["°"] = "degree",
+ ArcMinute = "arcminute",
+ ["′"] = "arcminute", -- 0x2032
+ ArcSecond = "arcsecond",
+ ["″"] = "arcsecond", -- 0x2033
+ Percent = "percent",
+ ["%"] = "percent",
+ Promille = "permille",
+ Permille = "permille",
}
local packaged_units = {
- Micron = [[\textmu m]],
+ Micron = "micron",
}
-- rendering:
@@ -357,41 +474,143 @@ local unitsC = context.unitsC
local unitsNstart = context.unitsNstart
local unitsNstop = context.unitsNstop
-local l_prefixes = allocate()
-local l_units = allocate()
-local l_operators = allocate()
-
-local labels = languages.data.labels or { }
+local labels = languages.data.labels
+
+labels.prefixes = {
+ yocto = { labels = { en = [[y]] } }, -- 10^{-24}
+ zepto = { labels = { en = [[z]] } }, -- 10^{-21}
+ atto = { labels = { en = [[a]] } }, -- 10^{-18}
+ femto = { labels = { en = [[f]] } }, -- 10^{-15}
+ pico = { labels = { en = [[p]] } }, -- 10^{-12}
+ nano = { labels = { en = [[n]] } }, -- 10^{-9}
+ micro = { labels = { en = [[\mu]] } }, -- 10^{-6}
+ milli = { labels = { en = [[m]] } }, -- 10^{-3}
+ centi = { labels = { en = [[c]] } }, -- 10^{-2}
+ deci = { labels = { en = [[d]] } }, -- 10^{-1}
+ deca = { labels = { en = [[da]] } }, -- 10^{1}
+ hecto = { labels = { en = [[h]] } }, -- 10^{2}
+ kilo = { labels = { en = [[k]] } }, -- 10^{3}
+ mega = { labels = { en = [[M]] } }, -- 10^{6}
+ giga = { labels = { en = [[G]] } }, -- 10^{9}
+ tera = { labels = { en = [[T]] } }, -- 10^{12}
+ peta = { labels = { en = [[P]] } }, -- 10^{15}
+ exa = { labels = { en = [[E]] } }, -- 10^{18}
+ zetta = { labels = { en = [[Z]] } }, -- 10^{21}
+ yotta = { labels = { en = [[Y]] } }, -- 10^{24}
+ kibi = { labels = { en = [[Ki]] } }, -- 2^{10} (not ki)
+ mebi = { labels = { en = [[Mi]] } }, -- 2^{20}
+ gibi = { labels = { en = [[Gi]] } }, -- 2^{30}
+ tebi = { labels = { en = [[Ti]] } }, -- 2^{40}
+ pebi = { labels = { en = [[Pi]] } }, -- 2^{50}
+ exbi = { labels = { en = [[Ei]] } }, -- 2^{60}
+ zebi = { labels = { en = [[Zi]] } }, -- binary
+ yobi = { labels = { en = [[Yi]] } }, -- binary
+ micro = { labels = { en = [[µ]] } }, -- 0x00B5 \textmu
+ root = { labels = { en = [[√]] } }, -- 0x221A
+}
-labels.prefixes = l_prefixes
-labels.units = l_units
-labels.operators = l_operators
+labels.units = {
+ meter = { labels = { en = [[m]] } },
+ gram = { labels = { en = [[g]] } }, -- strictly kg is the base unit
+ second = { labels = { en = [[s]] } },
+ ampere = { labels = { en = [[A]] } },
+ kelvin = { labels = { en = [[K]] } },
+ mole = { labels = { en = [[mol]] } },
+ candela = { labels = { en = [[cd]] } },
+ mol = { labels = { en = [[mol]] } },
+ radian = { labels = { en = [[rad]] } },
+ steradian = { labels = { en = [[sr]] } },
+ hertz = { labels = { en = [[Hz]] } },
+ newton = { labels = { en = [[N]] } },
+ pascal = { labels = { en = [[Pa]] } },
+ joule = { labels = { en = [[J]] } },
+ watt = { labels = { en = [[W]] } },
+ coulomb = { labels = { en = [[C]] } },
+ volt = { labels = { en = [[V]] } },
+ farad = { labels = { en = [[F]] } },
+ ohm = { labels = { en = [[Ω]] } }, -- 0x2126 \textohm
+ siemens = { labels = { en = [[S]] } },
+ weber = { labels = { en = [[Wb]] } },
+ tesla = { labels = { en = [[T]] } },
+ henry = { labels = { en = [[H]] } },
+ celsius = { labels = { en = [[\checkedtextcelsius]] } }, -- 0x2103
+ lumen = { labels = { en = [[lm]] } },
+ lux = { labels = { en = [[lx]] } },
+ bequerel = { labels = { en = [[Bq]] } },
+ gray = { labels = { en = [[Gr]] } },
+ sievert = { labels = { en = [[Sv]] } },
+ katal = { labels = { en = [[kat]] } },
+ minute = { labels = { en = [[min]] } },
+ hour = { labels = { en = [[h]] } },
+ day = { labels = { en = [[d]] } },
+ gon = { labels = { en = [[gon]] } },
+ grad = { labels = { en = [[grad]] } },
+ hectare = { labels = { en = [[ha]] } },
+ liter = { labels = { en = [[l]] } }, -- symbol l or L
+ tonne = { labels = { en = [[t]] } },
+ electronvolt = { labels = { en = [[eV]] } },
+ dalton = { labels = { en = [[Da]] } },
+ atomicmassunit = { labels = { en = [[u]] } },
+ astronomicalunit = { labels = { en = [[ua]] } },
+ bar = { labels = { en = [[bar]] } },
+-- ["Millimetre Of Mercury"] = { labels = { en = [[mmHg]] } },
+ angstrom = { labels = { en = [[Å]] } }, -- strictly Ångström
+ nauticalmile = { labels = { en = [[M]] } },
+ barn = { labels = { en = [[b]] } },
+ knot = { labels = { en = [[kn]] } },
+ neper = { labels = { en = [[Np]] } },
+ bel = { labels = { en = [[B]] } }, -- in practice only decibel used
+ erg = { labels = { en = [[erg]] } },
+ dyne = { labels = { en = [[dyn]] } },
+ poise = { labels = { en = [[P]] } },
+ stokes = { labels = { en = [[St]] } },
+ stilb = { labels = { en = [[sb]] } },
+ phot = { labels = { en = [[phot]] } },
+ gal = { labels = { en = [[gal]] } },
+ maxwell = { labels = { en = [[Mx]] } },
+ gauss = { labels = { en = [[G]] } },
+ oersted = { labels = { en = [[Oe]] } }, -- strictly Œrsted
+ bit = { labels = { en = [[bit]] } },
+ byte = { labels = { en = [[B]] } },
+ baud = { labels = { en = [[Bd]] } },
+ erlang = { labels = { en = [[E]] } },
+ atmosphere = { labels = { en = [[atm]] } },
+ revolution = { labels = { en = [[rev]] } },
+ fahrenheit = { labels = { en = [[\checkedtextfahrenheit]] } }, -- 0x2109
+ foot = { labels = { en = [[ft]] } },
+ inch = { labels = { en = [[inch]] } },
+ calorie = { labels = { en = [[cal]] } },
+ --
+ degree = { labels = { en = [[°]]} },
+ arcminute = { labels = { en = [[\checkedtextprime]] } }, -- ′ 0x2032
+ arcsecond = { labels = { en = [[\checkedtextdoubleprime]] } }, -- ″ 0x2033
+ percent = { labels = { en = [[\percent]] } },
+ permille = { labels = { en = [[\promille]] } },
+ --
+ micron = { labels = { en = [[\textmu m]] } },
+}
-l_prefixes .test = { Kilo = "kilo" }
-l_units .test = { Meter = "meter", Second = "second" }
-l_operators.test = { Solidus = " per " }
+labels.operators = {
+ times = { labels = { en = [[\unitsTIMES]] } },
+ solidus = { labels = { en = [[\unitsSOLIDUS]] } },
+ per = { labels = { en = [[\unitsSOLIDUS]] } },
+ outof = { labels = { en = [[\unitsOUTOF]] } },
+}
-local prefixes = { }
-local units = { }
-local operators = { }
-local suffixes = { }
+labels.suffixes = {
+ linear = { labels = { en = [[1]] } },
+ square = { labels = { en = [[2]] } },
+ cubic = { labels = { en = [[3]] } },
+ inverse = { labels = { en = [[-1]] } },
+ ilinear = { labels = { en = [[-1]] } },
+ isquare = { labels = { en = [[-2]] } },
+ icubic = { labels = { en = [[-3]] } },
+}
-local function dimpus(p,u,s,wherefrom)
+local function dimpus(p,u,s)
if trace_units then
- report_units("w: [%s], p: [%s], u: [%s], s: [%s]",wherefrom or "?",p or "?",u or "?",s or "?")
- end
- -- local c = connected[u]
- if wherefrom == "" then
- p = prefixes[p] or p
- u = units [u] or u
- else
- local lp = l_prefixes[wherefrom]
- local lu = l_units [wherefrom]
- p = lp and lp[p] or prefixes[p] or p
- u = lu and lu[u] or units [u] or u
- end
- s = suffixes[s] or s
- --
+ report_units("p: [%s], u: [%s], s: [%s]",p or "?",u or "?",s or "?")
+ end --
if p ~= "" then
if u ~= "" then
if s ~= "" then
@@ -421,28 +640,22 @@ local function dimpus(p,u,s,wherefrom)
end
end
-local function dimspu(s,p,u,wherefrom)
- return dimpus(p,u,s,wherefrom)
+local function dimspu(s,p,u)
+ return dimpus(p,u,s)
end
-local function dimop(o,wherefrom)
+local function dimop(o)
if trace_units then
- report_units("w: [%s], o: [%s]",wherefrom or "?",o or "?")
- end
- if wherefrom == "" then
- o = operators[o] or o
- else
- local lo = l_operators[wherefrom]
- o = lo and lo[o] or operators[o] or o
+ report_units("o: [%s]",o or "?")
end
if o then
unitsO(o)
end
end
-local function dimsym(s,wherefrom) -- Do we need to support wherefrom here?
+local function dimsym(s)
if trace_units then
- report_units("w: [%s], s: [%s]",wherefrom,s or "?")
+ report_units("s: [%s]",s or "?")
end
s = symbol_units[s] or s
if s then
@@ -450,9 +663,9 @@ local function dimsym(s,wherefrom) -- Do we need to support wherefrom here?
end
end
-local function dimpre(p,wherefrom) -- Do we need to support wherefrom here?
+local function dimpre(p)
if trace_units then
- report_units("w: [%s], p: [%s]",wherefrom,p or "?")
+ report_units("p: [%s]",p or "?")
end
p = packaged_units[p] or p
if p then
@@ -466,88 +679,103 @@ end
--
-- square centi meter per square kilo seconds
-local function update_parsers()
-
- local long_prefixes_to_long = { } for k, v in next, long_prefixes do long_prefixes_to_long [lower(k)] = k end
- local long_units_to_long = { } for k, v in next, long_units do long_units_to_long [lower(k)] = k end
- local long_operators_to_long = { } for k, v in next, long_operators do long_operators_to_long[lower(k)] = k end
- local short_prefixes = { } for k, v in next, short_prefixes_to_long do short_prefixes [k] = long_prefixes [v] end
- local short_units = { } for k, v in next, short_units_to_long do short_units [k] = long_units [v] end
- local short_operators = { } for k, v in next, short_operators_to_long do short_operators [k] = long_operators[v] end
-
- mergetable(long_suffixes, loweredkeys(long_suffixes))
- mergetable(symbol_units, loweredkeys(symbol_units))
- mergetable(packaged_units,loweredkeys(packaged_units))
-
- prefixes = long_prefixes -- used in above functions
- units = long_units -- used in above functions
- operators = long_operators -- used in above functions
- suffixes = long_suffixes -- used in above functions
-
- local somespace = P(" ")^0/""
-
- local l_prefix = appendlpeg(keys(long_prefixes))
- local l_unit = appendlpeg(keys(long_units))
- local l_operator = appendlpeg(keys(long_operators))
- local l_suffix = appendlpeg(keys(long_suffixes))
-
- local l_prefix = appendlpeg(long_prefixes_to_long,l_prefix)
- local l_unit = appendlpeg(long_units_to_long,l_unit)
- local l_operator = appendlpeg(long_operators_to_long,l_operator)
-
- local s_prefix = appendlpeg(short_prefixes_to_long)
- local s_unit = appendlpeg(short_units_to_long)
- local s_operator = appendlpeg(short_operators_to_long)
-
- local s_suffix = appendlpeg(keys(short_suffixes))
-
- local c_symbol = appendlpeg(keys(symbol_units))
- local p_unit = appendlpeg(keys(packaged_units))
-
- local combination = P { "start",
- l_prefix = Cs(somespace * l_prefix) + Cc(""),
- s_prefix = Cs(somespace * s_prefix) + Cc(""),
- l_unit = Cs(somespace * l_unit),
- s_unit = Cs(somespace * s_unit),
- start = V("l_prefix") * V("l_unit") -- centi meter
- + V("s_prefix") * V("s_unit") -- c m
- + V("l_prefix") * V("s_unit") -- centi m
- + V("s_prefix") * V("l_unit"), -- c meter
+-- todo 0x -> rm
+
+local function update_parsers() -- todo: don't remap utf sequences
+
+ local all_long_prefixes = { }
+ local all_long_units = { }
+ local all_long_operators = { }
+ local all_long_suffixes = { }
+ local all_symbol_units = { }
+ local all_packaged_units = { }
+
+ for k, v in next, long_prefixes do all_long_prefixes [k] = v all_long_prefixes [lower(k)] = v end
+ for k, v in next, long_units do all_long_units [k] = v all_long_units [lower(k)] = v end
+ for k, v in next, long_operators do all_long_operators[k] = v all_long_operators[lower(k)] = v end
+ for k, v in next, long_suffixes do all_long_suffixes [k] = v all_long_suffixes [lower(k)] = v end
+ for k, v in next, symbol_units do all_symbol_units [k] = v all_symbol_units [lower(k)] = v end
+ for k, v in next, packaged_units do all_packaged_units[k] = v all_packaged_units[lower(k)] = v end
+
+ local somespace = P(" ")^0/""
+
+ local p_long_prefix = appendlpeg(all_long_prefixes,nil,true)
+ local p_long_unit = appendlpeg(all_long_units,nil,true)
+ local p_long_operator = appendlpeg(all_long_operators,nil,true)
+ local p_long_suffix = appendlpeg(all_long_suffixes,nil,true)
+ local p_symbol = appendlpeg(all_symbol_units,nil,true)
+ local p_packaged = appendlpeg(all_packaged_units,nil,true)
+
+ local p_short_prefix = appendlpeg(short_prefixes)
+ local p_short_unit = appendlpeg(short_units)
+ local p_short_operator = appendlpeg(short_operators)
+ local p_short_suffix = appendlpeg(short_suffixes)
+
+ -- we can can cleanup some space issues here (todo)
+
+ local unitparser = P { "unit",
+ --
+ longprefix = Cs(V("somespace") * p_long_prefix),
+ shortprefix = Cs(V("somespace") * p_short_prefix),
+ longsuffix = Cs(V("somespace") * p_long_suffix),
+ shortsuffix = Cs(V("somespace") * p_short_suffix),
+ shortunit = Cs(V("somespace") * p_short_unit),
+ longunit = Cs(V("somespace") * p_long_unit),
+ longoperator = Cs(V("somespace") * p_long_operator),
+ shortoperator = Cs(V("somespace") * p_short_operator),
+ packaged = Cs(V("somespace") * p_packaged),
+ --
+ nothing = Cc(""),
+ somespace = somespace,
+ nospace = (1-somespace)^0,
+ ignore = P(-1),
+ --
+ somesymbol = V("somespace")
+ * (p_symbol/dimsym)
+ * V("somespace"),
+ somepackaged = V("somespace")
+ * (V("packaged") / dimpre)
+ * V("somespace"),
+ someunknown = V("somespace")
+ * (V("nospace")/unitsU)
+ * V("somespace"),
+ --
+ combination = V("longprefix") * V("longunit") -- centi meter
+ + V("nothing") * V("longunit")
+ + V("shortprefix") * V("shortunit") -- c m
+ + V("nothing") * V("shortunit")
+ + V("longprefix") * V("shortunit") -- centi m
+ + V("shortprefix") * V("longunit"), -- c meter
+ dimension = V("somespace")
+ * (
+ V("packaged") / dimpre
+ + (V("longsuffix") * V("combination")) / dimspu
+ + (V("combination") * (V("shortsuffix") + V("nothing"))) / dimpus
+ )
+ * V("somespace"),
+ operator = V("somespace")
+ * ((V("longoperator") + V("shortoperator")) / dimop)
+ * V("somespace"),
+ snippet = V("somesymbol")
+ * V("dimension")
+ + V("dimension")
+ + V("somesymbol"),
+ unit = V("snippet")^1
+ * (V("operator") * V("snippet"))^-1 -- V("snippet")^1 ?
+ + V("somesymbol")
+ + V("somepackaged")
+ + V("someunknown")
+ + V("ignore"),
}
- local l_suffix = Cs(somespace * l_suffix)
- local s_suffix = Cs(somespace * s_suffix) + Cc("")
- local l_operator = Cs(somespace * l_operator)
- local p_unit = Cs(somespace * p_unit)
-
- -- todo 0x -> rm
- -- pretty large lpeg (maybe do dimension lookup otherwise)
- -- not ok yet ... we have this p n s problem
-
- local dimension = (p_unit * Carg(1)) / dimpre
- + ((l_suffix * combination) * Carg(1)) / dimspu
- + ((combination * s_suffix) * Carg(1)) / dimpus
- local number = lpeg.patterns.number / unitsN
- local operator = ((l_operator + s_operator) * Carg(1)) / dimop -- weird, why is the extra C needed here
- local whatever = (P(1)^0) / unitsU
- local symbol = c_symbol / dimsym
- local packaged = p_unit / dimpre
-
- local number = (1-R("az","AZ")-P(" "))^1 / unitsN -- todo: catch { }
+-- lpeg.print(unitparser) -- 20111127: 2384 long
- symbol = somespace * symbol * somespace
- packaged = somespace * packaged * somespace
- dimension = somespace * dimension * somespace
- number = somespace * number * somespace
- operator = somespace * operator * somespace
+ local number = lpeg.patterns.number
- dimension = symbol * dimension + dimension + symbol -- too many space tests
-
- local unitparser = dimension^1 * (operator * dimension^1)^-1 -- dimension^-1 ?
- + symbol
- + packaged
- + whatever
- + P(-1)
+ local number = Cs( P("$") * (1-P("$"))^1 * P("$")
+ + P([[\m{]]) * (1-P("}"))^1 * P("}")
+ + (1-R("az","AZ")-P(" "))^1 -- todo: catch { } -- not ok
+ ) / unitsN
local p_c_unitdigitparser = (Cc(nil)/unitsNstart) * p_c_dparser * (Cc(nil)/unitsNstop) --
local c_p_unitdigitparser = (Cc(nil)/unitsNstart) * c_p_dparser * (Cc(nil)/unitsNstop) --
@@ -562,7 +790,7 @@ local p_c_parser = nil
local c_p_parser = nil
local dirty = true
-function commands.unit(str,wherefrom,p_c)
+function commands.unit(str,p_c)
if dirty then
if trace_units then
report_units("initializing parser")
@@ -571,9 +799,9 @@ function commands.unit(str,wherefrom,p_c)
dirty = false
end
if p_c == v_reverse then
- matchlpeg(p_c_parser,str,1,wherefrom or "")
+ matchlpeg(p_c_parser,str)
else
- matchlpeg(c_p_parser,str,1,wherefrom or "")
+ matchlpeg(c_p_parser,str)
end
end
@@ -592,9 +820,9 @@ local t_units = {
}
local t_shortcuts = {
- prefixes = setmetatablenewindex(short_prefixes_to_long,trigger),
- units = setmetatablenewindex(short_units_to_long,trigger),
- operators = setmetatablenewindex(short_operators_to_long,trigger),
+ prefixes = setmetatablenewindex(short_prefixes,trigger),
+ units = setmetatablenewindex(short_units,trigger),
+ operators = setmetatablenewindex(short_operators,trigger),
suffixes = setmetatablenewindex(short_suffixes,trigger),
}
@@ -603,17 +831,25 @@ physics.units.tables = {
shortcuts = t_shortcuts,
}
+local mapping = {
+ prefix = "prefixes",
+ unit = "units",
+ operator = "operators",
+ suffixe = "suffixes",
+ symbol = "symbols",
+ packaged = "packaged",
+}
+
function commands.registerunit(category,list)
if not list or list == "" then
list = category
- category = "units"
+ category = "unit"
end
- local t = t_units[category]
+ local t = t_units[mapping[category]]
if t then
for k, v in next, utilities.parsers.settings_to_hash(list or "") do
t[k] = v
end
end
- inspect(tables)
+ -- inspect(tables)
end
-