Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move all JS codegeneration to the generate-js pass #117

Merged
merged 9 commits into from
May 20, 2021
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ Released: TBD
messages have changed; use the `toString()` method on `GrammarError` to get
something close to the old text.
[@hildjj](https://github.com/peggyjs/peggy/pull/116)
- The code generator was slightly reworked to simplify reusing the bytecode generator
(`generate.generateBytecode` pass). Property `consts` on the `grammar` AST node,
has been creating by the pass in the past, was decoupled into 4 other properties
with the structured information:
- `literals`
- `classes`
- `expectations`
- `functions`

Now bytecode generation pass is independent from the JavaScript backend.
[@Mingun](https://github.com/peggyjs/peggy/pull/117)
- Some opcodes from `compiler/opcode.js` is deprecated. Although you shouldn't use
them directly because they are notconsidered as a public API, some plugins use them.
For that reason backward compatibility is preserved:
- Opcode `MATCH_REGEXP` is deprecated and replaced by `MATCH_CHAR_CLASS` with the same value.
- Added new opcode `PUSH_EMPTY_STRING` that put on stack new empty string
- Opcode `PUSH` is deprecated because it was used only for pushing empty string constants
and they now pushed with `PUSH_EMPTY_STRING`

Instead of relying on the library opcodes it is better to have a copy of them,
especially if your plugin replaces both `generateBytecode` as `generateJs` passes.

[@Mingun](https://github.com/peggyjs/peggy/pull/117)

### Bug fixes

Expand Down
54 changes: 0 additions & 54 deletions lib/compiler/js.js

This file was deleted.

69 changes: 37 additions & 32 deletions lib/compiler/opcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,64 @@
const opcodes = {
// Stack Manipulation

PUSH: 0, // PUSH c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this (as an example of several opcode changes) conflict with the typescript plugin? Is @metadevpro following the work here yet? How could they support both peggy and pegjs if they wanted?

Copy link
Member Author

@Mingun Mingun May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will. Instead of renaming that opcode we can just leave it unused and introduce a new opcode for PUSH_EMPTY_STRING. Renaming MATCH_REGEXP also should be reverted. Also we could add an alias instead of renaming or just do nothing. Then in version 2.0 we could completely remove the unused opcode.

On the other hand, @metadevpro could create a local copy of opcodes, he anyway is not needed to use exactly the same opcodes as peggy used because they used only as a glue between generate-bytecode-ts and generate-ts passes.

Copy link
Contributor

@hildjj hildjj May 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, my suggestion from your choices above:

  • Leave PUSH as 0. Mark it as deprecated with a comment.
  • Create a new opcode for PUSH_EMPTY_STRING
  • Leave MATCH_CHAR_CLASS and MATCH_REGEXP both as opcode 20, but mark MATCH_REGEXP as deprecated with a comment.

I think this will allow plugins to support both peggy and pegjs at the same time adequately. If you expect that plugins would need to take different actions for MATCH_CHAR_CLASS and MATCH_REGEXP, then don't alias them together, and instead leave MATCH_REGEXP as opcode 20, create a new opcode for MATCH_CHAR_CLASS.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All done as you said. I've make a rebase , changed only the last and the pre-pre-last commits. MATCH_REGEXP and PUSH marked @deprecated, noted this in the CHANGELOG.md

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

PUSH_UNDEFINED: 1, // PUSH_UNDEFINED
PUSH_NULL: 2, // PUSH_NULL
PUSH_FAILED: 3, // PUSH_FAILED
PUSH_EMPTY_ARRAY: 4, // PUSH_EMPTY_ARRAY
PUSH_CURR_POS: 5, // PUSH_CURR_POS
POP: 6, // POP
POP_CURR_POS: 7, // POP_CURR_POS
POP_N: 8, // POP_N n
NIP: 9, // NIP
APPEND: 10, // APPEND
WRAP: 11, // WRAP n
TEXT: 12, // TEXT
PLUCK: 36, // PLUCK n, k, p1, ..., pK
/** @deprecated Unused */
PUSH: 0, // PUSH c
PUSH_EMPTY_STRING: 35, // PUSH_EMPTY_STRING
PUSH_UNDEFINED: 1, // PUSH_UNDEFINED
PUSH_NULL: 2, // PUSH_NULL
PUSH_FAILED: 3, // PUSH_FAILED
PUSH_EMPTY_ARRAY: 4, // PUSH_EMPTY_ARRAY
PUSH_CURR_POS: 5, // PUSH_CURR_POS
POP: 6, // POP
POP_CURR_POS: 7, // POP_CURR_POS
POP_N: 8, // POP_N n
NIP: 9, // NIP
APPEND: 10, // APPEND
WRAP: 11, // WRAP n
TEXT: 12, // TEXT
PLUCK: 36, // PLUCK n, k, p1, ..., pK

// Conditions and Loops

IF: 13, // IF t, f
IF_ERROR: 14, // IF_ERROR t, f
IF_NOT_ERROR: 15, // IF_NOT_ERROR t, f
WHILE_NOT_ERROR: 16, // WHILE_NOT_ERROR b
IF: 13, // IF t, f
IF_ERROR: 14, // IF_ERROR t, f
IF_NOT_ERROR: 15, // IF_NOT_ERROR t, f
WHILE_NOT_ERROR: 16, // WHILE_NOT_ERROR b

// Matching

MATCH_ANY: 17, // MATCH_ANY a, f, ...
MATCH_STRING: 18, // MATCH_STRING s, a, f, ...
MATCH_STRING_IC: 19, // MATCH_STRING_IC s, a, f, ...
MATCH_REGEXP: 20, // MATCH_REGEXP r, a, f, ...
ACCEPT_N: 21, // ACCEPT_N n
ACCEPT_STRING: 22, // ACCEPT_STRING s
FAIL: 23, // FAIL e
MATCH_ANY: 17, // MATCH_ANY a, f, ...
MATCH_STRING: 18, // MATCH_STRING s, a, f, ...
MATCH_STRING_IC: 19, // MATCH_STRING_IC s, a, f, ...
MATCH_CHAR_CLASS: 20, // MATCH_CHAR_CLASS c, a, f, ...
/** @deprecated Replaced with `MATCH_CHAR_CLASS` */
MATCH_REGEXP: 20, // MATCH_REGEXP r, a, f, ...
ACCEPT_N: 21, // ACCEPT_N n
ACCEPT_STRING: 22, // ACCEPT_STRING s
FAIL: 23, // FAIL e

// Calls

LOAD_SAVED_POS: 24, // LOAD_SAVED_POS p
UPDATE_SAVED_POS: 25, // UPDATE_SAVED_POS
CALL: 26, // CALL f, n, pc, p1, p2, ..., pN
LOAD_SAVED_POS: 24, // LOAD_SAVED_POS p
UPDATE_SAVED_POS: 25, // UPDATE_SAVED_POS
CALL: 26, // CALL f, n, pc, p1, p2, ..., pN

// Rules

RULE: 27, // RULE r
RULE: 27, // RULE r

// Failure Reporting

SILENT_FAILS_ON: 28, // SILENT_FAILS_ON
SILENT_FAILS_OFF: 29 // SILENT_FAILS_OFF
SILENT_FAILS_ON: 28, // SILENT_FAILS_ON
SILENT_FAILS_OFF: 29 // SILENT_FAILS_OFF

// Because the tests have hard-coded opcode numbers, don't renumber
// existing opcodes. New opcodes that have been put in the correct
// sections above are repeated here in order to ensure we don't
// reuse them.
//
// 30-35 reserved for @mingun
// 30-34 reserved for @mingun
// PUSH_EMPTY_STRING: 35
// PLUCK: 36
};

Expand Down
Loading