futilehdl/src/parser/module.rs

121 lines
2.7 KiB
Rust

use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{char, multispace1},
combinator::{consumed, map},
error::context,
multi::{many0, separated_list0},
sequence::{delimited, terminated, tuple, preceded},
};
use crate::parser::{
assign_statement, declaration, identifier,
proc::{proc_block, ProcBlock},
ws0, Assign, IResult, NetDecl, Span, typename
};
#[derive(Debug)]
pub enum PortDirection {
Input,
Output,
}
#[derive(Debug)]
pub struct PortDecl<'a> {
pub pos: Span<'a>,
pub direction: PortDirection,
pub net: NetDecl<'a>,
}
#[derive(Debug)]
pub struct Module<'a> {
pub name: Span<'a>,
pub ports: Vec<PortDecl<'a>>,
pub items: Vec<ModuleItem<'a>>,
}
#[derive(Debug)]
pub enum ModuleItem<'a> {
Assign(Assign<'a>),
Proc(ProcBlock<'a>),
}
fn port_decl(i: Span) -> IResult<Span, PortDecl> {
map(
consumed(
declaration,
),
|(pos, net)| PortDecl {
pos,
direction: PortDirection::Input,
net,
},
)(i)
}
fn inputs_list(input: Span) -> IResult<Span, Vec<PortDecl>> {
separated_list0(ws0(char(',')), ws0(port_decl))(input)
}
fn assign_item(input: Span) -> IResult<Span, Assign> {
context(
"assignment",
delimited(
ws0(terminated(tag("assign"), multispace1)),
ws0(assign_statement),
ws0(char(';')),
),
)(input)
}
fn module_item(input: Span) -> IResult<Span, ModuleItem> {
alt((
map(assign_item, ModuleItem::Assign),
map(proc_block, ModuleItem::Proc),
))(input)
}
/// parse a top-level module declaration
pub fn module(input: Span) -> IResult<Span, Module> {
context(
"module",
map(
tuple((
tag("module"),
ws0(identifier),
ws0(delimited(char('('), ws0(inputs_list), char(')'))),
ws0(preceded(tag("->"), ws0(typename))),
ws0(delimited(char('{'), ws0(many0(ws0(module_item))), char('}'))),
)),
|(_, name, inputs, ret, items)| Module {
name,
// TODO: add back in returns
ports: inputs,
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();
}
}