Class: LXL::Parser
- Inherits:
-
Object
- Object
- LXL::Parser
- Defined in:
- lib/lxl.rb
Overview
Lexical Types
; Statement separator
, Argument separator
= Formula prefix
( Tuple open
) Tuple close
w Whitespace
o Operator
s String
r Range
p Percentage
f Float
i Integer
u Unknown
F Function
C Constant
Defined Under Namespace
Classes: Token
Constant Summary collapse
- RUBY_OPERATORS =
['+', '-', '*', '/', '<=', '>=', '==', '!=', '<', '>', '+', '**']
- EXCEL_OPERATORS =
['+', '-', '*', '/', '<=', '>=', '=', '<>', '<', '>', '&', '^' ]
Class Method Summary collapse
-
.eval(formula) ⇒ Object
Evaluate a formula.
-
.lexer_class ⇒ Object
Default lexer class.
-
.namespace_class ⇒ Object
Default namespace class.
-
.range_class ⇒ Object
Default range class.
Instance Method Summary collapse
-
#eval(string) ⇒ Object
Evaluate a formula.
-
#initialize(namespace = self.class.namespace_class.new) ⇒ Parser
constructor
Initialize namespace and parser.
Constructor Details
#initialize(namespace = self.class.namespace_class.new) ⇒ Parser
Initialize namespace and parser.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/lxl.rb', line 223 def initialize(namespace=self.class.namespace_class.new) @namespace = namespace ops = EXCEL_OPERATORS.collect { |v| Regexp.quote(v) }.join('|') xlr = LXL::Range::EXCEL_RANGE @lexer = self.class.lexer_class.new([ [/\A;+/, ?;], # Statement separator [/\A,+/, ?,], # Argument separator [/\A`=/, ?=], # Formula prefix [/\A\(/, ?(], # Tuple open [/\A\)/, ?)], # Tuple close [/\A\s+/, ?w], # Whitespace [/\A(#{ops})/, ?o], # Operator [/\A("([^"]|"")*")/m, ?s], # String [xlr, ?r], # Range [/\A\d+(\.\d+)?%/, ?p], # Percentage [/\A\d+\.\d+/, ?f], # Float [/\A\d+/, ?i], # Integer [/\A\w+/, ?u], # Unknown ]) end |
Class Method Details
.eval(formula) ⇒ Object
Evaluate a formula.
199 200 201 |
# File 'lib/lxl.rb', line 199 def self.eval(formula) self.new.eval(formula) end |
.lexer_class ⇒ Object
Default lexer class.
205 206 207 |
# File 'lib/lxl.rb', line 205 def self.lexer_class LXL::Lexer end |
Instance Method Details
#eval(string) ⇒ Object
Evaluate a formula.
246 247 248 249 250 251 |
# File 'lib/lxl.rb', line 246 def eval(string) formulas = tokenize(string) formulas.collect! { |f| f.collect { |t| t.value }.join } formulas.collect! { |f| Kernel.eval(f, binding) } formulas.size == 1 ? formulas.first : formulas end |