@@ -57,8 +57,34 @@ module Pack
57
57
58
58
# A directive in the pack template language.
59
59
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
61
62
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.
62
88
def initialize ( version , variant , source , type , signed , endian , size , length_type , length )
63
89
@version = version
64
90
@variant = variant
@@ -71,6 +97,7 @@ def initialize(version, variant, source, type, signed, endian, size, length_type
71
97
@length = length
72
98
end
73
99
100
+ # The descriptions of the various types of endianness.
74
101
ENDIAN_DESCRIPTIONS = {
75
102
AGNOSTIC_ENDIAN : "agnostic" ,
76
103
LITTLE_ENDIAN : "little-endian (VAX)" ,
@@ -79,12 +106,14 @@ def initialize(version, variant, source, type, signed, endian, size, length_type
79
106
ENDIAN_NA : "n/a"
80
107
}
81
108
109
+ # The descriptions of the various types of signedness.
82
110
SIGNED_DESCRIPTIONS = {
83
111
UNSIGNED : "unsigned" ,
84
112
SIGNED : "signed" ,
85
113
SIGNED_NA : "n/a"
86
114
}
87
115
116
+ # The descriptions of the various types of sizes.
88
117
SIZE_DESCRIPTIONS = {
89
118
SIZE_SHORT : "short" ,
90
119
SIZE_INT : "int-width" ,
@@ -97,6 +126,7 @@ def initialize(version, variant, source, type, signed, endian, size, length_type
97
126
SIZE_P : "pointer-width"
98
127
}
99
128
129
+ # Provide a human-readable description of the directive.
100
130
def describe
101
131
case type
102
132
when SPACE
@@ -161,15 +191,21 @@ def describe
161
191
end
162
192
end
163
193
164
- # A class used to describe what a pack template does .
194
+ # The result of parsing a pack template.
165
195
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
167
201
202
+ # Create a new Format with the given directives and encoding.
168
203
def initialize ( directives , encoding )
169
204
@directives = directives
170
205
@encoding = encoding
171
206
end
172
207
208
+ # Provide a human-readable description of the format.
173
209
def describe
174
210
source_width = directives . map { |d | d . source . inspect . length } . max
175
211
directive_lines = directives . map do |directive |
0 commit comments