#tree-sitter #newick #parser

tree-sitter-newick

A parser for (extended) Newick files (nh, nwk, nhx)

2 stable releases

1.1.0 Oct 8, 2025
1.0.0 Oct 8, 2025

#879 in Parser implementations

Download history 392/week @ 2025-10-22 13/week @ 2025-10-29 23/week @ 2025-11-05 855/week @ 2025-11-12 1121/week @ 2025-11-19 307/week @ 2025-11-26 275/week @ 2025-12-03 220/week @ 2025-12-10 277/week @ 2025-12-17 162/week @ 2025-12-24 400/week @ 2025-12-31 331/week @ 2026-01-07 388/week @ 2026-01-14 318/week @ 2026-01-21 671/week @ 2026-01-28 404/week @ 2026-02-04

1,826 downloads per month
Used in difftastic

Custom license

42KB
1.5K SLoC

C 1K SLoC Rust 32 SLoC // 0.1% comments JavaScript 29 SLoC Scheme 7 SLoC

tree-sitter-newick

A tree-sitter grammar for the Newick (nh, nwk) and New Hampshire X (nhx) formats for trees representation.

Example

NHX source

(
    (
        (
            ADH2:0.1[&&NHX:S=human:E=1.1.1.1],
            ADH1:0.11[&&NHX:S=human:E=1.1.1.1]
        ):0.05[&&NHX:S=Primates:E=1.1.1.1:D=Y:B=100],
        ADHY:0.1[&&NHX:S=nematode:E=1.1.1.1],
        ADHX:0.12[&&NHX:S=insect:E=1.1.1.1]
    ):0.1[&&NHX:S=Metazoa:E=1.1.1.1:D=N],
    (
    ADH4:0.09[&&NHX:S=yeast:E=1.1.1.1],
    ADH3:0.13[&&NHX:S=yeast:E=1.1.1.1],
    ADH2:0.12[&&NHX:S=yeast:E=1.1.1.1],
    ADH1:0.11[&&NHX:S=yeast:E=1.1.1.1]
    ):0.1[&&NHX:S=Fungi]
)[&&NHX:E=1.1.1.1:D=N];

tree-sitter-newick highlighting

NHX file highlighted by tree-sitter-newick

Syntax tree

(source_file [0, 0] - [16, 0]
  (tree [0, 0] - [15, 23]
    clade: (clade [0, 0] - [15, 22]
      clade: (clade [1, 4] - [8, 40]
        clade: (clade [2, 8] - [5, 52]
          leaf: (leaf [3, 12] - [3, 45]
            name: (name [3, 12] - [3, 16])
            attributes: (attributes [3, 16] - [3, 45]
              length: (length [3, 17] - [3, 20]
                (float [3, 17] - [3, 20]))
              data: (data [3, 20] - [3, 45]
                entry: (nhx_entry [3, 26] - [3, 34]
                  key: (nhx_val [3, 27] - [3, 28])
                  value: (nhx_val [3, 29] - [3, 34]))
                entry: (nhx_entry [3, 34] - [3, 44]
                  key: (nhx_val [3, 35] - [3, 36])
                  value: (nhx_val [3, 37] - [3, 44])))))
          leaf: (leaf [4, 12] - [4, 46]
            name: (name [4, 12] - [4, 16])
            attributes: (attributes [4, 16] - [4, 46]
              length: (length [4, 17] - [4, 21]
                (float [4, 17] - [4, 21]))
              data: (data [4, 21] - [4, 46]
                entry: (nhx_entry [4, 27] - [4, 35]
                  key: (nhx_val [4, 28] - [4, 29])
                  value: (nhx_val [4, 30] - [4, 35]))
                entry: (nhx_entry [4, 35] - [4, 45]
                  key: (nhx_val [4, 36] - [4, 37])
                  value: (nhx_val [4, 38] - [4, 45])))))
...

Dependencies

~17–250KB