Skip to content

Latest commit

 

History

History
36 lines (33 loc) · 1.87 KB

README.md

File metadata and controls

36 lines (33 loc) · 1.87 KB

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