module Rucc::Util

Public Class Methods

assert!(&block) click to toggle source
# File lib/rucc/util.rb, line 106
def assert!(&block)
  raise AssertError.new("Assertion failed!") if !block.call
end
ceil8(n) click to toggle source

@param [Integer] n @return [Integer]

# File lib/rucc/util.rb, line 69
def ceil8(n)
  rem = n % 8
  (rem == 0) ? n : (n - rem + 8)
end
errort!(tok, message) click to toggle source

@param [Token] tok @param [String] message @raise [RuntimeError]

# File lib/rucc/util.rb, line 81
def errort!(tok, message)
  raise_error(token_pos(tok), "ERROR", message)
end
quote(c) click to toggle source

@param [Char] c @return [char, NilClass] nil when c is not escapetable

# File lib/rucc/util.rb, line 10
def quote(c)
  case c
  when '"'  then '\\"'
  when "\\" then '\\\\'
  when "\b" then '\\b'
  when "\f" then '\\f'
  when "\n" then '\\n'
  when "\r" then '\\r'
  when "\t" then '\\t'
  when "\v" then '\\x0b'
  else           nil
  end
end
quote_append(b, c) click to toggle source

@param(return) [String] b @param [Char] c

# File lib/rucc/util.rb, line 26
def quote_append(b, c)
  q = quote(c)
  if q
    b << q
  elsif Libc.isprint(c)
    b << c
  else
    # TODO(south37) Fix this dirty hack.
    # In current impl, "\u00ff" (utf-8 two byte string) and "\xff" (one byte string) can not be distinguished.
    # By using byte array from the beginning, these can be expressed differently.
    if c.ord <= 0xff
      # One byte
      b << ("\\x%02x" % c.ord)
    else
      # Multi bytes
      c.bytes.each do |byte|
        b << ("\\x%02x" % byte)
      end
    end
  end
end
quote_char(c) click to toggle source

@param [Integer] c

# File lib/rucc/util.rb, line 61
def quote_char(c)
  return "\\\\" if c == '\\'.ord
  return "\\'"  if c == '\''.ord
  "%c" % c
end
quote_cstring(str) click to toggle source

@param [String] str @param [Char] c @return [String]

# File lib/rucc/util.rb, line 51
def quote_cstring(str)
  b = ""
  while (c = str[0])
    quote_append(b, c)
    str = str[1..-1]
  end
  b
end
raise_error(pos, label, message) click to toggle source
# File lib/rucc/util.rb, line 96
def raise_error(pos, label, message)
  raise "[#{label}] #{pos}: #{message}"
end
token_pos(tok) click to toggle source

@param [Token] tok @return [String]

# File lib/rucc/util.rb, line 87
def token_pos(tok)
  f = tok.file
  if !f
    return "(unknown)"
  end
  name = f.name || "(unknown)"
  "#{name}:#{tok.line}:#{tok.column}"
end