Class: Parser

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/xmk/parser.rb', line 6

def initialize(tokens)
  @failsafe = false
  @state = State::ROOT
  @tokens = tokens
  @env_stack = Array.new 
  @env_stack.push(Hash.new)
  @i = 0
  @i_memory = 0
  @env_label = ""
  @env_name = "root"
  @plat_name = ""
  @target_state = Hash.new
  @target_name = ""
end

Class Method Details

.run(cmd, noprint = false) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/xmk/parser.rb', line 319

def self.run(cmd, noprint = false)
  unless Xmk.options[:disable_cmd]
    if Xmk.options[:verbose]
      Xmk.print("execute #{cmd}")
    end
    v = %x{#{cmd}}
    return v if v.strip.empty?
    puts v unless noprint
    return v
  end
  nil
end

.run_print_verbose_only(cmd, noprint = false) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/xmk/parser.rb', line 332

def self.run_print_verbose_only(cmd, noprint = false)
  unless Xmk.options[:disable_cmd]
    if Xmk.options[:verbose]
      Xmk.print("execute #{cmd}")
    end
    v = %x{#{cmd}}
    return v if v.strip.empty?
    if Xmk.options[:verbose]
      puts v unless noprint
    end
    return v
  end
  nil
end

Instance Method Details

#add_varObject



301
302
303
304
# File 'lib/xmk/parser.rb', line 301

def add_var
  @env_stack.last[:"#{@tokens[@i-1].lexeme}"] <<
    self.var_replace(@tokens[@i+1].lexeme)
end

#env_stack_growObject



288
289
290
291
292
293
294
# File 'lib/xmk/parser.rb', line 288

def env_stack_grow
  h = Hash.new
  @env_stack.last.each do |k,v|
    h[k] = v.dup
  end
  @env_stack.push(h)
end

#leave_platformObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/xmk/parser.rb', line 251

def leave_platform
  i = @i
  loop do
    i -= 1
    break if @tokens[i].type == Token::PLAT
  end
  if Xmk.options[:verbose]
    Xmk.print("LEAVE PLATFORM: #{@tokens[i].lexeme}")
  end
  @state = State::TAR
  @env_name = @env_memory
  @env_stack.pop
  @target_state[:"#{@env_name}"].push(@tokens[i].lexeme)
  loop do
    if @tokens[@i].type == Token::EOF
      @i = @i_memory
      break
    end
    break if @tokens[@i+1].type == Token::PLAT
    @i += 1
  end
end

#leave_targetObject



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

def leave_target
  if Xmk.options[:verbose]
    i = @i
    loop do
      i -= 1
      break if @tokens[i].type == Token::TAR
    end
    Xmk.print("@LEAVE TARGET: #{@tokens[i].lexeme}")
  end
  @env_name = "root"
  @state = State::ROOT
  @env_stack.pop
end

#parseObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
82
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
# File 'lib/xmk/parser.rb', line 21

def parse
  loop do
    case @tokens[@i].type
    when Token::LBR
      if @env_label != ""
        if Xmk.options[:verbose]
          Xmk.print("leave label: #{@env_label}")
        end
        @env_label = ""
      end
      if @tokens[@i+1].type == Token::PLAT && @state == State::PLAT
        self.leave_platform
      end
      if @tokens[@i+1].type == Token::TAR && @state == State::TAR
        self.leave_target
      end
    when Token::PLAT
      @plat_name = @tokens[@i].lexeme
      case @state
      when State::ROOT
        loop do
          @i += 1
          break if @tokens[@i+1].type == Token::EOF
        end
      when State::TAR
        allow = false
        if Xmk.platforms.empty? || Xmk.platforms.include?(@plat_name)
          while @tokens[@i+1].type == Token::TARLINK
            @i += 1
            if @env_name == @tokens[@i].lexeme
              allow = true
              break
            end
          end
        end
        if @target_state[:"#{@env_name}"].include?(@plat_name)
          # leave target
          allow = false
          self.leave_target
          @state = State::QUIT
          loop do
            @i += 1
            break if @tokens[@i+1].type == Token::EOF
          end
        end
        if allow
          @src_files = Array.new
          @obj_files = Array.new
          @compilation_flags = Array.new
          if Xmk.options[:verbose]
            Xmk.print("ENTER PLATFORM: #{@plat_name}")
          end
          @env_memory = @env_name
          @env_name = @plat_name
          @state = State::PLAT
          self.env_stack_grow
        else
          unless @state == State::QUIT
            unless @failsafe
              @i = @i_memory
              @failsafe = true
            else
              loop do
                @i += 1
                break if @tokens[@i+1].type == Token::PLAT
              end
            end
          end
        end
      when State::PLAT
        Xmk.error("Invalid state.", @tokens[@i].line)
      end
    when Token::TAR
      case @state
      when State::ROOT, State::TAR
        if @state == State::TAR
          self.leave_target
        end
        if Xmk.targets.empty? || Xmk.targets.include?(@tokens[@i].lexeme)
          if Xmk.options[:verbose]
            Xmk.print("@ENTER TARGET: #{@tokens[@i].lexeme}")
          end
          @target_name = @tokens[@i].lexeme
          @target_state[:"#{@tokens[@i].lexeme}"] = Array.new
          @env_name = @tokens[@i].lexeme
          @state = State::TAR
          self.env_stack_grow
        else
          loop do
            @i += 1
            if @tokens[@i+1].type == Token::EOF
              break
            end
            break if @tokens[@i+1].type == Token::TAR
          end
        end
      when State::TAR
      when State::PLAT
        Xmk.error("Hierarchy violation. Target scope required" <<
          " to appear before platform scope.", $tokens[@i].line)
      end
    when Token::CMD
      if @env_label == "_rule"
        @src_files = Array.new
        @obj_files = Array.new
        @compilation_flags = Array.new
        @env_stack.last[:SRC].split.each do |src|
          Dir.glob(src) do |f|
            @src_files.push(f)
            i = File.dirname(f).index('/')
            i = File.dirname(f).length if i.nil?
            @obj_files.push(File.join(@env_stack.last[:OBJ],
              File.dirname(f)[i .. -1],
              "#{File.basename(f,'.*')}.o"))
          end
        end
        @obj_files.each do |o|
          Pathname(o).dirname.mkpath
          @compilation_flags.push(!File.exist?(o))
        end
        @src_files.each_with_index do |src, i|
          next if @compilation_flags[i] 
          recipe = Parser.run_print_verbose_only(
            var_replace(@tokens[@i].lexeme).gsub('$~', src), true)
          recipe.slice!(0..recipe.index(':'))
          recipe.gsub!("\n", '')
          recipe.gsub!("\\", '')
          recipe.squeeze(" ")
          recipe = recipe[1..-1]
          recipe = recipe.split(" ")
          objmtime = File.mtime(@obj_files[i])
          recipe.each do |dep|
            if File.mtime(dep) > objmtime
              @compilation_flags[i] = (File.mtime(dep) > objmtime)
            end
          end
        end
      elsif @env_label == "_compile"
        @src_files.each_with_index do |src, i|
          if @compilation_flags[i]
            if Xmk.options[:verbose]
              Xmk.print("Recompilation for #{src}.")
            end
            Parser.run(var_replace(@tokens[@i].lexeme.gsub('$~', src).gsub(
              '$&', @obj_files[i])))
          end
        end
      elsif @env_label == "_link"
        Parser.run(var_replace(@tokens[@i].lexeme.gsub('$&&',
          @obj_files.join(" "))))
      else
        Parser.run(var_replace(@tokens[@i].lexeme))
      end
    when Token::LAB
      if Xmk.options[:verbose]
        Xmk.print("attempt label: #{@tokens[@i].lexeme}")
      end
      unless @tokens[@i].lexeme[0] == "_"
        unless Xmk.labels.include?(@tokens[@i].lexeme)
          # if not included skip to end of label
          if Xmk.options[:verbose]
            Xmk.print("skip label: #{@tokens[@i].lexeme}")
          end
          loop do
            @i += 1
            break if @tokens[@i].type == Token::LBR
          end
        else
          if Xmk.options[:verbose]
            Xmk.print("enter label: #{@tokens[@i].lexeme}")
          end
          @env_label = @tokens[@i].lexeme
        end
      else
        if @tokens[@i].lexeme == "_plat" && @state == State::TAR
          @i_memory = @i
          loop do
            @i += 1
            if @tokens[@i].type == Token::EOF
              @i = @i_memory
              break
            end
            break if @tokens[@i+1].type == Token::PLAT
          end
        else
          if Xmk.options[:verbose]
            Xmk.print("enter label: #{@tokens[@i].lexeme}")
          end
          @env_label = @tokens[@i].lexeme
        end
      end
    when Token::VAR
      unless @tokens[@i+1].type == Token::SET || 
          @tokens[@i+1].type == Token::ADD
        Xmk.error("Expect '=' or '+=' after variable",
                  @tokens[@i].line)
      end
    when Token::SET, Token::ADD
      unless @tokens[@i-1].type == Token::VAR && 
          @tokens[@i+1].type == Token::VAL  
        Xmk.error("Expect <VAR>=|+=<VALUE>.",
                  @tokens[@i].line)
      else
        case @tokens[@i].type
        when Token::SET
          self.set_var
        when Token::ADD
          if @env_stack.last.has_key?(:"#{@tokens[@i-1].lexeme}")
            self.add_var
          else
            self.set_var
          end
        end
      end
      if Xmk.options[:verbose]
        Xmk.print("$(#{@tokens[@i-1].lexeme}): " <<
          " #{@env_stack.last[:"#{@tokens[@i-1].lexeme}"]}")
      end
    when Token::EOF
      if @state == State::PLAT
        self.leave_platform
      else
        break
      end
    else
    end
    @i += 1
  end 
end

#set_varObject



296
297
298
299
# File 'lib/xmk/parser.rb', line 296

def set_var
  @env_stack.last[:"#{@tokens[@i-1].lexeme}"] =
    self.var_replace(@tokens[@i+1].lexeme)
end

#var_replace(str) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/xmk/parser.rb', line 306

def var_replace(str)
  str2 = str.dup
  str2.scan(/\${1}\({1}([A-Z_]+){1}\)/) do |m|
    if @env_stack.last.has_key?(:"#{m[0]}")
      str.gsub!("$(#{m[0]})", @env_stack.last[:"#{m[0]}"])
    else
      Xmk.error("Variable #{m[0]} not defined",
        @tokens[@i+1].line)
    end
  end
  str.gsub('$@', @target_name).gsub('$!',@plat_name)
end