Skip to content

Commit 9537dc9

Browse files
committed
feat: Add type expressions, break/continue, and try to zig.zyn
Extended Zig grammar with: - Type expressions: pointer (*T, *const T), optional (?T), error union (!T), array ([N]T, []T) - Control flow: break and continue statements - Error handling: try expressions All features use JSON command format compatible with zpeg runtime.
1 parent 48d2af7 commit 9537dc9

1 file changed

Lines changed: 59 additions & 3 deletions

File tree

crates/zyn_peg/grammars/zig.zyn

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,43 @@ var_decl_untyped = { "var" ~ identifier ~ "=" ~ expr ~ ";" }
185185

186186
// ===== Type Expressions =====
187187

188-
type_expr = { primitive_type | identifier }
188+
type_expr = { pointer_type | optional_type | error_union_type | array_type | primitive_type | identifier }
189189
-> Type {
190190
"get_child": { "index": 0 }
191191
}
192192

193+
// Pointer type: *T or *const T
194+
pointer_type = { "*" ~ "const"? ~ type_expr }
195+
-> Type {
196+
"commands": [
197+
{ "define": "pointer_type", "args": { "pointee": "$1" } }
198+
]
199+
}
200+
201+
// Optional type: ?T
202+
optional_type = { "?" ~ type_expr }
203+
-> Type {
204+
"commands": [
205+
{ "define": "optional_type", "args": { "inner": "$1" } }
206+
]
207+
}
208+
209+
// Error union type: !T or E!T
210+
error_union_type = { "!" ~ type_expr }
211+
-> Type {
212+
"commands": [
213+
{ "define": "error_union_type", "args": { "payload": "$1" } }
214+
]
215+
}
216+
217+
// Array type: [N]T or []T (slice)
218+
array_type = { "[" ~ integer_literal? ~ "]" ~ type_expr }
219+
-> Type {
220+
"commands": [
221+
{ "define": "array_type", "args": { "size": "$1", "element": "$2" } }
222+
]
223+
}
224+
193225
primitive_type = { "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "f32" | "f64" | "bool" | "void" }
194226
-> Type {
195227
"get_text": true,
@@ -199,11 +231,27 @@ primitive_type = { "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" |
199231

200232
// ===== Statements =====
201233

202-
statement = { if_stmt | while_stmt | for_stmt | return_stmt | local_const | local_var | assign_stmt | expr_stmt }
234+
statement = { if_stmt | while_stmt | for_stmt | return_stmt | break_stmt | continue_stmt | local_const | local_var | assign_stmt | expr_stmt }
203235
-> TypedStatement {
204236
"get_child": { "index": 0 }
205237
}
206238

239+
// Break statement
240+
break_stmt = { "break" ~ ";" }
241+
-> TypedStatement {
242+
"commands": [
243+
{ "define": "break" }
244+
]
245+
}
246+
247+
// Continue statement
248+
continue_stmt = { "continue" ~ ";" }
249+
-> TypedStatement {
250+
"commands": [
251+
{ "define": "continue" }
252+
]
253+
}
254+
207255
// If statement variants
208256
if_stmt = { if_else | if_only }
209257
-> TypedStatement { "get_child": { "index": 0 } }
@@ -428,11 +476,19 @@ call_args = { expr ~ ("," ~ expr)* }
428476
}
429477

430478
// Atom: the base of postfix expressions
431-
atom = { array_literal | bool_literal | string_literal | integer_literal | identifier_expr | paren_expr }
479+
atom = { try_expr | array_literal | bool_literal | string_literal | integer_literal | identifier_expr | paren_expr }
432480
-> TypedExpression {
433481
"get_child": { "index": 0 }
434482
}
435483

484+
// Try expression: try expr
485+
try_expr = { "try" ~ primary }
486+
-> TypedExpression {
487+
"commands": [
488+
{ "define": "try", "args": { "expr": "$1" } }
489+
]
490+
}
491+
436492
paren_expr = _{ "(" ~ expr ~ ")" }
437493

438494
// Array literal: [a, b, c]

0 commit comments

Comments
 (0)