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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

87a6440 · Aug 20, 2024

History

42 Commits
Aug 20, 2024
Jul 10, 2018
Jul 17, 2020
Jul 10, 2018
Jul 10, 2018
Apr 11, 2024
Jul 20, 2020
Aug 20, 2024
Jul 10, 2018
Sep 27, 2022
Jul 13, 2020
Jul 20, 2020
Jul 10, 2018
Aug 20, 2024

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