Skip to content
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
1 change: 1 addition & 0 deletions lib/unit/class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
class Unit < Numeric
attr_reader :value, :normalized, :unit, :system

class ParseError < ArgumentError; end
class IncompatibleUnitError < TypeError; end

def initialize(value, unit, system)
Expand Down
8 changes: 4 additions & 4 deletions lib/unit/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def validate_unit(units)
# Unrecognized glyphs survive lexing (see +SYMBOL+) and fail loudly as an
# "Undefined unit" +TypeError+ during validation rather than being dropped.
#
# Raises +SyntaxError+ on unbalanced parentheses.
# Raises +Unit::ParseError+ on unbalanced parentheses.
def parse_unit(expr)
stack, result, implicit_mul = [], [], false
expr.to_s.scan(TOKENIZER).each do |tok|
Expand All @@ -92,7 +92,7 @@ def parse_unit(expr)
implicit_mul = false
elsif tok == ')'
compute(result, stack.pop) while !stack.empty? && stack.last != '('
raise(SyntaxError, 'Unexpected token )') if stack.empty?
raise(Unit::ParseError, 'Unexpected token )') if stack.empty?
stack.pop
implicit_mul = true
elsif OPERATOR.key?(tok)
Expand Down Expand Up @@ -151,12 +151,12 @@ def symbol_to_unit(symbol)

def compute(result, op)
b = result.pop
a = result.pop || raise(SyntaxError, "Unexpected token #{op}")
a = result.pop || raise(Unit::ParseError, "Unexpected token #{op}")
result << case op
when '*' then a + b
when '/' then a + Unit.power_unit(b, -1)
when '^' then Unit.power_unit(a, b[0][1])
else raise SyntaxError, "Unexpected token #{op}"
else raise Unit::ParseError, "Unexpected token #{op}"
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,18 @@
end
end

describe "parse failures for a malformed unit expression" do
it "raises Unit::ParseError for a dangling operator" do
expect { Unit(1, "m").in!("m//s") }.to raise_error(Unit::ParseError, "Unexpected token /")
end

it "raises Unit::ParseError for an unbalanced opening parenthesis" do
expect { Unit(1, "m").in!("(s") }.to raise_error(Unit::ParseError, "Unexpected token (")
end

it "raises Unit::ParseError for an unbalanced closing parenthesis" do
expect { Unit(1, "m").in!(")") }.to raise_error(Unit::ParseError, "Unexpected token )")
end
end

end
Loading