Accidental simplicity

觀看次數:19 次
跳至第一則未讀訊息

Mike Austin

未讀,
2017年5月14日 下午2:12:5614/5/2017
收件者︰ PEG.js: Parser Generator for JavaScript
I was planning to spend at least a few hours on translating JavaScript object notation to emit a Map instead. Turns out it took me about 5 minutes, thanks to the way the example JavaScript parser works!


 
var id = "name";

 
var data = {
    id
: "abc", 1: "one"
 
};

  console
.log(data);


This is is so simple to translate since the parser doesn't discriminate object key types. They can be strings, identifiers, numbers... not valid JavaScript, but who's to decide? The parser or the emitter? In my case it worked for my benefit.  The above outputs:


 
Map { 'name' => 'abc', 1 => 'one' }


Nice. And the emitter is only 10 lines of code -- I didn't even touch the parser. PEG.js is making it so easy to work on my project.

Mike

Mike Austin

未讀,
2017年5月14日 下午2:45:4814/5/2017
收件者︰ PEG.js: Parser Generator for JavaScript
Ok, looking at the parser, it's not quite accidental since { 1: "one" } is valid notation. But it's nice that it doesn't convert the key to a string in the parser. It leaves identifier and numbers in-tact.
回覆所有人
回覆作者
轉寄
0 則新訊息