|
| 1 | +/* |
| 2 | + * This file is part of android-tree-sitter. |
| 3 | + * |
| 4 | + * android-tree-sitter library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * android-tree-sitter library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with android-tree-sitter. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.itsaky.androidide.treesitter; |
| 19 | + |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +/** |
| 23 | + * Lookahead iterators can be useful to generate suggestions and improve syntax error diagnostics. |
| 24 | + * To get symbols valid in an ERROR node, use the lookahead iterator on its first leaf node state. |
| 25 | + * For `MISSING` nodes, a lookahead iterator created on the previous non-extra leaf node may be |
| 26 | + * appropriate. |
| 27 | + * <p> |
| 28 | + * Repeatedly using {@link #next()} and {@link #getCurrentSymbol()} will generate valid symbols in |
| 29 | + * the given parse state. Newly created lookahead iterators will contain the `ERROR` symbol. |
| 30 | + * |
| 31 | + * @author Akash Yadav |
| 32 | + */ |
| 33 | +public class TSLookaheadIterator extends TSNativeObject { |
| 34 | + |
| 35 | + private TSLanguage language; |
| 36 | + |
| 37 | + /** |
| 38 | + * Create a new lookahead iterator for the given language and parse state. |
| 39 | + * <p> |
| 40 | + * This returns <code>null</code> if state is invalid for the language. |
| 41 | + * |
| 42 | + * @param language The {@link TSLanguage} for this lookahead iterator. |
| 43 | + * @param stateId The parse state. |
| 44 | + * @return The {@link TSLookaheadIterator} or <code>null</code> if there was an error creating the |
| 45 | + * iterator. |
| 46 | + */ |
| 47 | + public static TSLookaheadIterator newInstance(TSLanguage language, short stateId) { |
| 48 | + language.checkAccess(); |
| 49 | + final var pointer = Native.newIterator(language.pointer, stateId); |
| 50 | + if (pointer == 0) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + return new TSLookaheadIterator(language, pointer); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a new {@link TSLookaheadIterator} instance with the given pointer. |
| 59 | + * |
| 60 | + * @param pointer The pointer to the native object. Subclasses can initialize this |
| 61 | + * {@link TSLookaheadIterator} with pointer set to 0 and then set the pointer |
| 62 | + * lazily. |
| 63 | + */ |
| 64 | + private TSLookaheadIterator(TSLanguage language, long pointer) { |
| 65 | + super(pointer); |
| 66 | + Objects.requireNonNull(language); |
| 67 | + this.language = language; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Advance the lookahead iterator to the next symbol. |
| 72 | + * <p> |
| 73 | + * This returns `true` if there is a new symbol and `false` otherwise. |
| 74 | + */ |
| 75 | + public boolean next() { |
| 76 | + checkAccess(); |
| 77 | + return Native.next(pointer); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get the current symbol of the lookahead iterator; |
| 82 | + */ |
| 83 | + public short getCurrentSymbol() { |
| 84 | + checkAccess(); |
| 85 | + return Native.currentSymbol(pointer); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Get the current symbol's name of the lookahead iterator; |
| 90 | + */ |
| 91 | + public String getCurrentSymbolName() { |
| 92 | + checkAccess(); |
| 93 | + return Native.currentSymbolName(pointer); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Reset the lookahead iterator to another state. |
| 98 | + * <p> |
| 99 | + * This returns `true` if the iterator was reset to the given state and `false` otherwise. |
| 100 | + */ |
| 101 | + public boolean resetState(short stateId) { |
| 102 | + checkAccess(); |
| 103 | + return Native.resetState(pointer, stateId); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Reset the lookahead iterator. |
| 108 | + * <p> |
| 109 | + * This returns `true` if the language was set successfully and `false` otherwise. |
| 110 | + */ |
| 111 | + public boolean reset(TSLanguage language, short stateId) { |
| 112 | + checkAccess(); |
| 113 | + language.checkAccess(); |
| 114 | + final var result = Native.reset(pointer, language.pointer, stateId); |
| 115 | + if (result) { |
| 116 | + this.language = language; |
| 117 | + } |
| 118 | + |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Get the current language of the lookahead iterator. |
| 124 | + */ |
| 125 | + public TSLanguage getLanguage() { |
| 126 | + checkAccess(); |
| 127 | + return language; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected void closeNativeObj() { |
| 132 | + Native.delete(pointer); |
| 133 | + } |
| 134 | + |
| 135 | + private static final class Native { |
| 136 | + |
| 137 | + public static native long newIterator(long language, short stateId); |
| 138 | + |
| 139 | + public static native void delete(long pointer); |
| 140 | + |
| 141 | + public static native boolean next(long pointer); |
| 142 | + |
| 143 | + public static native short currentSymbol(long pointer); |
| 144 | + |
| 145 | + public static native String currentSymbolName(long pointer); |
| 146 | + |
| 147 | + public static native boolean resetState(long pointer, short stateId); |
| 148 | + |
| 149 | + public static native boolean reset(long pointer, long pointer1, short stateId); |
| 150 | + } |
| 151 | +} |
0 commit comments