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.



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

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

Instance Attribute Details

#directoriesObject (readonly)

Returns the value of attribute directories.



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

def directories
  @directories
end

#lineObject (readonly)

Returns the value of attribute line.



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

def line
  @line
end

Instance Method Details

#add_body(node) ⇒ Object

Parser operations



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

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

#add_else(node) ⇒ Object



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

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

#closeObject



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

def close
  @stack.last.close
end

#parse(lines) ⇒ Object



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/daedalus/dependency_grapher.rb', line 437

def parse(lines)
  @line = 0

  re = /^\s*#(include|ifdef|ifndef|endif|else|define|undef|if|elif)(.*)$/o
  full_line = ""

  lines.each do |line|
    @line += 1

    full_line.chomp!

    if line[-2] == ?\\
      full_line += line.chomp("\\\n") + "\n"
      next
    else
      full_line += line
    end

    m = full_line.match re
    full_line = ""

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

#parse_file(name) ⇒ Object



428
429
430
# File 'lib/daedalus/dependency_grapher.rb', line 428

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

#process_define(macro) ⇒ Object



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

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

#process_elif(expression) ⇒ Object



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

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

#process_else(rest) ⇒ Object



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

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

#process_endif(rest) ⇒ Object



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

def process_endif(rest)
  close
end

#process_if(expression) ⇒ Object



412
413
414
415
416
# File 'lib/daedalus/dependency_grapher.rb', line 412

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

#process_ifdef(macro) ⇒ Object



381
382
383
384
385
# File 'lib/daedalus/dependency_grapher.rb', line 381

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

#process_ifndef(macro) ⇒ Object



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

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

#process_include(name) ⇒ Object

Events



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

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



407
408
409
410
# File 'lib/daedalus/dependency_grapher.rb', line 407

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

#stack_popObject



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

def stack_pop
  @stack.pop
end

#stack_push(node) ⇒ Object



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

def stack_push(node)
  @stack.push node
end

#stack_topObject



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

def stack_top
  @stack.last
end