prepare for removing module syntax

This commit is contained in:
NotAFile 2022-02-03 00:54:50 +01:00
parent dfae432055
commit aadf044f9d
5 changed files with 78 additions and 36 deletions

30
src/parser/adt.rs Normal file
View File

@ -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<NetDecl<'a>>,
}
pub fn state(input: TokenSpan) -> IResult<TokenSpan, StateBlock> {
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)
}

37
src/parser/comb.rs Normal file
View File

@ -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<TokenSpan, Module> {
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)
}

View File

@ -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>;

View File

@ -45,46 +45,12 @@ fn port_decl(i: TokenSpan) -> IResult<TokenSpan, PortDecl> {
})(i)
}
fn inputs_list(input: TokenSpan) -> IResult<TokenSpan, Vec<PortDecl>> {
pub fn inputs_list(input: TokenSpan) -> IResult<TokenSpan, Vec<PortDecl>> {
separated_list0(token(tk::Comma), port_decl)(input)
}
fn assign_item(input: TokenSpan) -> IResult<TokenSpan, Assign> {
context(
"assignment",
delimited(
token(tk::Assign),
cut(assign_statement),
cut(token(tk::Semicolon)),
),
)(input)
}
fn module_item(input: TokenSpan) -> IResult<TokenSpan, ModuleItem> {
alt((
map(assign_item, ModuleItem::Assign),
map(proc_block, ModuleItem::Proc),
))(input)
}
pub fn module(input: TokenSpan) -> IResult<TokenSpan, Module> {
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)]

View File

@ -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<Span, Token> {
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)