Class: Daedalus::DependencyGrapher::FileParser

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

Overview

Parses a file for all preprocessor control directives into a tree of Node objects. The parser can operate on a file or an array of lines.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, directories) ⇒ FileParser

Returns a new instance of FileParser.



290
291
292
293
# File 'lib/daedalus/dependency_grapher.rb', line 290

def initialize(root, directories)
  @stack = [root]
  @directories = directories
end

Instance Attribute Details

#directoriesObject (readonly)

Returns the value of attribute directories.



288
289
290
# File 'lib/daedalus/dependency_grapher.rb', line 288

def directories
  @directories
end

#lineObject (readonly)

Returns the value of attribute line.



288
289
290
# File 'lib/daedalus/dependency_grapher.rb', line 288

def line
  @line
end

Instance Method Details

#add_body(node) ⇒ Object

Parser operations



297
298
299
# File 'lib/daedalus/dependency_grapher.rb', line 297

def add_body(node)
  @stack.last.body << node
end

#add_else(node) ⇒ Object



301
302
303
# File 'lib/daedalus/dependency_grapher.rb', line 301

def add_else(node)
  @stack.last.add_else node
end

#closeObject



317
318
319
# File 'lib/daedalus/dependency_grapher.rb', line 317

def close
  @stack.last.close
end

#parse(lines) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/daedalus/dependency_grapher.rb', line 389

def parse(lines)
  @line = 0

  lines.each do |line|
    @line += 1
    m = line.match(/^\s*#(include|ifdef|ifndef|endif|else|define|undef|if|elif)(.*)$/)

    # TODO: continue reading if line ends in \

    send :"process_#{m[1]}", m[2] if m
  end
end

#parse_file(name) ⇒ Object



380
381
382
# File 'lib/daedalus/dependency_grapher.rb', line 380

def parse_file(name)
  parse IO.readlines(name, :encoding => "ascii-8bit")
end

#process_define(macro) ⇒ Object



354
355
356
357
# File 'lib/daedalus/dependency_grapher.rb', line 354

def process_define(macro)
  node = Define.new macro, self
  add_body node
end

#process_elif(expression) ⇒ Object



370
371
372
373
374
375
# File 'lib/daedalus/dependency_grapher.rb', line 370

def process_elif(expression)
  node = ElseIf.new expression, self
  add_else node
  add_body node
  stack_push node
end

#process_else(rest) ⇒ Object



349
350
351
352
# File 'lib/daedalus/dependency_grapher.rb', line 349

def process_else(rest)
  node = Else.new self
  add_else node
end

#process_endif(rest) ⇒ Object



345
346
347
# File 'lib/daedalus/dependency_grapher.rb', line 345

def process_endif(rest)
  close
end

#process_if(expression) ⇒ Object



364
365
366
367
368
# File 'lib/daedalus/dependency_grapher.rb', line 364

def process_if(expression)
  node = If.new expression, self
  add_body node
  stack_push node
end

#process_ifdef(macro) ⇒ Object



333
334
335
336
337
# File 'lib/daedalus/dependency_grapher.rb', line 333

def process_ifdef(macro)
  node = IfDefined.new macro, self
  add_body node
  stack_push node
end

#process_ifndef(macro) ⇒ Object



339
340
341
342
343
# File 'lib/daedalus/dependency_grapher.rb', line 339

def process_ifndef(macro)
  node = IfNotDefined.new macro, self
  add_body node
  stack_push node
end

#process_include(name) ⇒ Object

Events



323
324
325
326
327
328
329
330
331
# File 'lib/daedalus/dependency_grapher.rb', line 323

def process_include(name)
  # We do not process any <files>. This could be enabled as
  # an option, but we don't need it or want it now.
  name =~ /\s*"([^"]+)".*$/
  return unless $1

  node = IncludedFile.new $1, self
  add_body node
end

#process_undef(macro) ⇒ Object



359
360
361
362
# File 'lib/daedalus/dependency_grapher.rb', line 359

def process_undef(macro)
  node = Undefine.new macro, self
  add_body node
end

#stack_popObject



309
310
311
# File 'lib/daedalus/dependency_grapher.rb', line 309

def stack_pop
  @stack.pop
end

#stack_push(node) ⇒ Object



305
306
307
# File 'lib/daedalus/dependency_grapher.rb', line 305

def stack_push(node)
  @stack.push node
end

#stack_topObject



313
314
315
# File 'lib/daedalus/dependency_grapher.rb', line 313

def stack_top
  @stack.last
end