metadevpro/ts-pegjs

Invalid code generation for "pluck"

LeviticusMB opened this issue · 5 comments

The list example from Peggy does not work as intended:

list = head:word tail:(_ "," _ @word)* { return [head, ...tail]; }
word = $[a-z]i+
_ = [ \t]*

If invoked via tspegjs, there is no code difference in the TypeScript output if I change @word to just word in the first rule. On the other hand, if I invoke peggy (1.2.0) directly, the generated code (JavaScript) differs as expected:

727c728,729
<             s3 = s7;
---
>             s4 = [s4, s5, s6, s7];
>             s3 = s4;

OK. I see. We will need to add support for the "pluck" operator. It is not supported currently.
Thanks for reporting it. @LeviticusMB

Would you take a PR on this? Just ran into it here.

Sure, @hildjj if you have a clear path to fix, please feel free!

How much do you want to bring your generate-bytecode-ts.js up to synchronization with peggy? What are the key differences? In particular, there are more invasive changes coming that you can use for sourcemaping, if you want.

I propose to start with a small patch that just adds support for opcode 36:

// [36] PLUCK n, k, p1, ..., pK
//
//        value = [stack[p1], ..., stack[pK]]; // when k != 1
//        -or-
//        value = stack[p1];                   // when k == 1
//
//        stack.pop(n);
//        stack.push(value);

and the equivalent generate-ts bits.

Published in v. 2.1.0. Thanks to @hildjj for taking the time for doing the implementation.