Skip to content

Commit

Permalink
[frontend] cooler grammar for declarations
Browse files Browse the repository at this point in the history
Signed-off-by: anqurvanillapy <[email protected]>
  • Loading branch information
anqurvanillapy committed Aug 22, 2024
1 parent 0bb4ca6 commit 3ab2388
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
2 changes: 2 additions & 0 deletions reuse-frontend/src/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>),

Expand Down
1 change: 1 addition & 0 deletions reuse-frontend/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::{Display, Formatter};

pub type ID = u64;

#[derive(Debug)]
pub struct Ident<'src> {
id: ID,
raw: &'src str,
Expand Down
14 changes: 11 additions & 3 deletions reuse-frontend/src/surface/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -148,6 +151,7 @@ impl Surface {
.map(Vec::into_boxed_slice)
.map(CtorParamsExpr::Named),
)
.then_ignore(whitespace())
.then_ignore(just(')'))
}

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -204,6 +211,7 @@ impl Surface {
.collect::<Vec<_>>()
.map(Vec::into_boxed_slice),
)
.then_ignore(whitespace())
.then_ignore(just(']'))
}

Expand Down
15 changes: 11 additions & 4 deletions reuse-frontend/src/surface/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions reuse-frontend/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>,
}

#[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>]>,
Expand All @@ -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<T>]>),
Expand Down

0 comments on commit 3ab2388

Please sign in to comment.