Giter Club home page Giter Club logo

Comments (5)

keichi avatar keichi commented on June 3, 2024

Sorry for the unclarity, you need to call useContextVars() on the top-level parser. If you change

const parser = new Parser().array('data', {

to

const parser = new Parser().useContextVars().array('data', {

This works:

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

const buf = Buffer.from('a1012aa2027ab8a30460713d44ff03414243a401ff', 'hex');

const parser = new Parser().useContextVars().array('data', {
  readUntil: 'eof',
  type: new Parser()
    .uint8('type')
    .uint8('length')
    .choice('value', {
      tag: 'type',
      choices: {
        0xa1: new Parser().uint8('_a1'),
        0xa2: new Parser().uint16be('_a2'),
        0xa3: new Parser().buffer('_a3', { length: '$parent.length' }),
        0xa4: new Parser().uint8('_a4'),
      },
      defaultChoice: new Parser().buffer('other', { length: '$parent.length' }),
    }),
});

console.dir(parser.parse(buf), { depth: null });

Output:

{
  data: [
    { type: 161, length: 1, value: { _a1: 42 } },
    { type: 162, length: 2, value: { _a2: 31416 } },
    {
      type: 163,
      length: 4,
      value: { _a3: Buffer(4) [Uint8Array] [ 96, 113, 61, 68 ] }
    },
    {
      type: 255,
      length: 3,
      value: { other: Buffer(3) [Uint8Array] [ 65, 66, 67 ] }
    },
    { type: 164, length: 1, value: { _a4: 255 } }
  ]
}

from binary-parser.

wpyoga avatar wpyoga commented on June 3, 2024

I see... your solution works. I'm curious though, does this mean useContextVars() have to declared at the very top level parser, and at that level only?

from binary-parser.

keichi avatar keichi commented on June 3, 2024

Yes, you have to declare it at the top level. If you call it at the lower levels it's simply ignored. This is required because the top-level parser has to know if the context vars is enabled as it sets$root.

from binary-parser.

keichi avatar keichi commented on June 3, 2024

Closing since your initial problem seems resolved. Feel free to reopen if needed.

from binary-parser.

wpyoga avatar wpyoga commented on June 3, 2024

I see, thank you for the explanation.

from binary-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.