Class: ShellOpts::Analyzer

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

Instance Attribute 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

Instance Method Details

#analyzeObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/shellopts/analyzer.rb', line 143

def analyze()
  link_commands

  @grammar.traverse(Grammar::Command) { |command|
    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

#create_implicit_commands(cmd) ⇒ Object



79
80
81
82
83
# File 'lib/shellopts/analyzer.rb', line 79

def create_implicit_commands(cmd)
  path = cmd.path[0..-2]


end

Link up commands with supercommands. This is only done for commands that are nested within a different command than it belongs to. The parent/child relationship is not changed Example:

cmd!
cmd.subcmd!

Here subcmd is added to cmd's list of commands. It keeps its position in the program's parent/child relationship so that documentation will print the commands in the given order and with the given indentation level



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/shellopts/analyzer.rb', line 96

def link_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
    # TODO: Pick up parent-less commands
  }

  # Command to link
  link = []

  # Create implicit commands
  h.sort { |l,r| l.size <=> r.size }.each { |path, command|
    path = path[0..-2]
    while !h.key?(path)
      cmd = Grammar::Command.new(nil, command.token)
      cmd.set_name(path.last.to_s.sub(/!/, ""), path.dup)
      link << cmd
      h[cmd.path] = cmd
      path.pop
    end
  }

  # Find commands to link
  #
  # Commands are linked in two steps because the behaviour of #traverse is
  # not defined when the data structure changes beneath it. (FIXME: Does it
  # change when we don't touch the parent/child relationship?)
  @grammar.traverse(Grammar::Command) { |command|
    if command.path.size > 1 && command.parent && command.parent.path != command.path[0..-2]
#       if command.path.size > 1 && command.parent.path != command.path[0..-2]
      link << command
    else
      command.instance_variable_set(:@command, command.parent)
    end
  }

  # Link commands but do not change parent/child relationship
  link.each { |command|
    path = command.path[0..-2]
    path.pop while (supercommand = h[path]).nil?
    command.parent.commands.delete(command) if command.parent
    supercommand.commands << command
    command.instance_variable_set(:@command, supercommand)
  }
end