peggyjs / peggy

Peggy: Parser generator for JavaScript
https://peggyjs.org/
MIT License
914 stars 65 forks source link

test output format #414

Open yukulele opened 1 year ago

yukulele commented 1 year ago

Let's consider this command:

peggy math.peggy -t "1+2" > data.js

that generates this data.js file:

[ 'plus', 1, 2 ]

this output is a javascript statement but cant be used as a js file.

I suggest to add a --test-output-format option

examples:

This is the way I currently work around this problem:

echo 'export default ' > data.js 
peggy math.peggy -t '1+2' >> data.js
hildjj commented 1 year ago

No objection. I probably won't implement this myself, but would be happy to help someone through submitting a patch for it.

hildjj commented 1 year ago

Oh, I see that you closed this. I'm going to reopen it if that's ok, it's not a bad idea.

yukulele commented 1 year ago

Yes, I had closed this because I realized that the tests do not output javascript code but simply perform a console.log

Parsing it works with basic types (Plain object, array and primitive) but will not work with other types (Date, Set, Function, Symbol, ...)

So I'm not sure if this issue makes sense.

Eventually a JSON output could make sense but would be limited to stringifyable types.

hildjj commented 1 year ago

It depends on what you want to do with the output. If you want relatively high-fidelity, we could output CBOR for instance.

yukulele commented 6 months ago

a library like devalue could also be a solution.

hildjj commented 6 months ago

I could imagine something like devalue output as an option, but it's not really pretty enough for human consumption. If you want JS out, maybe something like serialize-javascript or node-tosource?