str2hash 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/str2hash.rb +145 -0
  2. metadata +45 -0
data/lib/str2hash.rb ADDED
@@ -0,0 +1,145 @@
1
+ require 'parslet'
2
+
3
+ # Tiny parser for converting strings to hashes
4
+ # karlll <karl@ninjacontrol.com>, 2013-03-23
5
+
6
+ class HashParse < Parslet::Parser
7
+
8
+ rule(:lcbracket) { str('{') >> sp? }
9
+ rule(:rcbracket) { str('}') >> sp? }
10
+ rule(:lbracket) { str('[') >> sp? }
11
+ rule(:rbracket) { str(']') >> sp? }
12
+ rule(:lparen) { str('(') >> sp? }
13
+ rule(:rparen) { str(')') >> sp? }
14
+ rule(:comma) { str(',') >> sp? }
15
+ rule(:larrow) { str('=>') >> sp? }
16
+
17
+ rule(:sp) { match('\s').repeat(1) }
18
+ rule(:sp?) { sp.maybe }
19
+
20
+ # values
21
+
22
+ rule(:hex_integer) {
23
+ ( str('-').maybe >> (str('0x')|str('0X')) >> match['0-9a-fA-F'].repeat(1) ).as(:hex_int) >> sp?
24
+ }
25
+
26
+ rule(:dec_integer) {
27
+ ( str('-').maybe >> match['0-9'].repeat(1) ).as(:dec_int) >> sp?
28
+ }
29
+
30
+ rule(:bin_integer) {
31
+ ( str('-').maybe >> (str('0b') | str('0B')) >> (str('1')|str('0')).repeat(1) ).as(:bin_int) >> sp?
32
+ }
33
+
34
+ rule(:dec_float) {
35
+ ( str('-').maybe >> match['0-9'].repeat(1) >> str('.') >> match['0-9'].repeat(1) ).as(:dec_f) >> sp?
36
+ }
37
+
38
+ rule(:string) {
39
+ (
40
+ (str("'") >> match["^'"].repeat(1).as(:str) >> str("'")) |
41
+ (str('"') >> match['^"'].repeat(1).as(:str) >> str('"'))
42
+ ) >> sp?
43
+ }
44
+
45
+ rule(:pattern) {
46
+ str("/") >> match['^/'].repeat(1).as(:pat) >> str("/") >> sp?
47
+ }
48
+
49
+ rule(:symbol) {
50
+ str(":") >> ( match['a-zA-Z_'] >> match['a-zA-Z0-9_'].repeat ).as(:sym) >> sp?
51
+ }
52
+ rule(:h_symbol) {
53
+ ( match['a-zA-Z_'] >> match['a-zA-Z0-9_'].repeat ).as(:sym) >> str(':') >> sp?
54
+ }
55
+
56
+ rule(:boolean) {
57
+ ( str('true') | str('false') ).as(:boolean) >> sp?
58
+ }
59
+
60
+ rule(:value) {
61
+
62
+ hex_integer |
63
+ dec_float |
64
+ bin_integer |
65
+ dec_integer |
66
+ string |
67
+ pattern |
68
+ symbol |
69
+ boolean
70
+
71
+ }
72
+
73
+ # hashes
74
+
75
+ rule(:key_value) {
76
+ ( value.as(:key) >> larrow | h_symbol.as(:key) ) >> value.as(:val)
77
+ }
78
+
79
+ rule(:key_value_lst) {
80
+ key_value >> (comma >> key_value).repeat
81
+ }
82
+
83
+ rule(:simple_hash) {
84
+ ( lcbracket >> key_value_lst >> rcbracket | key_value_lst ).as(:hash)
85
+ }
86
+
87
+ root :simple_hash
88
+
89
+ end
90
+
91
+ class HashTransform < Parslet::Transform
92
+
93
+ rule(:dec_int => simple(:i)) { Integer(i) }
94
+
95
+ rule(:hex_int => simple(:i)) { Integer(i) }
96
+
97
+ rule(:bin_int => simple(:i)) { Integer(i) }
98
+
99
+ rule(:str => simple(:s)) { s.to_s }
100
+
101
+ rule(:pat => simple(:p)) { /#{p}/ }
102
+
103
+ rule(:sym => simple(:symb)) { symb.to_sym }
104
+
105
+ rule(:dec_f => simple(:f)) { Float(f) }
106
+
107
+ rule(:boolean => simple(:b)) {
108
+ case b
109
+ when "true"
110
+ true
111
+ when "false"
112
+ false
113
+ end
114
+ }
115
+
116
+ rule(:hash => subtree(:h)) {
117
+ (h.is_a?(Array) ? h : [ h ]).inject({}) { |h, e| h[e[:key]] = e[:val] ;h }
118
+ }
119
+
120
+ end
121
+
122
+
123
+ module Str2Hash
124
+
125
+ def str_to_hash(str)
126
+
127
+ end
128
+
129
+ module String
130
+ def to_h
131
+
132
+ p = HashParse.new
133
+ t = HashTransform.new
134
+
135
+ h_str = self.clone
136
+ h_str.strip!
137
+ t.apply(p.parse(self.strip))
138
+
139
+ end
140
+ end
141
+
142
+
143
+ end
144
+
145
+ ::String.send :include, ::Str2Hash::String
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: str2hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - karl l
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Tiny parser for converting strings to hashes (without using eval).
15
+ email: karl@ninjacontrol.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/str2hash.rb
21
+ homepage: https://github.com/karlll/str2hash/
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Convert string to hash
45
+ test_files: []