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

#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.



59
60
61
62
63
# File 'lib/copy_expander.rb', line 59

def break_up_source_line line
  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)


69
70
71
# File 'lib/copy_expander.rb', line 69

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

#commentize(line) ⇒ Object

Make the current source line a comment line.



90
91
92
93
# File 'lib/copy_expander.rb', line 90

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/copy_expander.rb', line 35

def expand_copybook copy_statement
  copybook = File.open("#{copy_dir}/#{copy_statement.copybook_name}", 'r')
  begin
    line = copybook.readline
    if 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  
end

#has_copy_statement?Boolean

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

Returns:

  • (Boolean)


76
77
78
# File 'lib/copy_expander.rb', line 76

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
# File 'lib/copy_expander.rb', line 24

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

#reconstruct_lineObject

Reconstruct the original source line.



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

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

#replace_tokens(line, copy_statement) ⇒ Object

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



98
99
100
# File 'lib/copy_expander.rb', line 98

def replace_tokens line, copy_statement
  line.gsub(copy_statement.old_value, copy_statement.new_value)
end