Skip to content

Commit 0049118

Browse files
refactor(lookahead-iterator)!: implement Iterator
Refs: #21
1 parent 8f19cd7 commit 0049118

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/main/java/io/github/treesitter/jtreesitter/LookaheadIterator.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* For {@index MISSING} nodes, a lookahead iterator created on the previous non-extra leaf node may be appropriate.
2020
*/
2121
@NullMarked
22-
public final class LookaheadIterator implements AutoCloseable, Enumeration<LookaheadIterator.Symbol> {
22+
public final class LookaheadIterator implements AutoCloseable, Iterator<LookaheadIterator.Symbol> {
2323
private final Arena arena;
2424
private final MemorySegment self;
2525
private final short state;
@@ -80,8 +80,9 @@ public boolean reset(@Unsigned short state, Language language) {
8080
return ts_lookahead_iterator_reset(self, language.segment(), state);
8181
}
8282

83+
/** Check if the lookahead iterator has more symbols. */
8384
@Override
84-
public boolean hasMoreElements() {
85+
public boolean hasNext() {
8586
if (iterFirst) {
8687
iterFirst = false;
8788
hasNext = ts_lookahead_iterator_next(self);
@@ -96,8 +97,8 @@ public boolean hasMoreElements() {
9697
* @throws NoSuchElementException If there are no more symbols.
9798
*/
9899
@Override
99-
public Symbol nextElement() throws NoSuchElementException {
100-
if (!hasMoreElements()) throw new NoSuchElementException();
100+
public Symbol next() throws NoSuchElementException {
101+
if (!hasNext()) throw new NoSuchElementException();
101102
hasNext = ts_lookahead_iterator_next(self);
102103
return new Symbol(getCurrentSymbol(), getCurrentSymbolName());
103104
}
@@ -127,6 +128,12 @@ public void close() throws RuntimeException {
127128
arena.close();
128129
}
129130

131+
/** @hidden */
132+
@Override
133+
public void remove() {
134+
Iterator.super.remove();
135+
}
136+
130137
/** A class that pairs a symbol ID with its name. */
131138
public record Symbol(@Unsigned short id, String name) {}
132139

src/test/java/io/github/treesitter/jtreesitter/LookaheadIteratorTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,28 @@ void getCurrentSymbolName() {
4545
@Test
4646
@DisplayName("reset(state)")
4747
void resetState() {
48-
assertDoesNotThrow(() -> lookahead.nextElement());
48+
assertDoesNotThrow(() -> lookahead.next());
4949
assertTrue(lookahead.reset(state));
5050
assertEquals("ERROR", lookahead.getCurrentSymbolName());
5151
}
5252

5353
@Test
5454
@DisplayName("reset(language)")
5555
void resetLanguage() {
56-
assertDoesNotThrow(() -> lookahead.nextElement());
56+
assertDoesNotThrow(() -> lookahead.next());
5757
assertTrue(lookahead.reset(state, language));
5858
assertEquals("ERROR", lookahead.getCurrentSymbolName());
5959
}
6060

6161
@Test
62-
void hasMoreElements() {
63-
assertTrue(lookahead.hasMoreElements());
62+
void hasNext() {
63+
assertTrue(lookahead.hasNext());
6464
assertEquals("ERROR", lookahead.getCurrentSymbolName());
6565
}
6666

6767
@Test
68-
void nextElement() {
69-
assertEquals("end", lookahead.nextElement().name());
68+
void next() {
69+
assertEquals("end", lookahead.next().name());
7070
}
7171

7272
@Test

0 commit comments

Comments
 (0)