Module: RASN2::SchemaParser

Defined in:
lib/rasn2/schema_parser.rb

Overview

Parser for ASN.1 text schema definitions.

Parses ASN.1 module definitions and generates Model subclasses from them.

Examples:

Parse a schema file

models = RASN2::SchemaParser.parse_file('path/to/schema.asn')
# => { 'PersonnelRecord' => PersonnelRecord (class < RASN2::Model) }

Parse a schema string

schema = <<~ASN1
  MyModule DEFINITIONS ::=
  BEGIN
    MyRecord ::= SEQUENCE {
      name  PrintableString,
      age   INTEGER
    }
  END
ASN1
models = RASN2::SchemaParser.parse(schema)

Author:

  • rickmark

Since:

  • 0.16.0

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

TYPE_MAP =

ASN.1 type name to RASN2 type DSL method mapping

Since:

  • 0.16.0

{
  'BOOLEAN' => :boolean,
  'INTEGER' => :integer,
  'BIT STRING' => :bit_string,
  'OCTET STRING' => :octet_string,
  'NULL' => :null,
  'OBJECT IDENTIFIER' => :objectid,
  'ENUMERATED' => :enumerated,
  'UTF8String' => :utf8_string,
  'PrintableString' => :printable_string,
  'IA5String' => :ia5_string,
  'VisibleString' => :visible_string,
  'NumericString' => :numeric_string,
  'BMPString' => :bmp_string,
  'UniversalString' => :universal_string,
  'UTCTime' => :utc_time,
  'GeneralizedTime' => :generalized_time
}.freeze
CONSTRUCTED_TYPES =

Constructed type names

Since:

  • 0.16.0

%w[SEQUENCE SET TAG].freeze

Class Method Summary collapse

Class Method Details

.parse(schema, namespace: Object) ⇒ Hash{String => Class}

Parse an ASN.1 schema string and generate Model classes.

Parameters:

  • schema (String)

    the ASN.1 schema text

  • namespace (Module) (defaults to: Object)

    module in which to define the generated classes (default: Object)

Returns:

  • (Hash{String => Class})

    hash mapping type names to generated Model subclasses

Raises:

Since:

  • 0.16.0



68
69
70
71
# File 'lib/rasn2/schema_parser.rb', line 68

def parse(schema, namespace: Object)
  mod = parse_module(schema)
  build_models(mod, namespace: namespace)
end

.parse_file(filename, namespace: Object) ⇒ Hash{String => Class}

Parse an ASN.1 schema from a file and generate Model classes.

Parameters:

  • filename (String)

    path to the ASN.1 schema file

  • namespace (Module) (defaults to: Object)

    module in which to define the generated classes (default: Object)

Returns:

  • (Hash{String => Class})

    hash mapping type names to generated Model subclasses

Raises:

Since:

  • 0.16.0



59
60
61
# File 'lib/rasn2/schema_parser.rb', line 59

def parse_file(filename, namespace: Object)
  parse(File.read(filename), namespace: namespace)
end

.type_to_class(type_name) ⇒ Class?

Convert an ASN.1 type name to the corresponding RASN2::Types class

Parameters:

  • type_name (String)

Returns:

  • (Class, nil)

Since:

  • 0.16.0



458
459
460
461
462
463
464
465
466
467
468
# File 'lib/rasn2/schema_parser.rb', line 458

def self.type_to_class(type_name)
  method_sym = TYPE_MAP[type_name]
  return nil unless method_sym

  class_name = type_name.gsub(/\s+/, '')
  begin
    Types.const_get(class_name)
  rescue NameError
    nil
  end
end

.type_to_method(type_name) ⇒ Symbol?

Convert an ASN.1 type name to the corresponding RASN2 Model DSL method symbol

Parameters:

  • type_name (String)

Returns:

  • (Symbol, nil)

Since:

  • 0.16.0



451
452
453
# File 'lib/rasn2/schema_parser.rb', line 451

def self.type_to_method(type_name)
  TYPE_MAP[type_name]
end