futilehdl/src/parser.rs

267 lines
6.4 KiB
Rust

use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{alpha1, alphanumeric1, char, multispace0, multispace1, u64 as decimal},
combinator::{consumed, map, opt, recognize},
error::{context, ParseError, VerboseError},
multi::{many0, many1, separated_list0},
sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
};
use nom_locate::{position, LocatedSpan};
// custom span type for nom_locate
pub type Span<'a> = LocatedSpan<&'a str>;
// custom IResult type for VerboseError
pub type IResult<I, O, E = VerboseError<I>> = nom::IResult<I, O, E>;
use crate::literals::hexadecimal;
fn ws0<'a, F: 'a, O, E: ParseError<Span<'a>>>(
inner: F,
) -> impl FnMut(Span<'a>) -> IResult<Span<'a>, O, E>
where
F: FnMut(Span<'a>) -> IResult<Span<'a>, O, E>,
{
delimited(multispace0, inner, multispace0)
}
fn identifier(input: Span) -> IResult<Span, Span> {
recognize(pair(
alt((alpha1, tag("_"))),
many0(alt((alphanumeric1, tag("_")))),
))(input)
}
fn widthspec(input: Span) -> IResult<Span, u64> {
delimited(char('['), ws0(decimal), char(']'))(input)
}
fn intliteral(input: Span) -> IResult<Span, (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<'a> {
pub pos: Span<'a>,
pub direction: PortDirection,
pub net: NetDecl,
}
#[derive(Debug)]
pub struct Module<'a> {
pub name: String,
pub ports: Vec<PortDecl<'a>>,
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: Span) -> IResult<Span, NetDecl> {
map(
tuple((
ws0(alt((tag("reg"), tag("wire")))),
opt(ws0(widthspec)),
identifier,
opt(preceded(ws0(char('=')), intliteral)),
)),
|(_, width, ident, value)| NetDecl {
name: (*ident.fragment()).into(),
width,
value,
},
)(i)
}
fn port_decl(i: Span) -> IResult<Span, PortDecl> {
map(
consumed(tuple((
alt((
map(tag("input"), |_| PortDirection::Input),
map(tag("output"), |_| PortDirection::Output),
)),
declaration,
))),
|(pos, (direction, net))| PortDecl {
pos,
direction,
net,
},
)(i)
}
fn ports_list(input: Span) -> IResult<Span, Vec<PortDecl>> {
separated_list0(ws0(char(',')), ws0(port_decl))(input)
}
fn operation(input: Span) -> IResult<Span, 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.fragment()).into(),
b,
},
),
map(
separated_pair(ws0(identifier), char('|'), ws0(expression)),
|(a, b)| Operation::Or {
a: (*a.fragment()).into(),
b,
},
),
))(input)
}
fn call_item(input: Span) -> IResult<Span, Call> {
map(
tuple((
ws0(identifier),
delimited(
char('('),
ws0(separated_list0(char(','), expression)),
char(')'),
),
)),
|(name, args)| Call {
name: (*name.fragment()).into(),
args,
},
)(input)
}
fn expression(input: Span) -> IResult<Span, 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.fragment()).into())
}),
))(input)
}
fn assign_statement(input: Span) -> IResult<Span, Statement> {
context(
"assignment",
delimited(
ws0(terminated(tag("assign"), multispace1)),
map(
separated_pair(ws0(identifier), char('='), ws0(expression)),
|(lhs, expr)| {
Statement::Assign(Assign {
lhs: (*lhs.fragment()).into(),
expr,
})
},
),
ws0(char(';')),
),
)(input)
}
pub fn module(input: Span) -> IResult<Span, 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.fragment()).into(),
ports,
statements,
},
),
)(input)
}
pub fn parse(input: Span) -> IResult<Span, Module> {
module(input)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_decl() {
declaration("reg abcd".into()).unwrap();
}
#[test]
fn test_operation() {
operation(" a | b ".into()).unwrap();
operation(" a & b ".into()).unwrap();
}
#[test]
fn test_expression() {
expression(" a ".into()).unwrap();
expression(" a | b ".into()).unwrap();
expression(" a | b | c ".into()).unwrap();
}
#[test]
fn test_assignment() {
assign_statement(" assign a = b ; ".into()).unwrap();
assign_statement(" assign a = b | c ; ".into()).unwrap();
}
#[test]
fn test_call() {
call_item("thing ( )".into()).unwrap();
call_item("thing ( a , b , c )".into()).unwrap();
call_item("thing(a,b,c)".into()).unwrap();
}
}