From f1e9aea9d957504253ca7ca063de57c20217c4f9 Mon Sep 17 00:00:00 2001 From: Dieter Wimberger Date: Mon, 28 Dec 2015 14:27:56 +0100 Subject: [PATCH 1/3] Constructors for nested parsers Beginning to add the possibilities of nested parser constructors. In the original version, nested parsers ignore the create(constructorFn). --- lib/binary_parser.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/binary_parser.js b/lib/binary_parser.js index 978e9484..b747088f 100644 --- a/lib/binary_parser.js +++ b/lib/binary_parser.js @@ -539,7 +539,12 @@ Parser.prototype.generateChoice = function(ctx) { Parser.prototype.generateNest = function(ctx) { var nestVar = ctx.generateVariable(this.varName); - ctx.pushCode('{0} = {};', nestVar); + console.log('====> NEST %j', this.varName); + if(this.options.type.constructorFn) { + ctx.pushCode('{0} = new constructorFn("' + this.varName + '");', nestVar); + } else { + ctx.pushCode('{0} = {};', nestVar); + } ctx.pushPath(this.varName); this.options.type.generate(ctx); ctx.popPath(); From 4fe176ca66a9d4f142d5a7f3d18f8439867f0457 Mon Sep 17 00:00:00 2001 From: Dieter Wimberger Date: Mon, 28 Dec 2015 14:44:39 +0100 Subject: [PATCH 2/3] Differentiate main constructor Differentiate the constructor for the main parser from nested parsers without constructor. --- lib/binary_parser.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/binary_parser.js b/lib/binary_parser.js index b747088f..51bb0c1c 100644 --- a/lib/binary_parser.js +++ b/lib/binary_parser.js @@ -203,7 +203,7 @@ Parser.prototype.getCode = function() { var ctx = new Context(); if (this.constructorFn) { - ctx.pushCode('var vars = new constructorFn();'); + ctx.pushCode('var vars = new constructorFn("main");'); } else { ctx.pushCode('var vars = {};'); } @@ -539,7 +539,6 @@ Parser.prototype.generateChoice = function(ctx) { Parser.prototype.generateNest = function(ctx) { var nestVar = ctx.generateVariable(this.varName); - console.log('====> NEST %j', this.varName); if(this.options.type.constructorFn) { ctx.pushCode('{0} = new constructorFn("' + this.varName + '");', nestVar); } else { From b58295f55709f56005c2e31539cb4cabe690c52d Mon Sep 17 00:00:00 2001 From: Dieter Wimberger Date: Mon, 28 Dec 2015 14:50:10 +0100 Subject: [PATCH 3/3] Update constructor nested parsers Added docs for adding constructors to nested parsers. --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index c11e6c4a..e987b3ea 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,24 @@ Nest a parser in this position. Parse result of the nested parser is stored in t - `type` - (Required) A `Parser` object. +If a constructor is required for a nested parser, then the constructor function should be a selector +for the corresponding constructor (none, `main` or nested Parser variable name). + +```javascript +new Parser() + .create(function(param) { + if (!param) { + return {}; + } else if (param == 'main') { + //replace mainConstructorFn with main parser constructor function + return new mainConstructorFn(); + } else if (param === 'nested1') { + //replace nestedConstructorFn with nested parser constructor function + //nested1 is the varName of the nested parser + return new nestedConstructorFn();//call; + } + }) +``` ### skip(length) Skip parsing for `length` bytes.