Skip to content

SylphxAI/ast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

28 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

AST - Abstract Syntax Tree Tools ๐ŸŒณ

ast โ€” Sylphx Mark banner

**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.


MCP Family Role

AST is a foundation package for the SylphxAI MCP family, not an MCP server. It owns the @sylphlab/ast-* package line: ANTLR-backed parser contracts, typed node interfaces, source spans, grammar fixtures, and JavaScript-first package behavior.

Rust-native MCP products such as Architecture Reader MCP and CodeRAG may use AST outputs only through public package exports, generated fixtures, or stable contract files. They must not import workspace internals or add a TypeScript MCP adapter here.

See the family roadmap: docs/roadmap/mcp-family-ast-foundation.md.


โšก 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 Bun workspaces + Turborepo
  • Modern tooling - Bun, Biome, Turborepo, and 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 Bun workspaces Dependency management
Build Turborepo Task orchestration
Bundler tsup Fast ESM/CJS builds
Testing Vitest Unit testing
Linting Biome Code quality
Formatting Biome Code style
Git Hooks commitlint Commit message checks
Versioning Changesets Version management

๐Ÿš€ Quick Start

Installation

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

# Install dependencies
bun install

# Build all packages
bun run build

# Run tests
bun run 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
โ”œโ”€โ”€ turbo.json             # Turborepo configuration
โ””โ”€โ”€ README.md

Common Commands

# Development
bun run test:watch            # Watch mode for tests

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

# Testing
bun run test                  # Run all tests
bun run test:watch            # Watch mode for tests

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

# Full Validation
bun run validate              # Format/lint, generated parser freshness, typecheck, test, build

# ANTLR (JavaScript package)
cd packages/javascript
bun run antlr                 # Regenerate committed parser files after grammar changes

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 Bun + 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 (Biome, commitlint)

๐Ÿšง 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
bun run test

# Watch mode
bun run test:watch

# Test specific package
cd packages/javascript
bun run 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

Biome

Biome owns formatting and linting. The root biome.json is the source of truth for code style and lint behavior.

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 bun run 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

Project Control and Release Proof

Repository metadata for tools and agents lives in project.manifest.json.

Package releases run through the shared Sylphx release workflow and are complete only after CI, the Release workflow, and npm registry readback for changed AST packages. Parser behavior changes additionally require package tests and consumer smoke evidence for affected parser contracts.

๐Ÿ“„ 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

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages