Build parsers in Java with composable parser combinators. Use parseWorks to express grammars in code, get clear error messages, and keep your parsers fast and testable.
- Compose parsers with a small set of powerful combinators.
- Get precise, readable error messages for parse failures.
- Write thread-safe code with immutable parsers and inputs.
- Keep dependencies minimal (only JUnit in tests).
- Avoid left-recursion pitfalls with built-in protection.
- Detect infinite loops on empty input.
Requirements: Java 17 or higher.
Maven (latest release):
<dependency>
<groupId>io.github.parseworks</groupId>
<artifactId>parseworks</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>Gradle:
repositories {
maven { url 'https://central.sonatype.com/repository/maven/' }
}
dependencies {
implementation 'io.github.parseworks:parseworks:3.0.0'
}Use parser combinators to build a simple addition parser:
import io.github.parseworks.parsers.Lexical;
import static io.github.parseworks.parsers.Numeric.*;
// Define a parser for a simple addition expression
Parser<Character, Integer> sum =
Numeric.number.thenSkip(Lexical.chr('+')).then(number).map(Integer::sum);
// Parse the input "1+2"
int value = sum.parse(Input.of("1+2")).value();
assert value ==3;
// Handle a parsing error
String message = sum.parse(Input.of("1+z")).handle(
success -> "Match: " + success,
failure -> "Error: " + failure.error()
);
// Example output contains: "Error: Parse error at line 1 position 3"- Read the User guide to get started.
- Explore the Advanced user guide for complex patterns and performance tips.
- Follow the Parser design guide to plan and implement robust parsers.
parseWorks is available under the MIT License.