Class: Banalize::Parser

Inherits:
Object
  • Object
show all
Includes:
PodStyleComments, ShellVariables
Defined in:
lib/banalize/parser.rb,
lib/banalize/parser/variables.rb,
lib/banalize/parser/pod_comments.rb

Overview

Instance attributes


Class sets following attribute accessor methods:

Direct Known Subclasses

Registry

Defined Under Namespace

Modules: PodStyleComments, ShellVariables

Instance Attribute Summary collapse

Attributes included from ShellVariables

#variables

Instance Method Summary collapse

Methods included from ShellVariables

#shell_variables

Methods included from PodStyleComments

#pod_comments

Constructor Details

#initialize(path) ⇒ Parser

Returns a new instance of Parser.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/banalize/parser.rb', line 21

def initialize path
  @lines     = IO.read(path).force_encoding("utf-8").split($/)
  @shebang   = Numbered.new
  @comments  = Numbered.new 
  @code      = Numbered.new
  @variables = []
  
  @shebang.add @lines.shift if @lines.first =~ /^#!/

  @lines.each_index do |idx|

    next if @lines[idx] =~ /^\s*$/

    lineno = idx + 1 + (@shebang ? 1 : 0) # Compensate for base-0 and shebang line

    if @lines[idx] =~ /^\s*\#/
      @comments.add @lines[idx], lineno
    else
      @code.add @lines[idx], lineno
    end
  end
  pod_comments
  shell_variables
end

Instance Attribute Details

#codeObject

Contains all non-comments of the script, excluding shebang line.

Same as with comments, excluded from code blocks are only hash-comments, but here-documents even if they are not executing any action are included here.



71
72
73
# File 'lib/banalize/parser.rb', line 71

def code
  @code
end

#commentsObject

Contains all block comments of the file, excluding shebang line

Only hash-comments are suported at this time. Comments in the style of here-documents are not.



63
64
65
# File 'lib/banalize/parser.rb', line 63

def comments
  @comments
end

#linesObject

Lines of the tested bash file, split by n‘s



47
48
49
# File 'lib/banalize/parser.rb', line 47

def lines
  @lines
end

#pathObject

UNIX path to the tested file



50
51
52
# File 'lib/banalize/parser.rb', line 50

def path
  @path
end

#shebangObject

Shebang contains first line of the script if it’s in ‘#!` format. Otherwise it is nil.



56
57
58
# File 'lib/banalize/parser.rb', line 56

def shebang
  @shebang
end