module Rucc::Lexer::Preprocessor::SpecialMacro

Private Instance Methods

define_special_macro(name, fn) click to toggle source

@param [Symbol] fn handler name of special macro

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 23
def define_special_macro(name, fn)
  # @param [Token] tok
  handler = -> (tok) { self.send(fn, tok) }
  @macros[name] = make_special_macro(handler)
end
define_special_macros!() click to toggle source
# File lib/rucc/lexer/preprocessor/special_macro.rb, line 8
def define_special_macros!
  define_special_macro "__DATE__",          :handle_date_macro
  define_special_macro "__TIME__",          :handle_time_macro
  define_special_macro "__FILE__",          :handle_file_macro
  define_special_macro "__LINE__",          :handle_line_macro
  define_special_macro "_Pragma",           :handle_pragma_macro

  # [GNU] Non-standard macros
  define_special_macro "__BASE_FILE__",     :handle_base_file_macro
  define_special_macro "__COUNTER__",       :handle_counter_macro
  define_special_macro "__INCLUDE_LEVEL__", :handle_include_level_macro
  define_special_macro "__TIMESTAMP__",     :handle_timestamp_macro
end
handle_base_file_macro(tmpl) click to toggle source

@param [Token] tmpl

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 76
def handle_base_file_macro(tmpl)
  make_token_pushback(tmpl, T::STRING, @impl.infile.name)
end
handle_counter_macro(tmpl) click to toggle source

@param [Token] tmpl

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 81
def handle_counter_macro(tmpl)
  @counter ||= 0
  i = @counter
  make_token_pushback(tmpl, T::NUMBER, "%d" % i)
  @counter += 1
end
handle_date_macro(tmpl) click to toggle source

@param [Token] tmpl

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 34
def handle_date_macro(tmpl)
  s = @now.strftime("%b %e %Y")
  make_token_pushback(tmpl, T::STRING, s)
end
handle_file_macro(tmpl) click to toggle source

@param [Token] tmpl

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 54
def handle_file_macro(tmpl)
  make_token_pushback(tmpl, T::STRING, tmpl.file.name)
end
handle_include_level_macro(tmpl) click to toggle source

@param [Token] tmpl

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 89
def handle_include_level_macro(tmpl)
  make_token_pushback(tmpl, T::NUMBER, "%d" % (@impl.stream_depth - 1))
end
handle_line_macro(tmpl) click to toggle source

@param [Token] tmpl

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 59
def handle_line_macro(tmpl)
  make_token_pushback(tmpl, T::NUMBER, sprintf("%d", tmpl.file.line))
end
handle_pragma_macro(tmpl) click to toggle source

@param [Token] tmpl

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 64
def handle_pragma_macro(tmpl)
  expect!('(')
  operand = read_token
  if operand.kind != T::STRING
    Util.errort!(operand, "_Pragma takes a string literal, but got #{operand}")
  end
  expect!(')')
  parse_pragma_operand(operand)
  make_token_pushback(tmpl, T::NUMBER, "1")
end
handle_time_macro(tmpl) click to toggle source

@param [Token] tmpl

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 40
def handle_time_macro(tmpl)
  s = @now.strftime("%T")
  make_token_pushback(tmpl, T::STRING, s)
end
handle_timestamp_macro(tmpl) click to toggle source

@param [Token] tmpl

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 46
def handle_timestamp_macro(tmpl)
  # [GNU] __TIMESTAMP__ is expanded to a string that describes the date
  # and time of the last modification time of the current source file.
  s = tmpl.file.mtime.strftime("%a %b %e %T %Y")
  make_token_pushback(tmpl, T::STRING, s)
end
make_token_pushback(tmpl, kind, sval) click to toggle source

@param [Token] tmpl @param [T] kind @param [String] sval

# File lib/rucc/lexer/preprocessor/special_macro.rb, line 100
def make_token_pushback(tmpl, kind, sval)
  tok = tmpl.dup
  tok.kind = kind
  tok.sval = sval
  tok.enc = ENC::NONE
  unget_token(tok)
end