JavaParser-AST-Inspector icon indicating copy to clipboard operation
JavaParser-AST-Inspector copied to clipboard

initial inclusion of a live template

Open MysterAitch opened this issue 3 years ago • 1 comments

This "live template" creates a complete boilerplate for parsing and handling String parse input.

  • While I'm not 100% on the actual content of the template and it can probably be improved (suggestions/issues/pull requests welcome!), it's a start showing it can be done! :+1: :grin:

  • As an aside, it highlights how much the StaticJavaParser hides from you and the logic paths/branches that are not handled/able to be handled if using it...

Here is a sample of the output (note it will auto-format based on your editor settings):

        String inputToParse = "class X {}";
        JavaParser javaParser = new JavaParser();
        ParseResult<CompilationUnit> parseResult = javaParser.parse(inputToParse);
        if (parseResult.isSuccessful()) {
            if (parseResult.getResult().isPresent()) {
                CompilationUnit compilationUnit = parseResult.getResult().get();
                // Do things with the compilation unit

            } else {
                System.out.println("Warning: Parsing was successful, but no CompilationUnit found. ");
            }
        } else {
            System.out.println("Warning: Parse was unsuccessful. Attempts to inspect/alter the AST may be possible, but may lead to unintended outputs.");
            if (parseResult.getResult().isPresent()) {
                CompilationUnit compilationUnitWithParseError = parseResult.getResult().get();
                // Do things with the compilation unit (with parse error)

            } else {
                System.out.println("Warning: Parsing was successful, but no CompilationUnit found. ");
            }
        }

Here's a screenshot showing the places where input is prompted for.

image


Here's a screenshot showing how it reacts to parsing an Expression instead of a full CompilationUnit.

Note the auto-suggested variable names update based on the previous selections.

image


Here's a screenshot showing how it reacts to using a different variable name

Note here that I manually tweak it to expr instead of expression, and further down the variable has updated to exprWithParseError.

image

MysterAitch avatar Jan 23 '22 19:01 MysterAitch

closes #210

MysterAitch avatar Jan 23 '22 19:01 MysterAitch