class Regexgen::Ast::Alternation

Represents an alternation (e.g. `foo|bar`)

Attributes

options[R]
precedence[R]

Public Class Methods

new(*options) click to toggle source
# File lib/regexgen/ast.rb, line 10
def initialize(*options)
  @precedence = 1
  @options = flatten(options).sort { |a, b| b.length - a.length }
end

Public Instance Methods

length() click to toggle source
# File lib/regexgen/ast.rb, line 15
def length
  @options[0].length
end
to_s() click to toggle source
# File lib/regexgen/ast.rb, line 19
def to_s
  @options.map { |o| Ast.parens(o, self) }.join('|')
end

Private Instance Methods

flatten(options) click to toggle source
# File lib/regexgen/ast.rb, line 25
def flatten(options)
  options.map { |option| option.is_a?(Alternation) ? flatten(option.options) : option }
         .flatten
end