Class: ShellOpts::Grammar::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/shellopts/grammar.rb,
lib/shellopts/dump.rb,
lib/shellopts/parser.rb,
lib/shellopts/grammar.rb,
lib/shellopts/analyzer.rb,
lib/shellopts/formatter.rb

Overview

Except for #parent, #children, and #token, all members are initialized by calling #parse on the object

Direct Known Subclasses

Arg, ArgSpec, DocNode, IdrNode, OptionGroup, Section

Constant Summary collapse

ALLOWED_PARENTS =
{
  Program => [NilClass],
  Command => [Command],
  OptionGroup => [Command],
  Option => [OptionGroup],
  ArgSpec => [Command],
  Arg => [ArgSpec],
  ArgDescr => [Command],
  Brief => [Command, OptionGroup, ArgSpec, ArgDescr],
  Paragraph => [Command, OptionGroup],
  Code => [Command, OptionGroup],
  Section => [Program]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, token) ⇒ Node

Note that in derived classes ‘super’ should be called after member initialization because Node#initialize calls #attach on the parent that may need to access the members



14
15
16
17
18
19
20
21
22
23
# File 'lib/shellopts/grammar.rb', line 14

def initialize(parent, token)
  constrain parent, Node, nil
  constrain parent, nil, lambda { |node| ALLOWED_PARENTS[self.class].any? { |klass| node.is_a?(klass) } }
  constrain token, Token

  @parent = parent
  @children = []
  @token = token
  @parent.send(:attach, self) if @parent
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



8
9
10
# File 'lib/shellopts/grammar.rb', line 8

def children
  @children
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/shellopts/grammar.rb', line 7

def parent
  @parent
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/shellopts/grammar.rb', line 9

def token
  @token
end

Class Method Details

.parse(parent, token) ⇒ Object



8
9
10
11
12
# File 'lib/shellopts/parser.rb', line 8

def self.parse(parent, token)
  this = self.new(parent, token)
  this.parse
  this
end

Instance Method Details

#analyzer_error(token, message) ⇒ Object

Raises:



17
18
19
# File 'lib/shellopts/analyzer.rb', line 17

def analyzer_error(token, message)
  raise AnalyzerError.new(token), message
end

#ancestorsObject



30
# File 'lib/shellopts/grammar.rb', line 30

def ancestors() parents.reverse end

#dump_astObject



35
36
37
38
# File 'lib/shellopts/dump.rb', line 35

def dump_ast
  puts "#{classname} @ #{token.pos} #{token.source}"
  indent { children.each(&:dump_ast) }
end

#dump_attrs(*attrs) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/shellopts/dump.rb', line 44

def dump_attrs(*attrs)
  indent {
    Array(attrs).flatten.select { |attr| attr.is_a?(Symbol) }.each { |attr|
      value = self.send(attr)
      case value
        when Brief
          puts "#{attr}: #{value.text}"
        when Node
          puts "#{attr}:"
          indent { value.dump_idr }
        when Array
          case value.first
            when nil
              puts "#{attr}: []"
            when Node
              puts "#{attr}:"
              indent { value.each(&:dump_idr) }
          else
            puts "#{attr}: #{value.inspect}"
          end
        when ArgumentType
          puts "#{attr}: #{value}"
      else
#             value = value.inspect if value.nil? || !value.respond_to?(:to_s)
        puts "#{attr}: #{value.inspect}"
      end
    }
  }
end

#dump_idr(short = false) ⇒ Object



40
41
42
# File 'lib/shellopts/dump.rb', line 40

def dump_idr(short = false)
  puts "#{classname}" if !short
end

#inspectObject



32
33
34
# File 'lib/shellopts/grammar.rb', line 32

def inspect
  self.class.to_s
end

#parentsObject



29
# File 'lib/shellopts/grammar.rb', line 29

def parents() parent ? [parent] + parent.parents : [] end

#parseObject



6
# File 'lib/shellopts/parser.rb', line 6

def parse() end

#parser_error(token, message) ⇒ Object

Raises:



14
# File 'lib/shellopts/parser.rb', line 14

def parser_error(token, message) raise ParserError, "#{token.pos} #{message}" end

#puts_helpObject



6
# File 'lib/shellopts/formatter.rb', line 6

def puts_help() end

#puts_usageObject



7
# File 'lib/shellopts/formatter.rb', line 7

def puts_usage() end

#remove_arg_descr_nodesObject



9
10
11
# File 'lib/shellopts/analyzer.rb', line 9

def remove_arg_descr_nodes
  children.delete_if { |node| node.is_a?(ArgDescr) }
end

#remove_arg_spec_nodesObject



13
14
15
# File 'lib/shellopts/analyzer.rb', line 13

def remove_arg_spec_nodes
  children.delete_if { |node| node.is_a?(ArgSpec) }
end

#remove_brief_nodesObject



5
6
7
# File 'lib/shellopts/analyzer.rb', line 5

def remove_brief_nodes
  children.delete_if { |node| node.is_a?(Brief) }
end

#traverse(*klasses, &block) ⇒ Object



25
26
27
# File 'lib/shellopts/grammar.rb', line 25

def traverse(*klasses, &block)
  do_traverse(Array(klasses).flatten, &block)
end