Skip to content

Commit 724aeca

Browse files
committed
docs: Simplify custom types example to use actual ZynPEG features
Remove non-existent "type_alias" and "match" commands. Use realistic pattern: grammar parses type names with "get_text", type mapping to primitives happens during compilation phase.
1 parent bbf4cce commit 724aeca

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

book/15-building-dsls.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -546,34 +546,35 @@ 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 types:
549+
Your DSL can parse custom type names and map them to TypedAST primitive types. The grammar handles parsing while semantic actions build the AST:
550550

551551
```zyn
552-
// Define custom types that map to underlying TypedAST types
553-
type_annotation = { ":" ~ custom_type }
552+
// Parse type annotations - dispatch to the matched child
553+
type_annotation = { ":" ~ type_expr }
554554
-> Type {
555555
"get_child": { "index": 0 }
556556
}
557557
558+
// Type expression - each alternative produces a type
559+
type_expr = { primitive_type | custom_type }
560+
-> Type {
561+
"get_child": { "index": 0 }
562+
}
563+
564+
// Standard primitive types
565+
primitive_type = { "i32" | "i64" | "f32" | "f64" | "bool" | "void" }
566+
-> Type {
567+
"get_text": true
568+
}
569+
570+
// DSL-specific type names that compile to primitives
571+
// Currency/Percentage → f64, Date/Duration → i64
558572
custom_type = { "Currency" | "Percentage" | "Date" | "Duration" }
559573
-> 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+
"get_text": true
574575
}
575576
576-
// Variable declaration with custom type
577+
// Variable declaration using type annotation
577578
var_decl = { "let" ~ identifier ~ type_annotation ~ "=" ~ expr }
578579
-> TypedStatement {
579580
"commands": [
@@ -586,7 +587,7 @@ var_decl = { "let" ~ identifier ~ type_annotation ~ "=" ~ expr }
586587
}
587588
```
588589

589-
This allows DSL code like:
590+
The type mapping (Currency → f64, Date → i64) happens during compilation. Your DSL plugin can validate and transform types as needed. This allows DSL code like:
590591

591592
```text
592593
let price: Currency = 99.99

0 commit comments

Comments
 (0)