futilehdl/src/parser.rs

242 lines
5.7 KiB
Rust

use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{alpha1, alphanumeric1, char, multispace0, multispace1},
combinator::{map, opt, recognize},
error::{context, ParseError},
multi::{many0, many1, separated_list0},
sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
};
use crate::literals::{decimal, hexadecimal};
use crate::IResult;
fn ws0<'a, F: 'a, O, E: ParseError<&'a str>>(
inner: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
where
F: FnMut(&'a str) -> IResult<&'a str, O, E>,
{
delimited(multispace0, inner, multispace0)
}
fn identifier(input: &str) -> IResult<&str, &str> {
recognize(pair(
alt((alpha1, tag("_"))),
many0(alt((alphanumeric1, tag("_")))),
))(input)
}
fn widthspec(input: &str) -> IResult<&str, u64> {
delimited(char('['), ws0(decimal), char(']'))(input)
}
fn intliteral(input: &str) -> IResult<&str, (u64, u64)> {
tuple((terminated(decimal, char('\'')), alt((decimal, hexadecimal))))(input)
}
#[derive(Debug)]
pub struct NetDecl {
pub name: String,
pub width: Option<u64>,
pub value: Option<(u64, u64)>,
}
#[derive(Debug)]
pub enum PortDirection {
Input,
Output,
}
#[derive(Debug)]
pub struct PortDecl {
pub direction: PortDirection,
pub net: NetDecl,
}
#[derive(Debug)]
pub struct Module {
pub name: String,
pub ports: Vec<PortDecl>,
pub statements: Vec<Statement>,
}
#[derive(Debug)]
pub enum Statement {
Assign(Assign),
}
#[derive(Debug)]
pub struct Assign {
pub lhs: String,
pub expr: Expression,
}
#[derive(Debug)]
pub enum Operation {
And { a: String, b: Expression },
Or { a: String, b: Expression },
}
#[derive(Debug)]
pub struct Call {
pub name: String,
pub args: Vec<Expression>,
}
#[derive(Debug)]
pub enum Expression {
Ident(String),
Call(Box<Call>),
Operation(Box<Operation>),
}
fn declaration(i: &str) -> IResult<&str, NetDecl> {
map(
tuple((
ws0(alt((tag("reg"), tag("wire")))),
opt(ws0(widthspec)),
identifier,
opt(preceded(ws0(char('=')), intliteral)),
)),
|(_, width, ident, value)| NetDecl {
name: ident.into(),
width,
value,
},
)(i)
}
fn port_decl(i: &str) -> IResult<&str, PortDecl> {
map(
tuple((
alt((
map(tag("input"), |_| PortDirection::Input),
map(tag("output"), |_| PortDirection::Output),
)),
declaration,
)),
|(direction, net)| PortDecl { direction, net },
)(i)
}
fn ports_list(input: &str) -> IResult<&str, Vec<PortDecl>> {
separated_list0(ws0(char(',')), ws0(port_decl))(input)
}
fn operation(input: &str) -> IResult<&str, Operation> {
// temporarily given up on before I learn the shunting yard algorithm
alt((
map(
separated_pair(ws0(identifier), char('&'), ws0(expression)),
|(a, b)| Operation::And { a: a.into(), b },
),
map(
separated_pair(ws0(identifier), char('|'), ws0(expression)),
|(a, b)| Operation::Or { a: a.into(), b },
),
))(input)
}
fn call_item(input: &str) -> IResult<&str, Call> {
map(
tuple((
ws0(identifier),
delimited(
char('('),
ws0(separated_list0(char(','), expression)),
char(')'),
),
)),
|(name, args)| Call {
name: name.into(),
args,
},
)(input)
}
fn expression(input: &str) -> IResult<&str, Expression> {
alt((
map(ws0(operation), |op| Expression::Operation(Box::new(op))),
map(ws0(call_item), |call| Expression::Call(Box::new(call))),
map(ws0(identifier), |ident| Expression::Ident(ident.into())),
))(input)
}
fn assign_statement(input: &str) -> IResult<&str, Statement> {
context(
"assignment",
delimited(
ws0(terminated(tag("assign"), multispace1)),
map(
separated_pair(ws0(identifier), char('='), ws0(expression)),
|(lhs, expr)| {
Statement::Assign(Assign {
lhs: lhs.into(),
expr: expr.into(),
})
},
),
ws0(char(';')),
),
)(input)
}
pub fn module(input: &str) -> IResult<&str, Module> {
context(
"module",
map(
tuple((
tag("module"),
ws0(identifier),
ws0(delimited(char('('), ws0(ports_list), char(')'))),
ws0(delimited(
char('{'),
many1(ws0(assign_statement)),
char('}'),
)),
)),
|(_, name, ports, statements)| Module {
name: name.into(),
ports,
statements,
},
),
)(input)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_decl() {
declaration("reg abcd").unwrap();
}
#[test]
fn test_operation() {
operation(" a | b ").unwrap();
operation(" a & b ").unwrap();
}
#[test]
fn test_expression() {
expression(" a ").unwrap();
expression(" a | b ").unwrap();
expression(" a | b | c ").unwrap();
}
#[test]
fn test_assignment() {
assign_statement(" assign a = b ; ").unwrap();
assign_statement(" assign a = b | c ; ").unwrap();
}
#[test]
fn test_call() {
call_item("thing ( )").unwrap();
call_item("thing ( a , b , c )").unwrap();
call_item("thing(a,b,c)").unwrap();
}
}