1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
if not modules then modules = { } end modules ['phys-dim'] = {
version = 1.001,
comment = "companion to phys-dim.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- This is pretty old code that I found back, but let's give it a try
-- in practice. It started out as m-units.lua but as we want to keep that
-- module around we moved the code to the dimensions module.
--
-- todo: maybe also an sciunit command that converts to si units (1 inch -> 0.0254 m)
-- etc .. typical something to do when listening to a news whow or b-movie
local P, S, R, C, Cc, Cs, matchlpeg = lpeg.P, lpeg.S, lpeg.R, lpeg.C, lpeg.Cc, lpeg.Cs, lpeg.match
local format = string.format
local appendlpeg = lpeg.append
local mergetable, mergedtable, keys, loweredkeys = table.merge, table.merged, table.keys, table.loweredkeys
physics = physics or { }
physics.patterns = physics.patterns or { }
-- digits parser (todo : use patterns)
--~ local done = false
--~ local mode = 0
local digit = R("09")
local sign = S("+-")
local power = S("^e")
local digitspace = S("~@_")
local digitspacex = digitspace + P(" ")
local comma = P(",")
local period = P(".")
local signspace = P("/")
local positive = S("p")
local negative = S("n")
local highspace = P("s")
local padding = P("=")
local plus = P("+")
local minus = P("-")
local digits = (digit^1)
local ddigitspacex = digitspacex / "" / context.digitsspace
local ddigitspace = digitspace / "" / context.digitsspace
local ddigit = digits / context.digitsdigit
local dseparator = comma / "" / context.digitscomma
+ period / "" / context.digitsperiod
local dsignspace = signspace / "" / context.digitssignspace
local dpositive = positive / "" / context.digitspositive
local dnegative = negative / "" / context.digitsnegative
local dhighspace = highspace / "" / context.digitshighspace
local dsomesign = plus / "" / context.digitsplus
+ minus / "" / context.digitsminus
local dpower = power / "" * (
plus * C(digits) / context.digitspowerplus
+ minus * C(digits) / context.digitspowerminus
+ C(digits) / context.digitspower
)
local dpadding = padding / "" / context.digitszeropadding -- todo
local digitparserspace =
(dsomesign + dsignspace + dpositive + dnegative + dhighspace)^0
* (dseparator^0 * (ddigitspacex + ddigit)^1)^1
* dpower^0
local digitparser =
(dsomesign + dsignspace + dpositive + dnegative + dhighspace)^0
* (dseparator^0 * (ddigitspace + ddigit)^1)^1
* dpower^0
physics.patterns.digitparserspace = digitparserspace
physics.patterns.digitparser = digitparser
function commands.digits(str)
--~ done = false
matchlpeg(digitparserspace,str)
end
-- units parser
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
}
local long_units = {
Meter = [[m]],
Hertz = [[hz]],
Second = [[s]],
Hour = [[h]],
Liter = [[l]],
Litre = [[l]],
Gram = [[g]],
Newton = [[N]],
Pascal = [[Pa]],
Atom = [[u]],
Joule = [[W]],
Watt = [[J]],
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]],
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]],
Bell = [[B]],
At = [[at]],
Atm = [[atm]],
Bar = [[bar]],
Foot = [[ft]],
Inch = [[inch]],
Cal = [[cal]],
Force = [[f]],
Lux = [[lux]],
Gray = [[Gr]],
Weber = [[Wb]],
Henry = [[H]],
Sterant = [[sr]],
Angstrom = [[Å]],
Gauss = [[G]],
Rad = [[rad]],
Deg = [[°]],
RPS = [[RPS]],
RPM = [[RPM]],
RevPerSec = [[RPS]],
RevPerMin = [[RPM]],
Percent = [[\percent]],
Promille = [[\promille]],
}
local long_operators = {
Times = [[\unitsTIMES]], -- cdot
Solidus = [[\unitsSOLIDUS]],
Per = [[\unitsSOLIDUS]],
OutOf = [[\unitsOUTOF]],
}
local long_suffixes = {
Linear = [[1]],
Square = [[2]],
Cubic = [[3]],
Inverse = [[-1]],
ILinear = [[-1]],
ISquare = [[-2]],
ICubic = [[-3]],
}
mergetable(long_prefixes, loweredkeys(long_prefixes))
mergetable(long_units, loweredkeys(long_units))
mergetable(long_operators, loweredkeys(long_operators))
mergetable(long_suffixes, loweredkeys(long_suffixes))
local short_prefixes = {
y = long_prefixes.Yocto,
z = long_prefixes.Zetto,
a = long_prefixes.Atto,
f = long_prefixes.Femto,
p = long_prefixes.Pico,
n = long_prefixes.Nano,
u = long_prefixes.Micro,
m = long_prefixes.Milli,
c = long_prefixes.Centi,
d = long_prefixes.Deci,
da = long_prefixes.Deca,
h = long_prefixes.Hecto,
k = long_prefixes.Kilo,
M = long_prefixes.Mega,
G = long_prefixes.Giga,
T = long_prefixes.Tera,
P = long_prefixes.Peta,
E = long_prefixes.Exa,
Z = long_prefixes.Zetta,
Y = long_prefixes.Yotta,
}
local short_units = {
m = long_units.Meter,
hz = long_units.Hertz,
u = long_units.Hour,
h = long_units.Hour,
s = long_units.Second,
}
local short_operators = {
["."] = long_operators.Times,
["*"] = long_operators.Times,
["/"] = long_operators.Solidus,
[":"] = long_operators.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,
}
local prefixes = mergedtable(long_prefixes,short_prefixes)
local units = mergedtable(long_units,short_units)
local operators = mergedtable(long_operators,short_operators)
local suffixes = mergedtable(long_suffixes,short_suffixes)
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 s_prefix = appendlpeg(keys(short_prefixes))
local s_unit = appendlpeg(keys(short_units))
local s_operator = appendlpeg(keys(short_operators))
local s_suffix = appendlpeg(keys(short_suffixes))
-- space inside Cs else funny captures and args to function
-- square centi meter per square kilo seconds
local l_suffix = Cs(somespace * l_suffix)
local s_suffix = Cs(somespace * s_suffix) + Cc("")
local l_operator = Cs(somespace * l_operator)
local l_combination = (Cs(somespace * l_prefix) + Cc("")) * Cs(somespace * l_unit)
local s_combination = Cs(somespace * s_prefix) * Cs(somespace * s_unit) + Cc("") * Cs(somespace * s_unit)
local combination = l_combination + s_combination
-- square kilo meter
-- square km
local unitsPUS = context.unitsPUS
local unitsPU = context.unitsPU
local unitsPS = context.unitsPS
local unitsP = context.unitsP
local unitsUS = context.unitsUS
local unitsU = context.unitsU
local unitsS = context.unitsS
local unitsO = context.unitsO
local unitsN = context.unitsN
local unitsNstart = context.unitsNstart
local unitsNstop = context.unitsNstop
local function dimpus(p,u,s)
p = prefixes[p] or p
u = units[u] or u
s = suffixes[s] or s
if p ~= "" then
if u ~= "" then
if s ~= "" then
unitsPUS(p,u,s)
else
unitsPU(p,u)
end
elseif s ~= "" then
unitsPS(p,s)
else
unitsP(p)
end
else
if u ~= "" then
if s ~= "" then
unitsUS(u,s)
else
unitsU(u)
end
elseif s ~= "" then
unitsS(s)
else
unitsP(p)
end
end
end
local function dimspu(s,p,u)
return dimpus(p,u,s)
end
local function dimop(o)
o = operators[o] or o
if o then
unitsO(o)
end
end
local dimension = (l_suffix * combination) / dimspu + (combination * s_suffix) / dimpus
local number = lpeg.patterns.number / unitsN
local operator = (l_operator + s_operator) / dimop
local whatever = (P(1)^0) / unitsU
dimension = somespace * dimension * somespace
number = somespace * number * somespace
operator = somespace * operator * somespace
----- unitparser = dimension * dimension^0 * (operator * dimension^1)^-1 + whatever
local unitparser = dimension^1 * (operator * dimension^1)^-1 + whatever
local unitdigitparser = (P(true)/unitsNstart) * digitparser * (P(true)/unitsNstop)
local combinedparser = (unitdigitparser + number)^-1 * unitparser
physics.patterns.unitparser = unitparser
physics.patterns.combinedparser = combinedparser
function commands.unit(str)
matchlpeg(combinedparser,str)
end
|