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.



329
330
331
332
# File 'lib/daedalus/dependency_grapher.rb', line 329

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

Instance Attribute Details

#directoriesObject (readonly)

Returns the value of attribute directories.



327
328
329
# File 'lib/daedalus/dependency_grapher.rb', line 327

def directories
  @directories
end

#lineObject (readonly)

Returns the value of attribute line.



327
328
329
# File 'lib/daedalus/dependency_grapher.rb', line 327

def line
  @line
end

Instance Method Details

#add_body(node) ⇒ Object

Parser operations



336
337
338
# File 'lib/daedalus/dependency_grapher.rb', line 336

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

#add_else(node) ⇒ Object



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

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

#closeObject



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

def close
  @stack.last.close
end

#parse(lines) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/daedalus/dependency_grapher.rb', line 428

def parse(lines)
  @line = 0

  inlined_line = ""
  unless lines.empty?
    while (line = lines.shift.chomp(''))
      @line += 1
      line.rstrip!
      if line.end_with?('\\')
        # continue reading if line ends in          inlined_line += line.chop
      else
        # stop reading
        inlined_line += line
        break
      end
    end

    m = inlined_line.match(/^\s*#(include|ifdef|ifndef|endif|else|define|undef|if|elif)(.*)$/)

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

#parse_file(name) ⇒ Object



419
420
421
# File 'lib/daedalus/dependency_grapher.rb', line 419

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

#process_define(macro) ⇒ Object



393
394
395
396
# File 'lib/daedalus/dependency_grapher.rb', line 393

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

#process_elif(expression) ⇒ Object



409
410
411
412
413
414
# File 'lib/daedalus/dependency_grapher.rb', line 409

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

#process_else(rest) ⇒ Object



388
389
390
391
# File 'lib/daedalus/dependency_grapher.rb', line 388

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

#process_endif(rest) ⇒ Object



384
385
386
# File 'lib/daedalus/dependency_grapher.rb', line 384

def process_endif(rest)
  close
end

#process_if(expression) ⇒ Object



403
404
405
406
407
# File 'lib/daedalus/dependency_grapher.rb', line 403

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

#process_ifdef(macro) ⇒ Object



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

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

#process_ifndef(macro) ⇒ Object



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

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

#process_include(name) ⇒ Object

Events



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

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



398
399
400
401
# File 'lib/daedalus/dependency_grapher.rb', line 398

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

#stack_popObject



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

def stack_pop
  @stack.pop
end

#stack_push(node) ⇒ Object



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

def stack_push(node)
  @stack.push node
end

#stack_topObject



352
353
354
# File 'lib/daedalus/dependency_grapher.rb', line 352

def stack_top
  @stack.last
end