thrift-parser-typescript

raw JSON →
0.0.5 verified Fri May 01 auth: no javascript

A TypeScript library for parsing Apache Thrift IDL files into an AST. Current stable version is 0.0.5, with irregular releases. It provides a complete ANTLR-based parser for .thrift files and exports the generated visitor, parser, lexer, and listener. This package is one of the few TypeScript-first Thrift parsers, ships its own type definitions, and is suitable for tooling like code generation or linting.

error Cannot find module 'antlr4ts'
cause Missing peer dependency antlr4ts.
fix
Run 'npm install antlr4ts' in your project.
error TypeError: Class extends value undefined is not a constructor or null
cause Importing ThriftVisitor incorrectly or missing import.
fix
Use 'import { ThriftVisitor } from 'thrift-parser-typescript'.
error Module not found: Can't resolve 'thrift-parser-typescript'
cause Package not installed or import path incorrect.
fix
Run 'npm install thrift-parser-typescript' and check import path.
error Cannot read property 'document' of undefined
cause Parser not instantiated correctly (missing tokens or input).
fix
Ensure you create CommonTokenStream and pass to ThriftParser constructor.
gotcha The package requires antlr4ts as a peer dependency; not installing it leads to runtime errors.
fix Install antlr4ts: npm install antlr4ts
breaking In v0.0.4, exports were reorganized; ThriftLexer and ThriftParser moved from subpaths to root.
fix Update imports to use root package path.
gotcha The package does not export a 'parse' convenience function; users must manually wire ANTLRInputStream, lexer, tokens, and parser.
fix Write boilerplate to create the parser pipeline as shown in quickstart.
deprecated Importing from 'thrift-parser-typescript/dist/*' is deprecated but still works; prefer root imports.
fix Use import { ... } from 'thrift-parser-typescript'.
npm install thrift-parser-typescript
yarn add thrift-parser-typescript
pnpm add thrift-parser-typescript

Parses a Thrift IDL string into an AST and visits it using a custom visitor.

import { ThriftLexer, ThriftParser, ThriftVisitor } from 'thrift-parser-typescript';

const input = `struct User { 1: required string name; 2: optional i32 age; }`;
const chars = new (require('antlr4ts').ANTLRInputStream)(input);
const lexer = new ThriftLexer(chars);
const tokens = new (require('antlr4ts').CommonTokenStream)(lexer);
const parser = new ThriftParser(tokens);
const tree = parser.document();

class MyVisitor extends ThriftVisitor<string> {
  visitStruct(ctx: any): string {
    return `struct ${ctx.name}`;
  }
}
const result = new MyVisitor().visit(tree);
console.log(result);