Skip to content

Commit

Permalink
Finish Ruby documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Nov 1, 2023
1 parent ca9a660 commit dfdcc98
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
29 changes: 20 additions & 9 deletions ext/prism/api_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ pack_encoding_to_ruby(pm_pack_encoding encoding) {
return rb_enc_from_encoding(rb_enc_from_index(index));
}

/**
* call-seq:
* Pack::parse(version, variant, source) -> Format
*
* Parse the given source and return a format object.
*/
static VALUE
pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_string) {
if (version_symbol != v3_2_0_symbol) {
Expand Down Expand Up @@ -223,15 +229,17 @@ pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_
break;
}

VALUE directive_args[9] = { version_symbol,
variant_symbol,
rb_usascii_str_new(directive_start, directive_end - directive_start),
pack_type_to_symbol(type),
pack_signed_to_symbol(signed_type),
pack_endian_to_symbol(endian),
pack_size_to_symbol(size),
pack_length_type_to_symbol(length_type),
UINT64T2NUM(length) };
VALUE directive_args[9] = {
version_symbol,
variant_symbol,
rb_usascii_str_new(directive_start, directive_end - directive_start),
pack_type_to_symbol(type),
pack_signed_to_symbol(signed_type),
pack_endian_to_symbol(endian),
pack_size_to_symbol(size),
pack_length_type_to_symbol(length_type),
UINT64T2NUM(length)
};

rb_ary_push(directives_array, rb_class_new_instance(9, directive_args, rb_cPrismPackDirective));
}
Expand All @@ -242,6 +250,9 @@ pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_
return rb_class_new_instance(2, format_args, rb_cPrismPackFormat);
}

/**
* The function that gets called when Ruby initializes the prism extension.
*/
void
Init_prism_pack(void) {
rb_cPrism = rb_define_module("Prism");
Expand Down
42 changes: 39 additions & 3 deletions lib/prism/pack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,34 @@ module Pack

# A directive in the pack template language.
class Directive
attr_reader :version, :variant, :source, :type, :signed, :endian, :size, :length_type, :length
# A symbol representing the version of Ruby.
attr_reader :version

# A symbol representing whether or not we are packing or unpacking.
attr_reader :variant

# A byteslice of the source string that this directive represents.
attr_reader :source

# The type of the directive.
attr_reader :type

# The type of signedness of the directive.
attr_reader :signed

# The type of endianness of the directive.
attr_reader :endian

# The size of the directive.
attr_reader :size

# The length type of this directive (used for integers).
attr_reader :length_type

# The length of this directive (used for integers).
attr_reader :length

# Initialize a new directive with the given values.
def initialize(version, variant, source, type, signed, endian, size, length_type, length)
@version = version
@variant = variant
Expand All @@ -71,6 +97,7 @@ def initialize(version, variant, source, type, signed, endian, size, length_type
@length = length
end

# The descriptions of the various types of endianness.
ENDIAN_DESCRIPTIONS = {
AGNOSTIC_ENDIAN: "agnostic",
LITTLE_ENDIAN: "little-endian (VAX)",
Expand All @@ -79,12 +106,14 @@ def initialize(version, variant, source, type, signed, endian, size, length_type
ENDIAN_NA: "n/a"
}

# The descriptions of the various types of signedness.
SIGNED_DESCRIPTIONS = {
UNSIGNED: "unsigned",
SIGNED: "signed",
SIGNED_NA: "n/a"
}

# The descriptions of the various types of sizes.
SIZE_DESCRIPTIONS = {
SIZE_SHORT: "short",
SIZE_INT: "int-width",
Expand All @@ -97,6 +126,7 @@ def initialize(version, variant, source, type, signed, endian, size, length_type
SIZE_P: "pointer-width"
}

# Provide a human-readable description of the directive.
def describe
case type
when SPACE
Expand Down Expand Up @@ -161,15 +191,21 @@ def describe
end
end

# A class used to describe what a pack template does.
# The result of parsing a pack template.
class Format
attr_reader :directives, :encoding
# A list of the directives in the template.
attr_reader :directives

# The encoding of the template.
attr_reader :encoding

# Create a new Format with the given directives and encoding.
def initialize(directives, encoding)
@directives = directives
@encoding = encoding
end

# Provide a human-readable description of the format.
def describe
source_width = directives.map { |d| d.source.inspect.length }.max
directive_lines = directives.map do |directive|
Expand Down

0 comments on commit dfdcc98

Please sign in to comment.