Module: CopyExpander

Includes:
ExpanderConfig
Included in:
Expander
Defined in:
lib/copy_expander.rb

Overview

Work with individual source lines to perform token replacement declared in COBOL COPY REPLACING statements.

Instance Method Summary collapse

Methods included from ExpanderConfig

#config_file, #hash, #load_config, #method_missing

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ExpanderConfig

Instance Method Details

#blank?(line) ⇒ Boolean

Is this line logically blank?

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'lib/copy_expander.rb', line 82

def blank? line
  if ! line.valid_encoding?
    line = line.encode("UTF-16be", :invalid=>:replace, :replace=>" ").encode('UTF-8')
  end
  line == nil || line.gsub(/\s/, '').length == 0
end

#break_up_source_line(line) ⇒ Object

Save the first six and last eight characters of the 80-character line so that we won’t shift characters into the interpreted area of the line when we make text substitutions.



68
69
70
71
72
73
74
75
76
77
# File 'lib/copy_expander.rb', line 68

def break_up_source_line line
  if line[70] == ' ' && line[71..72] != '  ' 
    line[72..79] = line[71..78]
    line[71] = ' '
  end  

  line.length >=  6 ? @first_six_characters  = line[0..5]   : nil
  line.length >= 80 ? @last_eight_characters = line[72..79] : nil
  line.length >= 72 ? @work_area             = line[6..71]  : nil
end

#comment?(line) ⇒ Boolean

Is this line a comment? Source comments in COBOL are identified by an asterisk in position 7.

Returns:

  • (Boolean)


93
94
95
# File 'lib/copy_expander.rb', line 93

def comment? line
  line[6] == '*'
end

#commentize(line) ⇒ Object

Make the current source line a comment line.



114
115
116
117
# File 'lib/copy_expander.rb', line 114

def commentize line
  line[6] = '*'
  line
end

#copy_statement_countObject



17
18
19
# File 'lib/copy_expander.rb', line 17

def copy_statement_count
  @copy_statement_count
end

#expand_copybook(copy_statement) ⇒ Object

Recursively expand copybooks and replace tokens



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/copy_expander.rb', line 38

def expand_copybook copy_statement
  @depth += 1
  if @depth > 8
    puts "recursion depth is #{@depth}"
    exit(1)
  end  
  copybook = File.open("#{copy_dir}/#{copy_statement.copybook_name}", 'r')
  begin
    line = ('%-80.80s' % copybook.readline.chomp)
    if blank?(line) || comment?(line)
      write_from line
    else
      break_up_source_line line
      if has_copy_statement?
        @copy_statement_count += 1
        expand_copybook CopyStatement.new(@work_area)
      else
        @work_area = replace_tokens(@work_area, copy_statement)
        write_from reconstruct_line 
      end  
    end  
  end while copybook.eof? == false  
  @depth -= 1
end

#has_copy_statement?Boolean

Does the working area of the source line contain a COPY statement?

Returns:

  • (Boolean)


100
101
102
# File 'lib/copy_expander.rb', line 100

def has_copy_statement?
  @work_area.match(/ COPY (?=([^\"\']*\"[^\"\']*\"\')*[^\"\']*$)/i) ? true : false
end

#initObject



12
13
14
15
# File 'lib/copy_expander.rb', line 12

def init
  @copy_statement_count = 0
  @copy_statement_depth = 0
end

#process(line) ⇒ Object

Expand COPY statement found on a single line of COBOL source code.



24
25
26
27
28
29
30
31
32
33
# File 'lib/copy_expander.rb', line 24

def process line
  return line if blank? line
  return line if comment? line
  break_up_source_line line
  return line unless has_copy_statement?
  @copy_statement_count += 1
  @depth = 0
  expand_copybook CopyStatement.new(@work_area)
  nil
end

#reconstruct_lineObject

Reconstruct the original source line.



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

def reconstruct_line
  @first_six_characters + ('%-66.66s' % @work_area) + @last_eight_characters
end

#replace_tokens(line, copy_statement) ⇒ Object

Carry out token replacement in a source line per the REPLACING option



122
123
124
125
126
127
128
# File 'lib/copy_expander.rb', line 122

def replace_tokens line, copy_statement
  if copy_statement.old_value == nil || copy_statement.new_value == nil
    line
  else
    line.gsub(copy_statement.old_value, copy_statement.new_value) unless copy_statement.old_value == nil
  end
end