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.



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

def initialize(exp)
  super()

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

  process(Sexp.new.concat(exp))
rescue StandardError => _e
  warn " ** bug: unable to process args #{exp} "
end

Instance Method Details

#definitionObject



27
28
29
# File 'lib/xumlidot/parsers/args.rb', line 27

def definition
  @arguments
end

#process_args(exp) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/xumlidot/parsers/args.rb', line 137

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 StandardError
  warn " ** bug: unable to process args #{exp}; failure to parse default value?"
end

#process_array(exp) ⇒ Object



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

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)



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

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| # rubocop:disable Style/SymbolProc
    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



83
84
85
86
# File 'lib/xumlidot/parsers/args.rb', line 83

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)



51
52
53
54
# File 'lib/xumlidot/parsers/args.rb', line 51

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

#process_hash(_exp) ⇒ Object



45
46
47
48
# File 'lib/xumlidot/parsers/args.rb', line 45

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

#process_kwarg(exp) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/xumlidot/parsers/args.rb', line 127

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

#process_lasgn(exp) ⇒ Object



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

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



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

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, 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.



35
36
37
38
# File 'lib/xumlidot/parsers/args.rb', line 35

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

#process_str(exp) ⇒ Object



40
41
42
43
# File 'lib/xumlidot/parsers/args.rb', line 40

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

#to_sObject



23
24
25
# File 'lib/xumlidot/parsers/args.rb', line 23

def to_s
  @arguments.to_s
end