futilehdl/src/parser.rs

230 lines
5.8 KiB
Rust
Raw Normal View History

2022-01-17 00:15:27 +00:00
pub mod module;
pub mod proc;
2022-01-16 21:06:52 +00:00
2022-01-01 21:43:38 +00:00
use nom::{
branch::alt,
bytes::complete::tag,
2022-01-17 16:37:52 +00:00
character::complete::{alpha1, alphanumeric1, char, multispace0, u64 as decimal},
2022-01-16 21:10:52 +00:00
combinator::{map, opt, recognize},
2022-01-17 16:37:52 +00:00
error::{ParseError, VerboseError},
2022-01-16 21:10:52 +00:00
multi::{many0, separated_list0},
2022-01-01 21:43:38 +00:00
sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
};
2022-01-14 14:32:00 +00:00
use nom_locate::LocatedSpan;
2022-01-04 16:28:43 +00:00
// custom span type for nom_locate
2022-01-04 19:05:10 +00:00
pub type Span<'a> = LocatedSpan<&'a str>;
2022-01-04 16:24:49 +00:00
2022-01-04 16:28:43 +00:00
// custom IResult type for VerboseError
pub type IResult<I, O, E = VerboseError<I>> = nom::IResult<I, O, E>;
2022-01-04 18:09:33 +00:00
use crate::literals::hexadecimal;
2022-01-16 21:06:52 +00:00
pub use crate::parser::module::{module, Module, ModuleItem, PortDirection};
2022-01-01 21:43:38 +00:00
2022-01-04 19:05:10 +00:00
fn ws0<'a, F: 'a, O, E: ParseError<Span<'a>>>(
2022-01-01 21:43:38 +00:00
inner: F,
2022-01-04 19:05:10 +00:00
) -> impl FnMut(Span<'a>) -> IResult<Span<'a>, O, E>
2022-01-01 21:43:38 +00:00
where
2022-01-04 19:05:10 +00:00
F: FnMut(Span<'a>) -> IResult<Span<'a>, O, E>,
2022-01-01 21:43:38 +00:00
{
delimited(multispace0, inner, multispace0)
}
2022-01-04 19:05:10 +00:00
fn identifier(input: Span) -> IResult<Span, Span> {
2022-01-01 21:43:38 +00:00
recognize(pair(
alt((alpha1, tag("_"))),
many0(alt((alphanumeric1, tag("_")))),
))(input)
}
2022-01-23 23:10:09 +00:00
// TODO: allow recursive generics
fn typename(input: Span) -> IResult<Span, TypeName> {
map(
tuple((
identifier,
opt(delimited(char('<'), ws0(expression), char('>')))
)),
|(ident, _)| {
TypeName {
name: ident,
generics: ()
}
}
)(input)
}
2022-01-04 19:05:10 +00:00
fn widthspec(input: Span) -> IResult<Span, u64> {
2022-01-01 21:43:38 +00:00
delimited(char('['), ws0(decimal), char(']'))(input)
}
2022-01-04 19:05:10 +00:00
fn intliteral(input: Span) -> IResult<Span, (u64, u64)> {
2022-01-01 21:43:38 +00:00
tuple((terminated(decimal, char('\'')), alt((decimal, hexadecimal))))(input)
}
2022-01-23 23:10:09 +00:00
#[derive(Debug)]
pub struct TypeName<'a> {
name: Span<'a>,
generics: (),
}
2022-01-01 21:43:38 +00:00
#[derive(Debug)]
pub struct NetDecl<'a> {
2022-01-23 23:10:09 +00:00
pub name: Span<'a>,
pub typ: TypeName<'a>,
2022-01-01 21:43:38 +00:00
pub value: Option<(u64, u64)>,
}
#[derive(Debug)]
pub struct Assign<'a> {
pub lhs: &'a str,
pub expr: Expression<'a>,
2022-01-01 21:43:38 +00:00
}
2022-01-17 18:20:51 +00:00
#[derive(Debug, Clone)]
pub enum Operation<'a> {
2022-01-17 16:37:15 +00:00
And {
a: Expression<'a>,
b: Expression<'a>,
},
Or {
a: Expression<'a>,
b: Expression<'a>,
},
Xor {
a: Expression<'a>,
b: Expression<'a>,
},
2022-01-17 00:15:27 +00:00
Not(Expression<'a>),
2022-01-01 21:43:38 +00:00
}
2022-01-17 18:20:51 +00:00
#[derive(Debug, Clone)]
pub struct Call<'a> {
pub name: Span<'a>,
pub args: Vec<Expression<'a>>,
2022-01-01 21:43:38 +00:00
}
2022-01-17 18:20:51 +00:00
#[derive(Debug, Clone)]
pub enum Expression<'a> {
Ident(&'a str),
2022-01-17 00:15:27 +00:00
Literal(u64),
Call(Box<Call<'a>>),
Operation(Box<Operation<'a>>),
2022-01-01 21:43:38 +00:00
}
2022-01-04 19:05:10 +00:00
fn declaration(i: Span) -> IResult<Span, NetDecl> {
2022-01-01 21:43:38 +00:00
map(
tuple((
2022-01-23 23:10:09 +00:00
separated_pair(identifier, ws0(char(':')), typename),
2022-01-01 21:43:38 +00:00
opt(preceded(ws0(char('=')), intliteral)),
)),
2022-01-23 23:10:09 +00:00
|((ident, typ), value)| NetDecl {
name: ident,
typ,
2022-01-01 21:43:38 +00:00
value,
},
)(i)
}
2022-01-04 19:05:10 +00:00
fn operation(input: Span) -> IResult<Span, Operation> {
2022-01-01 21:43:38 +00:00
// temporarily given up on before I learn the shunting yard algorithm
alt((
map(
2022-01-17 16:29:00 +00:00
separated_pair(ws0(expression_nonrecurse), char('&'), ws0(expression)),
2022-01-17 16:37:15 +00:00
|(a, b)| Operation::And { a, b },
2022-01-01 21:43:38 +00:00
),
map(
2022-01-17 16:29:00 +00:00
separated_pair(ws0(expression_nonrecurse), char('|'), ws0(expression)),
2022-01-17 16:37:15 +00:00
|(a, b)| Operation::Or { a, b },
2022-01-01 21:43:38 +00:00
),
2022-01-17 16:36:24 +00:00
map(
separated_pair(ws0(expression_nonrecurse), char('^'), ws0(expression)),
2022-01-17 16:37:15 +00:00
|(a, b)| Operation::Xor { a, b },
2022-01-17 00:15:27 +00:00
),
2022-01-17 16:37:52 +00:00
map(preceded(char('~'), expression), Operation::Not),
2022-01-01 21:43:38 +00:00
))(input)
}
2022-01-04 19:05:10 +00:00
fn call_item(input: Span) -> IResult<Span, Call> {
2022-01-01 21:43:38 +00:00
map(
tuple((
ws0(identifier),
2022-01-04 00:38:35 +00:00
delimited(
char('('),
ws0(separated_list0(char(','), expression)),
char(')'),
),
2022-01-01 21:43:38 +00:00
)),
2022-01-05 01:11:13 +00:00
|(name, args)| Call { name, args },
2022-01-04 00:38:35 +00:00
)(input)
2022-01-01 21:43:38 +00:00
}
2022-01-17 16:29:00 +00:00
/// parser combinators can not parse left-recursive grammars. To work around this, we split
/// expressions into a recursive and non-recursive portion.
/// Parsers reachable from this point must call expression_nonrecurse instead
2022-01-04 19:05:10 +00:00
fn expression(input: Span) -> IResult<Span, Expression> {
2022-01-01 21:43:38 +00:00
alt((
map(ws0(operation), |op| Expression::Operation(Box::new(op))),
2022-01-17 16:37:15 +00:00
expression_nonrecurse,
2022-01-17 16:29:00 +00:00
))(input)
}
/// the portion of the expression grammar that can be parsed without left recursion
fn expression_nonrecurse(input: Span) -> IResult<Span, Expression> {
alt((
2022-01-17 16:37:52 +00:00
map(ws0(decimal), Expression::Literal),
2022-01-01 21:43:38 +00:00
map(ws0(call_item), |call| Expression::Call(Box::new(call))),
2022-01-04 20:14:49 +00:00
map(ws0(identifier), |ident| {
2022-01-05 01:11:13 +00:00
Expression::Ident(*ident.fragment())
2022-01-04 20:14:49 +00:00
}),
2022-01-17 16:29:00 +00:00
delimited(char('('), expression, char(')')),
2022-01-01 21:43:38 +00:00
))(input)
}
2022-01-16 20:46:44 +00:00
fn assign_statement(input: Span) -> IResult<Span, Assign> {
2022-01-17 00:15:27 +00:00
map(
separated_pair(ws0(identifier), char('='), ws0(expression)),
|(lhs, expr)| Assign {
lhs: (*lhs.fragment()),
expr,
},
2022-01-01 21:43:38 +00:00
)(input)
}
2022-01-04 19:05:10 +00:00
pub fn parse(input: Span) -> IResult<Span, Module> {
2022-01-23 23:10:09 +00:00
ws0(module)(input)
2022-01-04 16:24:21 +00:00
}
2022-01-01 21:43:38 +00:00
#[cfg(test)]
mod test {
use super::*;
2022-01-17 00:15:27 +00:00
use nom::combinator::all_consuming;
2022-01-01 21:43:38 +00:00
#[test]
fn test_operation() {
2022-01-04 19:05:10 +00:00
operation(" a | b ".into()).unwrap();
operation(" a & b ".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
#[test]
fn test_expression() {
2022-01-04 19:05:10 +00:00
expression(" a ".into()).unwrap();
expression(" a | b ".into()).unwrap();
expression(" a | b | c ".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
#[test]
fn test_assignment() {
2022-01-17 00:15:27 +00:00
// TODO: make wrapper and use for all tests
all_consuming(assign_statement)(" a = b ".into()).unwrap();
all_consuming(assign_statement)(" a = b | c ".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
#[test]
fn test_call() {
2022-01-04 19:05:10 +00:00
call_item("thing ( )".into()).unwrap();
call_item("thing ( a , b , c )".into()).unwrap();
call_item("thing(a,b,c)".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
}