Skip to content

Commit bbf4cce

Browse files
committed
docs: Show complete semantic actions for custom DSL types
The Domain-Specific Type Systems section now shows: - Full semantic action blocks with "define" commands - How custom types map to underlying TypedAST types (f64, i64) - Example var_decl rule using the custom types - Sample DSL code demonstrating the types in action
1 parent 2881f58 commit bbf4cce

1 file changed

Lines changed: 43 additions & 8 deletions

File tree

book/15-building-dsls.md

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -546,18 +546,53 @@ The key insight: `zrtl_plugin!` **defines** what symbols a plugin exports. The r
546546

547547
### Domain-Specific Type Systems
548548

549-
Your DSL can have its own type system that maps to Zyntax's TypedAST:
549+
Your DSL can have its own type system that maps to Zyntax's TypedAST types:
550550

551551
```zyn
552-
// Define custom types in your grammar
552+
// Define custom types that map to underlying TypedAST types
553553
type_annotation = { ":" ~ custom_type }
554+
-> Type {
555+
"get_child": { "index": 0 }
556+
}
554557
555-
custom_type = {
556-
"Currency" | // Maps to f64 with formatting
557-
"Percentage" | // Maps to f64 with 0-100 range
558-
"Date" | // Maps to i64 timestamp
559-
"Duration" // Maps to i64 milliseconds
560-
}
558+
custom_type = { "Currency" | "Percentage" | "Date" | "Duration" }
559+
-> Type {
560+
"commands": [
561+
{ "define": "type_alias", "args": {
562+
"name": { "get_text": true },
563+
"underlying": {
564+
// Map DSL types to TypedAST primitive types
565+
"match": {
566+
"Currency": "f64",
567+
"Percentage": "f64",
568+
"Date": "i64",
569+
"Duration": "i64"
570+
}
571+
}
572+
}}
573+
]
574+
}
575+
576+
// Variable declaration with custom type
577+
var_decl = { "let" ~ identifier ~ type_annotation ~ "=" ~ expr }
578+
-> TypedStatement {
579+
"commands": [
580+
{ "define": "var_decl", "args": {
581+
"name": "$1",
582+
"type": "$2",
583+
"value": "$3"
584+
}}
585+
]
586+
}
587+
```
588+
589+
This allows DSL code like:
590+
591+
```text
592+
let price: Currency = 99.99
593+
let discount: Percentage = 15
594+
let created: Date = now()
595+
let timeout: Duration = 5000
561596
```
562597

563598
### Compile-Time Validation

0 commit comments

Comments
 (0)