Class: RegexpParse

Inherits:
Object show all
Extended by:
ClassMethods
Includes:
Regexp::Constants, Constants
Defined in:
app/models/regexp_parse.rb

Overview

parse tree internal format is nested Arrays. Postfix operators and brackets end embeddded arrays

Defined Under Namespace

Modules: ClassMethods, Constants

Constant Summary

Constants included from Constants

Constants::Any_binary_char, Constants::Any_binary_char_parse, Constants::Any_binary_string, Constants::Any_binary_string_parse, Constants::ClosingBrackets, Constants::Dot_star_array, Constants::Dot_star_parse, Constants::Dot_star_string, Constants::Empty_language_array, Constants::Empty_language_parse, Constants::Empty_language_string, Constants::OpeningBrackets, Constants::PostfixOperators

Constants included from Regexp::Constants

Regexp::Constants::Any, Regexp::Constants::Any_binary_char_string, Regexp::Constants::Default_options, Regexp::Constants::End_string, Regexp::Constants::End_string_less_newline, Regexp::Constants::Many, Regexp::Constants::Optional, Regexp::Constants::Start_string

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp_string, options = Default_options) ⇒ RegexpParse

Returns a new instance of RegexpParse.


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/regexp_parse.rb', line 111

def initialize(regexp_string, options=Default_options)
	@tokenIndex=-1 # start at end
	if regexp_string.kind_of?(RegexpParse) then
		@parse_tree=NestedArray.new(regexp_string.parse_tree)
		@regexp_string=regexp_string.to_s
		@errors=regexp_string.errors	
	elsif regexp_string.kind_of?(Array) then
		@parse_tree=NestedArray.new(regexp_string)
		@regexp_string=regexp_string.join	
		@errors=RegexpParse.regexp_error(@regexp_string, options)
	elsif regexp_string.kind_of?(String) then
		@regexp_string=regexp_string
		restartParse!
		@parse_tree=regexpTree!
		@errors=RegexpParse.regexp_error(@regexp_string, options)
	elsif regexp_string.kind_of?(Regexp) then
		@regexp_string=regexp_string.source
		restartParse!
		@parse_tree=regexpTree!
		@errors=RegexpParse.regexp_error(@regexp_string, options)
	else # unexpected
		raise "unexpected type regexp_string=#{regexp_string} of type #{regexp_string.class.name}"
	
	end #if
	@parse_tree=@parse_tree.merge_single_element_arrays?
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors


101
102
103
# File 'app/models/regexp_parse.rb', line 101

def errors
  @errors
end

#parse_treeObject (readonly)

Returns the value of attribute parse_tree


101
102
103
# File 'app/models/regexp_parse.rb', line 101

def parse_tree
  @parse_tree
end

#regexp_stringObject (readonly)

Returns the value of attribute regexp_string


101
102
103
# File 'app/models/regexp_parse.rb', line 101

def regexp_string
  @regexp_string
end

#tokenIndexObject (readonly)

Returns the value of attribute tokenIndex


101
102
103
# File 'app/models/regexp_parse.rb', line 101

def tokenIndex
  @tokenIndex
end

Class Method Details

.bracket_operator?(node) ⇒ Boolean

detect bracket operator (not postfix characters) node is parse tree or string to test (not Regexp) unlike postfix expression node, only last node should be passed return bracket parse tree - bracket expression detected return closing bracket character - closing bracket character return nil - no bracket

Returns:

  • (Boolean)

239
240
241
242
243
244
245
246
247
248
249
# File 'app/models/regexp_parse.rb', line 239

def RegexpParse.bracket_operator?(node)
	if node.kind_of?(NestedArray) && node[-1]=='}' then
		return node 
	elsif node.kind_of?(RegexpParse) && node.parse_tree[-1]=='}' then
		return node.parse_tree 
	elsif node.kind_of?(String) && node=='}' then
		return node 
	else
		return nil	
	end #if
end

.operator_range(postfix_operator) ⇒ Object

macro_call?


293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'app/models/regexp_parse.rb', line 293

def RegexpParse.operator_range(postfix_operator)
	if postfix_operator=='' then
		return UnboundedRange.new(1, 1)
	elsif postfix_operator=='*' then
		return UnboundedRange.new(0, nil)
	elsif postfix_operator=='+' then
		return UnboundedRange.new(1, nil)
	elsif postfix_operator=='?' then
		return UnboundedRange.new(0, 1)
	else
		raise "unexpected postfix_operator=#{node}"
	end #if
end

.postfix_expression?(node) ⇒ Boolean

If node has a postfix operator or a bracketed length range return postfix operator (what about | operator?) else return nil (no repetition) (not 1,1?)

Returns:

  • (Boolean)

216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/models/regexp_parse.rb', line 216

def RegexpParse.postfix_expression?(node)
	node=RegexpParse.promote(node)
	postfix_operator=RegexpParse.postfix_operator?(node.parse_tree[-1])
	if postfix_operator then
		return postfix_operator
	else
		bracket_operator=RegexpParse.bracket_operator?(node)
		if bracket_operator then
			bracket_operator
		else
			return nil #not postfix chars
		end #if
	end #if
end

.postfix_operator?(node) ⇒ Boolean

node is parse tree or string to test unlike postfix expression node, only last node should be passed returns node such as ['*'] or [““3”, “,”, “4”, “”] returns nil if not a postfix operator

Returns:

  • (Boolean)

254
255
256
257
258
259
260
261
262
263
264
265
# File 'app/models/regexp_parse.rb', line 254

def RegexpParse.postfix_operator?(node)
	if node.instance_of?(String) then
		index=PostfixOperators.index(node)	
		if index.nil? then
			nil
		else
			PostfixOperators[index]
		end #if
	else
		RegexpParse.bracket_operator?(node)
	end #if
end

.promotable?(node) ⇒ Boolean

compare

Returns:

  • (Boolean)

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/models/regexp_parse.rb', line 176

def RegexpParse.promotable?(node)
	if node.instance_of?(Regexp) then 
		true
	elsif node.instance_of?(String) then 
		true
	elsif node.kind_of?(Array) then #nested Arrays
		true
	elsif node.kind_of?(RegexpParse) then #nested Arrays
		false
	elsif node.instance_of?(RegexpParse) then 
		false
	else
		false
	end #if
end

.promote(node) ⇒ Object

RegexpParse.promotable


191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/models/regexp_parse.rb', line 191

def RegexpParse.promote(node)
	if RegexpParse.promotable?(node) then
		RegexpParse.new(node)
	elsif node.kind_of?(RegexpParse) then #nested Arrays
		node
	elsif node.instance_of?(RegexpParse) then 
		node
	elsif node.instance_of?(Regexp) then 
		RegexpParse.new(node.source)
	else
		raise "unexpected node=#{node.inspect}"
	end #if
end

.regexp_error(regexp_string, options = Default_options) ⇒ Object

regexp_rescued


148
149
150
151
152
153
# File 'app/models/regexp_parse.rb', line 148

def RegexpParse.regexp_error(regexp_string, options=Default_options)
	raise "expecting regexp_string=#{regexp_string.inspect}" unless regexp_string.instance_of?(String)
	return Regexp.new(regexp_string, options)
rescue RegexpError => exception
	return exception
end

.regexp_rescued(regexp_string, options = Default_options) ⇒ Object

Rescue bad regexp and return nil Example regexp with unbalanced bracketing characters


142
143
144
145
146
147
# File 'app/models/regexp_parse.rb', line 142

def RegexpParse.regexp_rescued(regexp_string, options=Default_options)
	raise "expecting regexp_string=#{regexp_string}" unless regexp_string.instance_of?(String)
	return Regexp.new(regexp_string, options)
rescue RegexpError
	return nil
end

.typed?(node) ⇒ Boolean

grep

Returns:

  • (Boolean)

441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'app/models/regexp_parse.rb', line 441

def RegexpParse.typed?(node)
	node=RegexpParse.promote(node)
	if node.parse_tree==[] then
		puts "nil"
		nil
	elsif node.instance_of?(String) then
		puts "node=#{node.inspect}, node.class.name=#{node.class.name}"
		RegexpToken[node] # commonly termination condition
	else
		node=RegexpParse.promote(node)
		if postfix_operator=RegexpParse.postfix_expression?(node) then
			RegexpRepetition.new(node.parse_tree)
		elsif node.to_a[-1]==']' then
			CharacterClass.new(node.parse_tree)
		elsif node.to_a[-1]==')' then
			RegexpParen.new(node.parse_tree)
		elsif node==Empty_language_parse then
			RegexpEmpty.new(node.parse_tree)
		elsif RegexpParse.postfix_expression?(node.to_a[0]) =='|' then
			RegexpAlternative.new(node.parse_tree)
		else #sequence is default
			if node.parse_tree.size == 0 then
				puts "nil"
				nil
			elsif node.parse_tree.size == 1 then
				puts "node.parse_tree.size == 1\nnode=#{node.inspect}, node.class.name=#{node.class.name}"
				RegexpParse.typed?(node.parse_tree[0]) #recurse to discard extra nesting
			else
				args=node.parse_tree.map do |n| 
					puts "n=#{n.inspect}, n.class.name=#{n.class.name}"
					RegexpParse.typed?(n)
				end #map
				RegexpSequence.new(args)
			end #if
		end #if
	end #if
end

Instance Method Details

#<=>(rhs) ⇒ Object

equal


169
170
171
172
173
174
175
# File 'app/models/regexp_parse.rb', line 169

def <=>(rhs)
	if self.parse_tree==rhs.parse_tree then
		return 0
	else
		return nil
	end #if
end

#==(rhs) ⇒ Object

regexp_error


155
156
157
158
159
160
161
# File 'app/models/regexp_parse.rb', line 155

def ==(rhs)
	if self.parse_tree==rhs.parse_tree then
		return true
	else
		return false
	end #if
end

#advanceToken!(increment) ⇒ Object

rest


378
379
380
# File 'app/models/regexp_parse.rb', line 378

def advanceToken!(increment)
	@tokenIndex=@tokenIndex+increment
end

#beyondString?(testPos = @tokenIndex) ⇒ Boolean

test if parsing has gone beyond end of string and should stop

Returns:

  • (Boolean)

382
383
384
# File 'app/models/regexp_parse.rb', line 382

def beyondString?(testPos=@tokenIndex)
	@regexp_string.nil? || testPos<0 || testPos>@regexp_string.length-1
end

#curlyTree!(regexp_string) ⇒ Object

handle 2,3-style specification of repetitions not currently used, since numbers are not interpreted currently handled identically to parenthesis and square brackets


388
389
390
391
392
393
394
# File 'app/models/regexp_parse.rb', line 388

def curlyTree!(regexp_string)
	remaining=rest
	matchData=/\{(\d*)(,(\d*))?\}/.match(remaining)
	increment=matchData[1].length+matchData[2].length+1
	advanceToken!(increment)
	return ['{',[matchData[1],matchData[2]],'}']
end

#eql?(rhs) ⇒ Boolean

equal_operator

Returns:

  • (Boolean)

162
163
164
165
166
167
168
# File 'app/models/regexp_parse.rb', line 162

def eql?(rhs)
	if self.parse_tree==rhs.parse_tree then
		return true
	else
		return false
	end #if
end

#grep(pattern, delimiter = "\n") ⇒ Object

pathnames


438
439
440
# File 'app/models/regexp_parse.rb', line 438

def grep(pattern, delimiter="\n")
	pathnames.files_grep(pattern, delimiter="\n")
end

#inspectObject

initialize


137
138
139
# File 'app/models/regexp_parse.rb', line 137

def inspect
	"@regexp_string=\"#{@regexp_string}\", @parse_tree=#{@parse_tree.inspect}, @tokenIndex=#{@tokenIndex.inspect}"
end

#macro_call?(macro = parse_tree) ⇒ Boolean

postfix_operator_walk

Returns:

  • (Boolean)

285
286
287
288
289
290
291
292
# File 'app/models/regexp_parse.rb', line 285

def macro_call?(macro=parse_tree)
	return false if '[' != macro[0]
	return false if ']' != macro[-1]
	inner_colons=macro[1..-2] # not another nested array
	return false if ':' != inner_colons[0]
	return false if ':' != inner_colons[-1]
	return	inner_colons[1..-2].join
end

#nextToken!Object

restartParse


359
360
361
362
363
364
365
366
367
368
369
370
# File 'app/models/regexp_parse.rb', line 359

def nextToken!
	if beyondString? then
		raise RuntimeError, "method nextToken! called after end of regexp_string."
	elsif @tokenIndex>1 && @regexp_string[@tokenIndex-1..@tokenIndex-1]=='\\' then
		ret='\\'+@regexp_string[@tokenIndex..@tokenIndex]
		@tokenIndex=@tokenIndex-2
	else
		ret=@regexp_string[@tokenIndex..@tokenIndex]
		@tokenIndex=@tokenIndex-1
	end
	return ret
end

#parseOneTerm!Object

parse matching brackets, postfix operator, or single character


396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'app/models/regexp_parse.rb', line 396

def parseOneTerm!
	ch=nextToken!
	index=ClosingBrackets.index(ch)
	if index then
		return  regexpTree!(OpeningBrackets[index].chr) << ch
	else
		index=PostfixOperators.index(ch)
		if index then
			return  NestedArray.new([parseOneTerm!, ch])
		else
			return ch
		end #if
	end #if
end

#pathnamesObject

to_pathname_glob


433
434
435
436
437
# File 'app/models/regexp_parse.rb', line 433

def pathnames
	Dir[to_pathname_glob].select do |pathname|
		to_regexp.match(pathname)
	end #select
end

#postfix_expression?Boolean

postfix_expression

Returns:

  • (Boolean)

230
231
232
# File 'app/models/regexp_parse.rb', line 230

def postfix_expression?
	RegexpParse.postfix_expression?(self)
end

#postfix_operator_walk(&visit_proc) ⇒ Object

postfix_operator


266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'app/models/regexp_parse.rb', line 266

def postfix_operator_walk(&visit_proc)
	# recurse on subtrees first so transformations ripple up generally
	# postfix expresion should not change operator
	branching=RegexpParse.new(parse_tree.map do |sub_tree|
		if sub_tree.kind_of?(Array) then
			RegexpParse.new(sub_tree).postfix_operator_walk(&visit_proc)
		else # leaf string
			sub_tree
		end #if
	end) #map and RegexpParse.new
#OK	raise "Unexpected #{self.inspect}.postfix_expression?=#{postfix_expression?} != #{branching.inspect}.postfix_expression?=#{RegexpParse.new(branching).postfix_expression?}"  unless postfix_expression? == RegexpParse.new(branching).postfix_expression?
	if postfix_expression? then #postfixes trickle up
		new_branching=visit_proc.call(branching)
			
	else
		new_branching=branching
	end
	return new_branching
end

#regexpTree!(terminator = nil) ⇒ Object

parseOneTerm!


410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'app/models/regexp_parse.rb', line 410

def regexpTree!(terminator=nil)
	ret=NestedArray.new([])
	begin
		if !beyondString? then
			term=parseOneTerm!
			ret << term
		else
			return ret.reverse
		end #if
	end until term==terminator
#not recursive	assert_invariant(ret.reverse)
	return ret.reverse
end

#repeated_pattern(node = self) ⇒ Object

Returns a NestedArray object


327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'app/models/regexp_parse.rb', line 327

def repeated_pattern(node=self)
	node=RegexpParse.promote(node)
#	if node.instance_of?(RegexpParse) then
#		node=node.parse_tree
#	else

#		node=RegexpParse.promote(node)
#	end #if	
	if node.postfix_expression? then # argument is not an Array
		return RegexpParse.new([node.parse_tree[0]])
#	elsif post_op=postfix_expression?(node) then
#		return RegexpParse.new(node[0])
#	elsif node[-1]=='}' then
#		node[0]
	else
		self
	end #if
end

#repetition_length(node = self) ⇒ Object

operator_range


306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'app/models/regexp_parse.rb', line 306

def repetition_length(node=self)
	node=RegexpParse.promote(node)
	if !node.kind_of?(RegexpParse) then
		return RegexpParse.operator_range(postfix_operator)
	elsif post_op=node.postfix_expression? then
		if post_op=='*' then
			return UnboundedRange.new(0, nil)
		elsif post_op=='+' then
			return UnboundedRange.new(1, nil)
		elsif post_op=='?' then
			return UnboundedRange.new(0, 1)
		else
			raise "unexpected post_op=#{post_op}"
		end #if
	elsif node.parse_tree[-1]=='}' then
		UnboundedRange.new(node.parse_tree[1][0].to_i, node.parse_tree[1][1].to_i)
	else
		UnboundedRange.new(node.parse_tree.length, node.parse_tree.length)
	end #if
end

#restObject

nextToken!


371
372
373
374
375
376
377
# File 'app/models/regexp_parse.rb', line 371

def rest
	if beyondString? then
		return ''
	else
		return @regexp_string[0..@tokenIndex]
	end #if
end

#restartParse!Object

Expect to eventually replace with inheritance Or use in parser (or post-parser initialization) to set classes correcly. Need more uniform subclasses Is there a useful hierarchy? Sequence (chars and trees) and alternatives (char class, |) Private methods that alter state end in ! private


351
352
353
354
355
356
357
358
# File 'app/models/regexp_parse.rb', line 351

def restartParse! # primarily for testing
	if @regexp_string.nil? then
		@tokenIndex=-1 # start at end
	else
		@tokenIndex=@regexp_string.length-1 # start at end
	end #if
	@parse_tree=NestedArray.new([])
end

#to_aObject

RegexpParse.promote


204
205
206
# File 'app/models/regexp_parse.rb', line 204

def to_a
	return @parse_tree
end

#to_pathname_globObject

regexpTree!


423
424
425
426
427
428
429
430
431
432
# File 'app/models/regexp_parse.rb', line 423

def to_pathname_glob
	ret=RegexpParse.new(parse_tree.map_branches{|b| (b[0]=='('?RegexpParse.new(b[1..-2]):RegexpParse.new(b))})
	ret=ret.postfix_operator_walk{|p| '*'}
	if ret.instance_of?(RegexpParse) then
		ret=ret.parse_tree.flatten.join
	elsif ret.kind_of?(Array) then
		ret=ret.flatten.join
	end #if
	return ret
end

#to_regexpObject

to_s


210
211
212
# File 'app/models/regexp_parse.rb', line 210

def to_regexp
	return Regexp.new(to_s)
end

#to_sObject

to_a


207
208
209
# File 'app/models/regexp_parse.rb', line 207

def to_s
	return to_a.join
end