Skip to content

A Java parser written in pure java (no jdk required, zero dependencies) that generates a syntax tree from source code

License

Notifications You must be signed in to change notification settings

JanHolger/pure-java-parser

Repository files navigation

pure-java-parser

A Java parser written in pure java (no jdk required, zero dependencies) that generates a syntax tree from source code

Usage

For parsing an entire class file it's as simple as initializing a parser and calling a single method.

public class SimpleExample {
    public static void main(String[] args) throws UnexpectedCharacterException, UnexpectedTokenException {
        String source = "class MyClass {}"; // Example source code
        PureJavaParser parser = new PureJavaParser(); // Initialize a new parser
        ClassFileDefinition classFile = parser.parse("MyClass.java", source); // Parse the source into an ast
        System.out.println(classFile.toString()); // Generate and print source code for visualization
    }
}

Most language constructs can also be manually parsed. This can be useful if you only want to parse a single expression for example.

public class ExpressionExample {
    public static void main(String[] args) throws UnexpectedCharacterException, UnexpectedTokenException {
        String source = "a + b * c"; // Example source code
        PureJavaParser parser = new PureJavaParser(); // Initialize a new parser
        TokenStack stack = Tokenizer.tokenize("MyClass.java", source); // Manually tokenize the source
        Expression expression = parser.getExpressionParser().parseExpression(stack); // Parse the source into an ast
        if (stack.trim().peek().getType() != TokenType.EOF) // Manually check if the end of input is reached
            throw new UnexpectedTokenException(stack.pop()); // Throw an error in case there are unconsumed tokens
        System.out.println(expression.toString()); // Generate and print source code for visualization
    }
}

TODO

These are features that are known to be missing

  • Assignment Operator as Expression instead of Statement
  • Prefix Incremental Operators
  • Array Class Literals

About

A Java parser written in pure java (no jdk required, zero dependencies) that generates a syntax tree from source code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages