Class: Aurum::Grammar::LexicalRules::CharacterSet

Inherits:
Object
  • Object
show all
Defined in:
lib/aurum/grammar/lexical_rules.rb

Defined Under Namespace

Classes: Interval

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(intervals = []) ⇒ CharacterSet

Returns a new instance of CharacterSet.



112
113
114
115
# File 'lib/aurum/grammar/lexical_rules.rb', line 112

def initialize(intervals = [])
  @intervals = intervals
  merge_intervals
end

Instance Attribute Details

#intervalsObject (readonly)

Returns the value of attribute intervals.



111
112
113
# File 'lib/aurum/grammar/lexical_rules.rb', line 111

def intervals
  @intervals
end

Class Method Details

.anyObject



108
109
110
# File 'lib/aurum/grammar/lexical_rules.rb', line 108

def self.any
  range(0, 65535)
end

.enum(literal) ⇒ Object



98
99
100
101
102
# File 'lib/aurum/grammar/lexical_rules.rb', line 98

def self.enum(literal)
  intervals = []
  literal.each_byte {|char| intervals << Interval.new(char)}
  CharacterSet.new(intervals)
end

.range(a, b = a) ⇒ Object



104
105
106
# File 'lib/aurum/grammar/lexical_rules.rb', line 104

def self.range(a, b=a)
  Interval.new(a, b).to_char_set
end

Instance Method Details

#+(other) ⇒ Object



125
126
127
# File 'lib/aurum/grammar/lexical_rules.rb', line 125

def + other
  CharacterSet.new(@intervals + other.intervals)
end

#-(other) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/aurum/grammar/lexical_rules.rb', line 129

def - other
  intervals = @intervals.dup
  for interval in other.intervals
    next unless to_be_replaced = intervals.find {|x| x.include?(interval.first) || x.include?(interval.last)}
    intervals.delete to_be_replaced
    intervals << Interval.new(to_be_replaced.first, interval.first-1) if to_be_replaced.first <= interval.first-1
    intervals << Interval.new(interval.last + 1, to_be_replaced.last) if interval.last + 1 <= to_be_replaced.last
  end
  CharacterSet.new(intervals)
end

#==(other) ⇒ Object



147
148
149
150
# File 'lib/aurum/grammar/lexical_rules.rb', line 147

def == other
  return false unless other.is_a?(Aurum::Grammar::LexicalRules::CharacterSet)
  other.intervals == @intervals
end

#empty?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/aurum/grammar/lexical_rules.rb', line 121

def empty?
  @intervals.empty?
end

#include?(char) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/aurum/grammar/lexical_rules.rb', line 117

def include? char
  @intervals.any? {|interval| interval.include?(char)}
end

#inspectObject



152
153
154
# File 'lib/aurum/grammar/lexical_rules.rb', line 152

def inspect
  @intervals.map{|interval| interval.inspect}.join(',')
end

#to_points(destination) ⇒ Object



140
141
142
143
144
145
# File 'lib/aurum/grammar/lexical_rules.rb', line 140

def to_points destination
  @intervals.inject [] do |points, interval|
    points << Point.new(interval.first, true, destination)
    points << Point.new(interval.last, false, destination)
  end
end