Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse errors make parsed tree useless #32

Closed
vinnichase opened this issue Feb 2, 2020 · 2 comments
Closed

Parse errors make parsed tree useless #32

vinnichase opened this issue Feb 2, 2020 · 2 comments
Assignees

Comments

@vinnichase
Copy link

vinnichase commented Feb 2, 2020

I have the following code snippet that can be run in node:

const { parseTree } = require('jsonc-parser');

const correctObj = '{"prop1":"foo","prop2":"foo2","prop3":{"prp1":{}}}';
console.log(parseTree(correctObj));

That produces the following correct output:

<ref *1> {
  type: 'object',
  offset: 0,
  length: 50,
  children: [
    {
      type: 'property',
      offset: 1,
      length: 13,
      parent: [Circular *1],
      children: [Array],
      colonOffset: 8
    },
    {
      type: 'property',
      offset: 15,
      length: 14,
      parent: [Circular *1],
      children: [Array],
      colonOffset: 22
    },
    {
      type: 'property',
      offset: 30,
      length: 19,
      parent: [Circular *1],
      children: [Array],
      colonOffset: 37
    }
  ]
}

However if I run it on a slightly malformed object like so (two double quotes in prp1):

const malformedObj = '{"prop1":"foo","prop2":"foo2","prop3":{"prp1":{""}}}';
console.log(parseTree(malformedObj));

I get a tree with two nodes which represent key and value of prop1:

<ref *1> {
  type: 'property',
  offset: 1,
  length: 13,
  children: [
    {
      type: 'string',
      value: 'prop1',
      offset: 1,
      length: 7,
      parent: [Circular *1]
    },
    {
      type: 'string',
      offset: 9,
      length: 5,
      parent: [Circular *1],
      value: 'foo'
    }
  ],
  colonOffset: 8
}

That makes it impossible to figure out which node is for example at offset 48.

My use case: I'm writing a VSCode extension for JSON completion. When the user starts typing, the object will always be malformed so that I get no information on the node which he is typing in.

Have you similar use cases and maybe solved it differently or is there any way to increase fault tolerance of the parser? My current workaround would be to remove the current word range from the text and parse the tree based on that. I'm by far not sure if this works in every possible situation.

@vinnichase
Copy link
Author

Any comments, why this is closed now?

@aeschli
Copy link
Contributor

aeschli commented Feb 4, 2020

It's fixed by 53456f5. I will publish a new version in the coming days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants