futilehdl/src/parser/module.rs

60 lines
1.2 KiB
Rust
Raw Normal View History

2022-02-03 00:55:12 +00:00
use nom::{
branch::alt,
combinator::map,
multi::{many1, separated_list0},
};
2022-01-16 21:06:52 +00:00
use crate::parser::{
2022-02-03 00:55:12 +00:00
adt::{state, StateBlock},
comb::{comb_block, CombBlock},
2022-02-02 23:55:40 +00:00
declaration,
proc::ProcBlock,
2022-02-02 00:03:03 +00:00
tokens::{token, TokenKind as tk, TokenSpan},
2022-02-03 20:49:44 +00:00
IResult, NetDecl,
2022-01-16 21:06:52 +00:00
};
#[derive(Debug)]
pub enum PortDirection {
Input,
Output,
}
#[derive(Debug)]
pub struct PortDecl<'a> {
pub direction: PortDirection,
pub net: NetDecl<'a>,
}
#[derive(Debug)]
pub struct Module<'a> {
pub items: Vec<ModuleItem<'a>>,
}
#[derive(Debug)]
pub enum ModuleItem<'a> {
2022-02-03 00:55:12 +00:00
Comb(CombBlock<'a>),
2022-01-16 21:06:52 +00:00
Proc(ProcBlock<'a>),
2022-02-03 00:55:12 +00:00
State(StateBlock<'a>),
2022-01-16 21:06:52 +00:00
}
2022-02-02 00:00:11 +00:00
fn port_decl(i: TokenSpan) -> IResult<TokenSpan, PortDecl> {
map(declaration, |net| PortDecl {
2022-02-01 18:46:06 +00:00
direction: PortDirection::Input,
net,
})(i)
2022-01-16 21:06:52 +00:00
}
2022-02-02 23:54:50 +00:00
pub fn inputs_list(input: TokenSpan) -> IResult<TokenSpan, Vec<PortDecl>> {
2022-02-02 00:00:11 +00:00
separated_list0(token(tk::Comma), port_decl)(input)
2022-01-16 21:06:52 +00:00
}
2022-02-03 00:55:12 +00:00
pub fn module(input: TokenSpan) -> IResult<TokenSpan, Module> {
map(
many1(alt((
map(state, ModuleItem::State),
map(comb_block, ModuleItem::Comb),
))),
|items| Module { items },
)(input)
2022-01-16 21:06:52 +00:00
}