GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — prettier-java/packages/java-parser at main · feedly/prettier-java

Expand file tree

/

java-parser

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

java-parser

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
docs
docs
 
 
resources/Unicode
resources/Unicode
 
 
scripts
scripts
 
 
src
src
 
 
test
test
 
 
.gitignore
.gitignore
 
 
.mocharc.cjs
.mocharc.cjs
 
 
.npmignore
.npmignore
 
 
README.md
README.md
 
 
api.d.ts
api.d.ts
 
 
package.json
package.json
 
 

README.md

Outline

npm

java-parser

A Java Parser implemented in JavaScript using the Chevrotain Parsing ToolKit. It outputs a Concrete Syntax Tree, rather than an Abstract Syntax Tree.

  • On the Difference between a CST and an AST

Currently the main focus of this project is to be used in implementing a prettier Java plugin. But it could also be used as the basis for other Java related tools in the JavaScript ecosystem.

Installation

npm install java-parser --save-dev

or

yarn add java-parser --dev

Usage

Parsing

const { parse } = require("java-parser");
const javaText = `
public class HelloWorldExample{
  public static void main(String args[]){
    System.out.println("Hello World !");
  }
}
`;

const cst = parse(javaText);
// explore the CST

Traversing the CST

See relevant Chevrotain documentation on CST Traversal.

const {
  BaseJavaCstVisitor,
  BaseJavaCstVisitorWithDefaults
} = require("java-parser");

// Use "BaseJavaCstVisitor" if you need to implement all the visitor methods yourself.
class LambdaArrowsPositionCollector extends BaseJavaCstVisitorWithDefaults {
  constructor() {
    super();
    this.customResult = [];
    this.validateVisitor();
  }

  lambdaExpression(ctx) {
    // Collects all the starting offsets of lambda arrows in lambdas with short (no parenthesis)
    // single argument lists: e.g:
    // - n -> n*n (will be collected)
    // - (n) -> n*n (not collected)
    if (ctx.lambdaParameters[0].children.Identifier) {
      this.customResult.push(ctx.Arrow[0].startOffset);
    }
  }
}

const lambdaArrowsCollector = new LambdaArrowsPositionCollector();
// The CST result from the previous code snippet
lambdaArrowsCollector.visit(cst);
lambdaArrowsCollector.customResult.forEach(arrowOffset => {
  console.log(arrowOffset);
});
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.