Class: Preprocessor::Parameters

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

Overview

class

Constant Summary collapse

VA_ARGS =
'__VA_ARGS__'.freeze
ELLIPSIS =
'...'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParameters

Returns a new instance of Parameters.



360
361
362
363
# File 'lib/caphir/define.rb', line 360

def initialize
  super
  @ellipses = nil
end

Class Method Details

.get_closing_braket(arg, tokens) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/caphir/define.rb', line 468

def Parameters.get_closing_braket(arg, tokens)
  begin
    t = tokens.shift
    tokens.unmatched_error('}') unless t and t[0]

    arg << t
    case t[1]
      when '('
        get_closing_paren(arg, tokens)
      when ')'
        tokens.unmatched_error(')')
      when '{'
        get_closing_braket(arg, tokens)
    end
  end until t[1] == '}'
end

.get_closing_paren(arg, tokens) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/caphir/define.rb', line 451

def Parameters.get_closing_paren(arg, tokens)
  begin
    t = tokens.shift
    tokens.unmatched_error(')') unless t and t[0]
  
    arg << t
    case t[1]
      when '{'
        get_closing_braket(arg, tokens)
      when '}'
        tokens.unmatched_error('}')
      when '('
        get_closing_paren(arg, tokens)
    end
  end until t[1] == ')'
end

Instance Method Details

#arguments(tokens) ⇒ Object

it is important that we keep track of the last resolving status when splitting up arguments. That way the argument does not inherit the last resolving status of the previous tokens when it is substituted.



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/caphir/define.rb', line 396

def arguments(tokens)

  # skip whitespace

  while true
    t = tokens.shift
    case t[0]
      when :RESOLVING, :SPACE, :COMMENT, :NEWLINE
        # do nothing

      else
        break
    end
  end

  tokens.error("expecting '('") unless t[1] == '('
  
  arguments = [arg = [ [:RESOLVING, tokens.resolving].freeze ] ]
  
  while true
    t = tokens.shift
    tokens.unmatched_error(')') unless t and t[0]

    # comma and the closing ')' are not outputed

    # bunch all the remaining parameters into the last parameter

    if t[1] == ',' and arguments.length < self.length
      # start of a new argument

      arguments.push([ [:RESOLVING, tokens.resolving].freeze ])
      arg = arguments.last
    elsif t[1] == ')'
      break # end of parameter list

    else
      arg << t
      case t[1]
        when '{'
          Parameters.get_closing_braket(arg, tokens)
        when '}'
          tokens.unmatched_error('}')
        when '('
          Parameters.get_closing_paren(arg, tokens)
      end
    end
  end

  got = arguments.length
  expected = self.length
  if got < expected
    tokens.error("wrong number of arguments (#{got} for #{expected})") unless ellipses?
    expected -= 1 # the variable argument list can be blank

    tokens.error("wrong number of arguments (#{got} for #{expected}+)") \
        unless got == expected
    arguments[expected] = [] # last arg is blank

  end

  arguments
end

#ellipses?Boolean

Returns:

  • (Boolean)


377
378
379
# File 'lib/caphir/define.rb', line 377

def ellipses?
  @ellipses
end

#inspectObject



389
390
391
# File 'lib/caphir/define.rb', line 389

def inspect
  "\#<#{self.class}:#{self.to_s}>"
end

#to_sObject



381
382
383
384
385
386
387
# File 'lib/caphir/define.rb', line 381

def to_s
  if ellipses?
    self[0, self.length - 1] << @ellipses
  else
    self
  end.join(',')
end

#with_ellipses(arg = nil) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
# File 'lib/caphir/define.rb', line 365

def with_ellipses(arg=nil)
  raise "already has ellipses: #{self}" if ellipses?
  if arg and arg != ELLIPSIS
    self << arg
    @ellipses = arg + ELLIPSIS # if arg is 'i' then 'i...'

  else
    self << VA_ARGS
    @ellipses = ELLIPSIS
  end
  self
end