Skip to content

Java Parser for JavaScript/TypeScript (based on antlr4ts)

License

Notifications You must be signed in to change notification settings

pascalgn/java-ast

Repository files navigation

java-ast

npm version code style: prettier

Java Parser for JavaScript/TypeScript, based on antlr4ts, grammar taken from antlr4's java grammar too (so please report bugs and open pull requests related to grammars upstream)

Usage Example

import { parse, createVisitor } from 'java-ast';

const countMethods = (source: string) => {
  let ast = parse(source);

  return createVisitor({
    visitMethodDeclaration: () => 1,
    defaultResult: () => 0,
    aggregateResult: (a, b) => a + b,
  }).visit(ast);
};

console.log(
  countMethods(`
    class A {
      int a;
      void b() {}
      void c() {}
    }
    class B {
      void z() {}
    }
  `),
); // logs 3

History

This project was originally created by Uri Shaked.

See also

  • java-model - Provides high-level access to the Java type model.

License

MIT