futilehdl/src/parser/module.rs

71 lines
1.4 KiB
Rust
Raw Normal View History

2022-02-02 23:55:40 +00:00
use nom::{combinator::map, multi::separated_list0};
2022-01-16 21:06:52 +00:00
use crate::parser::{
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-02 23:55:40 +00:00
Assign, IResult, NetDecl, Span,
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> {
2022-01-23 23:10:09 +00:00
pub name: Span<'a>,
2022-01-16 21:06:52 +00:00
pub ports: Vec<PortDecl<'a>>,
pub items: Vec<ModuleItem<'a>>,
}
#[derive(Debug)]
pub enum ModuleItem<'a> {
Assign(Assign<'a>),
Proc(ProcBlock<'a>),
}
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-02 23:55:40 +00:00
pub fn module(_input: TokenSpan) -> IResult<TokenSpan, Module> {
2022-02-02 23:54:50 +00:00
todo!();
2022-01-16 21:06:52 +00:00
}
2022-01-17 14:54:16 +00:00
#[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();
}
}