From 5fa3e77b8f5f008b28777a442bdeba76c4ce8ba7 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 17 May 2026 06:08:53 +0200 Subject: [PATCH] Added expr? syntax --- parser/src/ast/expr.rs | 1 + parser/src/grammar.pest | 5 +++++ parser/src/parser/common/expr.rs | 2 ++ src/codegen.rs | 2 ++ 4 files changed, 10 insertions(+) diff --git a/parser/src/ast/expr.rs b/parser/src/ast/expr.rs index 9680af9..ac776c5 100644 --- a/parser/src/ast/expr.rs +++ b/parser/src/ast/expr.rs @@ -71,6 +71,7 @@ pub enum Postfix { StructCall(Vec<(Identifier, Expression)>), Index(Expression), As(TypeExpr), + Try, } #[derive(Debug, Clone, Serialize)] diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 056511f..b843c39 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -500,6 +500,10 @@ as_px = { "as" ~ type_expr } +try_px = { + "?" +} + postfix = { field_px | call_px @@ -507,6 +511,7 @@ postfix = { | struct_px | index_px | as_px + | try_px } // ------------------------------------------------------ diff --git a/parser/src/parser/common/expr.rs b/parser/src/parser/common/expr.rs index 94baab8..312a406 100644 --- a/parser/src/parser/common/expr.rs +++ b/parser/src/parser/common/expr.rs @@ -183,6 +183,8 @@ impl<'a> TryFrom> for Postfix { ast_expr!(Postfix::As(inner.next().unwrap().try_into())) } + Rule::try_px => Ok(Postfix::Try), + _ => AstError::bug_unimplemented(pair), } } diff --git a/src/codegen.rs b/src/codegen.rs index 8ba285b..9b18cea 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -302,6 +302,8 @@ impl GetRust for Postfix { } Postfix::As(ty) => format!(" as {}", ty.get_rust()), + + Postfix::Try => String::from("?"), } } }