https://github.com/altai-man/asn-grammar
A subset of ASN.1 grammar in Raku along with a simple Actions class provided
https://github.com/altai-man/asn-grammar
asn1 grammar parser perl6 raku raku-module rakulang
Last synced: 5 months ago
JSON representation
A subset of ASN.1 grammar in Raku along with a simple Actions class provided
- Host: GitHub
- URL: https://github.com/altai-man/asn-grammar
- Owner: Altai-man
- License: artistic-2.0
- Created: 2019-01-14T16:21:54.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-04-01T17:51:49.000Z (over 5 years ago)
- Last Synced: 2025-04-01T21:04:15.709Z (over 1 year ago)
- Topics: asn1, grammar, parser, perl6, raku, raku-module, rakulang
- Language: Raku
- Homepage:
- Size: 16.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### ASN::Grammar
This module contains a grammar for parsing [ASN.1](https://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One) specification files.
#### Warnings
* The module was initially started with need to parse LDAP specification, so parts of ASN.1 grammar specification were deliberately omitted. However, Pull Requests with additional rules or fixes will be gladly accepted.
#### Synopsis
```perl6
my $spec = q:to/SPECEND/;
WorldSchema
DEFINITIONS IMPLICIT TAGS ::= BEGIN
Rocket ::= SEQUENCE
{
name UTF8String,
message UTF8String DEFAULT "Hello World",
fuel ENUMERATED {
solid(0),
liquid(1),
gas(2)
},
speed CHOICE
{
mph [0] INTEGER,
kmph [1] INTEGER
} OPTIONAL,
payload SEQUENCE OF UTF8String
}
END
SPECEND
use ASN::Grammar;
# ASN::Module represents ASN.1 module
my $module = parse-ASN($spec);
say $module.name; # WorldSchema
say $module.schema; # IMPLICIT
# TypeAssignment and ValueAssignment represent top-level ASN.1 custom definitions
my ASN::TypeAssignment $type = $module.types[0];
say $type.name; # Rocket
my ASN::RawType $rocket-type = $type.type;
say $rocket-type.type; # 'SEQUENCE'
# `.params` method returns a hash of various
# information about type, in this case,
# only `fields` key that returns descriptions
# of complex type (SEQUENCE) components is present
for $rocket-type.params<> -> $field {
say $field;
}
# Code above results in:
# Field `name` of type `UTF8String`, no parameters
# ASN::RawType.new(name => "name", type => "UTF8String", params => {})
# Field `message` of type `UTF8String`, defaults to `Hello World`
# ASN::RawType.new(name => "message", type => "UTF8String", params => {:default("\"Hello World\"")})
# Field `fuel` of type `ENUMERATED`,
# has options presented as hash by `defs`(definitions) key
# ASN::RawType.new(name => "fuel", type => "ENUMERATED", params => {:defs(${:gas(2), :liquid(1), :solid(0)})})
# Field `speed` of type `CHOICE`, has `optional` flag in params,
# has choices exposed as a mapping of a textual key into ASN::Tag class and integer value
# ASN::RawType.new(name => "speed", type => "CHOICE", params => {:choices(${:kmph((ASN::Tag.new(class => "CONTEXT-SPECIFIC", value => 1)) => "INTEGER"), :mph((ASN::Tag.new(class => "CONTEXT-SPECIFIC", value => 0)) => "INTEGER")}), :optional})
# Field `payload` of type `SEQUENCE OF` with `of` parameter that represents type of elements as ASN::RawType
# ASN::RawType.new(name => "payload", type => "SEQUENCE OF", params => {:of(ASN::RawType.new(name => "UTF8String", type => "UTF8String", params => {}))})
```
#### Room for improvement
* Value definition part of ASN.1
* Various spacing/newline variants
* Fixes