Skip to content
Kward Search API index

Class: Kward::NativeShell::Arithmetic::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/shell/native_arithmetic.rb

Constant Summary collapse

LEVELS =
[ %w[,], %w[= += -= *= /= %= <<= >>= &= ^= |=], %w[?], %w[||], %w[&&], %w[|], %w[^], %w[&], %w[== !=], %w[< <= > >=], %w[<< >>], %w[+ -], %w[* / %], %w[**] ].freeze
PRECEDENCE =
LEVELS.each_with_index.each_with_object({}) { |(operators, index), result| operators.each { |operator| result[operator] = index + 1 } }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Parser

Returns a new instance of Parser.



167
168
169
170
171
# File 'lib/kward/shell/native_arithmetic.rb', line 167

def initialize(source)
  @scanner = StringScanner.new(source)
  @depth = 0
  advance
end

Instance Method Details

#parse ⇒ Object

Raises:



173
174
175
176
177
# File 'lib/kward/shell/native_arithmetic.rb', line 173

def parse
  result = expression
  raise Error, "unexpected token in arithmetic expression" if @token
  result
end