formula_dsl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create 1.9.3-p125-perf@treetop
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'parslet', '~> 1.3.0'
4
+ gem 'jeweler', '~> 1.8.3'
5
+ gem 'rake', '~> 0.9.2'
6
+ gem 'rspec', '~> 2.9.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ blankslate (2.1.2.4)
5
+ diff-lcs (1.1.3)
6
+ git (1.2.5)
7
+ jeweler (1.8.3)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rdoc
12
+ json (1.6.5)
13
+ parslet (1.3.0)
14
+ blankslate (~> 2.0)
15
+ rake (0.9.2)
16
+ rdoc (3.12)
17
+ json (~> 1.4)
18
+ rspec (2.9.0)
19
+ rspec-core (~> 2.9.0)
20
+ rspec-expectations (~> 2.9.0)
21
+ rspec-mocks (~> 2.9.0)
22
+ rspec-core (2.9.0)
23
+ rspec-expectations (2.9.0)
24
+ diff-lcs (~> 1.1.3)
25
+ rspec-mocks (2.9.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ jeweler (~> 1.8.3)
32
+ parslet (~> 1.3.0)
33
+ rake (~> 0.9.2)
34
+ rspec (~> 2.9.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Sergio Junior
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ #Formula DSL
2
+
3
+ ### Alpha Version
4
+
5
+ #### Target
6
+ Build a parser that be capable to construct a AST for expression's like:
7
+
8
+ * 1 + 2
9
+ * Month(data) - 1
10
+
11
+ In that fase precedence order is not granted.
12
+
13
+ #### Operator Precedence
14
+ I try do this on branch named **complex_grammar**
15
+
16
+ == Contributing to formula_dsl
17
+
18
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
19
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
20
+ * Fork the project.
21
+ * Start a feature/bugfix branch.
22
+ * Commit and push until you are happy with your contribution.
23
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
24
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
25
+
26
+ == Copyright
27
+
28
+ Copyright (c) 2012 Sergio Junior. See LICENSE.txt for
29
+ further details.
30
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "formula_dsl"
18
+ gem.homepage = "http://github.com/sergiojunior/formula_dsl"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{ External DSL that creates AST representation (Hash) for a given mathematical expression. }
21
+ gem.description = %Q{ External DSL to express math operations like you do in MSExcel formula bar. The parser generate a AST (Hash) for a given expression. }
22
+ gem.email = "sergior.jr@gmail.com"
23
+ gem.authors = ["Sergio Junior"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "formula_dsl #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "formula_dsl"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sergio Junior"]
12
+ s.date = "2012-03-22"
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
+ s.email = "sergior.jr@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ ".rvmrc",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "formula_dsl.gemspec",
30
+ "lib/formula_dsl.rb",
31
+ "lib/formula_dsl/formula_parser.rb",
32
+ "spec/formula_dsl/formula_parser_spec.rb",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = "http://github.com/sergiojunior/formula_dsl"
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = "1.8.17"
39
+ s.summary = "External DSL that creates AST representation (Hash) for a given mathematical expression."
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<parslet>, ["~> 1.3.0"])
46
+ s.add_runtime_dependency(%q<jeweler>, ["~> 1.8.3"])
47
+ s.add_runtime_dependency(%q<rake>, ["~> 0.9.2"])
48
+ s.add_runtime_dependency(%q<rspec>, ["~> 2.9.0"])
49
+ else
50
+ s.add_dependency(%q<parslet>, ["~> 1.3.0"])
51
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
52
+ s.add_dependency(%q<rake>, ["~> 0.9.2"])
53
+ s.add_dependency(%q<rspec>, ["~> 2.9.0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<parslet>, ["~> 1.3.0"])
57
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
58
+ s.add_dependency(%q<rake>, ["~> 0.9.2"])
59
+ s.add_dependency(%q<rspec>, ["~> 2.9.0"])
60
+ end
61
+ end
62
+
@@ -0,0 +1,90 @@
1
+ #encoding: utf-8
2
+ class FormulaParser < Parslet::Parser
3
+
4
+ # Single char rules
5
+ rule(:lparen) { str('(') >> space? }
6
+ rule(:rparen) { str(')') >> space? }
7
+ rule(:comma) { str(',') >> space? }
8
+
9
+ # non significant character's
10
+ rule(:space) { match('\s').repeat(1) }
11
+ rule(:space?) { space.maybe }
12
+
13
+ # Literal's
14
+ rule(:number) { match('\d').repeat(1) }
15
+ rule(:quote) { match('"') }
16
+ rule(:string) { quote >> space? >> match('.') >> space? >> quote}
17
+ rule(:identifier) { match['\w'].repeat(1) }
18
+
19
+ # Argument
20
+ rule (:argument) { identifier | number }
21
+ rule (:arglist) { (argument >> comma.maybe).repeat }
22
+
23
+ # Operators
24
+ rule(:add_operator ) { match(['+']) }
25
+ rule(:sub_operator ) { match('-') }
26
+ rule(:mult_operator) { match(['*']) }
27
+ rule(:div_operator ) { match(['/']) }
28
+
29
+ rule :operator do
30
+ add_operator | sub_operator | mult_operator | div_operator
31
+ end
32
+
33
+ #binary operations + - / *
34
+ rule :addition do
35
+ ( number.as(:left) >> space? >> add_operator >> space? >> ( number ).as(:right) ).as(:+)
36
+ end
37
+
38
+ rule :subtraction do
39
+ ( number.as(:left) >> space? >> sub_operator >> space? >> ( number ).as(:right) ).as(:-)
40
+ end
41
+
42
+ rule :multiplication do
43
+ ( number.as(:left) >> space? >> mult_operator >> space? >> ( number ).as(:right) ).as(:*)
44
+ end
45
+
46
+ rule :division do
47
+ ( number.as(:left) >> space? >> div_operator >> space? >> ( number ).as(:right) ).as(:/)
48
+ end
49
+
50
+ rule :function do
51
+ (identifier.as(:name) >> lparen >> arglist.as(:args) >> rparen).as(:function)
52
+ end
53
+
54
+ rule :string_concat do
55
+ ( string.as(:left) >> space? >> add_operator >> space? >> ( string ).as(:right) ).as(:concat)
56
+ end
57
+
58
+ rule :single_expression do
59
+ function | addition | subtraction | multiplication | division | string_concat | number | string
60
+ end
61
+
62
+ # expression with other expression (compose expression)
63
+ # Ex: expression >> some_op >> number === [ Mont(data) - 1 ]
64
+ rule :subtraction_expression do
65
+ (single_expression.as(:left) >> space? >> sub_operator >> space? >> (single_expression | number).as(:right) ).as(:-)
66
+ end
67
+
68
+ rule :addition_expression do
69
+ (single_expression.as(:left) >> space? >> add_operator >> space? >> (single_expression | number).as(:right) ).as(:+)
70
+ end
71
+
72
+ rule :multiplication_expression do
73
+ (single_expression.as(:left) >> space? >> mult_operator >> space? >> (single_expression | number).as(:right) ).as(:*)
74
+ end
75
+
76
+ rule :division_expression do
77
+ (single_expression.as(:left) >> space? >> div_operator >> space? >> (single_expression | number).as(:right) ).as(:/)
78
+ end
79
+
80
+ rule :string_concat_expression do
81
+ (single_expression.as(:left) >> space? >> add_operator >> space? >> ( expression_list | string ).as(:right) ).as(:concat)
82
+ end
83
+
84
+ rule :expression_list do
85
+ string_concat_expression | addition_expression | subtraction_expression | multiplication_expression | division_expression | single_expression
86
+ end
87
+
88
+ # Entry Point rule
89
+ root :expression_list
90
+ end
@@ -0,0 +1,6 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ Bundler.require(:default)
5
+
6
+ require 'formula_dsl/formula_parser'
@@ -0,0 +1,80 @@
1
+ #encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe FormulaParser do
5
+
6
+ let(:parser){ subject }
7
+
8
+ context "Single Expression's" do
9
+
10
+ it "should recognize expression '2 + 1' " do
11
+ ast = parser.parse('2 + 1')
12
+ ast.to_s.should == %Q({:+=>{:left=>"2"@0, :right=>"1"@4}})
13
+ end
14
+
15
+ it "should recognize expression '2 - 1" do
16
+ ast = parser.parse('2 - 1')
17
+ ast.to_s.should == %Q({:-=>{:left=>"2"@0, :right=>"1"@4}})
18
+ end
19
+
20
+ it "should recognize expression '2 * 1" do
21
+ ast = parser.parse('2 * 1')
22
+ ast.to_s.should == %Q({:*=>{:left=>"2"@0, :right=>"1"@4}})
23
+ end
24
+
25
+ it "should recognize expression '2 / 2" do
26
+ ast = parser.parse('2 / 2')
27
+ ast.to_s.should == %Q({:/=>{:left=>"2"@0, :right=>"2"@4}})
28
+ end
29
+ end
30
+
31
+ context "Composed expression's " do
32
+ it "should recognize a function like 'Month(data)' " do
33
+ ast = parser.parse('Month(data)')
34
+ ast.to_s.should == %Q({:function=>{:name=>"Month"@0, :args=>"data"@6}})
35
+ end
36
+
37
+ it "should recognize the expression 'Month(data) - 1' " do
38
+ ast = parser.parse('Month(data) - 1')
39
+ ast.to_s.should == %Q({:-=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>"1"@14}})
40
+ end
41
+
42
+ it "should recognize the expression 'Month(data) * 1' " do
43
+ ast = parser.parse('Month(data) * 1')
44
+ ast.to_s.should == %Q({:*=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>"1"@14}})
45
+ end
46
+
47
+ it "should recognize the expression 'Month(data) / 1' " do
48
+ ast = parser.parse('Month(data) / 1')
49
+ ast.to_s.should == %Q({:/=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>"1"@14}})
50
+ end
51
+
52
+ it " should recognize expression with 2 functions 'Month(data) + Year(data)'" do
53
+ ast = parser.parse('Month(data) + Year(data)')
54
+ ast.to_s.should == %Q({:concat=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>{:function=>{:name=>"Year"@14, :args=>"data"@19}}}})
55
+ end
56
+ end
57
+
58
+ context "Expressions with string concatenation" do
59
+ it " should recognize expression with string concat \"A\" + \"B\"'" do
60
+ ast = parser.parse('"A" + "B"')
61
+ ast.to_s.should == %Q({:concat=>{:left=>"\\\"A\\\""@0, :right=>"\\\"B\\\""@6}})
62
+ end
63
+
64
+ it " should recognize expression with string concat '\"A\" + \"B\" + \"C\" ' " do
65
+ ast = parser.parse('"A" + "B" + "C"')
66
+ ast.to_s.should == %Q({:concat=>{:left=>{:concat=>{:left=>"\\\"A\\\""@0, :right=>"\\\"B\\\""@6}}, :right=>"\\\"C\\\""@12}})
67
+ end
68
+
69
+ it " should recognize expression with string concat 'Month(data) + \"/\"'" do
70
+ ast = parser.parse('Month(data) + "/"')
71
+ ast.to_s.should == %Q({:concat=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>"\\\"/\\\""@14}})
72
+ end
73
+
74
+ it " should recognize expression with string concat 'Month(data) + \"/\" + Year(data)'" do
75
+ ast = parser.parse('Month(data) + "/" + Year(data)')
76
+ ast.to_s.should == %Q({:concat=>{:left=>{:function=>{:name=>"Month"@0, :args=>"data"@6}}, :right=>{:concat=>{:left=>"\\\"/\\\""@14, :right=>{:function=>{:name=>"Year"@20, :args=>"data"@25}}}}}})
77
+ end
78
+ end
79
+
80
+ end
@@ -0,0 +1 @@
1
+ require './lib/formula_dsl'
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formula_dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sergio Junior
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: parslet
16
+ requirement: &70222683499400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70222683499400
25
+ - !ruby/object:Gem::Dependency
26
+ name: jeweler
27
+ requirement: &70222683498600 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.8.3
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70222683498600
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70222683497400 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.9.2
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70222683497400
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70222683496160 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.9.0
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70222683496160
58
+ description: ! ' External DSL to express math operations like you do in MSExcel formula
59
+ bar. The parser generate a AST (Hash) for a given expression. '
60
+ email: sergior.jr@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files:
64
+ - LICENSE.txt
65
+ - README.md
66
+ files:
67
+ - .document
68
+ - .rspec
69
+ - .rvmrc
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - VERSION
76
+ - formula_dsl.gemspec
77
+ - lib/formula_dsl.rb
78
+ - lib/formula_dsl/formula_parser.rb
79
+ - spec/formula_dsl/formula_parser_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: http://github.com/sergiojunior/formula_dsl
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ segments:
95
+ - 0
96
+ hash: 746988696143926257
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 1.8.17
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: External DSL that creates AST representation (Hash) for a given mathematical
109
+ expression.
110
+ test_files: []