Skip to content

Commit 5f9979d

Browse files
committed
fix: Fix struct/enum parsing by adding keyword exclusion and program rule fix
- Add keyword rule to prevent reserved words (struct, enum, fn, etc.) from being matched as identifiers - Split program rule into program + declarations for proper child collection - Fix TypedVariantFields import path This fixes the issue where struct and enum declarations were being parsed by pest but not appearing in the final TypedAST output.
1 parent 17f7bd5 commit 5f9979d

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

crates/zyn_peg/grammars/zig.zyn

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919

2020
// ===== Program Structure =====
2121

22-
program = { SOI ~ declaration* ~ EOI }
22+
program = { SOI ~ declarations ~ EOI }
23+
-> TypedProgram {
24+
"get_child": { "index": 0 }
25+
}
26+
27+
declarations = { declaration* }
2328
-> TypedProgram {
2429
"get_all_children": true,
2530
"define": "program",
@@ -541,7 +546,14 @@ escape_seq = { "\\" ~ ("n" | "r" | "t" | "\\" | "\"" | "0") }
541546

542547
// ===== Identifiers =====
543548

544-
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
549+
keyword = @{
550+
("struct" | "enum" | "fn" | "const" | "var" | "if" | "else" | "while" | "for" |
551+
"return" | "break" | "continue" | "try" | "and" | "or" | "true" | "false" |
552+
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "f32" | "f64" | "bool" | "void")
553+
~ !(ASCII_ALPHANUMERIC | "_")
554+
}
555+
556+
identifier = @{ !keyword ~ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
545557
-> String {
546558
"get_text": true
547559
}

crates/zyn_peg/src/runtime.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ use crate::ZynGrammar;
4848
pub use zyntax_typed_ast::{
4949
TypedASTBuilder, TypedProgram, TypedNode, TypedDeclaration, TypedExpression,
5050
TypedStatement, TypedBlock, BinaryOp, UnaryOp, Span, InternedString,
51-
TypedClass, TypedEnum, TypedField, TypedVariant, TypedVariantFields,
51+
TypedClass, TypedEnum, TypedField, TypedVariant,
52+
typed_ast::TypedVariantFields,
5253
type_registry::{Type, PrimitiveType, Mutability, Visibility},
5354
};
5455

0 commit comments

Comments
 (0)