Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/main/java/io/github/treesitter/jtreesitter/Logger.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package io.github.treesitter.jtreesitter;

import java.util.function.BiConsumer;
import org.jspecify.annotations.NonNull;

/** A function that logs parsing results. */
@FunctionalInterface
public interface Logger extends BiConsumer<Logger.Type, String> {
public interface Logger {
/**
* {@inheritDoc}
* Performs this operation on the given arguments.
*
* @param type the log type
* @param message the log message
*/
@Override
void accept(@NonNull Type type, @NonNull String message);
void log(@NonNull Type type, @NonNull String message);

/**
* Performs this operation on the given arguments.
*
* @param type the log type
* @param message the log message
* @deprecated Use {@link #log(Logger.Type, String)} instead
*/
@Deprecated(since = "0.26.0", forRemoval = true)
default void accept(@NonNull Type type, @NonNull String message) {
log(type, message);
}

/** The type of a log message. */
enum Type {
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/io/github/treesitter/jtreesitter/ParseCallback.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package io.github.treesitter.jtreesitter;

import java.util.function.BiFunction;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

/** A function that retrieves a chunk of text at a given byte offset and point. */
@FunctionalInterface
public interface ParseCallback extends BiFunction<Integer, Point, String> {
public interface ParseCallback {
/**
* {@inheritDoc}
* Applies this function to the given arguments.
*
* @param offset the current byte offset
* @param point the current point
* @return A chunk of text or {@code null} to indicate the end of the document.
*/
@Override
@Nullable
String apply(@Unsigned Integer offset, @NonNull Point point);
String read(@Unsigned int offset, @NonNull Point point);

/**
* Applies this function to the given arguments.
*
* @param offset the current byte offset
* @param point the current point
* @return A chunk of text or {@code null} to indicate the end of the document.
* @deprecated Use {@link #read(int, Point)} instead
*/
@Nullable
@Deprecated(since = "0.26.0", forRemoval = true)
default String apply(@Unsigned Integer offset, @NonNull Point point) {
return read(offset, point);
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/github/treesitter/jtreesitter/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Parser setLogger(@Nullable Logger logger) {
var log = TSLogger.log.allocate(
(p, type, message) -> {
var logType = Logger.Type.values()[type];
logger.accept(logType, message.getString(0));
logger.log(logType, message.getString(0));
},
arena);
TSLogger.log(segment, log);
Expand Down Expand Up @@ -308,7 +308,7 @@ public Optional<Tree> parse(
// NOTE: can't use _ because of palantir/palantir-java-format#934
var read = TSInput.read.allocate(
(payload, index, point, bytes) -> {
var result = parseCallback.apply(index, Point.from(point));
var result = parseCallback.read(index, Point.from(point));
if (result == null) {
bytes.set(C_INT, 0, 0);
return MemorySegment.NULL;
Expand Down
Loading