Parent

Randexp::Parser

Public Class Methods

balanced?(*args) click to toggle source
    # File lib/randexp/parser.rb, line 50
50:     def self.balanced?(*args)
51:       args.all? {|s| s.count('(') == s.count(')')}
52:     end
intersection(lhs, rhs) click to toggle source
    # File lib/randexp/parser.rb, line 81
81:     def self.intersection(lhs, rhs)
82:       if rhs.first == :intersection
83:         [:intersection, lhs] + rhs[1..1]
84:       else
85:         [:intersection, lhs, rhs]
86:       end
87:     end
literal(word) click to toggle source
    # File lib/randexp/parser.rb, line 93
93:     def self.literal(word)
94:       [:literal, word]
95:     end
parse(source) click to toggle source
    # File lib/randexp/parser.rb, line 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
parse_quantified(source, multiplicity) click to toggle source
    # File lib/randexp/parser.rb, line 36
36:     def self.parse_quantified(source, multiplicity)
37:       case source
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)
43:       end
44:     end
quantify(lhs, sym) click to toggle source
    # File lib/randexp/parser.rb, line 64
64:     def self.quantify(lhs, sym)
65:       [:quantify, lhs, sym]
66:     end
quantify_rhs(sexp, multiplicity) click to toggle source
    # File lib/randexp/parser.rb, line 54
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
random(char) click to toggle source
    # File lib/randexp/parser.rb, line 89
89:     def self.random(char)
90:       [:random, char.to_sym]
91:     end
union(lhs, *rhs) click to toggle source
    # File lib/randexp/parser.rb, line 68
68:     def self.union(lhs, *rhs)
69:       if lhs.nil?
70:         union(*rhs)
71:       elsif rhs.empty?
72:         lhs
73:       elsif lhs.first == :union
74:         rhs.each {|s| lhs << s}
75:         lhs
76:       else
77:         [:union, lhs, *rhs]
78:       end
79:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.