Skip to content

LLM-native codebase grammar system - Transform natural language patterns into enforceable code conventions. 75x token reduction through metadata-driven navigation and self-auditing mechanisms.

Notifications You must be signed in to change notification settings

prompt-stack/grammar-ops

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Grammar-Ops: Context-Aware Code Convention Enforcement

A smart, framework-aware tool for enforcing naming conventions in Python and TypeScript/JavaScript codebases. Unlike traditional linters, Grammar-Ops understands your code's context and respects framework idioms.

πŸš€ Quick Start

# Install grammar-ops cd grammar-ops export PATH="$PWD/bin:$PATH" # Analyze your project grammar-ops analyze /path/to/project # Learn from existing code grammar-ops learn /path/to/project -o my-conventions.json # Migrate code interactively grammar-ops migrate /path/to/project --dry-run

πŸ“ Project Structure

grammar-ops/ β”œβ”€β”€ bin/ # Executable scripts β”‚ └── grammar-ops # Main CLI entry point β”‚ β”œβ”€β”€ lib/ # Core library modules β”‚ β”œβ”€β”€ core/ # Core functionality β”‚ β”‚ └── framework_detector.py β”‚ β”œβ”€β”€ analyzers/ # Code analyzers β”‚ β”‚ β”œβ”€β”€ context_analyzer.py β”‚ β”‚ └── constant_detector.py β”‚ └── reporters/ # Output formatters β”‚ └── enhanced_reporter.py β”‚ β”œβ”€β”€ tools/ # Standalone tools β”‚ β”œβ”€β”€ learn.py # Learn patterns from code β”‚ └── migrate.py # Migrate code to conventions β”‚ β”œβ”€β”€ scripts/ # Original audit scripts β”‚ β”œβ”€β”€ audit-*.sh # Shell-based auditors β”‚ └── *.js/*.py # Language-specific scripts β”‚ β”œβ”€β”€ config/ # Configuration files β”‚ β”œβ”€β”€ schema-v2.json # Enhanced config schema β”‚ └── *.json # Other schemas β”‚ β”œβ”€β”€ examples/ # Example configurations β”‚ β”œβ”€β”€ sample-configs/ # Config examples β”‚ └── sample-projects/ # Demo projects β”‚ β”œβ”€β”€ docs/ # Documentation β”‚ β”œβ”€β”€ ENHANCED_FEATURES.md β”‚ └── *.md # Other docs β”‚ └── templates/ # Code templates 

🎯 Key Features

1. Framework Detection

Automatically detects and respects framework conventions:

  • Python: FastAPI, Django, Flask, Typer, Click, Pytest
  • JavaScript: React, Vue, Angular, Next.js (coming soon)

2. Context-Aware Analysis

Understands function context before applying rules:

  • CLI commands can use Rails-style naming (start, stop)
  • Test functions already have test_ prefix
  • Response factories follow constructor patterns
  • Fixtures are nouns, not actions

3. Smart Constant Detection

Distinguishes between:

  • True constants (MAX_RETRIES = 3)
  • Singleton instances (app = FastAPI())
  • TypeVar declarations (T = TypeVar('T'))
  • Logger instances (logger = logging.getLogger())

4. Learning Mode

Learn from your existing codebase:

grammar-ops learn . -o learned.json

5. Interactive Migration

Safely migrate with preview and rollback:

grammar-ops migrate . --dry-run # Preview grammar-ops migrate . # Apply grammar-ops migrate . --rollback # Undo

πŸ“‹ Commands

analyze - Check for violations

grammar-ops analyze [path] [options] Options: -p, --pattern File pattern (default: **/*.py) -v, --verbose Show detailed issues -m, --max-issues Maximum issues to show --no-color Disable colored output

learn - Learn from existing code

grammar-ops learn [path] [options] Options: -o, --output Output config file -r, --report Show detailed report

migrate - Fix violations interactively

grammar-ops migrate [path] [options] Options: -c, --config Config file to use -p, --pattern File pattern -d, --dry-run Preview changes --rollback Undo previous migration

detect - Detect frameworks

grammar-ops detect [path]

πŸ“ Configuration

Basic Configuration

Create .grammarops.config.json in your project:

{ "project": { "type": "fullstack", "language": "python" }, "frameworks": { "auto_detect": true }, "rules": { "python": { "functions": { "require_verb_prefix": { "enabled": true, "exceptions": { "cli_commands": "rails_style" } } } } } }

Learning from Your Code

Let Grammar-Ops learn your patterns:

# Learn patterns grammar-ops learn . -o .grammarops.config.json # Review and adjust cat .grammarops.config.json # Use it grammar-ops analyze .

πŸ”§ Original Scripts

The scripts/ directory contains the original grammar-ops scripts:

  • Metadata Management: add-*.sh scripts
  • Auditing: audit-*.sh scripts
  • Component Generation: generate-component.js
  • Validation: validate-*.js scripts

These scripts work independently and can be used directly:

./scripts/audit-python-naming.sh ./scripts/add-python-metadata.sh

🎨 Examples

CLI Command (Rails-style OK)

@cli.command() def start(): # βœ“ Grammar-Ops understands this """Start the service"""

Test Function (Already has prefix)

def test_user_can_login(): # βœ“ No "verb prefix" warning assert user.login()

Singleton Instance (Lowercase OK)

app = FastAPI() # βœ“ Not flagged as "should be UPPER_CASE" router = APIRouter() # βœ“ Framework pattern recognized

TypeVar (Special naming)

T = TypeVar('T') # βœ“ Typing convention understood UserT = TypeVar('UserT', bound=User) # βœ“ Also OK

🀝 Contributing

  1. Framework patterns not recognized? Add to lib/core/framework_detector.py
  2. New context needed? Update lib/analyzers/context_analyzer.py
  3. Better error messages? Enhance lib/reporters/enhanced_reporter.py

πŸ“š Learn More

πŸ”„ Migration Path

  1. Analyze Current State

    grammar-ops analyze . --verbose
  2. Learn Your Patterns

    grammar-ops learn . -o my-patterns.json
  3. Configure Exceptions

    cp my-patterns.json .grammarops.config.json # Edit to adjust rules
  4. Gradual Migration

    # Start with new files only # Then expand to existing code grammar-ops migrate . --dry-run
  5. Integrate with CI/CD

    - name: Check Grammar run: grammar-ops analyze .

🎯 Philosophy

Grammar-Ops believes that:

  • Context matters: A CLI command doesn't need execute_ prefix
  • Frameworks have idioms: app = FastAPI() is correct, not APP
  • Gradual adoption works: Don't break working code
  • Developers know best: Learn from existing patterns

Start using Grammar-Ops today for smarter, context-aware code conventions!

About

LLM-native codebase grammar system - Transform natural language patterns into enforceable code conventions. 75x token reduction through metadata-driven navigation and self-auditing mechanisms.

Topics

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published