Skip to content

SylphxAI/ast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

AST - Abstract Syntax Tree Tools ๐ŸŒณ

Type-safe AST parsing for JavaScript with extensible architecture

License bun TypeScript biome

ANTLR-powered โ€ข Type-safe โ€ข Extensible โ€ข Monorepo

Architecture โ€ข Packages โ€ข Development


๐Ÿš€ Overview

A production-grade Abstract Syntax Tree (AST) parsing toolkit built with TypeScript and ANTLR v4. Currently supports JavaScript parsing with an architecture designed for multi-language extensibility.

The Problem:

Traditional AST tools:
- Tightly coupled to single languages โŒ
- Difficult to extend or customize โŒ
- Limited type safety โŒ
- Complex integration โŒ

The Solution:

AST Toolkit:
- Clean, extensible architecture โœ…
- ANTLR-powered parsing โœ…
- Full TypeScript type safety โœ…
- Monorepo structure for modularity โœ…

Result: Production-ready AST parsing with a foundation for supporting any programming language.


โšก Key Features

Parser Architecture

Feature Description Benefit
ANTLR v4 Industry-standard parser generator Proven, battle-tested
Type-safe Full TypeScript with strict mode Catch errors at compile-time
Extensible Core + language modules Easy to add new languages
Visitor Pattern ANTLR parse tree โ†’ Custom AST Clean transformation

Development Experience

  • Monorepo structure - Organized with pnpm workspaces + Turborepo
  • Modern tooling - ESLint flat config, Prettier, Husky, commitlint
  • Testing - Vitest for fast, reliable tests
  • Build - tsup for lightning-fast builds with type declarations
  • Versioning - Changesets for version management

๐Ÿ“ฆ Packages

Core Package

@sylphlab/ast-core

  • Generic AST node interfaces and types
  • Foundation for all language parsers
  • Zero dependencies
  • Fully typed

Language Parsers

@sylphlab/ast-javascript

  • JavaScript/ECMAScript parser
  • ANTLR-generated lexer and parser
  • Custom visitor for AST transformation
  • Depends on: @sylphlab/ast-core, antlr4ts

Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  @sylphlab/ast-javascript               โ”‚
โ”‚  (JavaScript-specific parser)           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                 โ”‚ depends on
                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  @sylphlab/ast-core                     โ”‚
โ”‚  (Generic AST interfaces & types)       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ› ๏ธ Technology Stack

Component Technology Purpose
Language TypeScript 5.8 Type safety & modern JS
Parser ANTLR v4 Grammar-based parsing
Monorepo pnpm workspaces Dependency management
Build Turborepo Task orchestration
Bundler tsup Fast ESM/CJS builds
Testing Vitest Unit testing
Linting ESLint 9 (flat config) Code quality
Formatting Prettier Code style
Git Hooks Husky + lint-staged Pre-commit checks
Versioning Changesets Version management

๐Ÿš€ Quick Start

Installation

# Clone repository
git clone https://github.com/SylphxAI/ast.git
cd ast

# Install dependencies
pnpm install

# Build all packages
pnpm build

# Run tests
pnpm test

Using the Parser

import { parse } from '@sylphlab/ast-javascript';

const code = `
  const greeting = "Hello, World!";
  console.log(greeting);
`;

const ast = parse(code);
console.log(ast);

๐Ÿ—๏ธ Architecture

Design Principles

  1. Separation of Concerns

    • Core types separated from language implementations
    • Each language parser in its own package
  2. Extensibility

    • Easy to add new language parsers
    • Shared core interfaces for consistency
  3. Type Safety

    • Strict TypeScript throughout
    • Full type inference
  4. ANTLR Integration

    • Grammar files (.g4) define language syntax
    • Generated lexer/parser from grammar
    • Custom visitor transforms parse tree to AST

Parsing Flow

Source Code (String)
       โ”‚
       โ–ผ
  ANTLR Lexer
  (Tokenization)
       โ”‚
       โ–ผ
  ANTLR Parser
  (Parse Tree)
       โ”‚
       โ–ผ
  Custom Visitor
  (Transformation)
       โ”‚
       โ–ผ
  Custom AST
  (@sylphlab/ast-core types)

๐Ÿ’ป Development

Workspace Structure

ast/
โ”œโ”€โ”€ packages/
โ”‚   โ”œโ”€โ”€ core/              # @sylphlab/ast-core
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ”‚   โ””โ”€โ”€ tsconfig.json
โ”‚   โ””โ”€โ”€ javascript/        # @sylphlab/ast-javascript
โ”‚       โ”œโ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ grammar/       # .g4 grammar files
โ”‚       โ”œโ”€โ”€ package.json
โ”‚       โ””โ”€โ”€ tsconfig.json
โ”œโ”€โ”€ package.json           # Root workspace config
โ”œโ”€โ”€ pnpm-workspace.yaml    # pnpm workspace definition
โ”œโ”€โ”€ turbo.json             # Turborepo configuration
โ””โ”€โ”€ README.md

Common Commands

# Development
pnpm dev                   # Watch mode for all packages

# Building
pnpm build                 # Build all packages
turbo run build            # Build with Turborepo

# Testing
pnpm test                  # Run all tests
pnpm test:watch            # Watch mode for tests

# Code Quality
pnpm lint                  # Lint all packages
pnpm lint:fix              # Auto-fix linting issues
pnpm format                # Format code with Prettier
pnpm check-format          # Check formatting
pnpm typecheck             # TypeScript type checking

# Full Validation
pnpm validate              # Run all checks (format, lint, typecheck, test)

# ANTLR (JavaScript package)
cd packages/javascript
pnpm antlr                 # Generate parser from grammar

Adding a New Language Parser

  1. Create package directory

    mkdir packages/new-language
    cd packages/new-language
  2. Create package.json

    {
      "name": "@sylphlab/ast-new-language",
      "version": "0.0.0",
      "dependencies": {
        "@sylphlab/ast-core": "workspace:*",
        "antlr4ts": "^0.5.0-alpha.4"
      }
    }
  3. Add grammar file

    • Find or create ANTLR .g4 grammar for the language
    • Place in grammar/ directory
  4. Implement visitor

    • Create custom visitor to transform ANTLR parse tree
    • Map to @sylphlab/ast-core types
  5. Export parser

    export function parse(code: string): AstNode {
      // Implementation
    }

๐ŸŽฏ Current Status

โœ… Completed

  • Monorepo structure with pnpm + Turborepo
  • Core AST type definitions (@sylphlab/ast-core)
  • JavaScript parser package (@sylphlab/ast-javascript)
  • ANTLR v4 integration
  • ECMAScript grammar integration
  • Basic visitor structure
  • Build system configuration
  • Testing infrastructure
  • Code quality tooling (ESLint, Prettier, Husky)

๐Ÿšง In Progress

  • Complete AST visitor implementation
  • Comprehensive test coverage
  • Position tracking improvements
  • Grammar validation and refinement

๐Ÿ”ฎ Planned

  • Support for additional JavaScript features
  • TypeScript parser
  • Python parser
  • Go parser
  • AST manipulation utilities
  • Pretty printer (AST โ†’ source code)
  • Documentation site

๐Ÿงช Testing

Running Tests

# Run all tests
pnpm test

# Watch mode
pnpm test:watch

# Test specific package
cd packages/javascript
pnpm test

Test Structure

import { describe, it, expect } from 'vitest';
import { parse } from '@sylphlab/ast-javascript';

describe('JavaScript Parser', () => {
  it('should parse variable declarations', () => {
    const code = 'const x = 42;';
    const ast = parse(code);
    expect(ast).toBeDefined();
  });
});

๐Ÿ”ง Configuration

TypeScript

Strict mode enabled with:

  • noImplicitAny: true
  • strictNullChecks: true
  • strictFunctionTypes: true

ESLint

Using ESLint 9 flat config format:

// eslint.config.js
export default [
  {
    ignores: ['**/dist/**', '**/generated/**'],
    // ... rules
  }
];

ANTLR

Grammar generation:

antlr4ts -visitor -listener -o src/generated grammar/*.g4

๐Ÿ“š Resources

ANTLR Documentation

AST Resources


๐Ÿค Contributing

Contributions are welcome! Please follow these guidelines:

  1. Open an issue - Discuss changes before implementing
  2. Fork the repository
  3. Create a feature branch - git checkout -b feature/my-feature
  4. Follow code standards - Run pnpm validate
  5. Write tests - Maintain high coverage
  6. Commit with conventional commits - feat:, fix:, docs:, etc.
  7. Submit a pull request

Commit Message Format

feat(javascript): add support for async/await
fix(core): correct position tracking for nested nodes
docs(readme): update installation instructions

๐Ÿ“„ License

MIT ยฉ Sylphx


๐Ÿ™ Credits

Built with:


Type-safe AST parsing for the modern web
Built with TypeScript and ANTLR

sylphx.com โ€ข @SylphxAI โ€ข hi@sylphx.com

About

๐ŸŒณ AST manipulation and code transformation utilities

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published