yaparc 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/test_metric.rb DELETED
@@ -1,617 +0,0 @@
1
- require 'lib/yaparc.rb'
2
- require 'test/unit'
3
-
4
-
5
- =begin Metric Interchange Syntax
6
-
7
- c.f. http://people.csail.mit.edu/jaffer/MIXF/MIXF-08
8
- Here is a YACC-like syntax for metric quantities (with [ISO 6093] numbers).
9
-
10
-
11
- quantity_value
12
- : numerical_value
13
- | numerical_value '.' unit
14
- ;
15
-
16
- unit
17
- : unit_product
18
- | unit_product '/' single_unit
19
- ;
20
-
21
- unit_product
22
- : single_unit
23
- | unit_product '.' single_unit
24
- ;
25
-
26
- single_unit
27
- : punit
28
- | punit '^' uxponent
29
- | '(' unit ')'
30
- | '(' unit ')^' uxponent
31
- ;
32
-
33
- uxponent
34
- : uinteger
35
- | '-' uinteger
36
- | '(' uinteger '/' uinteger ')'
37
- | '(-' uinteger '/' uinteger ')'
38
- ;
39
-
40
- punit
41
- : decimal_multiple_prefix unit_p_symbol
42
- | decimal_submultiple_prefix unit_n_symbol
43
- | decimal_multiple_prefix unit_b_symbol
44
- | decimal_submultiple_prefix unit_b_symbol
45
- | binary_prefix 'B'
46
- | binary_prefix 'bit'
47
- | unit_p_symbol
48
- | unit_n_symbol
49
- | unit_b_symbol
50
- | unit___symbol
51
- ;
52
-
53
- decimal_multiple_prefix
54
- : 'E' | 'G' | 'M' | 'P' | 'T' | 'Y' | 'Z' | 'da' | 'h' | 'k'
55
- ;
56
-
57
- decimal_submultiple_prefix
58
- : 'a' | 'c' | 'd' | 'f' | 'm' | 'n' | 'p' | 'u' | 'y' | 'z'
59
- ;
60
-
61
- binary_prefix
62
- : 'Ei' | 'Gi' | 'Ki' | 'Mi' | 'Pi' | 'Ti'
63
- ;
64
-
65
- unit_p_symbol
66
- : 'B' | 'Bd' | 'r' | 't'
67
- ;
68
-
69
- unit_n_symbol
70
- : 'L' | 'Np' | 'o' | 'oC' | 'rad' | 'sr'
71
- ;
72
-
73
- unit_b_symbol
74
- : 'A' | 'Bq' | 'C' | 'F' | 'Gy' | 'H' | 'Hz' | 'J' | 'K' | 'N'
75
- | 'Ohm' | 'Pa' | 'S' | 'Sv' | 'T' | 'V' | 'W' | 'Wb' | 'bit'
76
- | 'cd' | 'eV' | 'g' | 'kat' | 'lm' | 'lx' | 'm' | 'mol' | 's'
77
- ;
78
-
79
- unit___symbol
80
- : 'd' | 'dB' | 'h' | 'min' | 'u'
81
- ;
82
-
83
- real
84
- : ureal
85
- | '-' ureal
86
- ;
87
-
88
- ureal
89
- : numerical_value
90
- | numerical_value suffix
91
- ;
92
-
93
- numerical_value
94
- : uinteger
95
- | dot uinteger
96
- | uinteger dot uinteger
97
- | uinteger dot
98
- ;
99
-
100
- dot
101
- : '.' | ','
102
- ;
103
-
104
- uinteger
105
- : digit uinteger
106
- | uinteger
107
- ;
108
-
109
- suffix
110
- : exponent_marker uinteger
111
- | exponent_marker '-' uinteger
112
- ;
113
-
114
- exponent_marker
115
- : 'e' | 'E'
116
- ;
117
-
118
- digit
119
- : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
120
- ;
121
-
122
- =end
123
-
124
-
125
- module MISParser
126
-
127
- =begin
128
- quantity_value
129
- : numerical_value ['.' unit]*
130
- ;
131
- =end
132
- class QuantityValue
133
- include Yaparc::Parsable
134
-
135
- def initialize
136
- @parser = lambda do |input|
137
- Yaparc::Seq.new(NumericalValue.new,
138
- Yaparc::ZeroOne.new(
139
- Yaparc::Seq.new(Yaparc::Literal.new('.'),Unit.new)))
140
- end
141
- end
142
- end
143
-
144
-
145
- =begin
146
- unit
147
- : unit_product ['/' single_unit]*
148
- ;
149
- =end
150
- class Unit
151
- include Yaparc::Parsable
152
-
153
- def initialize
154
- @parser = lambda do |input|
155
- Yaparc::Seq.new(UnitProduct.new,
156
- Yaparc::ZeroOne.new(
157
- Yaparc::Seq.new(Yaparc::Literal.new('/'),SingleUnit.new)))
158
- end
159
- end
160
- end
161
-
162
- =begin
163
- unit_product
164
- : single_unit [unit_product '.' single_unit]*
165
- ;
166
- =end
167
- class UnitProduct
168
- include Yaparc::Parsable
169
-
170
- def initialize
171
- @parser = lambda do |input|
172
- Yaparc::Seq.new(SingleUnit.new,
173
- Yaparc::ZeroOne.new(
174
- Yaparc::Seq.new(UnitProduct.new, Yaparc::Literal.new('.'), SingleUnit.new)))
175
- end
176
- end
177
- end
178
-
179
- =begin
180
- single_unit
181
- : punit
182
- | punit '^' uxponent
183
- | '(' unit ')'
184
- | '(' unit ')^' uxponent
185
- ;
186
- =end
187
- class SingleUnit
188
- include Yaparc::Parsable
189
-
190
- def initialize
191
- @parser = lambda do |input|
192
- Yaparc::Alt.new(
193
- Yaparc::Seq.new(Punit.new,Yaparc::Literal.new('^'),Uxponent.new),
194
- Punit.new,
195
- Yaparc::Seq.new(Yaparc::Literal.new('('),Unit.new,Yaparc::Literal.new(')^'),Uxponent.new),
196
- Yaparc::Seq.new(Yaparc::Literal.new('('),Unit.new,Yaparc::Literal.new(')')))
197
- end
198
- end
199
- end
200
-
201
- =begin
202
- uxponent
203
- : uinteger
204
- | '-' uinteger
205
- | '(' uinteger '/' uinteger ')'
206
- | '(-' uinteger '/' uinteger ')'
207
- ;
208
- =end
209
- class Uxponent
210
- include Yaparc::Parsable
211
-
212
- def initialize
213
- @parser = lambda do |input|
214
- Yaparc::Alt.new(
215
- Uinteger.new,
216
- Yaparc::Seq.new(Yaparc::Literal.new('-'),Uinteger.new),
217
- Yaparc::Seq.new(Yaparc::Literal.new('('),Uinteger.new,Yaparc::Literal.new('/'),Uinteger.new,Yaparc::Literal.new(')')),
218
- Yaparc::Seq.new(Yaparc::Literal.new('(-'),Uinteger.new,Yaparc::Literal.new('/'),Uinteger.new,Yaparc::Literal.new(')')))
219
- end
220
- end
221
- end
222
-
223
- =begin
224
- punit
225
- : decimal_multiple_prefix unit_p_symbol
226
- | decimal_submultiple_prefix unit_n_symbol
227
- | decimal_multiple_prefix unit_b_symbol
228
- | decimal_submultiple_prefix unit_b_symbol
229
- | binary_prefix 'B'
230
- | binary_prefix 'bit'
231
- | unit_p_symbol
232
- | unit_n_symbol
233
- | unit_b_symbol
234
- | unit___symbol
235
- ;
236
- =end
237
- class Punit
238
- include Yaparc::Parsable
239
-
240
- def initialize
241
- @parser = lambda do |input|
242
- Yaparc::Alt.new(
243
- Yaparc::Seq.new(DecimalMultiplePrefix.new, UnitPSymbol.new),
244
- Yaparc::Seq.new(DecimalSubmultiplePrefix.new, UnitNSymbol.new),
245
- Yaparc::Seq.new(DecimalMultiplePrefix.new, UnitBSymbol.new),
246
- Yaparc::Seq.new(DecimalSubmultiplePrefix.new, UnitBSymbol.new),
247
- Yaparc::Seq.new(BinaryPrefix.new, Yaparc::Literal.new('B')),
248
- Yaparc::Seq.new(BinaryPrefix.new, Yaparc::Literal.new('bit')),
249
- UnitPSymbol.new,
250
- UnitNSymbol.new,
251
- UnitBSymbol.new,
252
- UnitSymbol.new)
253
-
254
-
255
- end
256
- end
257
- end
258
-
259
-
260
- =begin
261
- decimal_multiple_prefix
262
- =end
263
- class DecimalMultiplePrefix
264
- include Yaparc::Parsable
265
-
266
- def initialize
267
- @parser = lambda do |input|
268
- Yaparc::Regex.new(/\AE|G|M|P|T|Y|Z|da|h|k/)
269
- end
270
- end
271
- end
272
-
273
- =begin
274
- decimal_submultiple_prefix
275
- : 'a' | 'c' | 'd' | 'f' | 'm' | 'n' | 'p' | 'u' | 'y' | 'z'
276
- ;
277
- =end
278
- class DecimalSubmultiplePrefix
279
- include Yaparc::Parsable
280
-
281
- def initialize
282
- @parser = lambda do |input|
283
- Yaparc::Regex.new(/\A[acdfmnpuyz]/)
284
- end
285
- end
286
- end
287
-
288
- =begin
289
- binary_prefix
290
- : 'Ei' | 'Gi' | 'Ki' | 'Mi' | 'Pi' | 'Ti'
291
- ;
292
- =end
293
- class BinaryPrefix
294
- include Yaparc::Parsable
295
-
296
- def initialize
297
- @parser = lambda do |input|
298
- Yaparc::Regex.new(/\AEi|Gi|Ki|Mi|Pi|Ti/)
299
- end
300
- end
301
- end
302
-
303
- =begin
304
- unit_p_symbol
305
- : 'B' | 'Bd' | 'r' | 't'
306
- ;
307
- =end
308
- class UnitPSymbol
309
- include Yaparc::Parsable
310
-
311
- def initialize
312
- @parser = lambda do |input|
313
- Yaparc::Regex.new(/\ABd|B|r|t/)
314
- end
315
- end
316
- end
317
-
318
- =begin
319
- unit_n_symbol
320
- : 'L' | 'Np' | 'o' | 'oC' | 'rad' | 'sr'
321
- ;
322
- =end
323
- class UnitNSymbol
324
- include Yaparc::Parsable
325
-
326
- def initialize
327
- @parser = lambda do |input|
328
- Yaparc::Regex.new(/\AL|Np|o|oC|rad|sr/)
329
- end
330
- end
331
- end
332
-
333
- =begin
334
- unit_b_symbol
335
- : 'A' | 'Bq' | 'C' | 'F' | 'Gy' | 'H' | 'Hz' | 'J' | 'K' | 'N'
336
- | 'Ohm' | 'Pa' | 'S' | 'Sv' | 'T' | 'V' | 'W' | 'Wb' | 'bit'
337
- | 'cd' | 'eV' | 'g' | 'kat' | 'lm' | 'lx' | 'm' | 'mol' | 's'
338
- ;
339
- =end
340
- class UnitBSymbol
341
- include Yaparc::Parsable
342
-
343
- def initialize
344
- @parser = lambda do |input|
345
- Yaparc::Regex.new(/\AA|Bq|C|F|Gy|H|Hz|J|K|N|Ohm|Pa|S|Sv|T|V|W|Wb|bit|cd|eV|g|kat|lm|lx|m|mol|s/)
346
- end
347
- end
348
- end
349
-
350
- =begin
351
- unit___symbol
352
- : 'd' | 'dB' | 'h' | 'min' | 'u'
353
- ;
354
- =end
355
- class UnitSymbol
356
- include Yaparc::Parsable
357
-
358
- def initialize
359
- @parser = lambda do |input|
360
- Yaparc::Regex.new(/\Ad|dB|h|min|u/)
361
- end
362
- end
363
- end
364
-
365
- =begin
366
- real
367
- : ureal
368
- | '-' ureal
369
- ;
370
- =end
371
- class Real
372
- include Yaparc::Parsable
373
-
374
- def initialize
375
- @parser = lambda do |input|
376
- Yaparc::Seq.new(Yaparc::ZeroOne.new(Yaparc::Literal.new('-'),""),Ureal.new)
377
- end
378
- end
379
- end
380
-
381
- =begin
382
- ureal
383
- : numerical_value
384
- | numerical_value suffix
385
- ;
386
- =end
387
- class Ureal
388
- include Yaparc::Parsable
389
-
390
- def initialize
391
- @parser = lambda do |input|
392
- Yaparc::Seq.new(NumericalValue.new, Yaparc::ZeroOne.new(Suffix.new,""))
393
- end
394
- end
395
- end
396
-
397
- =begin
398
- numerical_value
399
- : uinteger
400
- | dot uinteger
401
- | uinteger dot uinteger
402
- | uinteger dot
403
- ;
404
- =end
405
-
406
- class NumericalValue
407
- include Yaparc::Parsable
408
-
409
- def initialize
410
- @parser = lambda do |input|
411
- Yaparc::Alt.new(Uinteger.new,
412
- Yaparc::Seq.new(Dot.new,Uinteger.new),
413
- Yaparc::Seq.new(Uinteger.new, Dot.new, Yaparc::ZeroOne.new(Uinteger.new,"")))
414
- end
415
- end
416
- end
417
-
418
- =begin
419
- dot
420
- : '.' | ','
421
- ;
422
- =end
423
-
424
- class Dot
425
- include Yaparc::Parsable
426
-
427
- def initialize
428
- @parser = lambda do |input|
429
- Yaparc::Regex.new(/\A[.,]/)
430
- end
431
- end
432
- end
433
-
434
- =begin
435
- suffix
436
- : exponent_marker uinteger
437
- | exponent_marker '-' uinteger
438
- ;
439
- =end
440
-
441
- class Suffix
442
- include Yaparc::Parsable
443
-
444
- def initialize
445
- @parser = lambda do |input|
446
- Yaparc::Seq.new(ExponentMarker.new,
447
- Yaparc::ZeroOne.new(Yaparc::Literal.new('-'),''),
448
- Uinteger.new)
449
- end
450
- end
451
- end
452
-
453
-
454
- =begin
455
- uinteger
456
- : [digit]+
457
- ;
458
- =end
459
- class Uinteger
460
- include Yaparc::Parsable
461
-
462
- def initialize
463
- @parser = lambda do |input|
464
- Yaparc::ManyOne.new(Digit.new,'')
465
- end
466
- end
467
- end
468
-
469
-
470
- =begin
471
- exponent_marker
472
- : 'e' | 'E'
473
- ;
474
- =end
475
-
476
- class ExponentMarker
477
- include Yaparc::Parsable
478
-
479
- def initialize
480
- @parser = lambda do |input|
481
- Yaparc::Regex.new(/\A[eE]/)
482
- end
483
- end
484
- end
485
-
486
- =begin
487
- digit
488
- : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
489
- ;
490
- =end
491
-
492
- class Digit
493
- include Yaparc::Parsable
494
-
495
- def initialize
496
- @parser = lambda do |input|
497
- Yaparc::Regex.new(/\A\d/)
498
- end
499
- end
500
- end
501
-
502
-
503
- end # of MISParser
504
-
505
- class MISTest < Test::Unit::TestCase
506
- include ::Yaparc
507
-
508
- def test_digit
509
- digit = MISParser::Digit.new
510
- assert_instance_of Result::OK, digit.parse("3")
511
- end
512
-
513
- def test_uinteger
514
- uinteger = MISParser::Digit.new
515
- assert_instance_of Result::OK, uinteger.parse("3")
516
- assert_instance_of Result::OK, uinteger.parse("123")
517
- assert_instance_of Result::OK, uinteger.parse("0123")
518
- end
519
-
520
- def test_suffix
521
- suffix = MISParser::Suffix.new
522
- assert_instance_of Result::OK, suffix.parse("e3")
523
- assert_instance_of Result::OK, suffix.parse("E-10")
524
- assert_instance_of Result::OK, suffix.parse("e09876")
525
- end
526
-
527
- def test_numerical_value
528
- numerical_value = MISParser::NumericalValue.new
529
- assert_instance_of Result::OK, numerical_value.parse("3")
530
- assert_instance_of Result::OK, numerical_value.parse("0.9")
531
- assert_instance_of Result::OK, numerical_value.parse(".987")
532
- assert_instance_of Result::OK, numerical_value.parse("0987.")
533
- end
534
-
535
-
536
- def test_ureal
537
- ureal = MISParser::Ureal.new
538
- assert_instance_of Result::OK, ureal.parse("3")
539
- assert_instance_of Result::OK, ureal.parse("0.9e10")
540
- assert_instance_of Result::OK, ureal.parse(".987E-098")
541
- end
542
-
543
- def test_real
544
- real = MISParser::Real.new
545
- assert_instance_of Result::OK, real.parse("-0.9e10")
546
- assert_instance_of Result::OK, real.parse("-.987E-098")
547
- end
548
-
549
- def test_unit
550
- unit = MISParser::Unit.new
551
- assert_instance_of Result::OK, unit.parse("s^-1")
552
- assert_instance_of Result::OK, unit.parse("dm^3")
553
- assert_instance_of Result::OK, unit.parse("rad^2")
554
- assert_instance_of Result::OK, unit.parse("Mg")
555
- assert_instance_of Result::OK, unit.parse("mol/s")
556
- assert_instance_of Result::OK, unit.parse("cd.sr")
557
- assert_instance_of Result::OK, unit.parse("lm/m^2")
558
- assert_instance_of Result::OK, unit.parse("m.kg.s^-2")
559
- assert_instance_of Result::OK, unit.parse("N/m^2")
560
- assert_instance_of Result::OK, unit.parse("N.m")
561
- assert_instance_of Result::OK, unit.parse("J/s")
562
- assert_instance_of Result::OK, unit.parse("s.A")
563
- assert_instance_of Result::OK, unit.parse("W/A")
564
- assert_instance_of Result::OK, unit.parse("C/V")
565
- assert_instance_of Result::OK, unit.parse("V/A")
566
- assert_instance_of Result::OK, unit.parse("A/V")
567
- assert_instance_of Result::OK, unit.parse("V.s")
568
- assert_instance_of Result::OK, unit.parse("Wb/m^2")
569
- assert_instance_of Result::OK, unit.parse("Wb/A")
570
- assert_instance_of Result::OK, unit.parse("m^2.s^-2")
571
- assert_instance_of Result::OK, unit.parse("m^2")
572
- assert_instance_of Result::OK, unit.parse("m^3")
573
- assert_instance_of Result::OK, unit.parse("m/s")
574
- assert_instance_of Result::OK, unit.parse("m/s^2")
575
- assert_instance_of Result::OK, unit.parse("m^-1")
576
- assert_instance_of Result::OK, unit.parse("kg/m^3")
577
- assert_instance_of Result::OK, unit.parse("m^3/kg")
578
- assert_instance_of Result::OK, unit.parse("A/m^2")
579
- assert_instance_of Result::OK, unit.parse("mol/m^3")
580
- assert_instance_of Result::OK, unit.parse("cd/m^3")
581
- assert_instance_of Result::OK, unit.parse("rad/s")
582
- assert_instance_of Result::OK, unit.parse("rad/s^2")
583
- assert_instance_of Result::OK, unit.parse("Pa.s")
584
- assert_instance_of Result::OK, unit.parse("W/m^2")
585
- assert_instance_of Result::OK, unit.parse("W/sr")
586
- assert_instance_of Result::OK, unit.parse("W/(m^2.sr)")
587
- assert_instance_of Result::OK, unit.parse("J/K")
588
- assert_instance_of Result::OK, unit.parse("J/(kg.K)")
589
- assert_instance_of Result::OK, unit.parse("J/kg")
590
- assert_instance_of Result::OK, unit.parse("W/(m.k)")
591
- assert_instance_of Result::OK, unit.parse("J/m^3")
592
- assert_instance_of Result::OK, unit.parse("C/m^3")
593
- assert_instance_of Result::OK, unit.parse("F/m")
594
- assert_instance_of Result::OK, unit.parse("H/m")
595
- assert_instance_of Result::OK, unit.parse("J/mol")
596
- assert_instance_of Result::OK, unit.parse("J/(mol.k)")
597
- assert_instance_of Result::OK, unit.parse("C/kg")
598
- assert_instance_of Result::OK, unit.parse("r/min")
599
- assert_instance_of Result::OK, unit.parse("kat/m^3")
600
- assert_instance_of Result::OK, unit.parse("Mib/s")
601
- assert_instance_of Result::OK, unit.parse("nV/Hz^(1/2)")
602
- end
603
-
604
- def test_quantity_value
605
- quantity_value = MISParser::QuantityValue.new
606
- assert_instance_of Result::OK, quantity_value.parse("60.s")
607
- assert_instance_of Result::OK, quantity_value.parse("60.min")
608
- assert_instance_of Result::OK, quantity_value.parse("24.h")
609
- assert_instance_of Result::OK, quantity_value.parse("6.283185307179586.rad")
610
- assert_instance_of Result::OK, quantity_value.parse("2.777777777777778e-3.r")
611
- assert_instance_of Result::OK, quantity_value.parse("8.bit")
612
- assert_instance_of Result::OK, quantity_value.parse("1.660538782e-27.kg")
613
- assert_instance_of Result::OK, quantity_value.parse("1.602176487e-19.J")
614
- assert_instance_of Result::OK, quantity_value.parse("0.1151293.Np")
615
- end
616
-
617
- end