Skip to content
New issue

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

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

Already on GitHub? # to your account

Add API for specifying the included ranges of a Parser #127

Merged
merged 23 commits into from
Feb 9, 2024
Merged

Conversation

dabico
Copy link
Member

@dabico dabico commented Feb 9, 2024

This PR introduces two new instance methods:

  • Parser#getIncludedRanges
  • Parser#setIncludedRanges(List)
  • Parser#setIncludedRanges(Range[])

The Parser#Builder was also modified to include new methods for specifying and clearing included ranges:

  • Parser#ranges()
  • Parser#ranges(List)
  • Parser#ranges(Range[])
  • Parser#range(Range)

dabico added 23 commits February 7, 2024 17:18
Its implementation is based on that of `__comparePoints`, and as such it
has been named `__compareRanges`
This includes:
- `POINT_ORIGIN`, which corresponds to (0,0)
- `POINT_MAX`, which corresponds to the point with maxed out `x` and `y`
- `RANGE_FULL`, which corresponds to `DEFAULT_RANGE` from `lexer.c`
Added bridge to the corresponding tree-sitter method
(ts_parser_included_ranges). However, unlike that
method, the Java implementation will return an empty
`Collection` if no ranges were included. Makes use of
all the previously introduced utilities.
The All-Args constructor should be publicly visible, while the one that
takes in a `Node` should be package-private. Not sure how this oversight
occurred in the first place.
Most of it was reorganised in order to be more easily read. In terms of
content, it remains largely the same. However, a minority of the text
was reworded for added clarity.
Comparing ranges did not make sense the way we were doing it initially.
It's far better to do this on the Java end, and just assume that ranges
are properly ordered when they are passed into the native code. The
functionality of `__compareRanges` was stripped down into a dedicated
`__pointEqual` and `__rangeEqual`, which are fare more practical and
make more sense. I'm not sure if these native implementations should
be used from the Java code, especially since its such simple code. We
do implement `Point#compareTo` natively, but even in that case I am
debating whether this is a good idea. At the end of the day, the
`RANGE_FULL` variable is now `RANGE_DEFAULT`, and the method to check
if a `TSRange` matches it is now aptly named `__isDefaultRange`.
There is no sense in constructing `Range` instances that have negative
values in terms of byte offsets or coordinates. By using a `Builder`,
we can more easily enforce preconditions that clients should respect.
Currently only tests `Range#Builder` functionalities
Added native implementation for a `Parser` mutation method that, as the
name implies, will set the list of ranges that are considered when
parsing documents. Note that while documented, the methods do not yet
check if the range precondition is satisfied or not. That will be
introduced in one of the subsequent commits.
This validator checks if all the elements of an array:
- Are not null;
- Respect the pairwise `Range` invariant specified in the JavaDoc
All the exceptional builder and setter test cases have been grouped into
two dedicated parametrized tests. Other than that, this commit also
introduces some additional assertions to existing tests:
- `ParserTest#testSetIncludedRanges`
- `ParserTest#testBuilder`
- `ParserTest#testToBuilder`
The method did not work as intended with points containing negative
coordinates. This is because on the JNI end, it was being unmarshalled
into a `TSPoint`, whose coordinates are all defined as _unsigned_ ints.
The `__comparePoints` will remain, as it seems to work fine with the
native methods that use it.
@dabico dabico merged commit 4f58d5d into master Feb 9, 2024
2 checks passed
@dabico dabico deleted the feature/ranges branch February 9, 2024 12:34
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant