randexp 0.1.0 → 0.1.1

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/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.1.1 "Still Quite Beta"
2
+ * Added Range#of method.
3
+ * Heavy refactoring of the Parser.parse method.
4
+ * Fixed the /\./ bug.
5
+
1
6
  == 0.1.0 "Very Beta" 2008-07-08
2
7
  * Initial version of randexp!
3
8
  * Has support for very simple regular expressions.
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ require 'rake/rdoctask'
7
7
 
8
8
  PROJECT_NAME = "randexp"
9
9
  GEM = "randexp"
10
- GEM_VERSION = "0.1.0"
10
+ GEM_VERSION = "0.1.1"
11
11
  AUTHOR = "Ben Burkert"
12
12
  EMAIL = "ben@benburkert.com"
13
13
  HOMEPAGE = "http://github.com/benburkert/randexp"
@@ -103,7 +103,7 @@ end
103
103
  ##############################################################################
104
104
  # release
105
105
  ##############################################################################
106
- task :release => [:specs, "doc:rubyforge", :package] do
106
+ task :release => [:specs, :package, :doc] do
107
107
  sh %{rubyforge add_release #{PROJECT_NAME} #{GEM} "#{GEM_VERSION}" pkg/#{GEM}-#{GEM_VERSION}.gem}
108
108
  %w[zip tgz].each do |ext|
109
109
  sh %{rubyforge add_file #{PROJECT_NAME} #{GEM} "#{GEM_VERSION}" pkg/#{GEM}-#{GEM_VERSION}.#{ext}}
@@ -2,4 +2,8 @@ class Range
2
2
  def pick
3
3
  to_a.pick
4
4
  end
5
+
6
+ def of(&blk)
7
+ pick.of(&blk)
8
+ end
5
9
  end
@@ -1,34 +1,45 @@
1
1
  class Randexp
2
2
  class Parser
3
3
  def self.parse(source)
4
+ case
5
+ when source =~ /^(.*)(\*|\*\?|\+|\+\?|\?)$/ && balanced?($1, $2)
6
+ parse_quantified($1, $2.to_sym) # ends with *, +, or ?: /(..)?/
7
+ when source =~ /^(.*)\{(\d+)\,(\d+)\}$/ && balanced?($1, $2)
8
+ parse_quantified($1, ($2.to_i)..($3.to_i)) #ends with a range: /(..){..,..}/
9
+ when source =~ /^(.*)\{(\d+)\}$/ && balanced?($1, $2)
10
+ parse_quantified($1, $2.to_i) #ends with a range: /..(..){..}/
11
+ when source =~ /^\((.*)\)\((.*)\)$/ && balanced?($1, $2)
12
+ union(parse($1), parse($2)) #balanced union: /(..)(..)/
13
+ when source =~ /^(\(.*\))\|(\(.*\))$/ && balanced?($1, $2)
14
+ intersection(parse($1), parse($2)) #balanced intersection: /(..)|(..)/
15
+ when source =~ /^(.*)\|(.*)$/ && balanced?($1, $2)
16
+ intersection(parse($1), parse($2)) #implied intersection: /..|../
17
+ when source =~ /^(.*)\|\((\(.*\))\)$/ && balanced?($1, $2)
18
+ intersection(parse($1), parse($2)) #unbalanced intersection: /(..)|((...))/
19
+ when source =~ /^(.+)(\(.*\))$/ && balanced?($1, $2)
20
+ union(parse($1), parse($2)) #unbalanced union: /...(...)/
21
+ when source =~ /^\((.*)\)$/ && balanced?($1)
22
+ union(parse($1)) #explicit group: /(..)/
23
+ when source =~ /^([^()]*)(\(.*\))$/ && balanced?($1, $2)
24
+ union(parse($1), parse($2)) #implied group: /..(..)/
25
+ when source =~ /^(.*)\[\:(.*)\:\]$/
26
+ union(parse($1), random($2)) #custom random: /[:word:]/
27
+ when source =~ /^(.*)\\([wsdc])$/
28
+ union(parse($1), random($2)) #reserved random: /..\w/
29
+ when source =~ /^(.*)\\(.)$/ || source =~ /(.*)(.|\s)$/
30
+ union(parse($1), literal($2)) #end with literal or space: /... /
31
+ else
32
+ nil
33
+ end
34
+ end
35
+
36
+ def self.parse_quantified(source, multiplicity)
4
37
  case source
5
- when /^\(([^()]*|\(.*\))\)$/ then union(parse($1))
6
- when /(.*)\|(?:(\({2,}.*\){2,}|[^()]*))$/ then intersection(parse($1), parse($2))
7
- when /(.*)\|(\(.*\))$/ then intersection(parse($1), parse($2))
8
- when /(.*)\(([^()]*)\)(\*|\*\?|\+|\+\?|\?)$/ then union(parse($1), quantify(parse($2), $3.to_sym))
9
- when /(.*)\(([^()]*)\)\{(\d+)\,(\d+)\}$/ then union(parse($1), quantify(parse($2), ($3.to_i)..($4.to_i)))
10
- when /(.*)\(([^()]*)\)\{(\d+)\}$/ then union(parse($1), quantify(parse($2), $3.to_i))
11
- when /(.*)\(([^()]*)\)$/ then union(parse($1), parse($2))
12
- when /(.*)\[:(\w+):\](\*|\*\?|\+|\+\?|\?)$/ then union(parse($1), quantify(random($2), $3.to_sym))
13
- when /(.*)\[:(\w+):\]\{(\d+)\,(\d+)\}$/ then union(parse($1), quantify(random($2), ($3.to_i)..($4.to_i)))
14
- when /(.*)\[:(\w+):\]\{(\d+)\}$/ then union(parse($1), quantify(random($2), $3.to_i))
15
- when /(.*)\[:(\w+):\]$/ then union(parse($1), random($2))
16
- when /(.*)\\([wsdc])(\*|\*\?|\+|\+\?|\?)$/ then union(parse($1), quantify(random($2), $3.to_sym))
17
- when /(.*)\\([wsdc])\{(\d+)\,(\d+)\}$/ then union(parse($1), quantify(random($2), ($3.to_i)..($4.to_i)))
18
- when /(.*)\\([wsdc])\{(\d+)\}$/ then union(parse($1), quantify(random($2), $3.to_i))
19
- when /(.*)\\([wsdc])$/ then union(parse($1), random($2))
20
- when /\((.*)\)(\*|\*\?|\+|\+\?|\?)$/ then quantify(parse($1), $2.to_sym)
21
- when /\((.*)\)\{(\d+)\,(\d+)\}$/ then quantify(parse($1), ($2.to_i)..($3.to_i))
22
- when /\((.*)\)\{(\d+)\}$/ then quantify(parse($1), $3.to_i)
23
- when /(.*)(.|\s)(\*|\*\?|\+|\+\?|\?)$/ then union(parse($1), quantify(literal($2), $3.to_sym))
24
- when /(.*)(.|\s)\{(\d+)\,(\d+)\}$/ then union(parse($1), quantify(literal($2), ($3.to_i)..($4.to_i)))
25
- when /(.*)(.|\s)\{(\d+)\}$/ then union(parse($1), quantify(literal($2), $3.to_i))
26
- when /(.*)\\([.\/])(\*|\*\?|\+|\+\?|\?)$/ then union(parse($1), quantify(literal($2), $3.to_sym))
27
- when /(.*)\\([.\/])\{(\d+)\,(\d+)\}$/ then union(parse($1), quantify(literal($2), ($3.to_i)..($4.to_i)))
28
- when /(.*)\\([.\/])\{(\d+)\}$/ then union(parse($1), quantify(literal($2), $3.to_i))
29
- when /(.*)\\([.\/])$/ then union(parse($1), literal($2))
30
- when /(.*)(.|\s)$/ then union(parse($1), literal($2))
31
- else nil
38
+ when /^[^()]*$/ then quantify_rhs(parse(source), multiplicity) #implied union: /...+/
39
+ when /^(\(.*\))$/ then quantify(parse(source), multiplicity) #group: /(...)?/
40
+ when /^(.*\))$/ then quantify_rhs(parse(source), multiplicity) #implied union: /...(...)?/
41
+ when /^(.*[^)]+)$/ then quantify_rhs(parse(source), multiplicity) #implied union: /...(...)...?/
42
+ else quantify(parse(source), multiplicity)
32
43
  end
33
44
  end
34
45
 
@@ -36,6 +47,20 @@ class Randexp
36
47
  alias_method :[], :parse
37
48
  end
38
49
 
50
+ def self.balanced?(*args)
51
+ args.all? {|s| s.count('(') == s.count(')')}
52
+ end
53
+
54
+ def self.quantify_rhs(sexp, multiplicity)
55
+ case sexp.first
56
+ when :union
57
+ rhs = sexp.pop
58
+ sexp << quantify(rhs, multiplicity)
59
+ else
60
+ quantify(sexp, multiplicity)
61
+ end
62
+ end
63
+
39
64
  def self.quantify(lhs, sym)
40
65
  [:quantify, lhs, sym]
41
66
  end
@@ -43,6 +43,12 @@ describe "#{'*' * 80}\nRegression Specs:" do
43
43
  end
44
44
  end
45
45
 
46
+ it "/ab(c(def))/ => 'abcdef'" do
47
+ 100.times do
48
+ /ab(c(def))/.gen.should == 'abcdef'
49
+ end
50
+ end
51
+
46
52
  it "/(\\w+)/ => /\\w+/" do
47
53
  100.times do
48
54
  /(\w+)/.gen.should =~ /\w+/
@@ -107,6 +113,18 @@ describe "#{'*' * 80}\nRegression Specs:" do
107
113
  end
108
114
  end
109
115
 
116
+ it "/abc(def)?hij/ => /abc(def)?hij/" do
117
+ 100.times do
118
+ /abc(def)?hij/.gen.should =~ /abc(def)?hij/
119
+ end
120
+ end
121
+
122
+ it "/ab(c(def))?h/ => /ab(c(def))?h/" do
123
+ 100.times do
124
+ /ab(c(def))?h/.gen.should =~ /ab(c(def))?h/
125
+ end
126
+ end
127
+
110
128
  it "/(\\d{3}-)?\\d{3}-\\d{4}/ => /(\\d{3}-)?\\d{3}-\\d{4}/" do
111
129
  100.times do
112
130
  /(\d{3}-)?\d{3}-\d{4}/.gen.should =~ /(\d{3}-)?\d{3}-\d{4}/
@@ -131,9 +149,15 @@ describe "#{'*' * 80}\nRegression Specs:" do
131
149
  end
132
150
  end
133
151
 
134
- it "/\\w+@\\w+\\.(com|org|net)/ => /\\w+@\\w+\\.(com|org|net)/.gen" do
152
+ it "/\\w+@\\w+\\.(com|org|net)/ => /\\w+@\\w+\\.(com|org|net)/.gen" do
135
153
  100.times do
136
154
  /\w+@\w+\.(com|org|net)/.gen.should =~ /\w+@\w+\.(com|org|net)/
137
155
  end
138
156
  end
157
+
158
+ it "/\\$\\d{2,3}\\.\\d{2}/ => /\\$\\d{2,3}\\.\\d{2}/" do
159
+ 100.times do
160
+ /\$\d{2,3}\.\d{2}/.gen.should =~ /\$\d{2,3}\.\d{2}/
161
+ end
162
+ end
139
163
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: randexp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Burkert
@@ -9,7 +9,7 @@ autorequire: randexp
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-18 00:00:00 -05:00
12
+ date: 2008-07-20 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15