futilehdl/src/parser.rs

71 lines
1.9 KiB
Rust

pub mod declaration;
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::declaration::{
assign_statement, declaration, typename, Assign, NetDecl, TypeName,
};
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};
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();
}
}