lxl 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +5 -0
- data/VERSION +1 -1
- data/lib/lxl.rb +14 -9
- metadata +3 -3
data/CHANGES
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/lib/lxl.rb
CHANGED
@@ -48,13 +48,16 @@
|
|
48
48
|
#
|
49
49
|
# w Whitespace (includes Commas)
|
50
50
|
# o Operator
|
51
|
+
# s String
|
51
52
|
# f Float
|
52
53
|
# i Integer
|
53
|
-
# s String
|
54
54
|
# t Token
|
55
55
|
# ( Open (
|
56
56
|
# ) Close )
|
57
57
|
#
|
58
|
+
# F Function
|
59
|
+
# C Constant
|
60
|
+
#
|
58
61
|
module LXL
|
59
62
|
|
60
63
|
module_function
|
@@ -124,7 +127,7 @@ class LXL::Parser
|
|
124
127
|
#
|
125
128
|
def eval(formula)
|
126
129
|
formulas = formula.to_s.split(';').collect { |f| f.strip }.find_all { |f| ! f.empty? }
|
127
|
-
formulas.collect! { |f| Kernel.eval(tokenize_text(f).join, binding) }
|
130
|
+
formulas.collect! { |f| Kernel.eval(tokenize_text(f).last.join, binding) }
|
128
131
|
formulas.size == 1 ? formulas.first : formulas
|
129
132
|
end
|
130
133
|
|
@@ -182,41 +185,43 @@ class LXL::Parser
|
|
182
185
|
tokenize(formula)
|
183
186
|
end
|
184
187
|
|
185
|
-
# Tokenize formula
|
188
|
+
# Tokenize formula into [TypesArray, TokensArray]
|
186
189
|
#
|
187
190
|
def tokenize(formula)
|
188
191
|
ops = Hash[*EXCEL_OPERATORS.zip(RUBY_OPERATORS).flatten]
|
189
192
|
|
190
193
|
# Parse formula
|
191
|
-
types,
|
194
|
+
types,tokens = @lexer.scan(formula.gsub(/^=/,''))
|
192
195
|
raise SyntaxError, 'unbalanced parentheses' unless balanced?(tokens)
|
193
196
|
|
194
197
|
# Parse tokens
|
195
198
|
tokens.each_index do |i|
|
196
199
|
type, token = types[i], tokens[i]
|
197
200
|
tokens[i] = case type
|
198
|
-
when :
|
199
|
-
when :f then token.to_f
|
201
|
+
when :o then ops[token]
|
200
202
|
when :s then token.gsub(/([^\\])""/,'\1\"') # "" to \"
|
203
|
+
when :f then token.to_f
|
204
|
+
when :i then token.to_i
|
201
205
|
when :t then
|
202
206
|
name = name(token)
|
203
207
|
if @functions.key?(name)
|
204
208
|
if tokens[i+1] != '('
|
205
209
|
raise ArgumentError, "wrong number of arguments for #{token}"
|
206
210
|
else
|
211
|
+
types[i] = :F
|
207
212
|
"@functions[:#{name}].call"
|
208
213
|
end
|
209
214
|
elsif @constants.key?(name)
|
215
|
+
types[i] = :C
|
210
216
|
"@constants[:#{name}]"
|
211
217
|
else
|
212
218
|
raise NameError, "unknown constant #{token}"
|
213
219
|
end
|
214
|
-
else
|
215
|
-
EXCEL_OPERATORS.include?(token) ? ops[token] : token
|
220
|
+
else token
|
216
221
|
end
|
217
222
|
end
|
218
223
|
|
219
|
-
tokens
|
224
|
+
[types,tokens]
|
220
225
|
end
|
221
226
|
|
222
227
|
# Check that parentheses are balanced
|
metadata
CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.3
|
|
3
3
|
specification_version: 1
|
4
4
|
name: lxl
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.2.
|
6
|
+
version: 0.2.3
|
7
7
|
date: 2005-02-08
|
8
8
|
summary: LXL (Like Excel) is a mini-language that mimics Microsoft Excel formulas. Easily extended with new constants and functions.
|
9
9
|
require_paths:
|
@@ -28,10 +28,10 @@ authors:
|
|
28
28
|
- Kevin Howe
|
29
29
|
files:
|
30
30
|
- lib
|
31
|
+
- VERSION
|
32
|
+
- README
|
31
33
|
- test
|
32
34
|
- CHANGES
|
33
|
-
- README
|
34
|
-
- VERSION
|
35
35
|
- lib/lxl.rb
|
36
36
|
- test/lxl_test.rb
|
37
37
|
test_files: []
|