Class: LXL::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/lxl.rb

Constant Summary collapse

RUBY_OPERATORS =
['+', '-', '*', '/', '<=', '>=', '==', '!=', '<', '>']
EXCEL_OPERATORS =
['+', '-', '*', '/', '<=', '>=', '=',  '<>', '<', '>']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Initialize parser



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lxl.rb', line 89

def initialize

  # Constants
  @constants = {
    :TRUE  => true,
    :FALSE => false,
    :NULL  => nil,
  }
  
  # Functions
  @functions = Hash.new
  register(:AND)   { |a,b| to_b(a) && to_b(b) }
  register(:OR)    { |a,b| to_b(a) || to_b(b) }
  register(:IF)    { |v,a,b| to_b(v) ? a : b }
  register(:LIST)  { |*args| args }
  register(:IN)    { |n,h| h.respond_to?(:include?) ? h.include?(n) : false }
  register(:TODAY) { Date.today.ajd.to_f }
  register(:NOW)   { DateTime.now.ajd.to_f }
  register(:DATE)  { |y,m,d| Date.new(y,m,d).ajd.to_f }
  register(:TIME)  { |h,m,s|
    DateTime.valid_time?(h,m,s) ? DateTime.valid_time?(h,m,s).to_f : raise(ArgumentError, 'invalid time')
  }
  register(:DATETIME) { |value| DateTime.parse(value).ajd.to_f }

  # Lexer
  ops = EXCEL_OPERATORS.collect { |v| Regexp.quote(v) }.join('|')
  #
  @lexer = LXL::LittleLexer.new([
                      [/\A[\s,]+/,?w] ,        # Whitespace (includes Commas)
                      [/\A;+/, ?;],            # Semi-Colon (statement separator)
                      [/\A(#{ops})/,?o],       # Operator
                      [/\A[0-9]+\.[0-9]+/,?f], # Float
                      [/\A[0-9]+/,?i],         # Integer
                      [/\A("[^\"]*")/m,?s],    # String (single quoted)
                      [/\A('[^\']*')/m,?S],    # String (double quoted)
                      [/\A\w+/,?t],            # Token
                      [/\A\(/,?(],             # Open (
                      [/\A\)/,?)],             # Close )
                    ], false)

  # Other
  @tokens = Array.new
  @types = Array.new
end

Instance Attribute Details

#constantsObject (readonly)

Returns the value of attribute constants.



76
77
78
# File 'lib/lxl.rb', line 76

def constants
  @constants
end

#functionsObject (readonly)

Returns the value of attribute functions.



76
77
78
# File 'lib/lxl.rb', line 76

def functions
  @functions
end

#lexerObject (readonly)

Returns the value of attribute lexer.



76
77
78
# File 'lib/lxl.rb', line 76

def lexer
  @lexer
end

#tokensObject (readonly)

Returns the value of attribute tokens.



76
77
78
# File 'lib/lxl.rb', line 76

def tokens
  @tokens
end

#typesObject (readonly)

Returns the value of attribute types.



76
77
78
# File 'lib/lxl.rb', line 76

def types
  @types
end

Class Method Details

.eval(formula) ⇒ Object

Evaluate a formula



83
84
85
# File 'lib/lxl.rb', line 83

def self.eval(formula)
  self.new.eval(formula)
end

Instance Method Details

#eval(formula) ⇒ Object

Evaluate formula



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/lxl.rb', line 136

def eval(formula)
  tokenize(formula.to_s.strip)
  @tokens.pop if @tokens.last == ';'
  if @tokens.include?(';')
    expr = [ [] ]
    @tokens.each do |token|
      if token == ';'
        expr << []
      else
        expr.last << token
      end
    end
    expr.collect { |e| Kernel.eval(e.join, binding) }
  else
    Kernel.eval(@tokens.join, binding)
  end
end