Class: ListElement

Inherits:
OperatorElement show all
Defined in:
lib/rpdf2txt-rockit/grammar.rb

Instance Attribute Summary

Attributes inherited from OperatorElement

#sub_elements

Attributes inherited from Element

#sub_elements, #tree_specification

Instance Method Summary collapse

Methods inherited from OperatorElement

#==, #name

Methods included from SourceCodeDumpable

as_code, as_method_named, as_module_method_named, #create_new, indent_lines, name_hash, #new_of_my_type, #parameter_named, #to_compact_src, #to_src_in_module, #type_to_src

Constructor Details

#initialize(subElements, separatorElement = ",") ⇒ ListElement

Returns a new instance of ListElement.



218
219
220
221
# File 'lib/rpdf2txt-rockit/grammar.rb', line 218

def initialize(subElements, separatorElement = ",")
  super(subElements)
  @separator = make_element(separatorElement)
end

Instance Method Details

#inspectObject



271
272
273
# File 'lib/rpdf2txt-rockit/grammar.rb', line 271

def inspect
  "list(#{inspect_sub_elements}, #{@separator.inspect})"
end

#normalize(productions) ⇒ Object

NT -> a b list(se1 … sen, sep) c d

is normalized to

NT -> a b se1 … sen NT-List-X c d

|  a b se1 ... sen c d

NT-List-X -> NT-List-X sep se1 … sen

|  sep se1 ... sen


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/rpdf2txt-rockit/grammar.rb', line 237

def normalize(productions)
  temp_nonterminal = temp_nonterminal("List", productions.first)
  num_sub_elements = @sub_elements.length
  ps1 = 
    clone_and_substitute_productions(productions, @sub_elements + 
		       [temp_nonterminal]) do |prod, elemindex|
    endindex = elemindex+@sub_elements.length-1
    prod.tree_builder = 
	ArrayNodeBuilder.new((elemindex..endindex).to_a,
	     endindex+1, prod.tree_builder,
	     elemindex, ((elemindex+1)..(endindex+1)).to_a)
    prod.tree_builder.append_element = false
    prod
  end
  ps2 = clone_and_substitute_productions(productions, 
			   @sub_elements) do |prod, elemindex|
    endindex = elemindex+@sub_elements.length-1
    prod.tree_builder = 
	ArrayNodeBuilder.new((elemindex..endindex).to_a,
	     nil, prod.tree_builder,
	     elemindex, ((elemindex+1)..endindex).to_a)
    prod
  end
  temp_production1 =
    Production.new(temp_nonterminal, 
     [temp_nonterminal, @separator] + @sub_elements,
     ArrayNodeBuilder.new((2...(2+num_sub_elements)).to_a, 0))
  temp_production2 = 
    Production.new(temp_nonterminal, [@separator] + @sub_elements,
     ArrayNodeBuilder.new((1...(1+num_sub_elements)).to_a))
  [ps1 + ps2,
   temp_production1.normalize + temp_production2.normalize]
end

#to_src(name = nil, nameHash = {}) ⇒ Object



223
224
225
226
227
# File 'lib/rpdf2txt-rockit/grammar.rb', line 223

def to_src(name = nil, nameHash = {})
  assign_to(name, 
     new_of_my_type(as_code(@sub_elements.to_src(nil,nameHash)), 
		     @separator))
end