142 lines
3.5 KiB
Rust
142 lines
3.5 KiB
Rust
pub mod error;
|
|
pub mod expression;
|
|
mod literals;
|
|
pub mod module;
|
|
pub mod proc;
|
|
pub mod tokens;
|
|
|
|
use nom::{
|
|
branch::alt,
|
|
bytes::complete::tag,
|
|
character::complete::{alpha1, alphanumeric1, multispace0},
|
|
combinator::{map, opt, recognize},
|
|
error::{ErrorKind, ParseError},
|
|
multi::{many0, separated_list0},
|
|
sequence::{delimited, pair, preceded, separated_pair, 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::expression::{expression, Call, Expression, Operation};
|
|
pub use crate::parser::module::{module, Module, ModuleItem, PortDirection};
|
|
use crate::parser::tokens::{token, TokenKind as tk, TokenSpan};
|
|
|
|
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
|
|
// TODO: allow expressions again
|
|
fn typename(input: TokenSpan) -> IResult<TokenSpan, TypeName> {
|
|
map(
|
|
tuple((
|
|
token(tk::Ident),
|
|
opt(delimited(token(tk::LAngle), expression, token(tk::RAngle))),
|
|
)),
|
|
|(ident, _)| TypeName {
|
|
name: ident.span(),
|
|
generics: (),
|
|
},
|
|
)(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<Span<'a>>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Assign<'a> {
|
|
pub lhs: &'a str,
|
|
pub expr: Expression<'a>,
|
|
}
|
|
|
|
fn assign_statement(input: TokenSpan) -> IResult<TokenSpan, Assign> {
|
|
map(
|
|
separated_pair(token(tk::Ident), token(tk::EqAssign), expression),
|
|
|(lhs, expr)| Assign {
|
|
lhs: (*lhs.span().fragment()),
|
|
expr,
|
|
},
|
|
)(input)
|
|
}
|
|
|
|
// TODO: reallow assignments
|
|
fn declaration(i: TokenSpan) -> IResult<TokenSpan, NetDecl> {
|
|
map(
|
|
tuple((
|
|
separated_pair(token(tk::Ident), token(tk::Colon), typename),
|
|
opt(preceded(token(tk::Assign), token(tk::Number))),
|
|
)),
|
|
|((ident, typ), _value)| NetDecl {
|
|
name: ident.span(),
|
|
typ,
|
|
value: None,
|
|
},
|
|
)(i)
|
|
}
|
|
|
|
pub fn parse(input: TokenSpan) -> IResult<TokenSpan, Module> {
|
|
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();
|
|
}
|
|
}
|