From aadf044f9d4cd9de68129ba657311cc3879513db Mon Sep 17 00:00:00 2001 From: NotAFile Date: Thu, 3 Feb 2022 00:54:50 +0100 Subject: [PATCH] prepare for removing module syntax --- src/parser/adt.rs | 30 ++++++++++++++++++++++++++++++ src/parser/comb.rs | 37 +++++++++++++++++++++++++++++++++++++ src/parser/mod.rs | 4 ++++ src/parser/module.rs | 38 ++------------------------------------ src/parser/tokens.rs | 5 +++++ 5 files changed, 78 insertions(+), 36 deletions(-) create mode 100644 src/parser/adt.rs create mode 100644 src/parser/comb.rs diff --git a/src/parser/adt.rs b/src/parser/adt.rs new file mode 100644 index 0000000..5b5abaf --- /dev/null +++ b/src/parser/adt.rs @@ -0,0 +1,30 @@ +use super::declaration::{declaration, NetDecl}; +use super::tokens::{token, Token, TokenKind as tk, TokenSpan}; +use super::IResult; +use nom::multi::many0; +use nom::sequence::tuple; +use nom::{ + combinator::{cut, map}, + sequence::{delimited, preceded}, +}; + +pub struct StateBlock<'a> { + name: Token<'a>, + items: Vec>, +} + +pub fn state(input: TokenSpan) -> IResult { + map( + preceded( + token(tk::State), + cut(tuple(( + token(tk::Ident), + delimited(token(tk::LBrace), many0(declaration), token(tk::RBrace)), + ))), + ), + |(name, items)| StateBlock { + name: name.clone(), + items, + }, + )(input) +} diff --git a/src/parser/comb.rs b/src/parser/comb.rs new file mode 100644 index 0000000..6fa5a39 --- /dev/null +++ b/src/parser/comb.rs @@ -0,0 +1,37 @@ +use super::module::inputs_list; +use super::tokens::TokenKind as tk; +use crate::parser::tokens::token; +use crate::parser::typename; +use crate::parser::IResult; +use crate::parser::Module; +use crate::parser::TokenSpan; +use nom::combinator::cut; +use nom::combinator::map; +use nom::multi::many0; +use nom::sequence::delimited; +use nom::sequence::preceded; +use nom::sequence::tuple; + +pub fn comb_block(input: TokenSpan) -> IResult { + map( + preceded( + token(tk::Comb), + cut(tuple(( + token(tk::Ident), + delimited(token(tk::LParen), inputs_list, token(tk::RParen)), + preceded(token(tk::RArrow), typename), + delimited( + token(tk::LBrace), + many0(token(tk::Error)), + token(tk::RBrace), + ), + ))), + ), + |(name, inputs, _ret, items)| Module { + // TODO: bring back returns + name: name.span(), + ports: inputs, + items: todo!(), + }, + )(input) +} diff --git a/src/parser/mod.rs b/src/parser/mod.rs index a29cd47..a7f675b 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,3 +1,5 @@ +pub mod adt; +pub mod comb; pub mod declaration; pub mod error; pub mod expression; @@ -8,6 +10,8 @@ pub mod tokens; use nom_locate::LocatedSpan; +use nom::branch::alt; + // custom span type for nom_locate pub type Span<'a> = LocatedSpan<&'a str>; diff --git a/src/parser/module.rs b/src/parser/module.rs index 7d11e0a..e9d5e69 100644 --- a/src/parser/module.rs +++ b/src/parser/module.rs @@ -45,46 +45,12 @@ fn port_decl(i: TokenSpan) -> IResult { })(i) } -fn inputs_list(input: TokenSpan) -> IResult> { +pub fn inputs_list(input: TokenSpan) -> IResult> { separated_list0(token(tk::Comma), port_decl)(input) } -fn assign_item(input: TokenSpan) -> IResult { - context( - "assignment", - delimited( - token(tk::Assign), - cut(assign_statement), - cut(token(tk::Semicolon)), - ), - )(input) -} - -fn module_item(input: TokenSpan) -> IResult { - alt(( - map(assign_item, ModuleItem::Assign), - map(proc_block, ModuleItem::Proc), - ))(input) -} - pub fn module(input: TokenSpan) -> IResult { - map( - preceded( - token(tk::Module), - cut(tuple(( - token(tk::Ident), - delimited(token(tk::LParen), inputs_list, token(tk::RParen)), - preceded(token(tk::RArrow), typename), - delimited(token(tk::LBrace), many0(module_item), token(tk::RBrace)), - ))), - ), - |(name, inputs, _ret, items)| Module { - // TODO: bring back returns - name: name.span(), - ports: inputs, - items, - }, - )(input) + todo!(); } #[cfg(test)] diff --git a/src/parser/tokens.rs b/src/parser/tokens.rs index 11daa64..2cc4427 100644 --- a/src/parser/tokens.rs +++ b/src/parser/tokens.rs @@ -14,6 +14,7 @@ use nom::{ }; use std::fmt; +#[derive(Clone)] pub struct Token<'a> { span: Span<'a>, kind: TokenKind, @@ -77,7 +78,9 @@ pub enum TokenKind { Module, Assign, Match, + State, Proc, + Comb, // Error Error, } @@ -206,6 +209,8 @@ fn lex_keywords(input: Span) -> IResult { map(tag("assign"), |_| TokenKind::Assign), map(tag("match"), |_| TokenKind::Match), map(tag("proc"), |_| TokenKind::Proc), + map(tag("comb"), |_| TokenKind::Comb), + map(tag("state"), |_| TokenKind::Proc), ))), |(span, kind)| Token::new(span, kind), )(input)