Class: Fabulator::Grammar::Expr::CharSet
- Inherits:
-
Object
- Object
- Fabulator::Grammar::Expr::CharSet
- Defined in:
- lib/fabulator/grammar/expr/char_set.rb
Direct Known Subclasses
Instance Method Summary collapse
- #but_not(c) ⇒ Object
- #compute_regex ⇒ Object
-
#initialize(cs = "") ⇒ CharSet
constructor
A new instance of CharSet.
- #or(c) ⇒ Object
- #set ⇒ Object
- #to_regex ⇒ Object
-
#universal ⇒ Object
for now, we restrict ourselves to 8-bit characters.
Constructor Details
#initialize(cs = "") ⇒ CharSet
Returns a new instance of CharSet.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 5 def initialize(cs = "") @set = BitSet.new if cs[0..0] == '-' @set.on(('-')[0]) cs = cs[1..cs.length-1] end bits = cs.split(/-/) # to pull out ranges if bits.size == 1 bits[0].each_char{ |c| @set.on(c[0]) } elsif bits.size > 1 if bits[0].size > 1 @set.on(bits[0][0]) end while(bits.size > 1) b = bits.shift if b.size > 2 b[1..b.size-2].each_char { |c| @set.on(c[0]) } end @set.on(b[b.size-1] .. bits[0][0]) end if bits[0].size > 1 bits[0][1..bits[0].size-2].each_char { |c| @set.on(c[0]) } end end self.compute_regex end |
Instance Method Details
#but_not(c) ⇒ Object
46 47 48 49 50 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 46 def but_not(c) @set = @set - c.set self.compute_regex self end |
#compute_regex ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 62 def compute_regex # want a compact set of ranges for the regex set_def = '' @set.to_ary.each do |r| if r.is_a?(Range) set_def += Regexp.quote(r.begin.to_i.chr) + '-' + Regexp.quote(r.end.to_i.chr) else set_def += Regexp.quote(r.to_i.chr) end end if set_def == '' @regex = %r{.} else @regex = %r{[#{set_def}]} end end |
#or(c) ⇒ Object
40 41 42 43 44 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 40 def or(c) @set = @set | c.set self.compute_regex self end |
#set ⇒ Object
36 37 38 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 36 def set @set end |
#to_regex ⇒ Object
58 59 60 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 58 def to_regex @regex end |
#universal ⇒ Object
for now, we restrict ourselves to 8-bit characters
53 54 55 56 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 53 def universal @set.on(0..0xff) self.compute_regex end |