Skip to content

feat: add options to parsing and query execution#3843

Merged
amaanq merged 4 commits intotree-sitter:masterfrom
amaanq:opts
Nov 1, 2024
Merged

feat: add options to parsing and query execution#3843
amaanq merged 4 commits intotree-sitter:masterfrom
amaanq:opts

Conversation

@amaanq
Copy link
Member

@amaanq amaanq commented Oct 29, 2024

Closes #2541
Would like #3833 to extend upon this

Problem

Currently, users have a hard time having manual control over when parsing or query execution is canceled. The API does offer a few ways to control this, typically by setting a timeout in microseconds or by setting a cancellation flag. However, this approach is very rigid, doesn't give the users flexibility to easily change this on the fly at runtime, and ties us to OS-specific time headers.

Solution

In the core library, two new functions, ts_parser_parse_with_options & ts_query_cursor_exec_with_options, were added. These functions take in the same parameters as their -_with_options variants, plus an additional parse_options parameter of type TSParseOptions for the parse function, and a query_options parameter of type TSQueryCursorOptions *.

Currently, these options consist of a payload field, and an interrupt_callback field. If the interrupt callback is not null, it is invoked to check whether or not to halt parsing or query execution. The callback is to be set by the user, and takes in one parameter, state, of type TSParseState/TSQueryCursorState, which currently only has a payload field (which makes working with FFI code easier). This type can easily be extended upon, in case we want to add additional data later on.

The plan is to deprecate the timeout_micros and cancellation_flag functions for the next version, and then eventually get rid of these functions + the platform-specific time code (clock.h) in the following version.

@amaanq
Copy link
Member Author

amaanq commented Oct 30, 2024

Ok this is now ready. I'm really happy with how the ergonomics for the Rust bindings turned out, and other bindings will have similar mechanisms involved for passing in the interrupt callback. This will also easily extend to passing in the decode callback too in #3833

@amaanq amaanq force-pushed the opts branch 2 times, most recently from 671905e to caa6a14 Compare October 30, 2024 05:33
Copy link
Contributor

@maxbrunsfeld maxbrunsfeld left a comment

Choose a reason for hiding this comment

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

This is definitely a good change. I left a few questions.

@amaanq amaanq force-pushed the opts branch 3 times, most recently from 9683a0b to 4a8bf98 Compare November 1, 2024 01:14
Currently, this allows users to pass in a callback that should be
invoked to check whether or not to halt parsing
Currently, this allows users to pass in a callback that should be
invoked to check whether or not to halt query execution
…ecate old functions

The normal `with` functions are now deprecated in favor of the
`with_options` ones.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ts_parser_parse_interruptible C API call

3 participants