-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from seart-group/feature/lookahead
- Loading branch information
Showing
9 changed files
with
220 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "ch_usi_si_seart_treesitter.h" | ||
#include "ch_usi_si_seart_treesitter_LookaheadIterator.h" | ||
#include <jni.h> | ||
#include <tree_sitter/api.h> | ||
|
||
JNIEXPORT void JNICALL Java_ch_usi_si_seart_treesitter_LookaheadIterator_delete( | ||
JNIEnv* env, jobject thisObject) { | ||
TSLookaheadIterator* iterator = (TSLookaheadIterator*)__getPointer(env, thisObject); | ||
ts_lookahead_iterator_delete(iterator); | ||
__clearPointer(env, thisObject); | ||
} | ||
|
||
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_LookaheadIterator_next( | ||
JNIEnv* env, jobject thisObject) { | ||
bool hasNext = (bool)env->GetBooleanField(thisObject, _lookaheadIteratorHasNextField); | ||
if (!hasNext) { | ||
__throwNSE(env, NULL); | ||
return NULL; | ||
} | ||
TSLookaheadIterator* iterator = (TSLookaheadIterator*)__getPointer(env, thisObject); | ||
TSSymbol symbol = ts_lookahead_iterator_current_symbol(iterator); | ||
const TSLanguage* language = ts_lookahead_iterator_language(iterator); | ||
const char* name = ts_language_symbol_name(language, symbol); | ||
TSSymbolType type = ts_language_symbol_type(language, symbol); | ||
env->SetBooleanField( | ||
thisObject, | ||
_lookaheadIteratorHasNextField, | ||
ts_lookahead_iterator_next(iterator) ? JNI_TRUE : JNI_FALSE | ||
); | ||
return env->NewObject( | ||
_symbolClass, | ||
_symbolConstructor, | ||
(jint)symbol, | ||
(jint)type, | ||
env->NewStringUTF(name) | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
40 changes: 40 additions & 0 deletions
40
src/main/java/ch/usi/si/seart/treesitter/LookaheadIterator.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ch.usi.si.seart.treesitter; | ||
|
||
import lombok.Getter; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
import java.util.Iterator; | ||
|
||
/** | ||
* Specialized iterator that can be used to generate suggestions and improve syntax error diagnostics. | ||
* To get symbols valid in an {@code ERROR} node, use the lookahead iterator on its first leaf node state. | ||
* For {@code MISSING} nodes, a lookahead iterator created on the previous non-extra leaf node may be appropriate. | ||
* | ||
* @since 1.12.0 | ||
* @author Ozren Dabić | ||
*/ | ||
@FieldDefaults(level = lombok.AccessLevel.PRIVATE, makeFinal = true) | ||
public class LookaheadIterator extends External implements Iterator<Symbol> { | ||
|
||
boolean hasNext; | ||
|
||
@Getter | ||
Language language; | ||
|
||
LookaheadIterator(long pointer, boolean hasNext, Language language) { | ||
super(pointer); | ||
this.hasNext = hasNext; | ||
this.language = language; | ||
} | ||
|
||
@Override | ||
protected native void delete(); | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return hasNext; | ||
} | ||
|
||
@Override | ||
public native Symbol next(); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/ch/usi/si/seart/treesitter/LookaheadIteratorTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ch.usi.si.seart.treesitter; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
class LookaheadIteratorTest extends TestBase { | ||
|
||
private static final Language language = Language.JAVA; | ||
|
||
@Test | ||
void testIterator() { | ||
try (LookaheadIterator iterator = language.iterator(0)) { | ||
Spliterator<Symbol> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED); | ||
List<Symbol> symbols = StreamSupport.stream(spliterator, false).collect(Collectors.toUnmodifiableList()); | ||
Assertions.assertFalse(symbols.isEmpty()); | ||
Assertions.assertTrue(language.getSymbols().containsAll(symbols)); | ||
Assertions.assertThrows(NoSuchElementException.class, iterator::next); | ||
} catch (NoSuchElementException ignored) { | ||
Assertions.fail(); | ||
} | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("resource") | ||
void testIteratorThrows() { | ||
int states = language.getTotalStates(); | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> language.iterator(Integer.MIN_VALUE)); | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> language.iterator(-1)); | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> language.iterator(states)); | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> language.iterator(Integer.MAX_VALUE)); | ||
} | ||
} |