class RegexpTree

Constants

EmptySequence
EmptySet

Public Class Methods

alt(*rs) click to toggle source
# File lib/regexptree.rb, line 139
def RegexpTree.alt(*rs)
  rs2 = []
  rs.each {|r|
    if r.empty_set?
      next
    elsif Alt === r
      rs2.concat r.rs
    elsif CharClass === r
      if CharClass === rs2.last
        rs2[-1] = CharClass.new(rs2.last.natset + r.natset)
      else
        rs2 << r
      end
    else
      rs2 << r
    end
  }
  case rs2.length
  when 0; EmptySet
  when 1; rs2.first
  else; Alt.new(rs2)
  end
end
backref(n) click to toggle source
# File lib/regexptree.rb, line 465
def RegexpTree.backref(n) Special.new("\\#{n}") end
charclass(natset) click to toggle source
# File lib/regexptree.rb, line 346
def RegexpTree.charclass(natset)
  if natset.empty?
    EmptySet
  else
    CharClass.new(natset)
  end
end
inherited(c) click to toggle source
# File lib/regexptree.rb, line 63
def RegexpTree.inherited(c)
  return if c.superclass != RegexpTree
  c.const_set(:Prec, @curr_prec)
  @curr_prec += 1
end
linebeg() click to toggle source
# File lib/regexptree.rb, line 457
def RegexpTree.linebeg() Special.new('^') end
lineend() click to toggle source
# File lib/regexptree.rb, line 458
def RegexpTree.lineend() Special.new('$') end
non_word_boundary() click to toggle source
# File lib/regexptree.rb, line 463
def RegexpTree.non_word_boundary() Special.new('\B') end
previous_match() click to toggle source
# File lib/regexptree.rb, line 464
def RegexpTree.previous_match() Special.new('\G') end
rep(r, m=0, n=nil, greedy=true) click to toggle source
# File lib/regexptree.rb, line 280
def RegexpTree.rep(r, m=0, n=nil, greedy=true)
  return EmptySequence if m == 0 && n == 0
  return r if m == 1 && n == 1
  return EmptySequence if r.empty_sequence?
  if r.empty_set?
    return m == 0 ? EmptySequence : EmptySet
  end
  Rep.new(r, m, n, greedy)
end
seq(*rs) click to toggle source
# File lib/regexptree.rb, line 205
def RegexpTree.seq(*rs)
  rs2 = []
  rs.each {|r|
    if r.empty_sequence?
      next
    elsif Seq === r
      rs2.concat r.rs
    elsif r.empty_set?
      return EmptySet
    else
      rs2 << r
    end
  }
  case rs2.length
  when 0; EmptySequence
  when 1; rs2.first
  else; Seq.new(rs2)
  end
end
str(str) click to toggle source

def RegexpTree.comment(str) … end # (?#…)

# File lib/regexptree.rb, line 523
def RegexpTree.str(str)
  ccs = []
  str.each_byte {|ch|
    ccs << CharClass.new(NatSet.new(ch))
  }
  seq(*ccs)
end
strbeg() click to toggle source
# File lib/regexptree.rb, line 459
def RegexpTree.strbeg() Special.new('\A') end
strend() click to toggle source
# File lib/regexptree.rb, line 460
def RegexpTree.strend() Special.new('\z') end
strlineend() click to toggle source
# File lib/regexptree.rb, line 461
def RegexpTree.strlineend() Special.new('\Z') end
word_boundary() click to toggle source
# File lib/regexptree.rb, line 462
def RegexpTree.word_boundary() Special.new('\b') end

Public Instance Methods

*(n) click to toggle source
# File lib/regexptree.rb, line 259
def *(n)
  case n
  when Integer
    RegexpTree.rep(self, n, n)
  when Range
    RegexpTree.rep(self, n.first, n.last - (n.exclude_end? ? 1 : 0))
  else
    raise TypeError.new("Integer or Range expected: #{n}")
  end
end
+(other) click to toggle source
# File lib/regexptree.rb, line 202
def +(other)
  RegexpTree.seq(self, other)
end
closure(greedy=true) click to toggle source
# File lib/regexptree.rb, line 274
def closure(greedy=true) RegexpTree.rep(self, 0, nil, greedy) end
empty_sequence?() click to toggle source
# File lib/regexptree.rb, line 132
def empty_sequence?
  false
end
empty_set?() click to toggle source
# File lib/regexptree.rb, line 128
def empty_set?
  false
end
group() click to toggle source
# File lib/regexptree.rb, line 488
def group() Paren.new(self, '') end
inspect() click to toggle source
# File lib/regexptree.rb, line 85
def inspect
  case_insensitive = case_insensitive? ? "i" : ""
  r = PrettyPrint.singleline_format('') {|out|
        (case_insensitive ? self.downcase : self).pretty_format(out)
      }
  if %r{/} =~ r
    "%r{#{r}}#{case_insensitive}"
  else
    "%r/#{r}/#{case_insensitive}"
  end
end
lookahead() click to toggle source
# File lib/regexptree.rb, line 490
def lookahead() Paren.new(self, '?=') end
negative_lookahead() click to toggle source
# File lib/regexptree.rb, line 491
def negative_lookahead() Paren.new(self, '?!') end
nongreedy_closure() click to toggle source
# File lib/regexptree.rb, line 269
def nongreedy_closure() RegexpTree.rep(self, 0, nil, false) end
nongreedy_ntimes(m, n=m) click to toggle source
# File lib/regexptree.rb, line 272
def nongreedy_ntimes(m, n=m) RegexpTree.rep(self, m, n, false) end
nongreedy_optional() click to toggle source
# File lib/regexptree.rb, line 271
def nongreedy_optional() RegexpTree.rep(self, 0, 1, false) end
nongreedy_positive_closure() click to toggle source
# File lib/regexptree.rb, line 270
def nongreedy_positive_closure() RegexpTree.rep(self, 1, nil, false) end
nongreedy_rep(m=0, n=nil) click to toggle source
# File lib/regexptree.rb, line 273
def nongreedy_rep(m=0, n=nil) RegexpTree.rep(self, m, n, false) end
ntimes(m, n=m, greedy=true) click to toggle source
# File lib/regexptree.rb, line 277
def ntimes(m, n=m, greedy=true) RegexpTree.rep(self, m, n, greedy) end
optional(greedy=true) click to toggle source
# File lib/regexptree.rb, line 276
def optional(greedy=true) RegexpTree.rep(self, 0, 1, greedy) end
paren() click to toggle source
# File lib/regexptree.rb, line 489
def paren() Paren.new(self) end
parenthesize(target) click to toggle source
# File lib/regexptree.rb, line 69
def parenthesize(target)
  if target::Prec <= self.class::Prec
    self
  else
    Paren.new(self)
  end
end
positive_closure(greedy=true) click to toggle source
# File lib/regexptree.rb, line 275
def positive_closure(greedy=true) RegexpTree.rep(self, 1, nil, greedy) end
pretty_print(pp) click to toggle source
# File lib/regexptree.rb, line 77
def pretty_print(pp)
  case_insensitive = case_insensitive?
  pp.group(3, '%r{', '}x') {
    (case_insensitive ? self.downcase : self).pretty_format(pp)
  }
  pp.text 'i' if case_insensitive
end
regexp(anchored=false) click to toggle source
# File lib/regexptree.rb, line 97
def regexp(anchored=false)
  if case_insensitive?
    r = downcase
    opt = Regexp::IGNORECASE
  else
    r = self
    opt = 0
  end
  r = RegexpTree.seq(RegexpTree.strbeg, r, RegexpTree.strend) if anchored
  Regexp.compile(
    PrettyPrint.singleline_format('') {|out|
      r.pretty_format(out)
    },
    opt)
end
rep(m=0, n=nil, greedy=true) click to toggle source
# File lib/regexptree.rb, line 278
def rep(m=0, n=nil, greedy=true) RegexpTree.rep(self, m, n, greedy) end
to_s() click to toggle source
# File lib/regexptree.rb, line 113
def to_s
  PrettyPrint.singleline_format('') {|out|
    # x flag is not required because all whitespaces are escaped.
    if case_insensitive?
      out.text '(?i-m:'
      downcase.pretty_format(out)
      out.text ')'
    else
      out.text '(?-im:'
      pretty_format(out)
      out.text ')'
    end
  }
end
|(other) click to toggle source
# File lib/regexptree.rb, line 136
def |(other)
  RegexpTree.alt(self, other)
end