-
Notifications
You must be signed in to change notification settings - Fork 78
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
Rewrite lexer and parser #196
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the beginning, jaq used pest for its parser and lexer.
Later (3e26651), a new lexer/parser written using chumsky replaced the pest-based lexer/parser.
Over time, several shortcomings of the chumsky-based parser became apparent: First, at runtime, it was so slow that it became necessary to cache parse results, in particular of the standard library (
std.jq
, the equivalent ofbuiltin.jq
in jq), to achieve fast startup times. This meant additional dependencies onserde
andbincode
. Second, the compilation speed of the jaq parser was quite slow as well (making up for the largest part of the build process), despite my investing quite some time in trying to remedy this problem (mostly by sprinkling.boxed()
throughout the parser).This PR adds a new, hand-written lexer/parser for jaq.
Its build time is 1.55 seconds, compared to 47.59 seconds for the old chumsky-based parser (measured with
cargo build --release --no-default-features -p jaq-{parse,syn}
). This solves the long-standing issue #2.The runtime speed of the new parser is significantly higher than that of the old parser; consider the following benchmark:
This writes a file containing 1M instances of
def a: 0;
(amounting to 9.6MB), then executes jaq on that file.Using the new parser, jaq takes 1.6 seconds, whereas with the old parser, jaq takes 30.3 seconds.
(For the same file, gojq 0.12.15 takes 4 minutes and 49 seconds, and jq 1.7.1 fails immediately with "error: memory exhausted".)
The executable size of jaq also decreases a bit, going down from 4.8MB (old parser) to about 3.8MB (new parser).
The new parser can parse a few constructs that the old parser could not; for example, object keys can now be keywords (e.g.
{if: 1}.if
).Furthermore, the new parser adds support for several new syntactical constructs such as
label ... break
, generalised nested definitions (#157) and module syntax (module
,import
,include
). However, while the parser can identify these constructs, the jaq compiler currently either ignores certain constructs (module
) or panics (label
,break
,import
,include
, generalised nested definitions) when encountering them. Handling this situation correctly (without panicking) will need to break the current API, so these syntactical constructs will be compiled only as of jaq 2.0.If you are using jaq as an API, you can either continue using the old parser (in
jaq-parse
) or transition to the new parser (injaq-syn
). Seejaq/src/main.rs
for how to use the new parser.