Newick tree format parser using Nearley.js. The primary API is intended for JavaScript modules, with a small CLI available for workflows that read a tree from a file path.
This package is ESM-only.
pnpm add nearley-newickimport { newickToAST, newickToJSON } from "nearley-newick";
const graph = newickToJSON("(A,B,(C,D)E)F;");
const ast = newickToAST("(A,B,(C,D)E)F;");newickToJSON returns a graph-shaped object:
{
nodes: [
{
id: '...',
type: 'leaf',
nodeMetadata: {
name: 'A',
edge_length: ''
}
}
],
edges: [
{
id: 'parentId:childId',
dir: 'uni',
weight: ''
}
]
}Invalid Newick input throws NewickParseError.
npx nearley-newick --help
npx nearley-newick path/to/tree.nwk
npx nearley-newick --pretty path/to/tree.nwk
npx nearley-newick --ast path/to/tree.nwkThe default CLI output is compact graph JSON from newickToJSON. Use --pretty to format the JSON and --ast to output the raw parser AST.
pnpm install
pnpm run compile
pnpm testEdit lib/newick_grammar.ne and run pnpm run compile to regenerate lib/newick_grammar.cjs.
├── bin
│ └── nearley-newick.js # CLI entry point
├── lib
│ ├── newick_grammar.ne # Nearley grammar source
│ ├── newick_grammar.cjs # generated parser from `pnpm run compile`
│ ├── index.js # public module API
│ ├── index.d.ts # public TypeScript declarations
│ ├── parser.js # Newick parser implementation
│ └── grammar.js # ESM wrapper around generated grammar