Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 6 additions & 2 deletions lib/binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};');
}
Expand Down Expand Up @@ -539,7 +539,11 @@ Parser.prototype.generateChoice = function(ctx) {

Parser.prototype.generateNest = function(ctx) {
var nestVar = ctx.generateVariable(this.varName);
ctx.pushCode('{0} = {};', nestVar);
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();
Expand Down