123456789_123456789_123456789_123456789_123456789_

Class: Prism::Pack::Directive

Relationships & Source Files
Inherits: Object
Defined in: lib/prism/pack.rb,
prism/api_pack.c

Overview

A directive in the pack template language.

Constant Summary

  • ENDIAN_DESCRIPTIONS =

    The descriptions of the various types of endianness.

    # File 'lib/prism/pack.rb', line 101
    {
      AGNOSTIC_ENDIAN: "agnostic",
      LITTLE_ENDIAN: "little-endian (VAX)",
      BIG_ENDIAN: "big-endian (network)",
      NATIVE_ENDIAN: "native-endian",
      ENDIAN_NA: "n/a"
    }
  • SIGNED_DESCRIPTIONS =

    The descriptions of the various types of signedness.

    # File 'lib/prism/pack.rb', line 110
    {
      UNSIGNED: "unsigned",
      SIGNED: "signed",
      SIGNED_NA: "n/a"
    }
  • SIZE_DESCRIPTIONS =

    The descriptions of the various types of sizes.

    # File 'lib/prism/pack.rb', line 117
    {
      SIZE_SHORT: "short",
      SIZE_INT: "int-width",
      SIZE_LONG: "long",
      SIZE_LONG_LONG: "long long",
      SIZE_8: "8-bit",
      SIZE_16: "16-bit",
      SIZE_32: "32-bit",
      SIZE_64: "64-bit",
      SIZE_P: "pointer-width"
    }

Class Method Summary

Instance Attribute Summary

  • #endian readonly

    The type of endianness of the directive.

  • #length readonly

    The length of this directive (used for integers).

  • #length_type readonly

    The length type of this directive (used for integers).

  • #signed readonly

    The type of signedness of the directive.

  • #size readonly

    The size of the directive.

  • #source readonly

    A byteslice of the source string that this directive represents.

  • #type readonly

    The type of the directive.

  • #variant readonly

    A symbol representing whether or not we are packing or unpacking.

  • #version readonly

    A symbol representing the version of Ruby.

Instance Method Summary

  • #describe

    Provide a human-readable description of the directive.

Constructor Details

.new(version, variant, source, type, signed, endian, size, length_type, length) ⇒ Directive

Initialize a new directive with the given values.

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 88

def initialize(version, variant, source, type, signed, endian, size, length_type, length)
  @version = version
  @variant = variant
  @source = source
  @type = type
  @signed = signed
  @endian = endian
  @size = size
  @length_type = length_type
  @length = length
end

Instance Attribute Details

#endian (readonly)

The type of endianness of the directive.

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 76

attr_reader :endian

#length (readonly)

The length of this directive (used for integers).

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 85

attr_reader :length

#length_type (readonly)

The length type of this directive (used for integers).

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 82

attr_reader :length_type

#signed (readonly)

The type of signedness of the directive.

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 73

attr_reader :signed

#size (readonly)

The size of the directive.

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 79

attr_reader :size

#source (readonly)

A byteslice of the source string that this directive represents.

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 67

attr_reader :source

#type (readonly)

The type of the directive.

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 70

attr_reader :type

#variant (readonly)

A symbol representing whether or not we are packing or unpacking.

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 64

attr_reader :variant

#version (readonly)

A symbol representing the version of Ruby.

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 61

attr_reader :version

Instance Method Details

#describe

Provide a human-readable description of the directive.

[ GitHub ]

  
# File 'lib/prism/pack.rb', line 130

def describe
  case type
  when SPACE
    "whitespace"
  when COMMENT
    "comment"
  when INTEGER
    if size == SIZE_8
      base = "#{SIGNED_DESCRIPTIONS[signed]} #{SIZE_DESCRIPTIONS[size]} integer"
    else
      base = "#{SIGNED_DESCRIPTIONS[signed]} #{SIZE_DESCRIPTIONS[size]} #{ENDIAN_DESCRIPTIONS[endian]} integer"
    end
    case length_type
    when LENGTH_FIXED
      if length > 1
        base + ", x#{length}"
      else
        base
      end
    when LENGTH_MAX
      base + ", as many as possible"
    end
  when UTF8
    "UTF-8 character"
  when BER
    "BER-compressed integer"
  when FLOAT
    "#{SIZE_DESCRIPTIONS[size]} #{ENDIAN_DESCRIPTIONS[endian]} float"
  when STRING_SPACE_PADDED
    "arbitrary binary string (space padded)"
  when STRING_NULL_PADDED
    "arbitrary binary string (null padded, count is width)"
  when STRING_NULL_TERMINATED
    "arbitrary binary string (null padded, count is width), except that null is added with *"
  when STRING_MSB
    "bit string (MSB first)"
  when STRING_LSB
    "bit string (LSB first)"
  when STRING_HEX_HIGH
    "hex string (high nibble first)"
  when STRING_HEX_LOW
    "hex string (low nibble first)"
  when STRING_UU
    "UU-encoded string"
  when STRING_MIME
    "quoted printable, MIME encoding"
  when STRING_BASE64
    "base64 encoded string"
  when STRING_FIXED
    "pointer to a structure (fixed-length string)"
  when STRING_POINTER
    "pointer to a null-terminated string"
  when MOVE
    "move to absolute position"
  when BACK
    "back up a byte"
  when NULL
    "null byte"
  else
    raise
  end
end