Class: RegexpSequence

Inherits:
RegexpTree show all
Includes:
Comparable
Defined in:
app/models/regexp_parse.rb,
app/models/regexp_sequence.rb

Overview

Anchoring

Defined Under Namespace

Modules: Constants, Examples

Constant Summary collapse

Empty_language =

if there is no intersection return Empty_language

RegexpSequence.new([])

Constants inherited from RegexpTree

RegexpTree::Ascii_characters, RegexpTree::Binary_bytes

Instance Method Summary collapse

Methods inherited from RegexpTree

#&, #<=>, [], canonical_regexp, canonical_regexp_tree, #compare_repetitions?, #probability_space_regexp, #probability_space_size, promote, #to_a, #to_regexp, #to_s

Methods included from RegexpTree::ClassMethods

#promote

Methods inherited from NestedArray

#[], #map_branches, #map_recursive, #merge_single_element_arrays?, #promote, #reverse, #to_s

Methods included from NestedArray::Assertions::ClassMethods

#assert_post_conditions

Methods included from NestedArray::Assertions

#assert_post_conditions, #assert_pre_conditions

Constructor Details

#initialize(*nodes) ⇒ RegexpSequence

Parse regexp_string into parse tree for editing


69
70
71
72
73
74
# File 'app/models/regexp_sequence.rb', line 69

def initialize(*nodes)
	nodes.each do |node|
		node=RegexpTree.promote(node)
		self.append(node)
	end #map
end

Instance Method Details

#+(other) ⇒ Object

intersetion should be interpreted as the intersection of the Languages (sets of possible matches) that can be matched by each regexp I == L & R then L >= I && R >= I if no intersetion: I==[] then L >= [] and R >= []


187
188
189
# File 'app/models/regexp_sequence.rb', line 187

def +(other)
	return RegexpSequence.new(self.to_a+other.to_a)
end

#compare_anchors?(other) ⇒ Boolean

probability_of_sequence

Returns:

  • (Boolean)

104
105
106
107
108
109
110
# File 'app/models/regexp_sequence.rb', line 104

def compare_anchors?(other)
	if self.to_s == other.to_s then
		return 0
	else
		Anchoring.new(self) <=> Anchoring.new(other)
	end #if
end

#compare_sequence?(other) ⇒ Boolean

exact_anchor

Returns:

  • (Boolean)

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/regexp_sequence.rb', line 123

def compare_sequence?(other)
	return nil if other.instance_of?(String)
	if self.length==1 || other.length==1 then
		comparison=self[0] <=> other[0]
		if self.length > other.length then
			return -1
		elsif self.length == other.length then
			return comparison
		else
			return 1
		end #if
	else
		comparison=self[0] <=> other[0]
		comparison1=self[1..-1] <=> other[1..-1]
		if comparison.nil? || comparison1.nil? then
			return nil # part incomparable
		else case [comparison, comparison1]
		when [0,0]
			0
		when [1,1], [0,1], [1,0]
			1
		when [-1,-1], [0,-1], [-1,0]
			-1
		when  [1,-1], [-1,1]
			nil
		else
			raise "bad case"
		end #case
		end #if
	end #if
end

#end_anchor(branch = self) ⇒ Object

start_anchor


117
118
119
# File 'app/models/regexp_sequence.rb', line 117

def end_anchor(branch=self)
	RegexpSequence.new([Anchoring.new(self)[:base_regexp], '$'])
end

#exact_anchor(branch = self) ⇒ Object

end_anchor


120
121
122
# File 'app/models/regexp_sequence.rb', line 120

def exact_anchor(branch=self)
	RegexpSequence.new(['^', Anchoring.new(self)[:base_regexp], '$'])
end

#probability_of_sequence(branch = self) ⇒ Object

branch must be a RegexpTree sequence


92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/regexp_sequence.rb', line 92

def probability_of_sequence(branch=self)
	raise "probability_of_sequence branch=#{branch.inspect} must be a kind of Array" unless branch.kind_of?(Array)
	branch.unanchor.reduce(1) do |product, element| 
		if element.instance_of?(String) then
			product * probability_of_repetition(1, 1, element) 
		elsif element.kind_of?(Array) then
			product * probability_of_sequence(RegexpSequence.new(element)) 
		else
			product * probability_of_repetition(element.repetition_length, 1, element.repeated_pattern) 
		end #if
	end #reduce
end

#sequence_intersect(rhs) ⇒ Object


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/models/regexp_sequence.rb', line 156

def sequence_intersect(rhs)
	if rhs.instance_of?(String) then
		if alternatives?.include(rhs) then
			return rhs	
		else
			Empty_language
		end #if
	elsif rhs[0].instance_of?(String) then
		if alternatives?.include(rhs[0]) then
			return rhs[0]	
		else
			Empty_language
		end #if
	elsif self.size==1 || rhs.size==1 then
		self[0].alternatives_intersect(rhs[0])			
	elsif alternatives?.nil? then
		Empty_language
	elsif alternatives?.empty? then
		Empty_language
	else
		first=self[0].alternatives_intersect(rhs[0])
		RegexpSequence.new([first, self[1..-1].alternatives_intersect(rhs[1..-1])])
	end #if
end

#start_anchor(branch = self) ⇒ Object

unanchor


114
115
116
# File 'app/models/regexp_sequence.rb', line 114

def start_anchor(branch=self)
	RegexpSequence.new(['^', Anchoring.new(self)[:base_regexp]])
end

#to_pathname_globObject


74
75
76
# File 'app/models/regexp_parse.rb', line 74

def to_pathname_glob
	map {|node| node.to_pathname_glob}
end

#unanchor(branch = self) ⇒ Object

anchoring


111
112
113
# File 'app/models/regexp_sequence.rb', line 111

def unanchor(branch=self)
	Anchoring.new(self)[:base_regexp]
end