Parser Implementation
The PSL parser transforms Prisma schema text into a validated Abstract Syntax Tree (AST). This page covers theschema-ast crate and the parsing pipeline.
Schema AST Crate
Theschema-ast crate provides the foundation for all PSL operations:
Core Components
Parser
Built with the Pest parser generator: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:- 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 parsingparse_model- Model blocksparse_view- View blocks (read-only models)parse_field- Field definitionsparse_enum- Enum typesparse_composite_type- Composite/embedded typesparse_attribute- Attributes (@and@@)parse_arguments- Attribute argumentsparse_expression- Value expressionsparse_types- Type referencesparse_source_and_generator- Configuration blocksparse_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:- Continue on errors - Parse as much as possible
- Collect diagnostics - Don’t stop at first error
- Meaningful spans - Precise error locations
- IDE-friendly - Supports incremental parsing
Example Error Handling
Multi-File Support
PSL supports schemas split across multiple files:Reformatting
Thereformat 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:\t- Tab\n- Newline\r- Carriage return\\- Backslash\"- Quote\uXXXX- Unicode escape
Renderer
The renderer converts AST back to schema text:- 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:- 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: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