232 lines
5.9 KiB
Rust
232 lines
5.9 KiB
Rust
pub mod error;
|
|
mod literals;
|
|
pub mod module;
|
|
pub mod proc;
|
|
pub mod tokens;
|
|
|
|
use nom::{
|
|
branch::alt,
|
|
bytes::complete::tag,
|
|
character::complete::{alpha1, alphanumeric1, char, multispace0, u64 as decimal},
|
|
combinator::{map, opt, recognize},
|
|
error::{ErrorKind, ParseError},
|
|
multi::{many0, separated_list0},
|
|
sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
|
|
};
|
|
use nom_greedyerror::GreedyError;
|
|
use nom_locate::LocatedSpan;
|
|
|
|
// custom span type for nom_locate
|
|
pub type Span<'a> = LocatedSpan<&'a str>;
|
|
|
|
pub type IErr<I> = GreedyError<I, ErrorKind>;
|
|
// custom IResult type for VerboseError
|
|
pub type IResult<I, O, E = IErr<I>> = nom::IResult<I, O, E>;
|
|
|
|
pub use crate::parser::module::{module, Module, ModuleItem, PortDirection};
|
|
use 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)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
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 TypeName<'a> {
|
|
name: Span<'a>,
|
|
generics: (),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct NetDecl<'a> {
|
|
pub name: Span<'a>,
|
|
pub typ: TypeName<'a>,
|
|
pub value: Option<(u64, u64)>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Assign<'a> {
|
|
pub lhs: &'a str,
|
|
pub expr: Expression<'a>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Operation<'a> {
|
|
And {
|
|
a: Expression<'a>,
|
|
b: Expression<'a>,
|
|
},
|
|
Or {
|
|
a: Expression<'a>,
|
|
b: Expression<'a>,
|
|
},
|
|
Xor {
|
|
a: Expression<'a>,
|
|
b: Expression<'a>,
|
|
},
|
|
Not(Expression<'a>),
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Call<'a> {
|
|
pub name: Span<'a>,
|
|
pub args: Vec<Expression<'a>>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Expression<'a> {
|
|
Ident(&'a str),
|
|
Literal(u64),
|
|
Call(Box<Call<'a>>),
|
|
Operation(Box<Operation<'a>>),
|
|
}
|
|
|
|
fn declaration(i: Span) -> IResult<Span, NetDecl> {
|
|
map(
|
|
tuple((
|
|
separated_pair(identifier, ws0(char(':')), typename),
|
|
opt(preceded(ws0(char('=')), intliteral)),
|
|
)),
|
|
|((ident, typ), value)| NetDecl {
|
|
name: ident,
|
|
typ,
|
|
value,
|
|
},
|
|
)(i)
|
|
}
|
|
|
|
fn operation(input: Span) -> IResult<Span, Operation> {
|
|
// temporarily given up on before I learn the shunting yard algorithm
|
|
alt((
|
|
map(
|
|
separated_pair(ws0(expression_nonrecurse), char('&'), ws0(expression)),
|
|
|(a, b)| Operation::And { a, b },
|
|
),
|
|
map(
|
|
separated_pair(ws0(expression_nonrecurse), char('|'), ws0(expression)),
|
|
|(a, b)| Operation::Or { a, b },
|
|
),
|
|
map(
|
|
separated_pair(ws0(expression_nonrecurse), char('^'), ws0(expression)),
|
|
|(a, b)| Operation::Xor { a, b },
|
|
),
|
|
map(preceded(char('~'), expression), Operation::Not),
|
|
))(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, args },
|
|
)(input)
|
|
}
|
|
|
|
/// 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
|
|
fn expression(input: Span) -> IResult<Span, Expression> {
|
|
alt((
|
|
map(ws0(operation), |op| Expression::Operation(Box::new(op))),
|
|
expression_nonrecurse,
|
|
))(input)
|
|
}
|
|
|
|
/// the portion of the expression grammar that can be parsed without left recursion
|
|
fn expression_nonrecurse(input: Span) -> IResult<Span, Expression> {
|
|
alt((
|
|
map(ws0(decimal), Expression::Literal),
|
|
map(ws0(call_item), |call| Expression::Call(Box::new(call))),
|
|
map(ws0(identifier), |ident| {
|
|
Expression::Ident(*ident.fragment())
|
|
}),
|
|
delimited(char('('), expression, char(')')),
|
|
))(input)
|
|
}
|
|
|
|
fn assign_statement(input: Span) -> IResult<Span, Assign> {
|
|
map(
|
|
separated_pair(ws0(identifier), char('='), ws0(expression)),
|
|
|(lhs, expr)| Assign {
|
|
lhs: (*lhs.fragment()),
|
|
expr,
|
|
},
|
|
)(input)
|
|
}
|
|
|
|
pub fn parse(input: Span) -> IResult<Span, Module> {
|
|
ws0(module)(input)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use nom::combinator::all_consuming;
|
|
|
|
#[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() {
|
|
// 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();
|
|
}
|
|
|
|
#[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();
|
|
}
|
|
}
|