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
-
#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 |
# 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 end |
Instance Method Details
#but_not(c) ⇒ Object
44 45 46 47 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 44 def but_not(c) @set = @set - c.set self end |
#or(c) ⇒ Object
39 40 41 42 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 39 def or(c) @set = @set | c.set self end |
#set ⇒ Object
35 36 37 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 35 def set @set end |
#to_regex ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 54 def to_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 == '' return %r{.} else %r{[#{set_def}]} end end |
#universal ⇒ Object
for now, we restrict ourselves to 8-bit characters
50 51 52 |
# File 'lib/fabulator/grammar/expr/char_set.rb', line 50 def universal @set.on(0..0xff) end |