Class: IfdefProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/ifdef_process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIfdefProcess

Returns a new instance of IfdefProcess.



6
7
8
# File 'lib/ifdef_process.rb', line 6

def initialize()
  @define_list = []
end

Instance Attribute Details

#define_listObject (readonly)

Returns the value of attribute define_list.



4
5
6
# File 'lib/ifdef_process.rb', line 4

def define_list
  @define_list
end

Instance Method Details

#check_ifdefined(cond_string, define_hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ifdef_process.rb', line 18

def check_ifdefined(cond_string, define_hash)
  pattern = /(!)?defined\(([^)]*)\)/
  define_hash_new = define_hash.dup
  matches = cond_string.scan(pattern)

  matches.each do |match|
    # !definedでKeyが未登録の場合は登録
    has_exclamation = !match[0].nil?
    key = match[1]
    if has_exclamation
      unless define_hash.key? key
        define_hash_new[key] = false
      end
    end
  end

  return define_hash_new
end

#condition_judge(cond_string, define_hash) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ifdef_process.rb', line 37

def condition_judge(cond_string, define_hash)
  ret = false
  #puts "cond_string=#{cond_string}"
  cond_string = remove_suffixes(cond_string)
  #puts "cond_string=#{cond_string}"
  define_hash_new = check_ifdefined(cond_string, define_hash)
  #pp define_hash_new
  cond_string.gsub!(/defined/, "")
  if cond_string.gsub(/ /, "").to_s == "0"
    # not define
    #puts "return ifdef_judge #{ret}"
    return false
  end
  eval_buf = []
  arg_hash = {}
  eval_buf.push "def judge_ifdef"
  cond_string.scan(/[_A-Za-z][_A-Za-z_0-9]+/) do |m|
    #puts "scan word=#{m}"
    @define_list.push m
    next if m == "false" or m == "true"
    if define_hash_new.key? m
      arg_hash[m] = define_hash_new[m]
    else
      # not define
      #puts "return ifdef_judge #{ret}"
      return false
    end
  end
  #puts "arg_hash=#{arg_hash}"
  arg_hash.each do |key, val|
    eval_buf.push "  #{key.downcase} = #{val}"
  end
  eval_buf.push "  if #{cond_string.downcase}"
  eval_buf.push "    return true"
  eval_buf.push "  else"
  eval_buf.push "    return false"
  eval_buf.push "  end"
  eval_buf.push "end"
  eval_buf.push "judge_ifdef()"
  eval_string = eval_buf.join("\n")
  #puts "eval_string=#{eval_string}"
  ret = eval eval_string
  #puts "return ifdef_judge #{ret}"
  return ret
end

#process_ifdef(file_buf, define_hash) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
137
138
# File 'lib/ifdef_process.rb', line 83

def process_ifdef(file_buf, define_hash)
  out_buf = []
  proc_list = [true]
  line_count = 1
  ifdef_flag = [false]
  file_buf.each_line do |line|
    line = line.strip
    case line
    when /^#ifdef\s+(.*)/
      proc_list.push condition_judge($1, define_hash)
      ifdef_flag.push proc_list[-1]
      #puts "#ifdef #{ifdef_flag[-1]}:#{line_count}:line=[#{line}]:#{$1}"
    when /^#ifndef\s+(.+)/
      proc_list.push !(condition_judge($1, define_hash))
      ifdef_flag.push proc_list[-1]
      #puts "#ifndef #{ifdef_flag[-1]}:#{line_count}:line=[#{line}]:#{$1}"
    when /^#if\s+(.+)/
      proc_list.push condition_judge($1, define_hash)
      ifdef_flag.push proc_list[-1]
      #puts "#if #{ifdef_flag[-1]}:#{line_count}:line=[#{line}]:#{$1}"
    when /^#elif\s+(.+)/
      if !ifdef_flag[-1]
        proc_list[-1] = condition_judge($1, define_hash)
        ifdef_flag[-1] = proc_list[-1]
      else
        proc_list[-1] = false
        ifdef_flag[-1] = true
      end
      #puts "#elif #{ifdef_flag[-1]}:#{line_count}:line=[#{line}]:#{$1}"
    when /^#else/
      if !ifdef_flag[-1]
        proc_list[-1] = !proc_list[-1]
      else
        proc_list[-1] = false
        ifdef_flag[-1] = true
      end
      #puts "#else #{ifdef_flag[-1]}:#{line_count}:line=[#{line}]"
    when /^#endif/
      proc_list.pop
      ifdef_flag.pop
      #puts "#endif #{ifdef_flag[-1]}:#{line_count}:line=[#{line}]"
    else
      #print "proc_list="
      #pp proc_list
      if 0 == proc_list.select { |p| p == false }.size
        #puts "#{ifdef_flag[-1]}:#{line_count}: #{line}"
        out_buf.push line
      else
        #puts "#{ifdef_flag[-1]}:#{line_count}: #{line}"
      end
    end
    line_count += 1
  end
  @define_list.uniq!
  return out_buf
end

#remove_suffixes(text) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/ifdef_process.rb', line 10

def remove_suffixes(text)
  suffixes = %w[ULL LL UL D F U L]
  suffix_pattern = suffixes.uniq.sort_by(&:length).reverse.join("|")
  #puts suffix_pattern
  regex = /(\d+\.?\d*)(?:#{suffix_pattern})\b/i
  text.gsub(regex, '\1')
end