Class: Sangi::Parser
- Inherits:
-
Object
- Object
- Sangi::Parser
- Defined in:
- lib/sangi/parser.rb
Constant Summary collapse
- PATTERN =
/\A\s*([+-]?\d+)\s*([+-])\s*([+-]?\d+)\s*\z/
Instance Method Summary collapse
-
#initialize(config) ⇒ Parser
constructor
A new instance of Parser.
- #parse(source) ⇒ Object
Constructor Details
#initialize(config) ⇒ Parser
Returns a new instance of Parser.
5 6 7 |
# File 'lib/sangi/parser.rb', line 5 def initialize(config) @config = config end |
Instance Method Details
#parse(source) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sangi/parser.rb', line 9 def parse(source) match = PATTERN.match(source.to_s) raise ParseError, unless match operator_text = match[2] right_text = match[3] raise ParseError, if operator_text == "+" && right_text.start_with?("+") left = normalize_zero(match[1].to_i) operator = operator_text.to_sym right = normalize_zero(right_text.to_i) validate_digits!(left) validate_digits!(right) Expression.new( left: left, operator: operator, right: right, source: source.to_s ) end |