Class: Randexp::Parser

### Home Classes Methods

### In Files

- lib/randexp/parser.rb

### Parent

Object

### Methods

- ::[]
- ::balanced?
- ::intersection
- ::literal
- ::parse
- ::parse_quantified
- ::quantify
- ::quantify_rhs
- ::random
- ::union

### Files

- LICENSE
- README
- TODO

### Class/Module Index

Quicksearch

- Randexp
- Randexp::Dictionary
- Randexp::Parser
- Randexp::Reducer
- Array
- Integer
- Randgen
- Range
- RealName
- Regexp

No matching classes.

# Randexp::Parser

### Public Class Methods

[](source) click to toggle source
Alias for: parse
balanced?(*args) click to toggle source

    # File lib/randexp/parser.rb, line 50
    def self.balanced?(*args)
      args.all? {|s| s.count('(') == s.count(')')}
    end

intersection(lhs, rhs) click to toggle source

    # 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

literal(word) click to toggle source

    # File lib/randexp/parser.rb, line 93
    def self.literal(word)
      [:literal, word]
    end

parse(source) click to toggle source

    # 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

Also aliased as: []
parse_quantified(source, multiplicity) click to toggle source

    # 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

quantify(lhs, sym) click to toggle source

    # File lib/randexp/parser.rb, line 64
    def self.quantify(lhs, sym)
      [:quantify, lhs, sym]
    end

quantify_rhs(sexp, multiplicity) click to toggle source

    # 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

random(char) click to toggle source

    # File lib/randexp/parser.rb, line 89
    def self.random(char)
      [:random, char.to_sym]
    end

union(lhs, *rhs) click to toggle source

    # File lib/randexp/parser.rb, line 68
    def self.union(lhs, *rhs)
      if lhs.nil?
        union(*rhs)
      elsif rhs.empty?
        lhs
      elsif lhs.first == :union
        rhs.each {|s| lhs << s}
        lhs
      else
        [:union, lhs, *rhs]
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.