Skip to main content

Parser Implementation

The PSL parser transforms Prisma schema text into a validated Abstract Syntax Tree (AST). This page covers the schema-ast crate and the parsing pipeline.

Schema AST Crate

The schema-ast crate provides the foundation for all PSL operations:

Core Components

Parser

Built with the Pest parser generator:
The parser is defined using PEG (Parsing Expression Grammar) in datamodel.pest.

Source Files

Wraps schema content with file information:
SourceFile can represent content from files, strings, or stdin. It’s designed to be lightweight and cloneable.

Main API

The primary parsing function:
Key characteristics:
  • Never fails - Always returns an AST, even with errors
  • Accumulates diagnostics - Errors added to the diagnostics collection
  • Source spans - Every AST node tracks its location

AST Structure

The AST faithfully represents Prisma Schema syntax with complete source span information.

Top-Level Items

Models

Fields

Attributes

Expressions

Schema expressions represent values:

Parsing Modules

The parser is organized into focused modules:
  • parse_schema - Top-level schema parsing
  • parse_model - Model blocks
  • parse_view - View blocks (read-only models)
  • parse_field - Field definitions
  • parse_enum - Enum types
  • parse_composite_type - Composite/embedded types
  • parse_attribute - Attributes (@ and @@)
  • parse_arguments - Attribute arguments
  • parse_expression - Value expressions
  • parse_types - Type references
  • parse_source_and_generator - Configuration blocks
  • parse_comments - Comment preservation
Each parsing module follows a consistent pattern: convert Pest pairs into AST nodes while tracking spans and reporting errors.

Error Recovery

The parser is designed for robust error recovery:
  1. Continue on errors - Parse as much as possible
  2. Collect diagnostics - Don’t stop at first error
  3. Meaningful spans - Precise error locations
  4. IDE-friendly - Supports incremental parsing

Example Error Handling

Multi-File Support

PSL supports schemas split across multiple files:
Each file is parsed independently, then merged during validation.

Reformatting

The reformat module provides code formatting:

Formatting Features

  • Consistent indentation - Configurable tab size
  • Preserves comments - Maintains comment placement
  • Idempotent - Running twice produces same result
  • Block alignment - Aligns field types and attributes

Reformat Testing

Reformatting tests are declarative:

String Literals

PSL string literals follow JSON string syntax:
Escaping rules:
  • \t - Tab
  • \n - Newline
  • \r - Carriage return
  • \\ - Backslash
  • \" - Quote
  • \uXXXX - Unicode escape

Renderer

The renderer converts AST back to schema text:
Used by:
  • The formatter
  • Code generation
  • Schema migrations
The renderer preserves semantic meaning but may not preserve exact formatting. Use reformat() for user-facing formatting.

Span Tracking

Every AST node includes span information:
Spans enable:
  • Precise error messages with line/column
  • IDE features like go-to-definition
  • Refactoring tools with exact locations
  • Syntax highlighting in editors

Parser Performance

The parser is optimized for IDE usage:
  • Fast incremental parsing
  • Minimal allocations
  • Efficient string interning in later phases
  • Streaming diagnostics
For large schemas, parsing typically takes less than 10ms. Validation is the more expensive phase.

Integration with Parser Database

The AST feeds into the parser-database for semantic analysis:
See Validation for details on the semantic analysis phase.

Testing

Parser tests use standard Rust tests:

Running Parser Tests

Common Patterns

Walking the AST

Finding Attributes

Next Steps

Validation

Learn about semantic validation with parser-database

Connectors

Explore database-specific validations