Skip to content

Commit dfdcc98

Browse files
committed
Finish Ruby documentation
1 parent ca9a660 commit dfdcc98

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

ext/prism/api_pack.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ pack_encoding_to_ruby(pm_pack_encoding encoding) {
164164
return rb_enc_from_encoding(rb_enc_from_index(index));
165165
}
166166

167+
/**
168+
* call-seq:
169+
* Pack::parse(version, variant, source) -> Format
170+
*
171+
* Parse the given source and return a format object.
172+
*/
167173
static VALUE
168174
pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_string) {
169175
if (version_symbol != v3_2_0_symbol) {
@@ -223,15 +229,17 @@ pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_
223229
break;
224230
}
225231

226-
VALUE directive_args[9] = { version_symbol,
227-
variant_symbol,
228-
rb_usascii_str_new(directive_start, directive_end - directive_start),
229-
pack_type_to_symbol(type),
230-
pack_signed_to_symbol(signed_type),
231-
pack_endian_to_symbol(endian),
232-
pack_size_to_symbol(size),
233-
pack_length_type_to_symbol(length_type),
234-
UINT64T2NUM(length) };
232+
VALUE directive_args[9] = {
233+
version_symbol,
234+
variant_symbol,
235+
rb_usascii_str_new(directive_start, directive_end - directive_start),
236+
pack_type_to_symbol(type),
237+
pack_signed_to_symbol(signed_type),
238+
pack_endian_to_symbol(endian),
239+
pack_size_to_symbol(size),
240+
pack_length_type_to_symbol(length_type),
241+
UINT64T2NUM(length)
242+
};
235243

236244
rb_ary_push(directives_array, rb_class_new_instance(9, directive_args, rb_cPrismPackDirective));
237245
}
@@ -242,6 +250,9 @@ pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_
242250
return rb_class_new_instance(2, format_args, rb_cPrismPackFormat);
243251
}
244252

253+
/**
254+
* The function that gets called when Ruby initializes the prism extension.
255+
*/
245256
void
246257
Init_prism_pack(void) {
247258
rb_cPrism = rb_define_module("Prism");

lib/prism/pack.rb

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,34 @@ module Pack
5757

5858
# A directive in the pack template language.
5959
class Directive
60-
attr_reader :version, :variant, :source, :type, :signed, :endian, :size, :length_type, :length
60+
# A symbol representing the version of Ruby.
61+
attr_reader :version
6162

63+
# A symbol representing whether or not we are packing or unpacking.
64+
attr_reader :variant
65+
66+
# A byteslice of the source string that this directive represents.
67+
attr_reader :source
68+
69+
# The type of the directive.
70+
attr_reader :type
71+
72+
# The type of signedness of the directive.
73+
attr_reader :signed
74+
75+
# The type of endianness of the directive.
76+
attr_reader :endian
77+
78+
# The size of the directive.
79+
attr_reader :size
80+
81+
# The length type of this directive (used for integers).
82+
attr_reader :length_type
83+
84+
# The length of this directive (used for integers).
85+
attr_reader :length
86+
87+
# Initialize a new directive with the given values.
6288
def initialize(version, variant, source, type, signed, endian, size, length_type, length)
6389
@version = version
6490
@variant = variant
@@ -71,6 +97,7 @@ def initialize(version, variant, source, type, signed, endian, size, length_type
7197
@length = length
7298
end
7399

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

109+
# The descriptions of the various types of signedness.
82110
SIGNED_DESCRIPTIONS = {
83111
UNSIGNED: "unsigned",
84112
SIGNED: "signed",
85113
SIGNED_NA: "n/a"
86114
}
87115

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

129+
# Provide a human-readable description of the directive.
100130
def describe
101131
case type
102132
when SPACE
@@ -161,15 +191,21 @@ def describe
161191
end
162192
end
163193

164-
# A class used to describe what a pack template does.
194+
# The result of parsing a pack template.
165195
class Format
166-
attr_reader :directives, :encoding
196+
# A list of the directives in the template.
197+
attr_reader :directives
198+
199+
# The encoding of the template.
200+
attr_reader :encoding
167201

202+
# Create a new Format with the given directives and encoding.
168203
def initialize(directives, encoding)
169204
@directives = directives
170205
@encoding = encoding
171206
end
172207

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

0 commit comments

Comments
 (0)