Module: Prism::Pack
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Defined in: | lib/prism/pack.rb, prism/api_pack.c |
Overview
A parser for the pack template language.
Class Method Summary
-
.parse(version, variant, source) ⇒ Format
Parse the given source and return a format object.
Class Method Details
.parse(version, variant, source) ⇒ Format
Parse the given source and return a format object.
# File 'prism/api_pack.c', line 173
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); }