Class: ShellOpts::Analyzer

Inherits:
Object
  • Object
show all
Includes:
Grammar
Defined in:
lib/shellopts/analyzer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grammar) ⇒ Analyzer

Returns a new instance of Analyzer.



75
76
77
# File 'lib/shellopts/analyzer.rb', line 75

def initialize(grammar)
  @grammar = grammar
end

Instance Attribute Details

#grammarObject (readonly)

Returns the value of attribute grammar.



73
74
75
# File 'lib/shellopts/analyzer.rb', line 73

def grammar
  @grammar
end

Class Method Details

.analyze(source) ⇒ Object



130
# File 'lib/shellopts/analyzer.rb', line 130

def Analyzer.analyze(source) self.new(source).analyze end

Instance Method Details

#analyzeObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/shellopts/analyzer.rb', line 109

def analyze()
  move_commands

  @grammar.traverse(Grammar::Command) { |command|
    command.set_supercommand
    command.reorder_options
    command.collect_options
    command.compute_option_hashes
  }

  @grammar.compute_command_hashes

  @grammar.traverse { |node| 
    node.remove_brief_nodes 
    node.remove_arg_descr_nodes
    node.remove_arg_spec_nodes
  }

  @grammar
end

#move_commandsObject

Move commands that are nested within a different command than it belongs to



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
# File 'lib/shellopts/analyzer.rb', line 80

def move_commands
  # We can't use Command#[] at this point so we collect the commands here
  h = {}
  @grammar.traverse(Grammar::Command) { |command|
    h[command.path] = command
  }

  # Find commands to move
  #
  # Commands are moved in two steps because the behaviour of #traverse is
  # not defined when the data structure changes beneath it
  move = []
  @grammar.traverse(Grammar::Command) { |command|
    if command.path.size > 1 && command.parent && command.parent.path != command.path[0..-2]
      move << command
    else
      command.instance_variable_set(:@command, command.parent)
    end
  }

  # Move commands but do not change parent/child relationship
  move.each { |command|
    supercommand = h[command.path[0..-2]] or analyzer_error "Can't find #{command.ident}!"
    command.parent.commands.delete(command)
    supercommand.commands << command
    command.instance_variable_set(:@command, supercommand)
  }
end