# parser-java

> A Java parser library for JavaScript/TypeScript

Latest version **1.0.1** (published 2022-08-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install parser-java
pnpm add parser-java
yarn add parser-java
bun add parser-java
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-08-21 |
| First published | 2022-08-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 50.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Katsumi Okuda |
| Maintainers | okuda |
| Keywords | Java, parser, PEG, pegisland, packrat, parsing |

## Links

- npm: https://www.npmjs.com/package/parser-java
- npm.io page: https://npm.io/package/parser-java

## Dependencies (3)

- [pegisland](https://npm.io/package/pegisland.md) ^2.0.1
- [typescript](https://npm.io/package/typescript.md) ^4.7.4
- [@types/node](https://npm.io/package/@types/node.md) ^18.7.6

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2022-08-21
- 1.0.0 — 2022-08-21

## README

# parser-java: A Java parser for JavaScript/TypeScript

## Example

The following code parses a Java program stored in the variable program and prints the result.

```TypeScript
import { exit } from 'process';
import { parse, traverseNonterminals } from 'parser-java';

const program = `
class Foo {
  public static int main(String[] args) {
    return (1+2-3)*4/5;
  }
}
`;

const parseTree = parse(program);
if (parseTree instanceof Error) {
  console.log(parseTree.message);
  exit(1);
}

traverseNonterminals(parseTree, (node) => {
  const start = node.range.start;
  const end = node.range.end;
  console.log(node.symbol, (node.childNodes[0] as Object).constructor.name, {
    start: `(${start.line}, ${start.column})`,
    end: `(${end.line}, ${end.column})`,
  });
});
```

The above code prints the following output.

```

Spacing NodeZeroOrMore { start: '(1, 1)', end: '(2, 1)' }
CompilationUnit NodeSequence { start: '(2, 1)', end: '(7, 1)' }
TypeDeclaration NodeOrderedChoice { start: '(2, 1)', end: '(7, 1)' }
ClassDeclaration NodeOrderedChoice { start: '(2, 1)', end: '(7, 1)' }
NormalClassDeclaration NodeSequence { start: '(2, 1)', end: '(7, 1)' }
CLASS NodeSequence { start: '(2, 1)', end: '(2, 7)' }
Spacing NodeZeroOrMore { start: '(2, 6)', end: '(2, 7)' }
Identifier NodeSequence { start: '(2, 7)', end: '(2, 11)' }
Letter NodeOrderedChoice { start: '(2, 7)', end: '(2, 8)' }
LetterOrDigit NodeOrderedChoice { start: '(2, 8)', end: '(2, 9)' }
LetterOrDigit NodeOrderedChoice { start: '(2, 9)', end: '(2, 10)' }
Spacing NodeZeroOrMore { start: '(2, 10)', end: '(2, 11)' }
ClassBody NodeSequence { start: '(2, 11)', end: '(7, 1)' }
LWING NodeSequence { start: '(2, 11)', end: '(3, 3)' }
...
RWING NodeSequence { start: '(5, 3)', end: '(6, 1)' }
Spacing NodeZeroOrMore { start: '(5, 4)', end: '(6, 1)' }
RWING NodeSequence { start: '(6, 1)', end: '(7, 1)' }
Spacing NodeZeroOrMore { start: '(6, 2)', end: '(7, 1)' }
EOT NodeNot { start: '(7, 1)', end: '(7, 1)' }

```

## Installation

To install, run:

    $ npm install parser-java

## Contributing

## License

parser-java is available as open-source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

```

```

---
_Source: https://npm.io/package/parser-java · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
