Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
[Finishes #114336031] Adds support for parsing ratios
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex committed Feb 23, 2016
1 parent 675aad7 commit a740527
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 80 deletions.
3 changes: 3 additions & 0 deletions lib/unit/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def _next_token
when (text = @ss.scan(/[-+]?[0-9]*\.?[0-9]+/i))
action { [:SCALAR, BigDecimal.new(text, 10)] }

when (text = @ss.scan(/[:]/i))
action { [:COLON, text] }

when (text = @ss.scan(/\b(?:gm|gram)\b/i))
action { [:MASS_UOM, 'g'] }

Expand Down
2 changes: 2 additions & 0 deletions lib/unit/lexer_definition.rex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ macro
UNIT_UOM \b(?:unit|u)\b
UNITLESS_UOM \b(?:ea)\b
EQUIVALENCE_UOM \b(?:meq|eq)\b
COLON [:]
rule
{BLANK}
{SCALAR} { [:SCALAR, BigDecimal.new(text, 10)] }
{COLON} { [:COLON, text] }

#Mass
\b(?:gm|gram)\b { [:MASS_UOM, 'g'] }
Expand Down
170 changes: 92 additions & 78 deletions lib/unit/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions lib/unit/parser_definition.y
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Unit::Parser
token SCALAR MASS_UOM VOLUME_UOM UNIT_UOM UNITLESS_UOM EQUIVALENCE_UOM PERCENT SLASH
token SCALAR MASS_UOM VOLUME_UOM UNIT_UOM UNITLESS_UOM EQUIVALENCE_UOM PERCENT SLASH COLON
rule
valid_unit:
concentration |
Expand All @@ -16,7 +16,8 @@ rule
unit |
unitless |
equivalence |
percent
percent |
solution

concentration : mass SLASH volume { return Concentration.new(val[0], val[2]) }
concentration_no_denom_scalar : mass SLASH VOLUME_UOM { return Concentration.new(val[0], Volume.new(1, val[2])) }
Expand All @@ -43,6 +44,8 @@ rule

equivalence : SCALAR EQUIVALENCE_UOM { return Equivalence.new(val[0], val[1]) }

solution : SCALAR COLON SCALAR { return Concentration.new(Mass.new(val[0] * 1000, 'mg'), Volume.new(val[2], 'ml')) }

end

---- header
Expand Down
10 changes: 10 additions & 0 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,15 @@ class ParserTest < Minitest::Test
assert_equal "mg/ml", conc.uom
end
end

context "solution" do
should "parse a normal solution" do
conc = Unit.parse("1:1000")

assert_equal true, conc.concentration?
assert_equal 1, conc.scalar
assert_equal "mg/ml", conc.uom
end
end
end
end

0 comments on commit a740527

Please sign in to comment.