82 lines
1.7 KiB
Rust
82 lines
1.7 KiB
Rust
use nom::{
|
|
branch::alt,
|
|
combinator::map,
|
|
multi::{many1, separated_list0},
|
|
};
|
|
|
|
use crate::parser::{
|
|
adt::{state, StateBlock},
|
|
comb::{comb_block, CombBlock},
|
|
declaration,
|
|
proc::ProcBlock,
|
|
tokens::{token, TokenKind as tk, TokenSpan},
|
|
IResult, NetDecl, Span,
|
|
};
|
|
|
|
#[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> {
|
|
Comb(CombBlock<'a>),
|
|
Proc(ProcBlock<'a>),
|
|
State(StateBlock<'a>),
|
|
}
|
|
|
|
fn port_decl(i: TokenSpan) -> IResult<TokenSpan, PortDecl> {
|
|
map(declaration, |net| PortDecl {
|
|
direction: PortDirection::Input,
|
|
net,
|
|
})(i)
|
|
}
|
|
|
|
pub fn inputs_list(input: TokenSpan) -> IResult<TokenSpan, Vec<PortDecl>> {
|
|
separated_list0(token(tk::Comma), port_decl)(input)
|
|
}
|
|
|
|
pub fn module(input: TokenSpan) -> IResult<TokenSpan, Module> {
|
|
map(
|
|
many1(alt((
|
|
map(state, ModuleItem::State),
|
|
map(comb_block, ModuleItem::Comb),
|
|
))),
|
|
|items| Module { items },
|
|
)(input)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use nom::combinator::all_consuming;
|
|
|
|
#[test]
|
|
fn test_decl() {
|
|
declaration("reg abcd".into()).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn test_assignment_item() {
|
|
all_consuming(assign_item)(" assign a = b ; ".into()).unwrap();
|
|
all_consuming(assign_item)(" assign a = b | c ; ".into()).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn test_module_item() {
|
|
all_consuming(module_item)(" assign a = b ;".into()).unwrap();
|
|
}
|
|
}
|