Module: Prism::Pack

Defined in:
lib/prism/pack.rb,
ext/prism/api_pack.c

Overview

A parser for the pack template language.

Defined Under Namespace

Classes: Directive, Format

Class Method Summary collapse

Class Method Details

.Pack::parse(version, variant, source) ⇒ Format

Parse the given source and return a format object.

Returns:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'ext/prism/api_pack.c', line 180

static VALUE
pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_string) {
    if (version_symbol != v3_2_0_symbol) {
        rb_raise(rb_eArgError, "invalid version");
    }

    pm_pack_variant variant;
    if (variant_symbol == pack_symbol) {
        variant = PM_PACK_VARIANT_PACK;
    } else if (variant_symbol == unpack_symbol) {
        variant = PM_PACK_VARIANT_UNPACK;
    } else {
        rb_raise(rb_eArgError, "invalid variant");
    }

    StringValue(format_string);

    const char *format = RSTRING_PTR(format_string);
    const char *format_end = format + RSTRING_LEN(format_string);
    pm_pack_encoding encoding = PM_PACK_ENCODING_START;

    VALUE directives_array = rb_ary_new();

    while (format < format_end) {
        pm_pack_type type;
        pm_pack_signed signed_type;
        pm_pack_endian endian;
        pm_pack_size size;
        pm_pack_length_type length_type;
        uint64_t length;

        const char *directive_start = format;

        pm_pack_result parse_result = pm_pack_parse(variant, &format, format_end, &type, &signed_type, &endian,
                                                    &size, &length_type, &length, &encoding);

        const char *directive_end = format;

        switch (parse_result) {
            case PM_PACK_OK:
                break;
            case PM_PACK_ERROR_UNSUPPORTED_DIRECTIVE:
                rb_raise(rb_eArgError, "unsupported directive");
            case PM_PACK_ERROR_UNKNOWN_DIRECTIVE:
                rb_raise(rb_eArgError, "unsupported directive");
            case PM_PACK_ERROR_LENGTH_TOO_BIG:
                rb_raise(rb_eRangeError, "pack length too big");
            case PM_PACK_ERROR_BANG_NOT_ALLOWED:
                rb_raise(rb_eRangeError, "bang not allowed");
            case PM_PACK_ERROR_DOUBLE_ENDIAN:
                rb_raise(rb_eRangeError, "double endian");
            default:
                rb_bug("parse result");
        }

        if (type == PM_PACK_END) {
            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)
        };

        rb_ary_push(directives_array, rb_class_new_instance(9, directive_args, rb_cPrismPackDirective));
    }

    VALUE format_args[2];
    format_args[0] = directives_array;
    format_args[1] = pack_encoding_to_ruby(encoding);
    return rb_class_new_instance(2, format_args, rb_cPrismPackFormat);
}