formula_dsl 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/formula_dsl.gemspec +2 -2
- data/lib/formula_dsl/parser.rb +4 -3
- data/spec/formula_dsl/parser_spec.rb +38 -4
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/formula_dsl.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "formula_dsl"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Sergio Junior"]
|
12
|
-
s.date = "2012-04-
|
12
|
+
s.date = "2012-04-11"
|
13
13
|
s.description = " External DSL to express math operations like you do in MSExcel formula bar. The parser generate a AST (Hash) for a given expression. "
|
14
14
|
s.email = "sergior.jr@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/formula_dsl/parser.rb
CHANGED
@@ -6,6 +6,7 @@ module FormulaDSL
|
|
6
6
|
rule(:lparen) { str('(') >> space? }
|
7
7
|
rule(:rparen) { str(')') >> space? }
|
8
8
|
rule(:comma) { str(',') >> space? }
|
9
|
+
rule(:dot) { str('.') >> space? }
|
9
10
|
|
10
11
|
# non significant character's
|
11
12
|
rule(:space) { match('\s').repeat(1) }
|
@@ -14,12 +15,12 @@ module FormulaDSL
|
|
14
15
|
# Literal's
|
15
16
|
rule(:number) { match('\d').repeat(1) }
|
16
17
|
rule(:quote) { match('"') }
|
17
|
-
rule(:string) { quote >> space? >> match('.') >> space? >> quote}
|
18
18
|
rule(:identifier) { match['\w'].repeat(1) }
|
19
|
+
rule(:string) { quote >> space? >> (str('"').absnt? >> any).repeat(1) >> space? >> quote}
|
19
20
|
|
20
21
|
# Argument
|
21
|
-
rule (:argument) { identifier | number }
|
22
|
-
rule (:arglist) { (argument >> comma.maybe).repeat }
|
22
|
+
rule (:argument) { function | identifier | number | string}
|
23
|
+
rule (:arglist) { ( argument >> comma.maybe).repeat }
|
23
24
|
|
24
25
|
# Operators
|
25
26
|
rule(:add_operator ) { match(['+']) }
|
@@ -8,6 +8,16 @@ module FormulaDSL
|
|
8
8
|
|
9
9
|
context "Single Expression's" do
|
10
10
|
|
11
|
+
it "should recognize a number '2' " do
|
12
|
+
ast = parser.parse('2')
|
13
|
+
ast.to_s.should == '2'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should recognize a string '\"abc\"' " do
|
17
|
+
ast = parser.parse('"abc"')
|
18
|
+
ast.to_s.should == %Q("abc")
|
19
|
+
end
|
20
|
+
|
11
21
|
it "should recognize expression '2 + 1' " do
|
12
22
|
ast = parser.parse('2 + 1')
|
13
23
|
ast.to_s.should == %Q({:+=>{:left=>"2"@0, :right=>"1"@4}})
|
@@ -62,12 +72,35 @@ module FormulaDSL
|
|
62
72
|
|
63
73
|
end
|
64
74
|
|
65
|
-
context "
|
75
|
+
context "Functions" do
|
76
|
+
it "should recognize functions with string arguments 'FormatDate(\"MM/yyyy\")'" do
|
77
|
+
ast = parser.parse('FormatDate("MM/yyyy")')
|
78
|
+
ast.to_s.should == %Q({:function=>{:name=>"FormatDate"@0, :args=>"\\\"MM/yyyy\\\""@11}})
|
79
|
+
end
|
80
|
+
|
66
81
|
it "should recognize a function like 'Month(data)' " do
|
67
82
|
ast = parser.parse('Month(data)')
|
68
83
|
ast.to_s.should == %Q({:function=>{:name=>"Month"@0, :args=>"data"@6}})
|
69
84
|
end
|
70
85
|
|
86
|
+
it "should recognize expression with composed functions 'Max( Year(data) )'" do
|
87
|
+
ast = parser.parse('Max( Year(data) )')
|
88
|
+
ast.to_s.should == %Q({:function=>{:name=>"Max"@0, :args=>[{:function=>{:name=>"Year"@5, :args=>"data"@10}}]}})
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should recognize expression with composed functions with 2 arguments 'Max( Year(data), Month(data) )'" do
|
92
|
+
ast = parser.parse('Max(Year(data), Month(data))')
|
93
|
+
ast.to_s.should == %Q({:function=>{:name=>"Max"@0, :args=>[{:function=>{:name=>"Year"@4, :args=>"data"@9}}, {:function=>{:name=>"Month"@16, :args=>"data"@22}}]}})
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should recognize expression with composed functions with 2 arguments 'FormatDate( PriorMonth(data), \"MM/yyyy\" )'" do
|
97
|
+
ast = parser.parse('FormatDate( PriorMonth(data), "MM/yyyy")')
|
98
|
+
ast.to_s.should == %Q({:function=>{:name=>"FormatDate"@0, :args=>[{:function=>{:name=>"PriorMonth"@12, :args=>"data"@23}}]}})
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
context "Composed expression's " do
|
71
104
|
it "should recognize the expression 'Month(data) - 1' " do
|
72
105
|
ast = parser.parse('Month(data) - 1')
|
73
106
|
ast.to_s.should == %Q({:-=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>"1"@14}})
|
@@ -87,6 +120,7 @@ module FormulaDSL
|
|
87
120
|
ast = parser.parse('Month(data) + Year(data)')
|
88
121
|
ast.to_s.should == %Q({:+=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>{:function=>{:name=>"Year"@14, :args=>"data"@19}}}})
|
89
122
|
end
|
123
|
+
|
90
124
|
end
|
91
125
|
|
92
126
|
context "Expressions with string concatenation" do
|
@@ -112,17 +146,17 @@ module FormulaDSL
|
|
112
146
|
|
113
147
|
it " should recognize expression with string concat 'Month(data) - 1 + \"/\" + Year(data)'" do
|
114
148
|
ast = parser.parse('Month(data) - 1 + "/" + Year(data)')
|
115
|
-
ast.to_s.should == %Q({:-=>{:left=>{:function=>{:name
|
149
|
+
ast.to_s.should == %Q({:-=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>{:+=>{:left=>{:+=>{:left=>"1"@14, :right=>"\\\"/\\\""@18}}, :right=>{:function=>{:name=>"Year"@24, :args=>"data"@29}}}}}})
|
116
150
|
end
|
117
151
|
|
118
152
|
it " should recognize expression with string concat 'Month(data) - 1 + \"/\" + Year(data)'" do
|
119
153
|
ast = parser.parse('Month(data) * 1 + "/" + Year(data)')
|
120
|
-
ast.to_s.should == %Q({:*=>{:left=>{:function=>{:name
|
154
|
+
ast.to_s.should == %Q({:*=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>{:+=>{:left=>{:+=>{:left=>"1"@14, :right=>"\\\"/\\\""@18}}, :right=>{:function=>{:name=>"Year"@24, :args=>"data"@29}}}}}})
|
121
155
|
end
|
122
156
|
|
123
157
|
it " should recognize expression with string concat 'Month(data) - 1 + \"/\" + Year(data)'" do
|
124
158
|
ast = parser.parse('Month(data) / 1 + "/" + Year(data)')
|
125
|
-
ast.to_s.should == %Q({:/=>{:left=>{:function=>{:name
|
159
|
+
ast.to_s.should == %Q({:/=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>{:+=>{:left=>{:+=>{:left=>"1"@14, :right=>"\\\"/\\\""@18}}, :right=>{:function=>{:name=>"Year"@24, :args=>"data"@29}}}}}})
|
126
160
|
end
|
127
161
|
|
128
162
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formula_dsl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parslet
|
@@ -125,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
125
|
version: '0'
|
126
126
|
segments:
|
127
127
|
- 0
|
128
|
-
hash: -
|
128
|
+
hash: -6232718774881797
|
129
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
130
|
none: false
|
131
131
|
requirements:
|