From ab931de08f9421d1852aa37ca29f6c95749d4bd0 Mon Sep 17 00:00:00 2001 From: Alain Zscheile Date: Fri, 3 Nov 2023 15:28:03 +0100 Subject: [PATCH] rust: 'official' yanais record and pattern syntax --- rust/crates/yanais-syntax/src/lib.rs | 5 ++-- .../src/pat.rs | 26 ++++++++++++------- rust/crates/yanais-syntax/src/record.rs | 2 +- rust/crates/yn-mrwy57u-core/src/lib.rs | 4 +-- 4 files changed, 21 insertions(+), 16 deletions(-) rename rust/crates/{yn-mrwy57u-core => yanais-syntax}/src/pat.rs (57%) diff --git a/rust/crates/yanais-syntax/src/lib.rs b/rust/crates/yanais-syntax/src/lib.rs index b6cd689..3457bac 100644 --- a/rust/crates/yanais-syntax/src/lib.rs +++ b/rust/crates/yanais-syntax/src/lib.rs @@ -9,6 +9,7 @@ use miette::{Diagnostic, SourceSpan}; use std::sync::Arc; pub mod lex; +pub mod pat; pub mod record; #[macro_export] @@ -251,8 +252,8 @@ impl<'a, Kw> Env<'a, Kw> { } } -pub trait Keywords: Sized + core::str::FromStr {} -impl Keywords for T {} +pub trait Keywords: Sized + core::cmp::PartialEq + core::str::FromStr {} +impl Keywords for T {} pub trait Parse: Sized { fn parse(env: &mut Env<'_, Kw>) -> Result; diff --git a/rust/crates/yn-mrwy57u-core/src/pat.rs b/rust/crates/yanais-syntax/src/pat.rs similarity index 57% rename from rust/crates/yn-mrwy57u-core/src/pat.rs rename to rust/crates/yanais-syntax/src/pat.rs index 2fa0f80..9d3963d 100644 --- a/rust/crates/yn-mrwy57u-core/src/pat.rs +++ b/rust/crates/yanais-syntax/src/pat.rs @@ -4,27 +4,31 @@ * SPDX-License-Identifier: Apache-2.0 */ -use crate::parser::{lex, Env as ParseEnv, Error, ErrorCtx, MaybeParse, ParseDefaultCtx}; +use crate::{ + lex, record::Record, Env as ParseEnv, Error, ErrorCtx, EvEqSourceSpan, Keywords, MaybeParse, + Parse, ParseDefaultCtx, +}; use std::sync::Arc; -use yanais_syntax::EvEqSourceSpan; // infallible patterns #[derive(Clone, Debug, PartialEq)] pub enum Pattern { + Ignore(EvEqSourceSpan), Name(EvEqSourceSpan, Arc), - // Record(Record), + Record(Record), } impl Pattern { pub fn alloc_slots(&self) -> usize { match self { + Pattern::Ignore(_) => 0, Pattern::Name(_, x) => { - if x.is_empty() { - 0 - } else { - 1 - } + assert!(!x.is_empty()); + 1 + } + Pattern::Record(rcd) => { + rcd.fields.iter().map(|(_, i)| i.alloc_slots()).sum() } } } @@ -34,8 +38,8 @@ impl ParseDefaultCtx for Pattern { const DFL_CTX: ErrorCtx = ErrorCtx::Pattern; } -impl MaybeParse for Pattern { - fn maybe_parse(env: &mut ParseEnv<'_>) -> Result, Error> { +impl MaybeParse for Pattern { + fn maybe_parse(env: &mut ParseEnv<'_, Kw>) -> Result, Error> { let mut nxtlxr = env.lxr.clone(); let lex::Token { kind, @@ -47,7 +51,9 @@ impl MaybeParse for Pattern { use lex::TokenKind as Tk; let ret = match kind { + Tk::PatOut(nam) if nam.is_empty() => Pattern::Ignore(tok_span), Tk::PatOut(nam) => Pattern::Name(tok_span, nam), + Tk::LBrace => return Record::parse(env).map(|i| Some(Pattern::Record(i))), _ => return Ok(None), }; env.lxr = nxtlxr; diff --git a/rust/crates/yanais-syntax/src/record.rs b/rust/crates/yanais-syntax/src/record.rs index 03b799f..7af81bf 100644 --- a/rust/crates/yanais-syntax/src/record.rs +++ b/rust/crates/yanais-syntax/src/record.rs @@ -13,7 +13,7 @@ use crate::{ Keywords, MaybeParse, Parse, ParseDefaultCtx, Result as Pres, }; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub struct Record { pub span: EvEqSourceSpan, pub fields: Vec<(Option>, V)>, diff --git a/rust/crates/yn-mrwy57u-core/src/lib.rs b/rust/crates/yn-mrwy57u-core/src/lib.rs index 4d38c33..be3cd23 100644 --- a/rust/crates/yn-mrwy57u-core/src/lib.rs +++ b/rust/crates/yn-mrwy57u-core/src/lib.rs @@ -58,12 +58,10 @@ pub mod parser { use parser::lex::Kw; mod expr; -mod pat; mod record; pub use expr::Expr; -pub use pat::Pattern; -pub use yanais_syntax::{record::Record, EvEqSourceSpan}; +pub use yanais_syntax::{pat::Pattern, record::Record, EvEqSourceSpan}; #[derive(Clone, Debug)] pub struct Lambda {