Search code examples
javascriptpegpegjs

Return key, value object with dynamic key name


In PEG.js I have the following rule

label  = l:[a-zA-Z\$\#\% ]*  { return word(l); } 
block  = "[" l:label "]" { return l; }
option = "\n"* key:block value:label "\n"? {return {key : value}; } 

If it parses [hello] world it results in:

{"key": "world"}.

I would like it to return

{"hello": "world"}.

Is this possible? How can I make sure the object key accepts a dynamic value.


Solution

  • Change

    … { return {key : value}; }
    

    to

    … { var o={}; o[key]=value; return o; }