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

Mixed solution parsing #54

Merged
merged 1 commit into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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