Object
# File lib/randexp/parser.rb, line 50 def self.balanced?(*args) args.all? {|s| s.count('(') == s.count(')')} end
# File lib/randexp/parser.rb, line 81 def self.intersection(lhs, rhs) if rhs.first == :intersection [:intersection, lhs] + rhs[1..-1] else [:intersection, lhs, rhs] end end
# File lib/randexp/parser.rb, line 93 def self.literal(word) [:literal, word] end
# File lib/randexp/parser.rb, line 3 def self.parse(source) case when source =~ /^(.*)(\*|\*\?|\+|\+\?|\?)$/ && balanced?($1, $2) parse_quantified($1, $2.to_sym) # ends with *, +, or ?: /(..)?/ when source =~ /^(.*)\{(\d+)\,(\d+)\}$/ && balanced?($1, $2) parse_quantified($1, ($2.to_i)..($3.to_i)) #ends with a range: /(..){..,..}/ when source =~ /^(.*)\{(\d+)\}$/ && balanced?($1, $2) parse_quantified($1, $2.to_i) #ends with a range: /..(..){..}/ when source =~ /^\((.*)\)\((.*)\)$/ && balanced?($1, $2) union(parse($1), parse($2)) #balanced union: /(..)(..)/ when source =~ /^(\(.*\))\|(\(.*\))$/ && balanced?($1, $2) intersection(parse($1), parse($2)) #balanced intersection: /(..)|(..)/ when source =~ /^(.*)\|(.*)$/ && balanced?($1, $2) intersection(parse($1), parse($2)) #implied intersection: /..|../ when source =~ /^(.*)\|\((\(.*\))\)$/ && balanced?($1, $2) intersection(parse($1), parse($2)) #unbalanced intersection: /(..)|((...))/ when source =~ /^(.+)(\(.*\))$/ && balanced?($1, $2) union(parse($1), parse($2)) #unbalanced union: /...(...)/ when source =~ /^\((.*)\)$/ && balanced?($1) union(parse($1)) #explicit group: /(..)/ when source =~ /^([^()]*)(\(.*\))$/ && balanced?($1, $2) union(parse($1), parse($2)) #implied group: /..(..)/ when source =~ /^(.*)\[\:(.*)\:\]$/ union(parse($1), random($2)) #custom random: /[:word:]/ when source =~ /^(.*)\\([wsdc])$/ union(parse($1), random($2)) #reserved random: /..\w/ when source =~ /^(.*)\\(.)$/ || source =~ /(.*)(.|\s)$/ union(parse($1), literal($2)) #end with literal or space: /... / else nil end end
# File lib/randexp/parser.rb, line 36 def self.parse_quantified(source, multiplicity) case source when /^[^()]*$/ then quantify_rhs(parse(source), multiplicity) #implied union: /...+/ when /^(\(.*\))$/ then quantify(parse(source), multiplicity) #group: /(...)?/ when /^(.*\))$/ then quantify_rhs(parse(source), multiplicity) #implied union: /...(...)?/ when /^(.*[^)]+)$/ then quantify_rhs(parse(source), multiplicity) #implied union: /...(...)...?/ else quantify(parse(source), multiplicity) end end
# File lib/randexp/parser.rb, line 64 def self.quantify(lhs, sym) [:quantify, lhs, sym] end
# File lib/randexp/parser.rb, line 54 def self.quantify_rhs(sexp, multiplicity) case sexp.first when :union rhs = sexp.pop sexp << quantify(rhs, multiplicity) else quantify(sexp, multiplicity) end end
Generated with the Darkfish Rdoc Generator 2.