Class: ABNF::Seq

Inherits:
Elt
  • Object
show all
Defined in:
lib/abnf/abnf.rb,
lib/abnf/regexp.rb,
lib/abnf/regexp.rb,
lib/abnf/grammar.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Elt

#*, #+, #empty_set?, #remove_left_recursion, #remove_right_recursion, #rep, #|

Constructor Details

#initialize(*elts) ⇒ Seq

Returns a new instance of Seq.



107
108
109
# File 'lib/abnf/grammar.rb', line 107

def initialize(*elts)
  @elts = elts
end

Instance Attribute Details

#eltsObject (readonly)

Returns the value of attribute elts.



110
111
112
# File 'lib/abnf/grammar.rb', line 110

def elts
  @elts
end

Class Method Details

._newObject



84
# File 'lib/abnf/grammar.rb', line 84

alias _new new

.new(*elts) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/abnf/grammar.rb', line 87

def Seq.new(*elts)
  elts2 = []
  elts.each {|e|
	if e.empty_sequence?
	  next
    elsif Seq === e
	  elts2.concat e.elts
	elsif e.empty_set?
	  return EmptySet
	else
	  elts2 << e
	end
  }
  case elts2.length
  when 0; EmptySequence
  when 1; elts2.first
  else; Seq._new(*elts2)
  end
end

Instance Method Details

#each_var(&block) ⇒ Object



116
# File 'lib/abnf/grammar.rb', line 116

def each_var(&block) @elts.each {|elt| elt.each_var(&block)} end

#empty_sequence?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/abnf/grammar.rb', line 112

def empty_sequence?
  @elts.empty?
end

#recursion(syms, lhs) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/abnf/regexp.rb', line 225

def recursion(syms, lhs)
  case @elts.length
  when 0
    NonRecursion
  when 1
    @elts.first.recursion(syms, lhs)  
  else
	(1...(@elts.length-1)).each {|i|
	  return OtherRecursion if @elts[i].recursion(syms, lhs) != NonRecursion
	}

    r_left = @elts.first.recursion(syms, lhs)
	return OtherRecursion if r_left & ~(NonRecursion|JustRecursion|LeftRecursion|SelfRecursion) != 0
	r_left = (r_left & ~JustRecursion) | LeftRecursion if r_left & JustRecursion != 0

    r_right = @elts.last.recursion(syms, lhs)
	return OtherRecursion if r_right & ~(NonRecursion|JustRecursion|RightRecursion|SelfRecursion) != 0
	r_right = (r_right & ~JustRecursion) | RightRecursion if r_right & JustRecursion != 0

	if r_left == NonRecursion
	  r_right
	elsif r_right == NonRecursion
	  r_left
	else
	  OtherRecursion
	end
  end
end

#regexp_treeObject



391
# File 'lib/abnf/regexp.rb', line 391

def regexp_tree() RegexpTree.seq(*@elts.map {|e| e.regexp_tree}) end

#remove_just_recursion(n) ⇒ Object



254
255
256
# File 'lib/abnf/regexp.rb', line 254

def remove_just_recursion(n)
  self
end

#split_left_recursion(n) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/abnf/regexp.rb', line 258

def split_left_recursion(n)
  case @elts.length
  when 0
    [self, EmptySet]
  when 1
    @elts.first.split_left_recursion(n)
  else
    nonrec, rest = @elts.first.split_left_recursion(n)
	rest1 = Seq.new(*@elts[1..-1])
	nonrec += rest1
	rest += rest1
	[nonrec, rest]
  end
end

#split_recursion(n) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/abnf/regexp.rb', line 288

def split_recursion(n)
  case @elts.length
  when 0
    [EmptySet, self, EmptySet]
  when 1
    @elts.first.split_recursion(n)
  else
    leftmost_nonrec, leftmost_rest_right = @elts.first.split_left_recursion(n)
    rightmost_nonrec, rightmost_rest_left = @elts.last.split_right_recursion(n)
	rest_middle = Seq.new(*@elts[1...-1])

	if leftmost_rest_right.empty_set?
	  [leftmost_nonrec + rest_middle + rightmost_rest_left,
leftmost_nonrec + rest_middle + rightmost_nonrec,
EmptySet]
	elsif rightmost_rest_left.empty_set?
	  [EmptySet,
leftmost_nonrec + rest_middle + rightmost_nonrec,
leftmost_rest_right + rest_middle + rightmost_nonrec]
	else
	  raise Exception.new("non left/right recursion") # bug
	end
  end
end

#split_right_recursion(n) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/abnf/regexp.rb', line 273

def split_right_recursion(n)
  case @elts.length
  when 0
    [self, EmptySet]
  when 1
    @elts.first.split_right_recursion(n)
  else
    nonrec, rest = @elts.last.split_right_recursion(n)
	rest1 = Seq.new(*@elts[0...-1])
	nonrec = rest1 + nonrec
	rest = rest1 + rest
	[nonrec, rest]
  end
end

#subst_var(&block) ⇒ Object



117
# File 'lib/abnf/grammar.rb', line 117

def subst_var(&block) Seq.new(*@elts.map {|elt| elt.subst_var(&block)}) end

#useful?(useful_names) ⇒ Boolean

Returns:

  • (Boolean)


121
# File 'lib/abnf/abnf.rb', line 121

def useful?(useful_names) @elts.all? {|e| e.useful?(useful_names)} end