public class SourceTokenCursor extends Object
Provides stream navigation and lexical refactoring helpers over tokenized source code.
SourceTokenCursor wraps a Tokenizer to iterate over code tokens while optionally skipping whitespace and comments, extract unique source identifiers for lexical code completion, or safely rename symbols without touching comments or string literals.
import klyn.code import klyn.code.klyn cursor = SourceTokenCursor.fromSource(KlynTokenizer(), "x = 1 # x in comment\ny = x + 2") renamed = SourceTokenCursor.renameIdentifier(KlynTokenizer(), "x = 1 # x\ny = x + 2", "x", "total") assert "total = 1 # x" in renamed
| Modifier and Type | Member | Description |
|---|---|---|
| public | SourceTokenCursorSourceTokenCursor(tokens as ArrayList<Token>, skipTrivia as Boolean = true) |
Constructs a cursor over a list of Tokens. |
| Modifier and Type | Member | Description |
|---|---|---|
| public static | extractIdentifiers | Extracts all unique identifiers appearing in executable code (excluding comments and literals). |
| public static | fromSourcefromSource(tokenizer as Tokenizer, source as String, skipTrivia as Boolean = true) as SourceTokenCursor |
Tokenizes source with tokenizer and returns a ready-to-use SourceTokenCursor. |
| public | hasNexthasNext() as Boolean |
Returns true when at least one more token is available. |
| public | nextnext() as Token |
Returns the current token and advances the cursor to the next token. |
| public | peekpeek() as Token |
Returns the current token without advancing the cursor, or null at end of stream. |
| public static | renameIdentifier | Safely renames all occurrences of an identifier in executable code without modifying comments, documentation blocks, or string/character/regex literals. |
| public | resetreset() as Void |
Resets the cursor back to the beginning of the token stream. |