parsr 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 Jean Boussier
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -5,9 +5,11 @@ class Parsr
5
5
  BASE_PATH = File.expand_path(File.join(File.dirname(__FILE__), 'parsr'))
6
6
 
7
7
  autoload :ArrayRule, "#{BASE_PATH}/array_rule"
8
+ autoload :ConstantsRule, "#{BASE_PATH}/constants_rule"
8
9
  autoload :FloatRule, "#{BASE_PATH}/float_rule"
9
10
  autoload :HashRule, "#{BASE_PATH}/hash_rule"
10
11
  autoload :IntegerRule, "#{BASE_PATH}/integer_rule"
12
+ autoload :RangeRule, "#{BASE_PATH}/range_rule"
11
13
  autoload :RawStringRule, "#{BASE_PATH}/raw_string_rule"
12
14
  autoload :StringRule, "#{BASE_PATH}/string_rule"
13
15
  autoload :SymbolRule, "#{BASE_PATH}/symbol_rule"
@@ -50,6 +52,8 @@ class Parsr
50
52
  @safe_eval_parser ||= self.new(
51
53
  ArrayRule,
52
54
  HashRule,
55
+ RangeRule,
56
+ ConstantsRule,
53
57
  SymbolRule,
54
58
  FloatRule,
55
59
  IntegerRule,
@@ -0,0 +1,13 @@
1
+ module Parsr::ConstantsRule
2
+
3
+ class << self
4
+
5
+ def match(scanner)
6
+ return Parsr::Token.new(nil) if scanner.scan(/nil/)
7
+ return Parsr::Token.new(true) if scanner.scan(/true/)
8
+ return Parsr::Token.new(false) if scanner.scan(/false/)
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -1,6 +1,6 @@
1
1
  module Parsr::FloatRule
2
2
 
3
- PATTERN = /[\-\+]?\d*\.\d+/
3
+ PATTERN = /[\-\+]?\d+\.\d+/
4
4
 
5
5
  class << self
6
6
 
@@ -0,0 +1,21 @@
1
+ module Parsr::RangeRule
2
+
3
+ NUMBER_PATTERN = Regexp.union(Parsr::FloatRule::PATTERN, Parsr::IntegerRule::PATTERN)
4
+ PATTERN = /(#{NUMBER_PATTERN})(\.{2,3})(#{NUMBER_PATTERN})/
5
+
6
+ class << self
7
+
8
+ def match(scanner)
9
+ if scanner.scan(PATTERN)
10
+ scanner.matched =~ PATTERN
11
+ Parsr::Token.new(Range.new(cast($1), cast($3), $2.length > 2))
12
+ end
13
+ end
14
+
15
+ def cast(float_or_integer)
16
+ Integer(float_or_integer) rescue Float(float_or_integer)
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -1,3 +1,3 @@
1
1
  class Parsr
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'
3
3
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Parsr::ConstantsRule do
4
+ include_context 'rule'
5
+
6
+ describe '.match' do
7
+
8
+ it 'should match nil' do
9
+ match('nil').value.should be_nil
10
+ end
11
+
12
+ it 'should match false' do
13
+ match('false').value.should be_false
14
+ end
15
+
16
+ it 'should match true' do
17
+ match('true').value.should be_true
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -21,18 +21,6 @@ describe Parsr::FloatRule do
21
21
  match('-42.42').value.should be == -42.42
22
22
  end
23
23
 
24
- it 'should match floats with a implicit 0 unit' do
25
- match('.42').value.should be == 0.42
26
- end
27
-
28
- it 'should match positive signed floats with a implicit 0 unit' do
29
- match('+.42').value.should be == 0.42
30
- end
31
-
32
- it 'should match negative signed floats with a implicit 0 unit' do
33
- match('-.42').value.should be == -0.42
34
- end
35
-
36
24
  end
37
25
 
38
26
  end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Parsr::RangeRule do
4
+ include_context 'rule'
5
+
6
+ describe '.match' do
7
+
8
+ it 'should match integer ranges' do
9
+ range = match('42..43').value
10
+ range.should be_a(Range)
11
+ range.first.should be == 42
12
+ range.last.should be == 43
13
+ end
14
+
15
+ it 'should match a simple float range' do
16
+ range = match('42.42..43.43').value
17
+ range.should be_a(Range)
18
+ range.first.should be == 42.42
19
+ range.last.should be == 43.43
20
+ end
21
+
22
+ it 'should match positive and negative signed float range' do
23
+ range = match('-42.42..+42.42').value
24
+ range.should be_a(Range)
25
+ range.first.should be == -42.42
26
+ range.last.should be == +42.42
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -40,8 +40,8 @@ describe Parsr do
40
40
 
41
41
  it 'should be able to parse some complexes literals' do
42
42
  Parsr::safe_literal_eval(%q{
43
- [1, "2", foo: {egg: 'spam', 'bar,' => 'plop'}, 3 => 4.2]
44
- }).should be == [1, "2", {:foo => {:egg => 'spam', 'bar,' => 'plop'}, 3 => 4.2}]
43
+ [1, "2", foo: {egg: 'spam', 'bar,' => true}, 3 => 4.2]
44
+ }).should be == [1, "2", {:foo => {:egg => 'spam', 'bar,' => true}, 3 => 4.2}]
45
45
  end
46
46
 
47
47
  it 'should be able to parse an array included in another array' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-13 00:00:00.000000000Z
12
+ date: 2011-11-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70315696184660 !ruby/object:Gem::Requirement
16
+ requirement: &70233276599520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70315696184660
24
+ version_requirements: *70233276599520
25
25
  description:
26
26
  email:
27
27
  - jean.boussier@gmail.com
@@ -32,12 +32,15 @@ files:
32
32
  - .gitignore
33
33
  - .rspec
34
34
  - Gemfile
35
+ - LICENSE
35
36
  - Rakefile
36
37
  - lib/parsr.rb
37
38
  - lib/parsr/array_rule.rb
39
+ - lib/parsr/constants_rule.rb
38
40
  - lib/parsr/float_rule.rb
39
41
  - lib/parsr/hash_rule.rb
40
42
  - lib/parsr/integer_rule.rb
43
+ - lib/parsr/range_rule.rb
41
44
  - lib/parsr/raw_string_rule.rb
42
45
  - lib/parsr/string_rule.rb
43
46
  - lib/parsr/symbol_rule.rb
@@ -45,9 +48,11 @@ files:
45
48
  - lib/parsr/version.rb
46
49
  - parsr.gemspec
47
50
  - spec/parsr/array_rule_spec.rb
51
+ - spec/parsr/constants_rule_spec.rb
48
52
  - spec/parsr/float_rule_spec.rb
49
53
  - spec/parsr/hash_rule_spec.rb
50
54
  - spec/parsr/integer_rule_spec.rb
55
+ - spec/parsr/range_rule_spec.rb
51
56
  - spec/parsr/raw_string_rule_spec.rb
52
57
  - spec/parsr/string_rule_spec.rb
53
58
  - spec/parsr/symbol_rule_spec.rb
@@ -80,9 +85,11 @@ specification_version: 3
80
85
  summary: Simple parser to safe eval ruby literals
81
86
  test_files:
82
87
  - spec/parsr/array_rule_spec.rb
88
+ - spec/parsr/constants_rule_spec.rb
83
89
  - spec/parsr/float_rule_spec.rb
84
90
  - spec/parsr/hash_rule_spec.rb
85
91
  - spec/parsr/integer_rule_spec.rb
92
+ - spec/parsr/range_rule_spec.rb
86
93
  - spec/parsr/raw_string_rule_spec.rb
87
94
  - spec/parsr/string_rule_spec.rb
88
95
  - spec/parsr/symbol_rule_spec.rb