Class: Xumlidot::Parsers::Args

Inherits:
MethodBasedSexpProcessor
  • Object
show all
Defined in:
lib/xumlidot/parsers/args.rb

Overview

Parser for the arguments to a method

e.g. formats def method(a, b = nil)

to a string 'a, b = nil'

Instance Method Summary collapse

Constructor Details

#initialize(exp) ⇒ Args

Returns a new instance of Args.



15
16
17
18
19
20
21
22
23
# File 'lib/xumlidot/parsers/args.rb', line 15

def initialize(exp)
  super()

  @arguments = ::Xumlidot::Types::Arguments.new

  process(exp)
rescue => e
  STDERR.puts " ** bug: unable to process args #{exp} "
end

Instance Method Details

#definitionObject



29
30
31
# File 'lib/xumlidot/parsers/args.rb', line 29

def definition
  @arguments
end

#process_args(exp) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/xumlidot/parsers/args.rb', line 141

def process_args(exp)
  exp.shift # remove :args
  exp.each do |arg|
    @argument = ::Xumlidot::Types::Argument.new
    if arg.is_a? Sexp
      @argument.assign = '='
      process(arg)
    else
      @argument.name = arg.to_s
    end

    @arguments << @argument
  end
rescue => e
  STDERR.puts " ** bug: unable to process args #{exp}; failure to parse default value? "
end

#process_array(exp) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/xumlidot/parsers/args.rb', line 90

def process_array(exp)
  @argument.default = []

  exp.shift # remove :array
  exp.each { |element| process(element) }
  s()
end

#process_colon2(exp) ⇒ Object

Colon2 means that we have a constant assignment such as (a = Foo::Bar)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/xumlidot/parsers/args.rb', line 59

def process_colon2(exp)
  name = exp.flatten
  name.delete :const
  name.delete :colon2

  leader = ''
  if name.first == :colon3
    leader = '::'
    name.delete :colon3
  end

  # I'm not sure how best to proceed here.
  #
  # I can use const_set to start creating the constants heirachy
  # but this is complex since it needs to be inserted into the right
  # place and for that I need the namespace...which suggests this ISNT
  # the place to do that. I possibly need a fake class ...
  @argument.default = leader + name.map do |v|
    v.to_s
  end.to_a.join('::')

  s()
end

#process_colon3(exp) ⇒ Object

Colon2 means that we have a constant assignment such as (a = ::Foo::Bar) again see the note in colon2 about how to proceed



85
86
87
88
# File 'lib/xumlidot/parsers/args.rb', line 85

def process_colon3(exp)
  process_colon2(exp)
  @argument.default = "::#{argument.default}"
end

#process_const(exp) ⇒ Object

const means that we have a constant assignment such as (a = Foo)



53
54
55
56
# File 'lib/xumlidot/parsers/args.rb', line 53

def process_const(exp)
  @argument.default = exp.value.to_s
  s()
end

#process_hash(exp) ⇒ Object



47
48
49
50
# File 'lib/xumlidot/parsers/args.rb', line 47

def process_hash(exp)
  @argument.default = {}
  s()
end

#process_kwarg(exp) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/xumlidot/parsers/args.rb', line 131

def process_kwarg(exp)
  exp.shift # remove :kwarg
  @argument.name = "#{exp[0]}:"
  process(exp)
  s()
rescue => e
  STDERR.puts " ** bug: unable to process kwarg #{exp}; failure to parse default value? "
  s()
end

#process_lasgn(exp) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/xumlidot/parsers/args.rb', line 98

def process_lasgn(exp)
  exp.shift # remove :lasgn

  @argument.name = exp.shift.to_s

  value = exp.shift
  process(value)
  s()
end

#process_lit(exp) ⇒ Object



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

def process_lit(exp)
  exp.shift # remove :lit

  case @argument.default
  when Array
    @argument.default << exp.value
  when nil
    @argument.default = exp.value
  when Symbol
    @argument.default = exp.value.to_s
  when String
    @argument.default = exp.value.to_s
  when Sexp
    # binding.pry
  when Hash
    # binding.pry
  else
    # binding.pry
  end
  exp.shift
  s()
end

#process_nil(exp) ⇒ Object

Note - special case since a value of nil for default means we shouldn’t display it and so we use the :nil symbol to represent an *actual assignment of nil* to a variable.



37
38
39
40
# File 'lib/xumlidot/parsers/args.rb', line 37

def process_nil(exp)
  @argument.default = :nil
  s()
end

#process_str(exp) ⇒ Object



42
43
44
45
# File 'lib/xumlidot/parsers/args.rb', line 42

def process_str(exp)
  @argument.default = exp.value
  s()
end

#to_sObject



25
26
27
# File 'lib/xumlidot/parsers/args.rb', line 25

def to_s
  @arguments.to_s
end