Generating body

This commit is contained in:
2026-06-06 23:50:40 +02:00
parent 7229cfd9f5
commit 330beaea75
5 changed files with 82 additions and 10 deletions
+6 -4
View File
@@ -3,10 +3,12 @@ use serde::Serialize;
use super::*;
#[derive(Debug, Clone, Serialize, Default)]
pub struct Block(
pub Vec<Spanned<Expression>>,
pub Option<Spanned<Expression>>,
);
pub struct Block {
pub is_unsafe: bool,
pub statements: Vec<Spanned<Expression>>,
pub soft_return: Option<Spanned<Expression>>,
}
#[derive(Debug, Clone, Serialize)]
pub enum StatementBody {
+3 -1
View File
@@ -63,6 +63,8 @@ static_path = {
identifier ~ ("::" ~ identifier)*
}
unsafe_kw = { "unsafe" }
// ======================================================
// PROGRAM
// ======================================================
@@ -340,7 +342,7 @@ top_level = {
// ======================================================
block = {
"{" ~ statement_list ~ expr? ~ "}"
unsafe_kw? ~ "{" ~ statement_list ~ expr? ~ "}"
}
statement_list = {
+7 -2
View File
@@ -2,7 +2,8 @@ use crate::{
Rule,
ast::*,
ast_ensure, ast_expr,
error::{AstError, IntoErr, collect_recovered},
error::{AstError, AstResult, IntoErr, collect_recovered},
parser::listen_rule,
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
@@ -12,7 +13,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
let mut inner = pair.clone().into_inner();
ast_ensure!(pair, Rule::block => {
ast_expr!(Block(collect_recovered(inner.next().unwrap().into_inner()), inner.next().map(Spanned::try_from).transpose()))
ast_expr!(Block {
is_unsafe: Ok(listen_rule(&mut inner, Rule::unsafe_kw)) as AstResult<bool>,
statements: collect_recovered(inner.next().unwrap().into_inner()),
soft_return: inner.next().map(Spanned::try_from).transpose(),
})
})
}
}