public class CodeTokenizer extends Object implements Tokenizer
High-performance, table-driven lexical scanner implementing Tokenizer for programming languages.
CodeTokenizer uses zero-allocation character classification and $O(1)$ HashSet vocabulary lookups from LanguageProfile. Language-specific tokenizers (such as KlynTokenizer, JavaTokenizer, PythonTokenizer, CppTokenizer, and SqlTokenizer) inherit from or configure CodeTokenizer.
import klyn.code
profile = LanguageProfile("demo", "Demo")
profile.withLineComments(["//"])
profile.withStringDelimiters(["\""])
profile.withKeywords(["if", "return", "class", "def"])
profile.withTypes(["Int", "String"])
tokenizer = CodeTokenizer(profile)
res = tokenizer.tokenizeLine("def compute(x as Int): // fast")
assert res.spans[0].kind == TokenKind.KEYWORD| Modifier and Type | Member | Description |
|---|---|---|
| public | CodeTokenizerCodeTokenizer(languageProfile as LanguageProfile) |
Constructs a CodeTokenizer driven by the given LanguageProfile. |
| Modifier and Type | Member | Description |
|---|---|---|
| public static | appendLiteralSpans | Emits string content and escape spans (TokenKind.STRING and TokenKind.STRING_ESCAPE) for the literal segment line[start:end]. |
| public | classifyIdentifier | Classifies an identifier token according to the active LanguageProfile and preceding word context. |
| public | collectIdentifiers | Collects all unique identifier tokens of length >= minLength occurring in code positions (outside comments and string/character/regex literals) in source. |
| public | identifierAt | Extracts the complete identifier at or immediately before (targetLine, targetColumn). |
| public | identifierPrefixAt | Extracts the identifier prefix immediately preceding (targetLine, targetColumn). |
| public static | isAsciiAlpha | Fast ASCII alphabetic character test ('a'..'z' or 'A'..'Z'). |
| public static | isAsciiDigit | Fast ASCII decimal digit test ('0'..'9'). |
| public | isCodePositionisCodePosition(line as String, column as Long, initialState as LexicalState = null) as Boolean |
Checks whether column in line is a code position (outside comments and string/character/regex literals). |
| public | isCodePositionInSource | Checks whether (targetLine, targetColumn) in a multi-line source document is a code position (outside comments and string/character/regex literals). |
| public static | isEscapedAt | Checks whether the character at index in line is escaped by an odd number of preceding backslashes. |
| public static | isHexDigit | Fast ASCII hexadecimal digit test ('0'..'9', 'a'..'f', 'A'..'F'). |
| public | isIdentPart | Checks whether ch can continue an identifier in this language. |
| public | isIdentStart | Checks whether ch can start an identifier in this language. |
| public | isNumberStart | Checks whether a numeric literal starts at index in line. |
| public static | isOperatorChar | Checks whether ch is a standard operator character. |
| public | isPreprocessorLine | Checks whether the line starts with a preprocessor directive (such as #include or #define). |
| public static | isPunctuationChar | Checks whether ch is a punctuation/delimiter symbol ((, ), [, ], {, }, ,, ;, .). |
| public | isValidIdentifier | Checks whether text is a syntactically valid identifier according to this tokenizer's profile. |
| public static | isValidIdentifier | Checks whether text is a valid identifier using standard identifier rules plus extraChars. |
| public | lineCommentAt | Checks whether a single-line comment starts at index in line. |
| public | literalDelimiterAt | Returns the character/secondary literal delimiter starting at index in line, or "". |
| public | nextLineStatenextLineState(line as String, initialState as LexicalState = null) as LexicalState |
Fast state-only line transition that computes the resulting LexicalState without allocating TokenSpan objects. |
| public | profileprofile() as LanguageProfile |
Returns the LanguageProfile driving this tokenizer. |
| public | renameIdentifier | Renames every occurrence of code identifier oldName to newName in source while leaving comments, string/character/regex literals, and line endings untouched. |
| public static | scanEscapeEnd | Scans a backslash escape sequence starting at index (where line[index] == '\\') up to limit. |
| public | scanNumberEnd | Scans to the end of a numeric literal starting at start in line. |
| public static | startsWithAt | Zero-allocation substring prefix check at start inside text. |
| public static | startsWithIgnoreCaseAt | Zero-allocation ASCII case-insensitive prefix check at start inside text. |
| public | stringDelimiterAt | Returns the string delimiter starting at index in line, or "" if none matches. |
| public | stringPrefixAt | Checks whether a string prefix (f, r, fr, etc.) starts at index in line and is immediately followed by a string delimiter. |
| public | stripLineCommentsstripLineComments(line as String, initialState as LexicalState, out endState as LexicalState) as String |
Strips line and block comments from a single line while preserving string literals and updating endState for multi-line block comments or strings. |
| public | tokenize | Tokenizes a complete multi-line source document into materialized Token instances. |
| public | tokenizeLinetokenizeLine(line as String, initialState as LexicalState = null) as LineScanResult |
Tokenizes a single line of source code starting from initialState. |