Class: Preprocessor::Define

Inherits:
Object
  • Object
show all
Defined in:
lib/caphir/define.rb

Overview

class

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, tokens) ⇒ Define

Returns a new instance of Define.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/caphir/define.rb', line 241

def initialize(params, tokens)
  tokens = tokens.to_s
  tokens.strip!
  @value = CTokenizer::Lexer.new(tokens).to_a
  if params
    unless params.class == Parameters
      @parameters = Parameters.new
      params.each { |p| @parameters << p }
    else
      @parameters = params
    end
  else
    # will not contain any parameters

    @value = parse(@value, nil)
    @parameters = nil
  end
  @value.freeze
end

Class Method Details

.resolve_argument(resolving, tokens, defines) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/caphir/define.rb', line 210

def Define.resolve_argument(resolving, tokens, defines)
  return tokens if tokens.empty?
  # this is important, as it sets the resolving value

  # for the rest of the token

  out = [ [:RESOLVING, resolving].freeze ]
  stack = MacroTokens.new(resolving, tokens)
  until stack.empty?
    t = stack.shift
    if t[0] == :IDENTIFIER and macro = defines[t_str = t[1]] \
    and not stack.resolving?(t_str)
      if macro.takes_args?
        unless stack.args_given?
          # no arguments given, don't resolve token

          out << t
          next
        end
        stack.add_function_macro(t_str, macro.value(stack) do |a|
          Define.resolve_argument(resolving, a, defines)
        end)
      else
        stack.add_object_macro(t_str, macro.value(stack))
      end
      # since we are saving out, must output this resolving token

      out << [:RESOLVING, stack.resolving].freeze
    else
      out << t
    end
  end # until

  out
end

Instance Method Details

#takes_args?Boolean

Returns:

  • (Boolean)


260
261
262
263
# File 'lib/caphir/define.rb', line 260

def takes_args?
  # if @parameters is empty still want to scan for '(' and ')'

  @parameters
end

#value(stack) ⇒ Object

returns a copy of the macro’s value with parameters substituted



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/caphir/define.rb', line 266

def value(stack)
  if takes_args?
    args = @parameters.arguments(stack).collect! do |a|
      # yield parameter tokens to be resolved

      v = a.collect do |t|
        case t[0]
          when :RESOLVING
            # these tokens are removed

          when :COMMENT, :NEWLINE then ' '
          else t[1]
        end
      end.join
      v.strip! # whitespace should be removed

      [:PARAMETER, v, yield(a)]
      # [ :PARAMETER, token text, resolved token ]

      # ie [ :PARAMETER, '__LINE__', [[:CONSTANT, 10]] ]

    end
  
    # do argument substitution

    parse( \
      @value.collect do |t|
        if t[0] == :IDENTIFIER and idx = @parameters.index(t[1])
          args[idx]
        else
          t
        end
      end , stack.resolving )
  else
    @value.dup
  end
end