Type-safe AST parsing for JavaScript with extensible architecture
ANTLR-powered โข Type-safe โข Extensible โข Monorepo
Architecture โข Packages โข Development
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.
| 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 |
- Monorepo structure - Organized with pnpm workspaces + Turborepo
- Modern tooling - ESLint flat config, Prettier, Husky, commitlint
- Testing - Vitest for fast, reliable tests
- Build -
tsupfor lightning-fast builds with type declarations - Versioning - Changesets for version management
@sylphlab/ast-core
- Generic AST node interfaces and types
- Foundation for all language parsers
- Zero dependencies
- Fully typed
@sylphlab/ast-javascript
- JavaScript/ECMAScript parser
- ANTLR-generated lexer and parser
- Custom visitor for AST transformation
- Depends on:
@sylphlab/ast-core,antlr4ts
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ @sylphlab/ast-javascript โ
โ (JavaScript-specific parser) โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโ
โ depends on
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ @sylphlab/ast-core โ
โ (Generic AST interfaces & types) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| 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 |
# Clone repository
git clone https://github.com/SylphxAI/ast.git
cd ast
# Install dependencies
pnpm install
# Build all packages
pnpm build
# Run tests
pnpm testimport { parse } from '@sylphlab/ast-javascript';
const code = `
const greeting = "Hello, World!";
console.log(greeting);
`;
const ast = parse(code);
console.log(ast);-
Separation of Concerns
- Core types separated from language implementations
- Each language parser in its own package
-
Extensibility
- Easy to add new language parsers
- Shared core interfaces for consistency
-
Type Safety
- Strict TypeScript throughout
- Full type inference
-
ANTLR Integration
- Grammar files (
.g4) define language syntax - Generated lexer/parser from grammar
- Custom visitor transforms parse tree to AST
- Grammar files (
Source Code (String)
โ
โผ
ANTLR Lexer
(Tokenization)
โ
โผ
ANTLR Parser
(Parse Tree)
โ
โผ
Custom Visitor
(Transformation)
โ
โผ
Custom AST
(@sylphlab/ast-core types)
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
# 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-
Create package directory
mkdir packages/new-language cd packages/new-language -
Create package.json
{ "name": "@sylphlab/ast-new-language", "version": "0.0.0", "dependencies": { "@sylphlab/ast-core": "workspace:*", "antlr4ts": "^0.5.0-alpha.4" } } -
Add grammar file
- Find or create ANTLR
.g4grammar for the language - Place in
grammar/directory
- Find or create ANTLR
-
Implement visitor
- Create custom visitor to transform ANTLR parse tree
- Map to
@sylphlab/ast-coretypes
-
Export parser
export function parse(code: string): AstNode { // Implementation }
- 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)
- Complete AST visitor implementation
- Comprehensive test coverage
- Position tracking improvements
- Grammar validation and refinement
- Support for additional JavaScript features
- TypeScript parser
- Python parser
- Go parser
- AST manipulation utilities
- Pretty printer (AST โ source code)
- Documentation site
# Run all tests
pnpm test
# Watch mode
pnpm test:watch
# Test specific package
cd packages/javascript
pnpm testimport { 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();
});
});Strict mode enabled with:
noImplicitAny: truestrictNullChecks: truestrictFunctionTypes: true
Using ESLint 9 flat config format:
// eslint.config.js
export default [
{
ignores: ['**/dist/**', '**/generated/**'],
// ... rules
}
];Grammar generation:
antlr4ts -visitor -listener -o src/generated grammar/*.g4- ESTree Specification (JavaScript AST spec)
- TypeScript Compiler API
Contributions are welcome! Please follow these guidelines:
- Open an issue - Discuss changes before implementing
- Fork the repository
- Create a feature branch -
git checkout -b feature/my-feature - Follow code standards - Run
pnpm validate - Write tests - Maintain high coverage
- Commit with conventional commits -
feat:,fix:,docs:, etc. - Submit a pull request
feat(javascript): add support for async/await
fix(core): correct position tracking for nested nodes
docs(readme): update installation instructions
MIT ยฉ Sylphx
Built with:
- ANTLR - Parser generator
- antlr4ts - ANTLR runtime for TypeScript
- TypeScript - Language
- pnpm - Package manager
- Turborepo - Monorepo tool
- Vitest - Testing framework
Type-safe AST parsing for the modern web
Built with TypeScript and ANTLR
sylphx.com โข
@SylphxAI โข
hi@sylphx.com