Search code examples
javascriptjsonparser-generator

How to write a parser in javascript to parse text in editor into JSON with a model?


I don't know if it is the place to ask this. But I will try.

What I need is, for example, if user writes some text in Code Mirror editor like:

a1=Some Text
    a11 = Some Child text

and the parser should check if the grammer is correct, and parse it to:

 {
    'a1' : {
             'value' : "Some Text",
             'a11' : "some child text"
    }
 }

Something like this according to the gramme.

So, is there any parser library or something in javascript?


Solution

  • You should be able to use PEG.js to parse the text on the client side. I would also recommend to start off from writing a proper grammar for whatever valid input you are expecting from the user.