Skip to content

saint1991/antlr-editor

Repository files navigation

ANTLR Editor

A comprehensive expression editor built with ANTLR4, featuring real-time syntax highlighting, error detection, and multi-platform support. The project consists of a modern web editor and a powerful analyzer engine that supports WebAssembly and Python FFI.

🌟 Features

Web Editor

  • Real-time syntax highlighting with CodeMirror 6
  • Error detection and display with inline diagnostics
  • Intelligent autocompletion with context-aware suggestions
  • Code formatting with proper indentation
  • Hover tooltips for function documentation
  • Dark/light theme support
  • Smart bracket matching and folding

Analyzer Engine

  • ANTLR4-based parsing for mathematical and logical expressions
  • WebAssembly support for browser integration
  • Python FFI bindings for native integration
  • Optimized WASM builds with TinyGo for minimal binary sizes
  • Multiple compilation targets (native Go, WASM, Python FFI)

Supported Expression Grammar

  • Arithmetic operators: +, -, *, /, ^ (with proper precedence)
  • Comparison operators: <, <=, >, >=, ==, !=
  • Logical operators: &&, ||
  • Literals: integers, floats, strings, booleans
  • Column references: [column_name] format
  • Function calls: FUNCTION_NAME(arg1, arg2, ...) format
  • Parentheses: for expression grouping

πŸš€ Quick Start

Web Editor

Start the development server:

cd editor-app
npm install
npm start

Navigate to http://localhost:4200 to use the editor.

Analyzer - WASM Build

Build optimized WASM modules for browser usage:

# From project root
docker build --target wasm-output --output=type=local,dest=editor-app/public -f analyzer/Dockerfile .

Analyzer - Python FFI

Install and use the Python bindings:

cd analyzer/ffi/python
pip install .

Example usage:

from antlr_analyzer import Analyzer

analyzer = Analyzer()
result = analyzer.tokenize("age > 18 AND name = 'John'")

if result.is_valid:
    for token in result.tokens:
        print(f"{token.text} -> {token.token_type.name}")
else:
    for error in result.errors:
        print(f"Error: {error.message}")

πŸ—οΈ Project Structure

antlr-editor/
β”œβ”€β”€ grammar/              # ANTLR4 grammar definitions
β”‚   β”œβ”€β”€ Expression.g4        # Main expression grammar file
β”‚   └── grammar.md           # Grammar documentation
β”œβ”€β”€ analyzer/             # Go-based ANTLR4 analyzer engine
β”‚   β”œβ”€β”€ core/                # Core analyzer logic
β”‚   β”œβ”€β”€ wasm/                # WebAssembly target
β”‚   β”œβ”€β”€ ffi/                 # Python FFI and C shared library
β”‚   β”œβ”€β”€ gen/                 # Generated parser code (git-ignored)
β”‚   β”œβ”€β”€ codegen.sh           # Parser generation script
β”‚   └── Dockerfile           # Multi-stage build for ANTLR generation
β”œβ”€β”€ editor-app/           # Angular-based web editor
β”‚   β”œβ”€β”€ src/app/antlr-editor/   # Main editor component
β”‚   β”‚   └── extensions/         # CodeMirror extensions
β”‚   β”‚       β”œβ”€β”€ completion/        # Autocompletion
β”‚   β”‚       β”œβ”€β”€ format/            # Code formatting
β”‚   β”‚       β”œβ”€β”€ lint/              # Error linting
β”‚   β”‚       β”œβ”€β”€ syntax-highlight/  # Syntax highlighting
β”‚   β”‚       └── tooltip/           # Hover tooltips
β”‚   └── src/wasm/             # WASM integration
β”œβ”€β”€ .github/workflows/    # CI/CD pipelines
└── .devcontainer/        # Development container configuration

πŸ”§ Development

Prerequisites

  • Node.js 18+ and npm
  • Go 1.24.6+
  • Docker (for ANTLR code generation)
  • Angular CLI (npm install -g @angular/cli)

Setup

  1. Clone the repository:

    git clone <repository-url>
    cd antlr-editor
  2. Generate ANTLR parser code:

    docker build --target antlr-generated --output=type=local,dest=analyzer/gen/parser -f analyzer/Dockerfile .
  3. Install editor dependencies:

    cd editor-app
    npm install
  4. Build WASM module:

    npm run build:wasm

Development Commands

Web Editor

cd editor-app
npm start          # Start development server
npm run build      # Build production bundle
npm run test       # Run unit tests
npm run lint       # Run linter
npm run format     # Format code

Analyzer

cd analyzer
./codegen.sh       # Generate ANTLR parser
go test ./...      # Run tests
go build ./...     # Build all targets
golangci-lint run  # Run linter

Testing Expressions

Try these sample expressions in the editor:

// Arithmetic
2 + 3 * 4
(10 + 5) / 3

// Comparisons and logic
age >= 18 && status == 'active'
[price] * [quantity] > 100

// Functions
MAX(a, b, c)
ROUND([value], 2)

// Complex expressions
([salary] + [bonus]) * 0.8 > MIN_THRESHOLD && [department] == 'Engineering'

πŸ“š Documentation

πŸ”„ CI/CD

The project includes GitHub Actions workflows for:

  • Analyzer CI: Go testing, linting, and building
  • Editor App CI: Angular testing, building, and deployment
  • WASM Build: Automated WASM generation and optimization
  • Python FFI: Testing and packaging Python bindings

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

PoC of syntax highlighting editor against custom expression language defined by ANTLR

Resources

License

Stars

Watchers

Forks

Contributors