From 3ab238851fc0acb09ced8a38102c37a6b528de8f Mon Sep 17 00:00:00 2001 From: anqurvanillapy Date: Thu, 22 Aug 2024 12:39:50 +0800 Subject: [PATCH] [frontend] cooler grammar for declarations Signed-off-by: anqurvanillapy --- reuse-frontend/src/concrete.rs | 2 ++ reuse-frontend/src/name.rs | 1 + reuse-frontend/src/surface/mod.rs | 14 +++++++++++--- reuse-frontend/src/surface/tests.rs | 15 +++++++++++---- reuse-frontend/src/syntax.rs | 7 +++++++ 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/reuse-frontend/src/concrete.rs b/reuse-frontend/src/concrete.rs index 4a9160b..827bd2c 100644 --- a/reuse-frontend/src/concrete.rs +++ b/reuse-frontend/src/concrete.rs @@ -10,11 +10,13 @@ pub type CtorExpr<'src> = Ctor<'src, Expr<'src>>; pub type CtorParamsExpr<'src> = CtorParams<'src, Expr<'src>>; #[allow(dead_code)] +#[derive(Debug)] pub struct File<'src> { pub decls: Box<[Decl<'src, Expr<'src>>]>, } #[allow(dead_code)] +#[derive(Debug)] pub enum Expr<'src> { Ident(Ident<'src>), diff --git a/reuse-frontend/src/name.rs b/reuse-frontend/src/name.rs index 9797095..6f66e96 100644 --- a/reuse-frontend/src/name.rs +++ b/reuse-frontend/src/name.rs @@ -2,6 +2,7 @@ use std::fmt::{Display, Formatter}; pub type ID = u64; +#[derive(Debug)] pub struct Ident<'src> { id: ID, raw: &'src str, diff --git a/reuse-frontend/src/surface/mod.rs b/reuse-frontend/src/surface/mod.rs index d2c5b3d..27fd32f 100644 --- a/reuse-frontend/src/surface/mod.rs +++ b/reuse-frontend/src/surface/mod.rs @@ -1,7 +1,7 @@ use chumsky::error::Rich; use chumsky::extra::Err; use chumsky::prelude::just; -use chumsky::text::{ident, inline_whitespace, newline}; +use chumsky::text::{ident, inline_whitespace, newline, whitespace}; use chumsky::{IterParser, Parser}; use crate::concrete::{CtorExpr, CtorParamsExpr, Expr, File, ParamExpr}; @@ -123,6 +123,7 @@ impl Surface { fn ctor_unnamed_params<'src>(&mut self) -> out!(CtorParamsExpr<'src>) { just('(') + .ignore_then(whitespace()) .ignore_then( self.type_expr() .padded() @@ -133,11 +134,13 @@ impl Surface { .map(Vec::into_boxed_slice) .map(CtorParamsExpr::Unnamed), ) + .then_ignore(whitespace()) .then_ignore(just(')')) } fn ctor_named_params<'src>(&mut self) -> out!(CtorParamsExpr<'src>) { just('(') + .ignore_then(whitespace()) .ignore_then( self.param() .padded() @@ -148,6 +151,7 @@ impl Surface { .map(Vec::into_boxed_slice) .map(CtorParamsExpr::Named), ) + .then_ignore(whitespace()) .then_ignore(just(')')) } @@ -167,15 +171,16 @@ impl Surface { fn param<'src>(&mut self) -> out!(ParamExpr<'src>) { self.ident() - .padded() + .then_ignore(whitespace()) .then_ignore(just(':')) - .padded() + .then_ignore(whitespace()) .then(self.type_expr()) .map(|(name, typ)| Param { name, typ }) } fn val_params<'src>(&mut self) -> out!(Box<[ParamExpr<'src>]>) { just('(') + .ignore_then(whitespace()) .ignore_then( self.param() .padded() @@ -186,11 +191,13 @@ impl Surface { .or_not() .map(Option::unwrap_or_default), ) + .then_ignore(whitespace()) .then_ignore(just(')')) } fn typ_params<'src>(&mut self) -> out!(Box<[ParamExpr<'src>]>) { just('[') + .ignore_then(whitespace()) .ignore_then( self.ident() .padded() @@ -204,6 +211,7 @@ impl Surface { .collect::>() .map(Vec::into_boxed_slice), ) + .then_ignore(whitespace()) .then_ignore(just(']')) } diff --git a/reuse-frontend/src/surface/tests.rs b/reuse-frontend/src/surface/tests.rs index a0f67d1..a533d4f 100644 --- a/reuse-frontend/src/surface/tests.rs +++ b/reuse-frontend/src/surface/tests.rs @@ -7,17 +7,24 @@ use crate::surface::Surface; fn it_parses_file() { const SRC: &str = " -def f0 () -> None : None -def f1 () -> None : None +def f0 ( ) -> None : None +def f1 ( +) -> None : None -def f2 [T, U,] (s: str) -> None : +def f2 [ + T, + U, +] (s: str) -> None : None data Foo [T]: A B(str) - + C( + s : str , + b: bool , + ) "; Surface::default() diff --git a/reuse-frontend/src/syntax.rs b/reuse-frontend/src/syntax.rs index ddb6bd1..d2ae1a1 100644 --- a/reuse-frontend/src/syntax.rs +++ b/reuse-frontend/src/syntax.rs @@ -3,24 +3,28 @@ use crate::name::Ident; pub trait Syntax {} #[allow(dead_code)] +#[derive(Debug)] pub struct Param<'src, T: Syntax> { pub name: Ident<'src>, pub typ: Box, } #[allow(dead_code)] +#[derive(Debug)] pub struct Decl<'src, T: Syntax> { pub name: Ident<'src>, pub def: Def<'src, T>, } #[allow(dead_code)] +#[derive(Debug)] pub enum Def<'src, T: Syntax> { Fn(FnDef<'src, T>), Data(DataDef<'src, T>), } #[allow(dead_code)] +#[derive(Debug)] pub struct FnDef<'src, T: Syntax> { pub typ_params: Box<[Param<'src, T>]>, pub val_params: Box<[Param<'src, T>]>, @@ -30,18 +34,21 @@ pub struct FnDef<'src, T: Syntax> { } #[allow(dead_code)] +#[derive(Debug)] pub struct DataDef<'src, T: Syntax> { pub typ_params: Box<[Param<'src, T>]>, pub ctors: Box<[Ctor<'src, T>]>, } #[allow(dead_code)] +#[derive(Debug)] pub struct Ctor<'src, T: Syntax> { pub name: Ident<'src>, pub params: CtorParams<'src, T>, } #[allow(dead_code)] +#[derive(Debug)] pub enum CtorParams<'src, T: Syntax> { None, Unnamed(Box<[Box]>),