Method: Cyc::Client#check_parenthesis
- Defined in:
- lib/cyc/client.rb
#check_parenthesis(message) ⇒ Object
Scans the message
to find out if the parenthesis are matched. Raises UnbalancedClosingParenthesis exception if there is a not matched closing parenthesis. The message of the exception contains the string with the unmatched parenthesis highlighted. Raises UnbalancedOpeningParenthesis exception if there is a not matched opening parenthesis.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/cyc/client.rb', line 182 def check_parenthesis() count = 0 .scan(/[()]/) do |char| count += (char == "(" ? 1 : -1) if count < 0 # this *is* thread safe position = $~.offset(0)[0] raise UnbalancedClosingParenthesis. new((position > 1 ? [0...position] : "") + "<error>)</error>" + [position+1..-1]) end end raise UnbalancedOpeningParenthesis.new(count) if count > 0 end |