brand banner

yang-ts-parser

CLI tool to automagically parse and compile YANG models into Typescript types with respect to Nokia SDN extended RESTCONF implementation. Lexer/parser grammar based on ANTLRv4.

Updated · 4 min read · #project · #typescript · #yang · #compilers

yang-ts-parser

CLI tool to automagically parse and compile YANG models into Typescript types with respect to Nokia SDN extended RESTCONF implementation. Lexer/parser grammar based on ANTLRv4.

yang-ts-compiler (YANG to TypeScript compiler)

NOTE: This project is a private, proprietary tool developed for Nokia. It is not open-source or publicly available. This document is an extract from the project’s README.md without any functioning source code.

CLI tool to automagically parse and compile 🔗 IETF RFC 6020,7950 YANG models into Typescript types with respect to Nokia’s 🔗 Network Services Platform (NSP) > Software-Defined Network (SDN) > IP/MPLS Optimization Module (NRC-P) extended RESTCONF (RFC 8040) implementation. Lexer/parser grammar based on ANTLRv4.

For example, given this YANG input file (extracted from 🔗 RFC 6991: Common YANG Data Types):

  grouping tunnel-path-srlgs {
    container tunnel-path-srlgs {
      choice style {
        case values {
          leaf usage { type identityref { base route-exclude-srlg; } }
          leaf-list values { type te-types:srlg; }
        }
        case named {
          list constraints {
            key "usage";
            leaf usage { type identityref { base route-exclude-srlg; } }
            container constraint {
              list srlg-names {
                key "name";
                leaf name { type string; }
  } } } } } } }

It will produce such an output:

export type TunnelPathSrlgs = {
  tunnelPathSrlgs: {
    usage: null | any;
    values: (null | { uint32: number })[];
    constraints: null | {
      [key: string]: {
        usage: any;
        constraint: { srlgNames: { [key: string]: { name: string } } };
      };
    };
  };
};

Installation

Copy the bundled executable binary to somewhere on PATH:

# To install
git clone git@internal-nokia-url:jyap/yang-ts-parser.git
sudo cp yang-ts-parser/dist/yang-ts-parser_osx-64 /usr/local/bin/yang-ts-parser

# To uninstall
sudo rm /usr/local/bin/yang-ts-parser

Usage

Run this command from inside the directory of your YANG files to generate Typescript types for each of the groupings found in the module:

# Format: yang-ts-parser <module name>
yang-ts-parser ietf-yang-types
Note that the module name is written without the .yang extension. As well, all other modules that the target module depends on (using import statements) must exist within the same folder.

You can also optionally specify an output filepath:

yang-ts-parser ietf-yang-types --output output.ts

If you want to generate some debug files like topology.png and type_map.json to view the internal structure generated by yang-ts-parser, use the --debug flag:

yang-ts-parser ietf-yang-types --debug

Example topology.png:

YANG module dependency graph with numbered import order

The red number above each node represents the order in which those nodes will be imported.

Example type_map.json:

{
  "binary": "(0 | 1)[]",
  "counter32": "{ uint32: number; }",
  "zero-based-counter32": "{ counter32: { uint32: number; }; }",
  "counter64": "{ uint64: number; }",
  "zero-based-counter64": "{ counter64: { uint64: number; }; }",
  "xpath1.0": "{ string: string; }",
  "hex-string": "{ string: string; }",
  "uuid": "{ string: string; }",
  "dotted-quad": "{ string: string; }"
}

Additionally, to format the output file automatically, use the --format flag:

yang-ts-parser ietf-yang-types --format
The --format command uses ‘prettier’, so make sure Prettier is installed using npm install -g prettier, and is accessible in your PATH: which prettier.

Features

Module Dependency Resolution

yang-ts-parser visits every module that is referenced by the target module recursively, and automatically builds a topological import order. Circular import detection is a side-effect of this algorithm, and will warn users if circular dependencies are detected (using Topological Sort and Directed Acycyclic Graphs [DAG]).

Lexer/Parser, Transformer, Compiler/Bundler, Tree Shaking

  • Lexer/Parser: Tokenization and pattern matching built with ANTLRv4.
  • Transformer: Custom logic that generates Typescript chunks from YANG.
  • Compiler/Bundler: Bundles types and groupings from different sources into one final file that contains everything.
  • Tree Shaking: Output only contains the types and groupings that are reachable from the target grouping. Irrelevant types are discarded from the final output to save space.

Linting and Error Checking

2025-03-04 17:05:38,809 - root - ERROR - ietf-yang-types - SyntaxError: [@2670,33542:33545='type',<7>,1233:12]: Encountered unknown type: String

When yang-ts-parser encounters unknown types or invalid syntax, it will gracefully handle the error and protest using human-legible messages. It also shows the module name and line number that the error was found to occur in (e.g., ietf-yang-types, line 1233 at char 12 for the above message).

Coverage

These are some known blocks/directives that are not covered by the parser due to ambiguity in how the NRCP handles them. In order to add support for them, an example instance of such blocks appearing in the REST API response must be provided.

In order to find an exhaustive list of blocks that aren’t fully supported yet, go to JsonYangListener.py and search for ‘any’.

Supported types

string, boolean, uint8, uint32, int32, enumeration, binary, list, leaf-list, union, choice

Unsupported types

empty, bits, decimal64, leafref, identityref, union (partial support)

Development

To build the project, navigate to the root and run these commands:

Generate ANTLR files

This step is only required if you have modified the Yang.g4 grammar file (or if it’s your first time building this project).

python3 -m pip install antlr4-tools antlr4-python3-runtime==4.13.0
cd src/yang_ts_parser/generated
antlr4 -Dlanguage=Python3 Yang.g4

I strongly recommend reading the ANTLR Mega Tutorial if you have not already. It is a great start to learning ANTLR and is very beginner friendly.

As well, use ANTLR Lab to debug and visualize the Yang.g4 grammar.

ANTLR Lab showing a YANG grammar and its parsed tree

  • Handwired: Programs as Neural Networks (39%) — How Handwired replaces control flow with numeric data flow, and what that does—and does not—make possible.
  • Superliminal in Three.js (27%) — Recreation of the 'Superliminal' game's 'Forced Perspective' effect using Three.js. Used Raycaster, Quaternion Rotations, Bounding Boxes, and Linear Transformations.
  • DriveSafe: Video Classification Notes (27%) — Early DriveSafe notes on driving behavior classification, training data, model evaluation, and ways to incorporate temporal information from video.
  • DriveSafe AI (26%) — Driver risk assessment from dashcam footage, using a ResNet50 + LSTM model to classify driving as safe, collision, tailgating, or weaving.
  • Linear Algebra LaTeX (24%) — Some basic LaTeX snippets common used in Linear Algebra.
  • AirBoard (21%) — AirBoard is a portable digital whiteboard system engineered to allow teachers to make handwritten annotations on any flat surface using infrared diodes (~940nm), optical sensors, Computer Vision, and perspective transform.
  • Regularization (20%) — Resources on regularization connect Lᵖ norms and their geometry to sparse model weights.
  • Maximum Likelihood and Cross-Entropy (20%) — A mathematical textbook connecting likelihood, entropy, KL divergence, statistical estimation, and probabilistic machine learning, with worked examples and solved exercises.

© 2026 James Yap

Personal Website and Knowledge Base