keichi / binary-parser

A blazing-fast declarative parser builder for binary data
MIT License
864 stars 134 forks source link

User defined types? #1

Open itsthatguy opened 10 years ago

itsthatguy commented 10 years ago

User defined types does not seem very straight forward. After mucking around with it a bit, it appears that I need to fork the repo and rewrite a large portion of the code, include the "SPECIAL_TYPES" to add a VLQ type.

Is this true, or am I missing something?

keichi commented 10 years ago

Hi @itsthatguy , unfortunately you're right, there's currently no way to define user defined types. Sorry if my documentation confused you, but I meant combinations of pre-defined primitive and compound types with "user defined types".

I could enhance the code to support any custom parsing logic. Any ideas for the API? Maybe it goes like this:

var parser = new Parser().custom(function() {
    this.vars.a = foo(offset);
    this.offset += bar;
});
itsthatguy commented 10 years ago

I feel like in a real environment you're going to want to define a parser independently from the header.

Being able to assign new datatypes to the parser class would be ideal.

ie;

var Parser = require('binary-parser').Parser;

Parser.createDataType("myDataType", function(params) {
  // parsing logic here
});

var parser = new Parser().myDataType({"param1": 0, "param2": 1});
waywardmonkeys commented 9 years ago

To some extent, my desire for user-defined types is handled by nest and by the extension that I submitted in pull request #6 to add a way to set the constructor method to be used to create the object.

Thoughts?

keichi commented 9 years ago

@waywardmonkeys I think what we're discussing here is about adding support for binary data types that can't be easily described with the built-in primitive/compound data types. You're talking about support for custom data types of the parsed result -- which is also important and your PR is appreciated. However, custom binary data types and custom parsing logic is also needed .