
Library enables tokenization and parsing of expressions with a tokenizer, lexer, and scanner. Supports JSON, CSV, and custom languages, offering configurable expression parsing and dynamic lookahead.
Parser library for Kotlin consisting of a tokenizer and expression parser.
Tokenization is the process of splitting the input into a stream of tokens that is consumed by a parser.
In Parsek, most parsers only need to interact with Scanner (and optionally RegularExpressions); the underlying Lexer is exposed as a raw Iterator<Token<T>> for cases that need custom token-stream processing.
The Scanner class (source, kdoc) provides a convenient API over a token stream:
Regex to tokenType pairs (mapping skipped tokens such as whitespace to null), or over any Iterator<Token<T>>.Scanner (e.g. class Pl0Scanner(input: String) : Scanner<TokenType>(input, TokenType.EOF, ...)) is recommended so parser methods can accept Pl0Scanner without repeating <TokenType>, and custom helper methods can be added if needed. Direct Scanner(...) instantiation is also fine for simple cases.Regex instances in constants (private val or RegularExpressions) rather than constructing them inside the Scanner constructor on each parse call.eofType (e.g. TokenType.EOF) keeps Token.type non-nullable. To avoid adding an EOF entry to an enum, pass null with a nullable type parameter (Scanner<TokenType?>(input, null, ...)), or use Scanner<Unit?>(input, null, ...) (mapping emitted tokens to Unit) when you don't need token types at all.current token that can be inspected multiple times (unlike Iterator.next()), unlimited dynamic lookAhead(n), and consume / tryConsume / consumeIgnoreCase / tryConsumeIgnoreCase helpers.Typical use cases that only need a scanner and no expression parser are data formats such as JSON or CSV.
For a simple example, please refer to the JSON parser example.
The RegularExpressions object (source, kdoc) contains precompiled regular expressions for common source code and data format tokens.
The token class (source, kdoc) stores the token type (typically a user-defined enum), the token text and the token position.
The lexer (source,
kdoc) implements Iterator<Token<T>>, splitting an input string into tokens using Regex to tokenType pairs (multiple regular expressions may map to the same token type, and rules mapped to null are skipped).
You only need to instantiate or subclass Lexer directly when installing additional processing between raw tokenization and Scanner — for instance via lexer.asSequence().filter { ... }.iterator() for context-sensitive newline filtering in indentation-based languages.
The Pratt expression parser (source, kdoc) operates on a scanner, is stateless and should be shared / reused.
A simple example evaluating mathematical expressions directly (opposed to building an explicit parse tree) can be found in the tests
A complete PL/0 parser is included in the examples module to illustrate how to use the expression parser and scanner for a simple but computationally complete language: Pl0Scanner.kt, Pl0Parser.kt, Pl0Test.kt
A parser for mathematical expressions: ExpressionLexer.kt, ExpressionScanner.kt, ExpressionParser.kt, ExpressionsTest.kt
A simple example for using the lexer, Sequence.filter, scanner, and expression parser to implement an indentation-based
programming language: mython,
MythonTest.kt
A BASIC interpreter using Parsek: https://github.com/stefanhaustein/basik
Lexer (Iterator<Token<T>>) rules to Pair<Regex, T?> directly, removing the mapping lambda and textNormalization.Scanner EOF token text to "<EOF>", added consumeIgnoreCase(...) / tryConsumeIgnoreCase(...), and deprecated Filter, 3-argument Scanner(input, eofType, eofText), and ignoreCase boolean overloads.ConfigurableExpressionParser (org.kobjects.parsek.expressionparser).RegularExpressions.NUMBER to UNSIGNED_NUMBER (with NUMBER deprecated for one release) and fixed RegularExpressions.SYMBOL escaping for JS/Wasm.Operator a sealed class, added PrattParser<S, Unit, R>.parseExpression(scanner), and dropped empty Javadoc JARs.Lexer factory experiment that broke Scanner subclassing; do not use.ConfigurableExpressionParser in favor of PrattParser.PrattParser (org.kobjects.parsek.expression).Scanner convenience constructor accepting regular expression pairs directly.Filter base class and expanded RegularExpressions.mython indentation-based language example.wasmJs target.org.kobjects.parsek), Maven Central publishing support, and JSON parser example.Parser library for Kotlin consisting of a tokenizer and expression parser.
Tokenization is the process of splitting the input into a stream of tokens that is consumed by a parser.
In Parsek, most parsers only need to interact with Scanner (and optionally RegularExpressions); the underlying Lexer is exposed as a raw Iterator<Token<T>> for cases that need custom token-stream processing.
The Scanner class (source, kdoc) provides a convenient API over a token stream:
Regex to tokenType pairs (mapping skipped tokens such as whitespace to null), or over any Iterator<Token<T>>.Scanner (e.g. class Pl0Scanner(input: String) : Scanner<TokenType>(input, TokenType.EOF, ...)) is recommended so parser methods can accept Pl0Scanner without repeating <TokenType>, and custom helper methods can be added if needed. Direct Scanner(...) instantiation is also fine for simple cases.Regex instances in constants (private val or RegularExpressions) rather than constructing them inside the Scanner constructor on each parse call.eofType (e.g. TokenType.EOF) keeps Token.type non-nullable. To avoid adding an EOF entry to an enum, pass null with a nullable type parameter (Scanner<TokenType?>(input, null, ...)), or use Scanner<Unit?>(input, null, ...) (mapping emitted tokens to Unit) when you don't need token types at all.current token that can be inspected multiple times (unlike Iterator.next()), unlimited dynamic lookAhead(n), and consume / tryConsume / consumeIgnoreCase / tryConsumeIgnoreCase helpers.Typical use cases that only need a scanner and no expression parser are data formats such as JSON or CSV.
For a simple example, please refer to the JSON parser example.
The RegularExpressions object (source, kdoc) contains precompiled regular expressions for common source code and data format tokens.
The token class (source, kdoc) stores the token type (typically a user-defined enum), the token text and the token position.
The lexer (source,
kdoc) implements Iterator<Token<T>>, splitting an input string into tokens using Regex to tokenType pairs (multiple regular expressions may map to the same token type, and rules mapped to null are skipped).
You only need to instantiate or subclass Lexer directly when installing additional processing between raw tokenization and Scanner — for instance via lexer.asSequence().filter { ... }.iterator() for context-sensitive newline filtering in indentation-based languages.
The Pratt expression parser (source, kdoc) operates on a scanner, is stateless and should be shared / reused.
A simple example evaluating mathematical expressions directly (opposed to building an explicit parse tree) can be found in the tests
A complete PL/0 parser is included in the examples module to illustrate how to use the expression parser and scanner for a simple but computationally complete language: Pl0Scanner.kt, Pl0Parser.kt, Pl0Test.kt
A parser for mathematical expressions: ExpressionLexer.kt, ExpressionScanner.kt, ExpressionParser.kt, ExpressionsTest.kt
A simple example for using the lexer, Sequence.filter, scanner, and expression parser to implement an indentation-based
programming language: mython,
MythonTest.kt
A BASIC interpreter using Parsek: https://github.com/stefanhaustein/basik
Lexer (Iterator<Token<T>>) rules to Pair<Regex, T?> directly, removing the mapping lambda and textNormalization.Scanner EOF token text to "<EOF>", added consumeIgnoreCase(...) / tryConsumeIgnoreCase(...), and deprecated Filter, 3-argument Scanner(input, eofType, eofText), and ignoreCase boolean overloads.ConfigurableExpressionParser (org.kobjects.parsek.expressionparser).RegularExpressions.NUMBER to UNSIGNED_NUMBER (with NUMBER deprecated for one release) and fixed RegularExpressions.SYMBOL escaping for JS/Wasm.Operator a sealed class, added PrattParser<S, Unit, R>.parseExpression(scanner), and dropped empty Javadoc JARs.Lexer factory experiment that broke Scanner subclassing; do not use.ConfigurableExpressionParser in favor of PrattParser.PrattParser (org.kobjects.parsek.expression).Scanner convenience constructor accepting regular expression pairs directly.Filter base class and expanded RegularExpressions.mython indentation-based language example.wasmJs target.org.kobjects.parsek), Maven Central publishing support, and JSON parser example.