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

Commit

Permalink
Merge pull request #54 from kitcheck/mixed_solution
Browse files Browse the repository at this point in the history
Mixed solution parsing
  • Loading branch information
Alex Johnson committed Mar 8, 2016
2 parents 4ed6227 + f0ccc90 commit f703ad3
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 87 deletions.
6 changes: 5 additions & 1 deletion lib/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@

module Unit
def self.parse(string)
#This is a magic gsub to turn separators into @ symbols.
#This will break if you expect a negative number after the initial scalar
#e.g. 1%-1:-1000
string.gsub!(/(?!^)-/, '@')
tokens = Lexer.new.tokenize(string)
Parser.new.parse(tokens)
rescue
raise IncompatibleUnitsError
raise IncompatibleUnitsError.new("#{string} is not a valid unit string")
end

def self.from_scalar_and_uom(scalar, uom)
Expand Down
3 changes: 3 additions & 0 deletions lib/unit/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def _next_token
when (text = @ss.scan(/[:]/i))
action { [:COLON, text] }

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

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

Expand Down
3 changes: 3 additions & 0 deletions lib/unit/lexer_definition.rex
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ macro
UNITLESS_UOM \b(?:ea)\b
EQUIVALENCE_UOM \b(?:meq|eq)\b
COLON [:]
SEPERATOR [@]

rule
{BLANK}
{SCALAR} { [:SCALAR, BigDecimal.new(text, 10)] }
{COLON} { [:COLON, text] }
{SEPERATOR} { [:SEPERATOR, text] }

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

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

Loading

0 comments on commit f703ad3

Please sign in to comment.